Skip to content

Commit f9e5552

Browse files
committed
added more tests
1 parent a5126bf commit f9e5552

File tree

4 files changed

+202
-0
lines changed

4 files changed

+202
-0
lines changed

TODO

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,12 @@
11
write tests
22
compare tests to node-sqlite3
3+
4+
.each tests should cover the limiting effects of the in_each property
5+
6+
Int64 tests should cover the default safeIntegers state for statements and transactions
7+
8+
.run, .get, .all, and .each tests should verify that their requirements match Statement#readonly
9+
10+
a second database#close test file should test every statement and transaction method on a closed database
11+
12+
.run tests should test the accuracy of the "changes" property in INSERT, DELETE, UPDATE, CREATE TABLE, and DROP TABLE operations

test/13.database.prepare.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
var expect = require('chai').expect;
2+
var Database = require('../.');
3+
var util = require('../tools/test-util.js');
4+
5+
describe('Database#prepare()', function () {
6+
it('should throw an exception if a string is not provided', function (done) {
7+
var db = new Database(util.next());
8+
db.on('open', function () {
9+
expect(function () {db.prepare(123);}).to.throw(TypeError);
10+
expect(function () {db.prepare(0);}).to.throw(TypeError);
11+
expect(function () {db.prepare(null);}).to.throw(TypeError);
12+
expect(function () {db.prepare();}).to.throw(TypeError);
13+
expect(function () {db.prepare(new String('CREATE TABLE people (name TEXT)'));}).to.throw(TypeError);
14+
done();
15+
});
16+
});
17+
it('should throw an exception if invalid SQL is provided', function (done) {
18+
var db = new Database(util.next());
19+
db.on('open', function () {
20+
expect(function () {db.prepare('CREATE TABLE people (name TEXT');}).to.throw(Error);
21+
expect(function () {db.prepare('INSERT INTO people VALUES (?)');}).to.throw(Error);
22+
done();
23+
});
24+
});
25+
it('should throw an exception if no statements are provided', function (done) {
26+
var db = new Database(util.next());
27+
db.on('open', function () {
28+
expect(function () {db.prepare('');}).to.throw(TypeError);
29+
expect(function () {db.prepare(';');}).to.throw(TypeError);
30+
done();
31+
});
32+
});
33+
it('should throw an exception if more than one statement is provided', function (done) {
34+
var db = new Database(util.next());
35+
db.on('open', function () {
36+
expect(function () {db.prepare('CREATE TABLE people (name TEXT);CREATE TABLE animals (name TEXT)');}).to.throw(TypeError);
37+
expect(function () {db.prepare('CREATE TABLE people (name TEXT); ');}).to.throw(TypeError);
38+
expect(function () {db.prepare('CREATE TABLE people (name TEXT);;');}).to.throw(TypeError);
39+
done();
40+
});
41+
});
42+
it('should throw an exception if the read-only statement returns no columns', function (done) {
43+
var db = new Database(util.next());
44+
db.on('open', function () {
45+
expect(function () {db.prepare('BEGIN TRANSACTION');}).to.throw(TypeError);
46+
expect(function () {db.prepare('COMMIT TRANSACTION');}).to.throw(TypeError);
47+
expect(function () {db.prepare('ROLLBACK TRANSACTION');}).to.throw(TypeError);
48+
done();
49+
});
50+
});
51+
it('should create a prepared Statement object', function (done) {
52+
function assertStmt(stmt, source) {
53+
expect(stmt.source).to.equal(source);
54+
expect(stmt.constructor.name).to.equal('Statement');
55+
expect(stmt.database).to.equal(db);
56+
expect(stmt.readonly).to.equal(false);
57+
expect(function () {
58+
new stmt.constructor(source);
59+
}).to.throw(TypeError);
60+
}
61+
var db = new Database(util.next());
62+
db.on('open', function () {
63+
var stmt1 = db.prepare('CREATE TABLE people (name TEXT)');
64+
var stmt2 = db.prepare('CREATE TABLE people (name TEXT);');
65+
assertStmt(stmt1, 'CREATE TABLE people (name TEXT)');
66+
assertStmt(stmt2, 'CREATE TABLE people (name TEXT);');
67+
expect(stmt1).to.not.equal(stmt2);
68+
expect(stmt1).to.not.equal(db.prepare('CREATE TABLE people (name TEXT)'));
69+
done();
70+
});
71+
});
72+
it('should create a prepared Statement object with just an expression', function (done) {
73+
var db = new Database(util.next());
74+
db.on('open', function () {
75+
var stmt = db.prepare('SELECT 555');
76+
expect(stmt.source).to.equal('SELECT 555');
77+
expect(stmt.constructor.name).to.equal('Statement');
78+
expect(stmt.database).to.equal(db);
79+
expect(stmt.readonly).to.equal(true);
80+
expect(function () {
81+
new stmt.constructor('SELECT 555');
82+
}).to.throw(TypeError);
83+
done();
84+
});
85+
});
86+
});

test/14.database.transaction.js

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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+
});
File renamed without changes.

0 commit comments

Comments
 (0)