|
| 1 | +var expect = require('chai').expect; |
| 2 | +var fs = require('fs'); |
| 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('Database#close()', function () { |
| 12 | + it('should prevent statements and transactions from operating', function (done) { |
| 13 | + db.prepare('CREATE TABLE people (name TEXT)').run(); |
| 14 | + var stmt1 = db.prepare('SELECT * FROM people'); |
| 15 | + var stmt2 = db.prepare("INSERT INTO people VALUES ('foobar')"); |
| 16 | + var trans = db.transaction(["INSERT INTO people VALUES ('foobar')"]); |
| 17 | + |
| 18 | + db.prepare('SELECT * FROM people').bind(); |
| 19 | + db.prepare("INSERT INTO people VALUES ('foobar')").bind(); |
| 20 | + db.transaction(["INSERT INTO people VALUES ('foobar')"]).bind(); |
| 21 | + db.prepare('SELECT * FROM people').get(); |
| 22 | + db.prepare('SELECT * FROM people').all(); |
| 23 | + db.prepare('SELECT * FROM people').each(function () {}); |
| 24 | + db.prepare("INSERT INTO people VALUES ('foobar')").run(); |
| 25 | + db.transaction(["INSERT INTO people VALUES ('foobar')"]).run(); |
| 26 | + |
| 27 | + db.close(); |
| 28 | + |
| 29 | + expect(function () {stmt1.bind();}).to.throw(Error); |
| 30 | + expect(function () {stmt2.bind();}).to.throw(Error); |
| 31 | + expect(function () {trans.bind();}).to.throw(Error); |
| 32 | + expect(function () {stmt1.get();}).to.throw(Error); |
| 33 | + expect(function () {stmt1.all();}).to.throw(Error); |
| 34 | + expect(function () {stmt1.each(function () {});}).to.throw(Error); |
| 35 | + expect(function () {stmt2.run();}).to.throw(Error); |
| 36 | + expect(function () {trans.run();}).to.throw(Error); |
| 37 | + |
| 38 | + db.on('close', function () { |
| 39 | + expect(function () {stmt1.bind();}).to.throw(Error); |
| 40 | + expect(function () {stmt2.bind();}).to.throw(Error); |
| 41 | + expect(function () {trans.bind();}).to.throw(Error); |
| 42 | + expect(function () {stmt1.get();}).to.throw(Error); |
| 43 | + expect(function () {stmt1.all();}).to.throw(Error); |
| 44 | + expect(function () {stmt1.each(function () {});}).to.throw(Error); |
| 45 | + expect(function () {stmt2.run();}).to.throw(Error); |
| 46 | + expect(function () {trans.run();}).to.throw(Error); |
| 47 | + done(); |
| 48 | + }); |
| 49 | + }); |
| 50 | +}); |
0 commit comments