Skip to content

Commit db8ac0d

Browse files
committed
added .get tests
1 parent bb5c626 commit db8ac0d

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed

TODO

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@ Int64 tests should cover the default safeIntegers state for statements and trans
99

1010
a second database#close test file should test every statement and transaction method on a closed database
1111

12-
.run tests should test the accuracy of the "changes" property in INSERT, DELETE, UPDATE, CREATE TABLE, and DROP TABLE operations
12+
.run tests should test the accuracy of the "changes" property in INSERT, DELETE, UPDATE, CREATE TABLE, and DROP TABLE operations
13+
14+
all read-only execution tests should also test .pluck

test/21.statement.get.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
'use strict';
2+
var expect = require('chai').expect;
3+
var Database = require('../.');
4+
var util = require('../tools/test-util.js');
5+
var db;
6+
7+
before(function (done) {
8+
db = new Database(util.next());
9+
db.on('open', done);
10+
});
11+
12+
describe('Statement#get()', function () {
13+
it('should throw an exception when used on a write statement', function () {
14+
db.prepare('CREATE TABLE entries (a TEXT, b INTEGER, c REAL, d BLOB, e TEXT)').run();
15+
var stmt = db.prepare("INSERT INTO entries VALUES ('foo', 1, 3.14, x'dddddddd', NULL)");
16+
expect(stmt.readonly).to.be.false;
17+
expect(function () {stmt.get();}).to.throw(TypeError);
18+
});
19+
it('should return the first matching row', function () {
20+
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();
21+
22+
var stmt = db.prepare("SELECT * FROM entries");
23+
expect(stmt.readonly).to.be.true;
24+
expect(stmt.get()).to.deep.equal({a: 'foo', b: 1, c: 3.14, d: Buffer.alloc(4).fill(0xdd), e: null});
25+
26+
stmt = db.prepare("SELECT * FROM entries WHERE b > 5");
27+
expect(stmt.get()).to.deep.equal({a: 'foo', b: 6, c: 3.14, d: Buffer.alloc(4).fill(0xdd), e: null});
28+
});
29+
it('should obey the current pluck setting', function () {
30+
var stmt = db.prepare("SELECT * FROM entries");
31+
var row = {a: 'foo', b: 1, c: 3.14, d: Buffer.alloc(4).fill(0xdd), e: null};
32+
expect(stmt.get()).to.deep.equal(row);
33+
expect(stmt.pluck(true).get()).to.equal('foo');
34+
expect(stmt.get()).to.equal('foo');
35+
expect(stmt.pluck(false).get()).to.deep.equal(row);
36+
expect(stmt.get()).to.deep.equal(row);
37+
expect(stmt.pluck().get()).to.equal('foo');
38+
expect(stmt.get()).to.equal('foo');
39+
});
40+
it('should return undefined when no rows were found', function () {
41+
var stmt = db.prepare("SELECT * FROM entries WHERE b == 999");
42+
expect(stmt.get()).to.be.undefined;
43+
expect(stmt.pluck().get()).to.be.undefined;
44+
});
45+
it('should accept bind parameters', function () {
46+
var row = {a: 'foo', b: 1, c: 3.14, d: Buffer.alloc(4).fill(0xdd), e: null};
47+
var SQL1 = 'SELECT * FROM entries WHERE a=? AND b=? AND c=? AND d=? AND e IS ?';
48+
var SQL2 = 'SELECT * FROM entries WHERE a=@a AND b=@b AND c=@c AND d=@d AND e IS @e';
49+
var result = db.prepare(SQL1).get('foo', 1, 3.14, Buffer.alloc(4).fill(0xdd), null);
50+
expect(result).to.deep.equal(row);
51+
52+
result = db.prepare(SQL1).get(['foo', 1, 3.14, Buffer.alloc(4).fill(0xdd), null]);
53+
expect(result).to.deep.equal(row);
54+
55+
result = db.prepare(SQL1).get(['foo', 1], [3.14], Buffer.alloc(4).fill(0xdd), [,]);
56+
expect(result).to.deep.equal(row);
57+
58+
result = db.prepare(SQL2).get({a: 'foo', b: 1, c: 3.14, d: Buffer.alloc(4).fill(0xdd), e: undefined});
59+
expect(result).to.deep.equal(row);
60+
61+
result = db.prepare(SQL2).get({a: 'foo', b: 1, c: 3.14, d: Buffer.alloc(4).fill(0xaa), e: undefined});
62+
expect(result).to.be.undefined;
63+
64+
expect(function () {
65+
db.prepare(SQL2).get({a: 'foo', b: 1, c: 3.14, d: Buffer.alloc(4).fill(0xdd)});
66+
}).to.throw(Error);
67+
68+
expect(function () {
69+
db.prepare(SQL1).get();
70+
}).to.throw(Error);
71+
72+
expect(function () {
73+
db.prepare(SQL2).get({});
74+
}).to.throw(Error);
75+
});
76+
});

0 commit comments

Comments
 (0)