|
| 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('Statement#all()', function () { |
| 12 | + it('should throw an exception when used on a write statement', function () { |
| 13 | + db.prepare('CREATE TABLE entries (a TEXT, b INTEGER, c REAL, d BLOB, e TEXT)').run(); |
| 14 | + var stmt = db.prepare("INSERT INTO entries VALUES ('foo', 1, 3.14, x'dddddddd', NULL)"); |
| 15 | + expect(stmt.readonly).to.be.false; |
| 16 | + expect(function () {stmt.all();}).to.throw(TypeError); |
| 17 | + }); |
| 18 | + it('should return the first matching row', function () { |
| 19 | + db.prepare("INSERT INTO entries WITH RECURSIVE temp(a, b, c, d, e) AS (SELECT 'foo', 1, 3.14, x'dddddddd', NULL UNION ALL SELECT a, b + 1, c, d, e FROM temp LIMIT 10) SELECT * FROM temp").run(); |
| 20 | + var row = {a: 'foo', b: 1, c: 3.14, d: Buffer.alloc(4).fill(0xdd), e: null}; |
| 21 | + |
| 22 | + var stmt = db.prepare("SELECT * FROM entries"); |
| 23 | + expect(stmt.readonly).to.be.true; |
| 24 | + matchesFrom(stmt.all(), 1); |
| 25 | + |
| 26 | + stmt = db.prepare("SELECT * FROM entries WHERE b > 5"); |
| 27 | + matchesFrom(stmt.all(), 6); |
| 28 | + |
| 29 | + function matchesFrom(rows, i) { |
| 30 | + for (var index = 0; i<=10; ++i, ++index) { |
| 31 | + row.b = i; |
| 32 | + expect(rows[index]).to.deep.equal(row); |
| 33 | + } |
| 34 | + expect(index).to.equal(rows.length); |
| 35 | + } |
| 36 | + }); |
| 37 | + it('should obey the current pluck setting', function () { |
| 38 | + var stmt = db.prepare("SELECT * FROM entries"); |
| 39 | + var plucked = new Array(10).fill('foo'); |
| 40 | + expect(stmt.all()).to.not.deep.equal(plucked); |
| 41 | + expect(stmt.pluck(true).all()).to.deep.equal(plucked); |
| 42 | + expect(stmt.all()).to.deep.equal(plucked); |
| 43 | + expect(stmt.pluck(false).all()).to.not.deep.equal(plucked); |
| 44 | + expect(stmt.all()).to.not.deep.equal(plucked); |
| 45 | + expect(stmt.pluck().all()).to.deep.equal(plucked); |
| 46 | + expect(stmt.all()).to.deep.equal(plucked); |
| 47 | + }); |
| 48 | + it('should return an empty array when no rows were found', function () { |
| 49 | + var stmt = db.prepare("SELECT * FROM entries WHERE b == 999"); |
| 50 | + expect(stmt.all()).to.deep.equal([]); |
| 51 | + expect(stmt.pluck().all()).to.deep.equal([]); |
| 52 | + }); |
| 53 | + it('should accept bind parameters', function () { |
| 54 | + var rows = [{a: 'foo', b: 1, c: 3.14, d: Buffer.alloc(4).fill(0xdd), e: null}]; |
| 55 | + var SQL1 = 'SELECT * FROM entries WHERE a=? AND b=? AND c=? AND d=? AND e IS ?'; |
| 56 | + var SQL2 = 'SELECT * FROM entries WHERE a=@a AND b=@b AND c=@c AND d=@d AND e IS @e'; |
| 57 | + var result = db.prepare(SQL1).all('foo', 1, 3.14, Buffer.alloc(4).fill(0xdd), null); |
| 58 | + expect(result).to.deep.equal(rows); |
| 59 | + |
| 60 | + result = db.prepare(SQL1).all(['foo', 1, 3.14, Buffer.alloc(4).fill(0xdd), null]); |
| 61 | + expect(result).to.deep.equal(rows); |
| 62 | + |
| 63 | + result = db.prepare(SQL1).all(['foo', 1], [3.14], Buffer.alloc(4).fill(0xdd), [,]); |
| 64 | + expect(result).to.deep.equal(rows); |
| 65 | + |
| 66 | + result = db.prepare(SQL2).all({a: 'foo', b: 1, c: 3.14, d: Buffer.alloc(4).fill(0xdd), e: undefined}); |
| 67 | + expect(result).to.deep.equal(rows); |
| 68 | + |
| 69 | + result = db.prepare(SQL2).all({a: 'foo', b: 1, c: 3.14, d: Buffer.alloc(4).fill(0xaa), e: undefined}); |
| 70 | + expect(result).to.deep.equal([]); |
| 71 | + |
| 72 | + expect(function () { |
| 73 | + db.prepare(SQL2).all({a: 'foo', b: 1, c: 3.14, d: Buffer.alloc(4).fill(0xdd)}); |
| 74 | + }).to.throw(Error); |
| 75 | + |
| 76 | + expect(function () { |
| 77 | + db.prepare(SQL1).all(); |
| 78 | + }).to.throw(Error); |
| 79 | + |
| 80 | + expect(function () { |
| 81 | + db.prepare(SQL2).all({}); |
| 82 | + }).to.throw(Error); |
| 83 | + }); |
| 84 | +}); |
0 commit comments