|
| 1 | +var sqlite3 = require('..'); |
| 2 | +var assert = require('assert'); |
| 3 | + |
| 4 | +describe('update_hook', function() { |
| 5 | + var db; |
| 6 | + |
| 7 | + beforeEach(function(done) { |
| 8 | + db = new sqlite3.Database(':memory:', function(err) { |
| 9 | + if (err) return done(err); |
| 10 | + |
| 11 | + db.run("CREATE TABLE update_hooks_test (id int PRIMARY KEY, value text)", done); |
| 12 | + }); |
| 13 | + }); |
| 14 | + |
| 15 | + it('emits insert event on inserting data to table', function(done) { |
| 16 | + db.addListener('change', function(eventType, database, table, rowId) { |
| 17 | + assert.equal(eventType, 'insert'); |
| 18 | + assert.equal(database, 'main'); |
| 19 | + assert.equal(table, 'update_hooks_test'); |
| 20 | + assert.equal(rowId, 1); |
| 21 | + |
| 22 | + return done(); |
| 23 | + }); |
| 24 | + |
| 25 | + db.run("INSERT INTO update_hooks_test VALUES (1, 'value')", function(err) { |
| 26 | + if (err) return done(err); |
| 27 | + }); |
| 28 | + }); |
| 29 | + |
| 30 | + it('emits update event on row modification in table', function(done) { |
| 31 | + db.run("INSERT INTO update_hooks_test VALUES (2, 'value'), (3, 'value4')", function(err) { |
| 32 | + if (err) return done(err); |
| 33 | + |
| 34 | + db.addListener('change', function(eventType, database, table, rowId) { |
| 35 | + assert.equal(eventType, 'update'); |
| 36 | + assert.equal(database, 'main'); |
| 37 | + assert.equal(table, 'update_hooks_test'); |
| 38 | + assert.equal(rowId, 1); |
| 39 | + |
| 40 | + db.all("SELECT * FROM update_hooks_test WHERE rowid = ?", rowId, function(err, rows) { |
| 41 | + assert.deepEqual(rows, [{ id: 2, value: 'new_val' }]); |
| 42 | + |
| 43 | + return done(err); |
| 44 | + }); |
| 45 | + }); |
| 46 | + |
| 47 | + db.run("UPDATE update_hooks_test SET value = 'new_val' WHERE id = 2", function(err) { |
| 48 | + if (err) return done(err); |
| 49 | + }); |
| 50 | + }); |
| 51 | + }); |
| 52 | + |
| 53 | + it('emits delete event on row was deleted from table', function(done) { |
| 54 | + db.run("INSERT INTO update_hooks_test VALUES (2, 'value')", function(err) { |
| 55 | + if (err) return done(err); |
| 56 | + |
| 57 | + db.addListener('change', function(eventType, database, table, rowId) { |
| 58 | + assert.equal(eventType, 'delete'); |
| 59 | + assert.equal(database, 'main'); |
| 60 | + assert.equal(table, 'update_hooks_test'); |
| 61 | + assert.equal(rowId, 1); |
| 62 | + |
| 63 | + return done(); |
| 64 | + }); |
| 65 | + |
| 66 | + db.run("DELETE FROM update_hooks_test WHERE id = 2", function(err) { |
| 67 | + if (err) return done(err); |
| 68 | + }); |
| 69 | + }); |
| 70 | + }); |
| 71 | + |
| 72 | + afterEach(function(done) { |
| 73 | + db.close(done); |
| 74 | + }); |
| 75 | +}); |
0 commit comments