From 4bf376c1a603caa7bd093a1c36817f8ea21180b1 Mon Sep 17 00:00:00 2001 From: Christian Schneider Date: Tue, 26 Sep 2017 16:38:14 +0200 Subject: Added update/insert/upsert tracking and delete_untouched() to delete rows not touch by one of those functions --- tests/it_dbi.t | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'tests') diff --git a/tests/it_dbi.t b/tests/it_dbi.t index cec097e..b29b164 100755 --- a/tests/it_dbi.t +++ b/tests/it_dbi.t @@ -327,3 +327,38 @@ $record->update(['-key3' => "2*2"]); is($record->key1, "val1"); is($ $record->update(['key1' => "val0"]); is($record->key1, "val0"); is($record->key2, "val2"); is($record->key3, 4); $record->replace(['ID' => 6, 'key4' => "val4"]); is($record->key4, "val4"); $record->select(['key2' => "val2"]); is($record->key2, "val2"); is($record->key4, null, "clear previous fields"); + +# +# Test tracked update +# +function allrecs() +{ + foreach (new it_dbi_test() as $r) + $result[] = ['ID' => $r->ID, 'foo' => $r->foo]; + return json_encode($result); +} + +$record = new it_dbi_test; +$record->delete(['WHERE 1' ]); +$record->upsert(['ID' => 1, 'foo' => "a"]); +$record->upsert(['ID' => 2, 'foo' => "b"]); +$record->upsert(['ID' => 2, 'foo' => "B"]); +is($record->delete_untouched(), 0, 'delete_untouched'); +is(allrecs(), '[{"ID":1,"foo":"a"},{"ID":2,"foo":"B"}]', 'data after delete_untouched'); + +$record->upsert(['ID' => 1, 'foo' => "A"]); +$record->upsert(['ID' => 3, 'foo' => "c"]); +is($record->delete_untouched([ 'ID >' => 2 ]), 0, 'delete_untouched with query'); +is(allrecs(), '[{"ID":1,"foo":"A"},{"ID":2,"foo":"B"},{"ID":3,"foo":"c"}]', 'data after delete_untouched with query'); + +$record->upsert(['ID' => 3, 'foo' => "C"]); +is($record->delete_untouched(), 2, 'delete_untouched with query'); +is(allrecs(), '[{"ID":3,"foo":"C"}]', 'data after delete_untouched with query'); + +$record->replace(['ID' => 4, 'foo' => "C"]); +is($record->delete_untouched(), 1, 'delete_untouched after replace'); +is(allrecs(), '[{"ID":4,"foo":"C"}]', 'data after delete_untouched after replace'); + +$record->upsert(['ID' => 4, 'foo' => "C"]); +is($record->delete_untouched(), 0, 'delete_untouched after upsert without changes'); +is(allrecs(), '[{"ID":4,"foo":"C"}]', 'data after delete_untouched after upsert without changes'); -- cgit v1.2.3