|
| 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#bind()', function () { |
| 12 | + it('should permanently bind the given parameters', function () { |
| 13 | + db.transaction(['CREATE TABLE entries (a TEXT, b INTEGER, c BLOB)']).run(); |
| 14 | + var trans = db.transaction(['INSERT INTO entries VALUES (?, ?, ?)']); |
| 15 | + var buffer = Buffer.alloc(4).fill(0xdd); |
| 16 | + trans.bind('foobar', 25, buffer) |
| 17 | + trans.run(); |
| 18 | + buffer.fill(0xaa); |
| 19 | + trans.run(); |
| 20 | + var row1 = db.prepare('SELECT * FROM entries WHERE rowid=1').get(); |
| 21 | + var row2 = db.prepare('SELECT * FROM entries WHERE rowid=2').get(); |
| 22 | + expect(row1.a).to.equal(row2.a); |
| 23 | + expect(row1.b).to.equal(row2.b); |
| 24 | + expect(row1.c).to.deep.equal(row2.c); |
| 25 | + }); |
| 26 | + it('should not allow you to bind temporary parameters afterwards', function () { |
| 27 | + var trans = db.transaction(['INSERT INTO entries VALUES (?, ?, ?)']); |
| 28 | + var buffer = Buffer.alloc(4).fill(0xdd); |
| 29 | + trans.bind('foobar', 25, buffer) |
| 30 | + expect(function () {trans.run(null);}).to.throw(TypeError); |
| 31 | + expect(function () {trans.run(buffer);}).to.throw(TypeError); |
| 32 | + expect(function () {trans.run('foobar', 25, buffer);}).to.throw(TypeError); |
| 33 | + }); |
| 34 | + it('should throw an exception when invoked twice on the same transaction', function () { |
| 35 | + var trans = db.transaction(['INSERT INTO entries VALUES (?, ?, ?)']); |
| 36 | + trans.bind('foobar', 25, null); |
| 37 | + expect(function () {trans.bind('foobar', 25, null);}).to.throw(TypeError); |
| 38 | + expect(function () {trans.bind();}).to.throw(TypeError); |
| 39 | + |
| 40 | + trans = db.transaction(["INSERT INTO entries VALUES ('foobar', 25, null)"]); |
| 41 | + trans.bind(); |
| 42 | + expect(function () {trans.bind();}).to.throw(TypeError); |
| 43 | + }); |
| 44 | + it('should throw an exception when invoked after the first execution', function () { |
| 45 | + var trans = db.transaction(["INSERT INTO entries VALUES ('foobar', 25, NULL)"]); |
| 46 | + trans.run(); |
| 47 | + expect(function () {trans.bind();}).to.throw(TypeError); |
| 48 | + |
| 49 | + trans = db.transaction(["INSERT INTO entries VALUES ('foobar', 25, NULL)"]); |
| 50 | + trans.bind(); |
| 51 | + }); |
| 52 | + it('should throw an exception when invalid parameters are given', function () { |
| 53 | + var trans = db.transaction(['INSERT INTO entries VALUES (?, ?, ?)']); |
| 54 | + |
| 55 | + expect(function () { |
| 56 | + trans.bind('foo', 25); |
| 57 | + }).to.throw(Error); |
| 58 | + |
| 59 | + expect(function () { |
| 60 | + trans.bind('foo', 25, null, null); |
| 61 | + }).to.throw(Error); |
| 62 | + |
| 63 | + expect(function () { |
| 64 | + trans.bind('foo', new Number(25), null); |
| 65 | + }).to.throw(Error); |
| 66 | + |
| 67 | + expect(function () { |
| 68 | + trans.bind(); |
| 69 | + }).to.throw(Error); |
| 70 | + |
| 71 | + trans.bind('foo', 25, null); |
| 72 | + |
| 73 | + trans = db.transaction(['INSERT INTO entries VALUES (@a, @a, ?)']); |
| 74 | + |
| 75 | + expect(function () { |
| 76 | + trans.bind({a: '123'}); |
| 77 | + }).to.throw(Error); |
| 78 | + |
| 79 | + expect(function () { |
| 80 | + trans.bind({a: '123', 1: null}); |
| 81 | + }).to.throw(Error); |
| 82 | + |
| 83 | + expect(function () { |
| 84 | + trans.bind({a: '123', b: null}, null); |
| 85 | + }).to.throw(Error); |
| 86 | + |
| 87 | + expect(function () { |
| 88 | + trans.bind({a: '123'}, null, null); |
| 89 | + }).to.throw(Error); |
| 90 | + |
| 91 | + trans.bind({a: '123'}, null); |
| 92 | + }); |
| 93 | +}); |
0 commit comments