|
| 1 | +'use strict'; |
| 2 | +var expect = require('chai').expect; |
| 3 | +var Database = require('../.'); |
| 4 | +var db; |
| 5 | + |
| 6 | +before(function (done) { |
| 7 | + db = new Database('temp/' + require('path').basename(__filename).split('.')[0] + '.db'); |
| 8 | + db.on('open', done); |
| 9 | +}); |
| 10 | + |
| 11 | +describe('Transaction#run()', function () { |
| 12 | + it('should work with CREATE TABLE', function () { |
| 13 | + var trans = db.transaction(['CREATE TABLE entries (a TEXT, b INTEGER, c REAL, d BLOB)']); |
| 14 | + var info = trans.run(); |
| 15 | + expect(info.changes).to.equal(0); |
| 16 | + expect(info.lastInsertROWID).to.equal(0); |
| 17 | + }); |
| 18 | + it('should work with INSERT INTO', function () { |
| 19 | + var trans = db.transaction(["INSERT INTO entries VALUES ('foo', 25, 3.14, x'1133ddff')"]); |
| 20 | + var info = trans.run(); |
| 21 | + expect(info.changes).to.equal(1); |
| 22 | + expect(info.lastInsertROWID).to.equal(1); |
| 23 | + |
| 24 | + info = trans.run(); |
| 25 | + expect(info.changes).to.equal(1); |
| 26 | + expect(info.lastInsertROWID).to.equal(2); |
| 27 | + |
| 28 | + trans = db.transaction(["INSERT INTO entries VALUES ('foo', 25, 3.14, x'1133ddff')", "INSERT INTO entries VALUES ('foo', 25, 3.14, x'1133ddff')"]); |
| 29 | + info = trans.run(); |
| 30 | + expect(info.changes).to.equal(2); |
| 31 | + expect(info.lastInsertROWID).to.equal(4); |
| 32 | + }); |
| 33 | + it('should work with UPDATE', function () { |
| 34 | + var trans = db.transaction(["UPDATE entries SET a='bar' WHERE rowid=1"]); |
| 35 | + expect(trans.run().changes).to.equal(1); |
| 36 | + }); |
| 37 | + it('should work with DELETE FROM', function () { |
| 38 | + var trans = db.transaction(["DELETE FROM entries WHERE a='foo'"]); |
| 39 | + expect(trans.run().changes).to.equal(3); |
| 40 | + |
| 41 | + trans = db.transaction(["INSERT INTO entries VALUES ('foo', 25, 3.14, x'1133ddff')"]); |
| 42 | + var info = trans.run(); |
| 43 | + expect(info.changes).to.equal(1); |
| 44 | + expect(info.lastInsertROWID).to.equal(2); |
| 45 | + }); |
| 46 | + it('should work with DROP TABLE', function () { |
| 47 | + var trans = db.transaction(["DROP TABLE entries"]); |
| 48 | + expect(trans.run().changes).to.equal(0); |
| 49 | + }); |
| 50 | + it('should rollback and throw an exception for failed constraints', function () { |
| 51 | + db.transaction(['CREATE TABLE people (id INTEGER PRIMARY KEY, name TEXT)']).run(); |
| 52 | + db.transaction(['CREATE TABLE ages (age INTEGER, person INTEGER NOT NULL REFERENCES people ON DELETE CASCADE ON UPDATE CASCADE)']).run(); |
| 53 | + db.transaction([ |
| 54 | + "INSERT INTO people VALUES (NULL, 'bob')", |
| 55 | + "INSERT INTO people VALUES (NULL, 'sarah')" |
| 56 | + ]).run(); |
| 57 | + db.transaction([ |
| 58 | + "INSERT INTO ages VALUES (25, 1)", |
| 59 | + "INSERT INTO ages VALUES (30, 2)", |
| 60 | + "INSERT INTO ages VALUES (35, 2)" |
| 61 | + ]).run(); |
| 62 | + var trans = db.transaction([ |
| 63 | + "INSERT INTO ages VALUES (40, 1)", |
| 64 | + "INSERT INTO ages VALUES (30, 3)" |
| 65 | + ]); |
| 66 | + expect(function () {trans.run();}).to.throw(Error); |
| 67 | + trans = db.transaction([ |
| 68 | + "INSERT INTO ages VALUES (40, 1)", |
| 69 | + "INSERT INTO ages VALUES (30, NULL)" |
| 70 | + ]); |
| 71 | + expect(function () {trans.run();}).to.throw(Error); |
| 72 | + expect(db.prepare('SELECT * FROM ages WHERE age==35').get()).to.not.be.undefined; |
| 73 | + expect(db.prepare('SELECT * FROM ages WHERE age==40').get()).to.be.undefined; |
| 74 | + db.transaction([ |
| 75 | + "INSERT INTO ages VALUES (40, 1)", |
| 76 | + "INSERT INTO ages VALUES (30, 2)" |
| 77 | + ]).run(); |
| 78 | + expect(db.prepare('SELECT * FROM ages WHERE age==40').get()).to.not.be.undefined; |
| 79 | + }); |
| 80 | + it('should not count changes from indirect mechanisms', function () { |
| 81 | + var trans = db.transaction(["UPDATE people SET id=55 WHERE id=2"]); |
| 82 | + expect(trans.run().changes).to.equal(1); |
| 83 | + }); |
| 84 | + it('should count accurate DELETE changes when a dropped table has side effects', function () { |
| 85 | + var trans = db.transaction(["DROP TABLE people"]); |
| 86 | + expect(trans.run().changes).to.equal(2); |
| 87 | + }); |
| 88 | + // it('should accept bind parameters', function () { |
| 89 | + // db.transaction("CREATE TABLE entries (a TEXT CHECK(typeof(a)=='text'), b INTEGER CHECK(typeof(b)=='integer' OR typeof(b)=='real'), c REAL CHECK(typeof(c)=='real' OR typeof(c)=='integer'), d BLOB CHECK(typeof(d)=='blob'))").run(); |
| 90 | + // db.transaction('INSERT INTO entries VALUES (?, ?, ?, ?)').run('foo', 25, 25, Buffer.alloc(8).fill(0xdd)); |
| 91 | + // db.transaction('INSERT INTO entries VALUES (?, ?, ?, ?)').run(['foo', 25, 25, Buffer.alloc(8).fill(0xdd)]); |
| 92 | + // db.transaction('INSERT INTO entries VALUES (?, ?, ?, ?)').run(['foo', 25], [25], Buffer.alloc(8).fill(0xdd)); |
| 93 | + // db.transaction('INSERT INTO entries VALUES (@a, @b, @c, @d)').run({a: 'foo', b: 25, c: 25, d: Buffer.alloc(8).fill(0xdd)}); |
| 94 | + // db.transaction('INSERT INTO entries VALUES ($a, $b, $c, $d)').run({a: 'foo', b: 25, c: 25, d: Buffer.alloc(8).fill(0xdd)}); |
| 95 | + // db.transaction('INSERT INTO entries VALUES (:a, :b, :c, :d)').run({a: 'foo', b: 25, c: 25, d: Buffer.alloc(8).fill(0xdd)}); |
| 96 | + // db.transaction('INSERT INTO entries VALUES (?, @a, @a, ?)').run({a: 25}, ['foo'], Buffer.alloc(8).fill(0xdd)); |
| 97 | + // expect(function () { |
| 98 | + // db.transaction('INSERT INTO entries VALUES (?, @a, @a, ?)').run({a: 25}, ['foo'], Buffer.alloc(8).fill(0xdd), Buffer.alloc(8).fill(0xdd)); |
| 99 | + // }).to.throw(Error); |
| 100 | + // expect(function () { |
| 101 | + // db.transaction('INSERT INTO entries VALUES (?, @a, @a, ?)').run({a: 25}, ['foo']); |
| 102 | + // }).to.throw(Error); |
| 103 | + // expect(function () { |
| 104 | + // db.transaction('INSERT INTO entries VALUES (?, @a, @a, ?)').run({a: 25, c: 25}, ['foo'], Buffer.alloc(8).fill(0xdd)); |
| 105 | + // }).to.throw(Error); |
| 106 | + // expect(function () { |
| 107 | + // db.transaction('INSERT INTO entries VALUES (?, @a, @a, ?)').run({}, ['foo'], Buffer.alloc(8).fill(0xdd)); |
| 108 | + // }).to.throw(Error); |
| 109 | + // expect(function () { |
| 110 | + // db.transaction('INSERT INTO entries VALUES (?, ?, ?, ?)').run(25, 'foo', 25, Buffer.alloc(8).fill(0xdd)); |
| 111 | + // }).to.throw(Error); |
| 112 | + // expect(function () { |
| 113 | + // db.transaction('INSERT INTO entries VALUES (?, ?, ?, ?)').run('foo', 25, 25, Buffer.alloc(8).fill(0xdd), {foo: 'foo'}); |
| 114 | + // }).to.throw(Error); |
| 115 | + // db.transaction('INSERT INTO entries VALUES (?, ?, ?, ?)').run('foo', 25, 25, Buffer.alloc(8).fill(0xdd), {}); |
| 116 | + // expect(function () { |
| 117 | + // db.transaction('INSERT INTO entries VALUES (?, ?, ?, ?)').run('foo', 25, 25, {4: Buffer.alloc(8).fill(0xdd)}); |
| 118 | + // }).to.throw(Error); |
| 119 | + // expect(function () { |
| 120 | + // db.transaction('INSERT INTO entries VALUES (?, ?, ?, ?)').run(); |
| 121 | + // }).to.throw(Error); |
| 122 | + // expect(function () { |
| 123 | + // db.transaction('INSERT INTO entries VALUES (?, ?, ?, ?)').run({length: 4, 0: 'foo', 1: 25, 2: 25, 3: Buffer.alloc(8).fill(0xdd)}); |
| 124 | + // }).to.throw(Error); |
| 125 | + // expect(function () { |
| 126 | + // db.transaction('INSERT INTO entries VALUES (?, ?, ?, ?)').run('foo', 25, new Number(25), Buffer.alloc(8).fill(0xdd)); |
| 127 | + // }).to.throw(Error); |
| 128 | + // expect(function () { |
| 129 | + // db.transaction('INSERT INTO entries VALUES (?, ?, ?, ?)').run('foo', {low: 25, high: 25}, 25, Buffer.alloc(8).fill(0xdd)); |
| 130 | + // }).to.throw(Error); |
| 131 | + // function Foo() { |
| 132 | + // this.a = 'foo'; |
| 133 | + // this.b = 25; |
| 134 | + // this.c = 25; |
| 135 | + // this.d = Buffer.alloc(8).fill(0xdd); |
| 136 | + // } |
| 137 | + // expect(function () { |
| 138 | + // db.transaction('INSERT INTO entries VALUES (@a, @b, @c, @d)').run(new Foo); |
| 139 | + // }).to.throw(Error); |
| 140 | + |
| 141 | + // // This part of the test may fail is Statement#get() does not work. |
| 142 | + // var i = 0; |
| 143 | + // var row; |
| 144 | + // while (row = db.transaction('SELECT * FROM entries WHERE rowid=' + ++i).get()) { |
| 145 | + // expect(row).to.deep.equal({a: 'foo', b: 25, c: 25, d: Buffer.alloc(8).fill(0xdd)}) |
| 146 | + // } |
| 147 | + // expect(i).to.equal(9); |
| 148 | + // }); |
| 149 | +}); |
0 commit comments