Skip to content

Commit 2c11372

Browse files
committed
added tests for new returnsData behavior
1 parent 8a63a49 commit 2c11372

File tree

6 files changed

+61
-22
lines changed

6 files changed

+61
-22
lines changed

src/objects/database/create-statement.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ NAN_METHOD(Database::CreateStatement) {
4646
stmt->column_count = 0;
4747
} else {
4848
stmt->column_count = column_count;
49-
stmt->state |= RETURNS_DATA
49+
stmt->state |= RETURNS_DATA;
5050
}
5151
Nan::ForceSet(statement, NEW_INTERNAL_STRING_FAST("source"), source, FROZEN);
5252
Nan::ForceSet(statement, NEW_INTERNAL_STRING_FAST("database"), info.This(), FROZEN);

test/13.database.prepare.js

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,21 +49,12 @@ describe('Database#prepare()', function () {
4949
done();
5050
});
5151
});
52-
it('should throw an exception if the read-only statement returns no columns', function (done) {
53-
var db = new Database(util.next());
54-
db.on('open', function () {
55-
expect(function () {db.prepare('BEGIN TRANSACTION');}).to.throw(TypeError);
56-
expect(function () {db.prepare('COMMIT TRANSACTION');}).to.throw(TypeError);
57-
expect(function () {db.prepare('ROLLBACK TRANSACTION');}).to.throw(TypeError);
58-
done();
59-
});
60-
});
6152
it('should create a prepared Statement object', function (done) {
6253
function assertStmt(stmt, source) {
6354
expect(stmt.source).to.equal(source);
6455
expect(stmt.constructor.name).to.equal('Statement');
6556
expect(stmt.database).to.equal(db);
66-
expect(stmt.readonly).to.equal(false);
57+
expect(stmt.returnsData).to.equal(false);
6758
expect(function () {
6859
new stmt.constructor(source);
6960
}).to.throw(TypeError);
@@ -86,7 +77,7 @@ describe('Database#prepare()', function () {
8677
expect(stmt.source).to.equal('SELECT 555');
8778
expect(stmt.constructor.name).to.equal('Statement');
8879
expect(stmt.database).to.equal(db);
89-
expect(stmt.readonly).to.equal(true);
80+
expect(stmt.returnsData).to.equal(true);
9081
expect(function () {
9182
new stmt.constructor('SELECT 555');
9283
}).to.throw(TypeError);

test/20.statement.run.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ before(function (done) {
99
});
1010

1111
describe('Statement#run()', function () {
12-
it('should throw an exception when used on a read-only statement', function () {
12+
it('should throw an exception when used on a statement that returns data', function () {
1313
var stmt = db.prepare('SELECT 555');
1414
expect(function () {stmt.run();}).to.throw(TypeError);
1515
});
@@ -19,6 +19,12 @@ describe('Statement#run()', function () {
1919
expect(info.changes).to.equal(0);
2020
expect(info.lastInsertROWID).to.equal(0);
2121
});
22+
it('should work with CREATE TABLE IF NOT EXISTS', function () {
23+
var stmt = db.prepare('CREATE TABLE IF NOT EXISTS entries (a TEXT, b INTEGER, c REAL, d BLOB)');
24+
var info = stmt.run();
25+
expect(info.changes).to.equal(0);
26+
expect(info.lastInsertROWID).to.equal(0);
27+
});
2228
it('should work with INSERT INTO', function () {
2329
var stmt = db.prepare("INSERT INTO entries VALUES ('foo', 25, 3.14, x'1133ddff')");
2430
var info = stmt.run();
@@ -47,6 +53,13 @@ describe('Statement#run()', function () {
4753
expect(info.changes).to.equal(1);
4854
expect(info.lastInsertROWID).to.equal(2);
4955
});
56+
it('should work with BEGIN and COMMIT', function () {
57+
expect(db.prepare("BEGIN TRANSACTION").run().changes).to.equal(0);
58+
var info = db.prepare("INSERT INTO entries VALUES ('foo', 25, 3.14, x'1133ddff')").run();
59+
expect(info.changes).to.equal(1);
60+
expect(info.lastInsertROWID).to.equal(3);
61+
expect(db.prepare("COMMIT TRANSACTION").run().changes).to.equal(0);
62+
});
5063
it('should work with DROP TABLE', function () {
5164
var stmt = db.prepare("DROP TABLE entries");
5265
expect(stmt.run().changes).to.equal(0);
@@ -64,6 +77,13 @@ describe('Statement#run()', function () {
6477
stmt = db.prepare("INSERT INTO ages VALUES (30, NULL)");
6578
expect(function () {stmt.run();}).to.throw(Error);
6679
});
80+
it('should allow ad-hoc transactions', function () {
81+
expect(db.prepare("BEGIN TRANSACTION").run().changes).to.equal(0);
82+
expect(db.prepare("INSERT INTO ages VALUES (45, 2)").run().changes).to.equal(1);
83+
var stmt = db.prepare("INSERT INTO ages VALUES (30, 3)");
84+
expect(function () {stmt.run()}).to.throw(Error);
85+
expect(db.prepare("ROLLBACK TRANSACTION").run().changes).to.equal(0);
86+
});
6787
it('should not count changes from indirect mechanisms', function () {
6888
var stmt = db.prepare("UPDATE people SET id=55 WHERE id=2");
6989
expect(stmt.run().changes).to.equal(1);

test/21.statement.get.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,26 @@ before(function (done) {
99
});
1010

1111
describe('Statement#get()', function () {
12-
it('should throw an exception when used on a write statement', function () {
12+
it('should throw an exception when used on a statement that returns no data', function () {
1313
db.prepare('CREATE TABLE entries (a TEXT, b INTEGER, c REAL, d BLOB, e TEXT)').run();
14+
1415
var stmt = db.prepare("INSERT INTO entries VALUES ('foo', 1, 3.14, x'dddddddd', NULL)");
15-
expect(stmt.readonly).to.be.false;
16+
expect(stmt.returnsData).to.be.false;
17+
expect(function () {stmt.get();}).to.throw(TypeError);
18+
19+
var stmt = db.prepare("CREATE TABLE IF NOT EXISTS entries (a TEXT, b INTEGER, c REAL, d BLOB, e TEXT)");
20+
expect(stmt.returnsData).to.be.false;
21+
expect(function () {stmt.get();}).to.throw(TypeError);
22+
23+
var stmt = db.prepare("BEGIN TRANSACTION");
24+
expect(stmt.returnsData).to.be.false;
1625
expect(function () {stmt.get();}).to.throw(TypeError);
1726
});
1827
it('should return the first matching row', function () {
1928
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();
2029

2130
var stmt = db.prepare("SELECT * FROM entries");
22-
expect(stmt.readonly).to.be.true;
31+
expect(stmt.returnsData).to.be.true;
2332
expect(stmt.get()).to.deep.equal({a: 'foo', b: 1, c: 3.14, d: Buffer.alloc(4).fill(0xdd), e: null});
2433

2534
stmt = db.prepare("SELECT * FROM entries WHERE b > 5");

test/22.statement.all.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,27 @@ before(function (done) {
99
});
1010

1111
describe('Statement#all()', function () {
12-
it('should throw an exception when used on a write statement', function () {
12+
it('should throw an exception when used on a statement that returns no data', function () {
1313
db.prepare('CREATE TABLE entries (a TEXT, b INTEGER, c REAL, d BLOB, e TEXT)').run();
14+
1415
var stmt = db.prepare("INSERT INTO entries VALUES ('foo', 1, 3.14, x'dddddddd', NULL)");
15-
expect(stmt.readonly).to.be.false;
16+
expect(stmt.returnsData).to.be.false;
17+
expect(function () {stmt.all();}).to.throw(TypeError);
18+
19+
var stmt = db.prepare("CREATE TABLE IF NOT EXISTS entries (a TEXT, b INTEGER, c REAL, d BLOB, e TEXT)");
20+
expect(stmt.returnsData).to.be.false;
21+
expect(function () {stmt.all();}).to.throw(TypeError);
22+
23+
var stmt = db.prepare("BEGIN TRANSACTION");
24+
expect(stmt.returnsData).to.be.false;
1625
expect(function () {stmt.all();}).to.throw(TypeError);
1726
});
1827
it('should return an array of every matching row', function () {
1928
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();
2029
var row = {a: 'foo', b: 1, c: 3.14, d: Buffer.alloc(4).fill(0xdd), e: null};
2130

2231
var stmt = db.prepare("SELECT * FROM entries");
23-
expect(stmt.readonly).to.be.true;
32+
expect(stmt.returnsData).to.be.true;
2433
matchesFrom(stmt.all(), 1);
2534

2635
stmt = db.prepare("SELECT * FROM entries WHERE b > 5");

test/23.statement.each.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,21 @@ before(function (done) {
99
});
1010

1111
describe('Statement#each()', function () {
12-
it('should throw an exception when used on a write statement', function () {
12+
it('should throw an exception when used on a statement that returns no data', function () {
1313
db.prepare('CREATE TABLE entries (a TEXT, b INTEGER, c REAL, d BLOB, e TEXT)').run();
14+
1415
var stmt = db.prepare("INSERT INTO entries VALUES ('foo', 1, 3.14, x'dddddddd', NULL)");
15-
expect(stmt.readonly).to.be.false;
16+
expect(stmt.returnsData).to.be.false;
17+
expect(function () {stmt.each(function () {});}).to.throw(TypeError);
18+
19+
var stmt = db.prepare("CREATE TABLE IF NOT EXISTS entries (a TEXT, b INTEGER, c REAL, d BLOB, e TEXT)");
20+
expect(stmt.returnsData).to.be.false;
1621
expect(function () {stmt.each(function () {});}).to.throw(TypeError);
22+
23+
var stmt = db.prepare("BEGIN TRANSACTION");
24+
expect(stmt.returnsData).to.be.false;
25+
expect(function () {stmt.each(function () {});}).to.throw(TypeError);
26+
1727
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();
1828
});
1929
it('should throw an exception when the last argument is not a function', function () {
@@ -28,7 +38,7 @@ describe('Statement#each()', function () {
2838

2939
var count = 0;
3040
var stmt = db.prepare("SELECT * FROM entries");
31-
expect(stmt.readonly).to.be.true;
41+
expect(stmt.returnsData).to.be.true;
3242
var ret = stmt.each(function (data) {
3343
row.b = ++count;
3444
expect(data).to.deep.equal(row);

0 commit comments

Comments
 (0)