-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathtest_update_hook.js
72 lines (53 loc) · 2.33 KB
/
test_update_hook.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
exports.test = function(SQL, assert){
var db = new SQL.Database();
db.exec(
"CREATE TABLE consoles (id INTEGER PRIMARY KEY, company TEXT, name TEXT);" +
"INSERT INTO consoles VALUES (1, 'Sony', 'Playstation');" +
"INSERT INTO consoles VALUES (2, 'Microsoft', 'Xbox');"
);
// {operation: undefined, databaseName: undefined, tableName: undefined, rowId: undefined};
var updateHookCalls = []
db.updateHook(function(operation, databaseName, tableName, rowId) {
updateHookCalls.push({operation, databaseName, tableName, rowId});
});
// INSERT
db.exec("INSERT INTO consoles VALUES (3, 'Sega', 'Saturn');");
assert.deepEqual(updateHookCalls, [
{operation: "insert", databaseName: "main", tableName: "consoles", rowId: 3}
], "insert a single row");
// UPDATE
updateHookCalls = []
db.exec("UPDATE consoles SET name = 'Playstation 5' WHERE id = 1");
assert.deepEqual(updateHookCalls, [
{operation: "update", databaseName: "main", tableName: "consoles", rowId: 1}
], "update a single row");
// UPDATE (multiple rows)
updateHookCalls = []
db.exec("UPDATE consoles SET name = name + ' [legacy]' WHERE id IN (2,3)");
assert.deepEqual(updateHookCalls, [
{operation: "update", databaseName: "main", tableName: "consoles", rowId: 2},
{operation: "update", databaseName: "main", tableName: "consoles", rowId: 3},
], "update two rows");
// DELETE
updateHookCalls = []
db.exec("DELETE FROM consoles WHERE company = 'Sega'");
assert.deepEqual(updateHookCalls, [
{operation: "delete", databaseName: "main", tableName: "consoles", rowId: 3}
], "delete a single row");
// UNREGISTER
updateHookCalls = []
db.updateHook(null);
db.exec("DELETE FROM consoles WHERE company = 'Microsoft'");
assert.deepEqual(updateHookCalls, [], "unregister the update hook");
// REGISTER AGAIN
updateHookCalls = []
db.updateHook(function(operation, databaseName, tableName, rowId) {
updateHookCalls.push({operation, databaseName, tableName, rowId});
});
// need a where clause, just running "DELETE FROM consoles" would result in
// a TRUNCATE and not yield any update hook callbacks
db.exec("DELETE FROM consoles WHERE id > 0");
assert.deepEqual(updateHookCalls, [
{operation: 'delete', databaseName: 'main', tableName: 'consoles', rowId: 1}
], "register the update hook again");
}