-
-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
table.enable_counts() method, closes #212
- Loading branch information
Showing
3 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
def test_enable_counts(fresh_db): | ||
foo = fresh_db["foo"] | ||
assert fresh_db.table_names() == [] | ||
for i in range(10): | ||
foo.insert({"name": "item {}".format(i)}) | ||
assert fresh_db.table_names() == ["foo"] | ||
assert foo.count == 10 | ||
# Now enable counts | ||
foo.enable_counts() | ||
assert foo.triggers_dict == { | ||
"foo_counts_insert": "CREATE TRIGGER [foo_counts_insert] AFTER INSERT ON [foo]\nBEGIN\n INSERT OR REPLACE INTO [_counts]\n VALUES (\n 'foo',\n COALESCE(\n (SELECT count FROM [_counts] WHERE [table] = 'foo'),\n 0\n ) + 1\n );\nEND", | ||
"foo_counts_delete": "CREATE TRIGGER [foo_counts_delete] AFTER DELETE ON [foo]\nBEGIN\n INSERT OR REPLACE INTO [_counts]\n VALUES (\n 'foo',\n COALESCE(\n (SELECT count FROM [_counts] WHERE [table] = 'foo'),\n 0\n ) - 1\n );\nEND", | ||
} | ||
assert fresh_db.table_names() == ["foo", "_counts"] | ||
assert list(fresh_db["_counts"].rows) == [{"count": 10, "table": "foo"}] | ||
# Add some items to test the triggers | ||
for i in range(5): | ||
foo.insert({"name": "item {}".format(10 + i)}) | ||
assert foo.count == 15 | ||
assert list(fresh_db["_counts"].rows) == [{"count": 15, "table": "foo"}] | ||
# Delete some items | ||
foo.delete_where("rowid < 7") | ||
assert foo.count == 9 | ||
assert list(fresh_db["_counts"].rows) == [{"count": 9, "table": "foo"}] | ||
foo.delete_where() | ||
assert foo.count == 0 | ||
assert list(fresh_db["_counts"].rows) == [{"count": 0, "table": "foo"}] |