|
| 1 | +var expect = require('chai').expect; |
| 2 | +var Database = require('../.'); |
| 3 | +var util = require('../tools/test-util.js'); |
| 4 | + |
| 5 | +describe('Database#transaction()', function () { |
| 6 | + it('should throw an exception if an array of strings is not provided', function (done) { |
| 7 | + var db = new Database(util.next()); |
| 8 | + db.on('open', function () { |
| 9 | + expect(function () {db.transaction(123);}).to.throw(TypeError); |
| 10 | + expect(function () {db.transaction(0);}).to.throw(TypeError); |
| 11 | + expect(function () {db.transaction(null);}).to.throw(TypeError); |
| 12 | + expect(function () {db.transaction();}).to.throw(TypeError); |
| 13 | + expect(function () {db.transaction(new String('CREATE TABLE people (name TEXT)'));}).to.throw(TypeError); |
| 14 | + expect(function () {db.transaction({0: 'CREATE TABLE people (name TEXT)', length: 1});}).to.throw(TypeError); |
| 15 | + expect(function () {db.transaction([123]);}).to.throw(TypeError); |
| 16 | + expect(function () {db.transaction([0]);}).to.throw(TypeError); |
| 17 | + expect(function () {db.transaction([null]);}).to.throw(TypeError); |
| 18 | + expect(function () {db.transaction([new String('CREATE TABLE people (name TEXT)')]);}).to.throw(TypeError); |
| 19 | + done(); |
| 20 | + }); |
| 21 | + }); |
| 22 | + it('should throw an exception if no strings are provided', function (done) { |
| 23 | + var db = new Database(util.next()); |
| 24 | + db.on('open', function () { |
| 25 | + expect(function () {db.transaction([]);}).to.throw(TypeError); |
| 26 | + done(); |
| 27 | + }); |
| 28 | + }); |
| 29 | + it('should propagate exceptions thrown from array accessors', function (done) { |
| 30 | + var db = new Database(util.next()); |
| 31 | + var err = new Error('foobar'); |
| 32 | + var arr = ['foo']; |
| 33 | + Object.defineProperty(arr, '0', {get: function () {throw err;}}); |
| 34 | + db.on('open', function () { |
| 35 | + expect(function () {db.transaction(arr);}).to.throw(err); |
| 36 | + done(); |
| 37 | + }); |
| 38 | + }); |
| 39 | + it('should throw an exception if invalid SQL is provided', function (done) { |
| 40 | + var db = new Database(util.next()); |
| 41 | + db.on('open', function () { |
| 42 | + expect(function () {db.transaction(['CREATE TABLE people (name TEXT']);}).to.throw(Error); |
| 43 | + expect(function () {db.transaction(['INSERT INTO people VALUES (?)']);}).to.throw(Error); |
| 44 | + done(); |
| 45 | + }); |
| 46 | + }); |
| 47 | + it('should throw an exception if a string contains no statements', function (done) { |
| 48 | + var db = new Database(util.next()); |
| 49 | + db.on('open', function () { |
| 50 | + expect(function () {db.transaction(['']);}).to.throw(TypeError); |
| 51 | + expect(function () {db.transaction([';']);}).to.throw(TypeError); |
| 52 | + expect(function () {db.transaction(['CREATE TABLE people (name TEXT)', '']);}).to.throw(TypeError); |
| 53 | + expect(function () {db.transaction(['CREATE TABLE people (name TEXT)', ';']);}).to.throw(TypeError); |
| 54 | + done(); |
| 55 | + }); |
| 56 | + }); |
| 57 | + it('should throw an exception if multiple statements exist in one string', function (done) { |
| 58 | + var db = new Database(util.next()); |
| 59 | + db.on('open', function () { |
| 60 | + expect(function () {db.transaction(['CREATE TABLE people (name TEXT);CREATE TABLE animals (name TEXT)']);}).to.throw(TypeError); |
| 61 | + expect(function () {db.transaction(['CREATE TABLE people (name TEXT); ']);}).to.throw(TypeError); |
| 62 | + expect(function () {db.transaction(['CREATE TABLE people (name TEXT);;']);}).to.throw(TypeError); |
| 63 | + done(); |
| 64 | + }); |
| 65 | + }); |
| 66 | + it('should throw an exception if any read-only statements are provided', function (done) { |
| 67 | + var db = new Database(util.next()); |
| 68 | + db.on('open', function () { |
| 69 | + expect(function () {db.transaction(['SELECT 555']);}).to.throw(TypeError); |
| 70 | + expect(function () {db.transaction(['BEGIN TRANSACTION']);}).to.throw(TypeError); |
| 71 | + expect(function () {db.transaction(['COMMIT TRANSACTION']);}).to.throw(TypeError); |
| 72 | + expect(function () {db.transaction(['ROLLBACK TRANSACTION']);}).to.throw(TypeError); |
| 73 | + expect(function () {db.transaction(['CREATE TABLE people (name TEXT)', 'SELECT 555']);}).to.throw(TypeError); |
| 74 | + expect(function () {db.transaction(['SELECT 555', 'CREATE TABLE people (name TEXT)']);}).to.throw(TypeError); |
| 75 | + expect(function () {db.transaction(['CREATE TABLE people (name TEXT)', 'BEGIN TRANSACTION']);}).to.throw(TypeError); |
| 76 | + expect(function () {db.transaction(['CREATE TABLE people (name TEXT)', 'COMMIT TRANSACTION']);}).to.throw(TypeError); |
| 77 | + expect(function () {db.transaction(['CREATE TABLE people (name TEXT)', 'ROLLBACK TRANSACTION']);}).to.throw(TypeError); |
| 78 | + done(); |
| 79 | + }); |
| 80 | + }); |
| 81 | + it('should create a prepared Transaction object', function (done) { |
| 82 | + var db = new Database(util.next()); |
| 83 | + db.on('open', function () { |
| 84 | + var trans1 = db.transaction(['CREATE TABLE people (name TEXT)']); |
| 85 | + var trans2 = db.transaction(['CREATE TABLE people (name TEXT)', 'CREATE TABLE animals (name TEXT);']); |
| 86 | + var trans3 = db.transaction(['CREATE TABLE people (name TEXT);', 'CREATE TABLE animals (name TEXT)']); |
| 87 | + expect(trans1.source).to.equal('CREATE TABLE people (name TEXT);'); |
| 88 | + expect(trans2.source).to.equal('CREATE TABLE people (name TEXT);\nCREATE TABLE animals (name TEXT);'); |
| 89 | + expect(trans2.source).to.equal(trans3.source); |
| 90 | + expect(trans1.constructor.name).to.equal('Transaction'); |
| 91 | + expect(trans2.constructor.name).to.equal('Transaction'); |
| 92 | + expect(trans3.constructor.name).to.equal('Transaction'); |
| 93 | + expect(trans1.database).to.equal(db); |
| 94 | + expect(trans2.database).to.equal(db); |
| 95 | + expect(trans3.database).to.equal(db); |
| 96 | + expect(function () { |
| 97 | + new trans1.constructor(['CREATE TABLE people (name TEXT)']); |
| 98 | + }).to.throw(TypeError); |
| 99 | + expect(trans1).to.not.equal(trans2); |
| 100 | + expect(trans1).to.not.equal(trans3); |
| 101 | + expect(trans2).to.not.equal(trans3); |
| 102 | + expect(trans1).to.not.equal(db.transaction(['CREATE TABLE people (name TEXT)'])); |
| 103 | + done(); |
| 104 | + }); |
| 105 | + }); |
| 106 | +}); |
0 commit comments