Skip to content

Commit a5126bf

Browse files
committed
improved tests
1 parent c5c4c43 commit a5126bf

File tree

5 files changed

+83
-26
lines changed

5 files changed

+83
-26
lines changed

test/10.database.open.js

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,66 @@
11
var expect = require('chai').expect;
22
var fs = require('fs');
33
var Database = require('../.');
4+
var util = require('../tools/test-util.js');
45

56
describe('new Database()', function () {
67
it('should throw an exception when file path is not a string', function () {
78
expect(function () {new Database();}).to.throw(TypeError);
89
expect(function () {new Database(null);}).to.throw(TypeError);
910
expect(function () {new Database(0);}).to.throw(TypeError);
1011
expect(function () {new Database(123);}).to.throw(TypeError);
11-
expect(function () {new Database(new String('temp/10.1.db'));}).to.throw(TypeError);
12+
expect(function () {new Database(new String(util.next()));}).to.throw(TypeError);
1213
expect(function () {new Database(function () {});}).to.throw(TypeError);
13-
expect(function () {new Database(['temp/10.2.db']);}).to.throw(TypeError);
14+
expect(function () {new Database([util.next()]);}).to.throw(TypeError);
1415
});
1516
it('should throw an exception when file path is empty', function () {
1617
expect(function () {new Database('');}).to.throw(TypeError);
1718
});
1819
it('should not allow URI file paths', function () {
19-
expect(function () {new Database('file:temp/10.3.db');}).to.throw(TypeError);
20-
expect(function () {new Database('file:temp/10.4.db?mode=memory');}).to.throw(TypeError);
20+
expect(function () {new Database('file:' + util.next());}).to.throw(TypeError);
21+
expect(function () {new Database('file:' + util.next() + '?mode=memory&cache=shared');}).to.throw(TypeError);
2122
});
2223
it('should not allow ":memory:" databases', function () {
2324
expect(function () {new Database(':memory:');}).to.throw(TypeError);
2425
});
2526
it('should allow disk-based databases to be created', function (done) {
26-
expect(function () {fs.accessSync('temp/10.5.db');}).to.throw(Error);
27-
var db = new Database('temp/10.5.db');
27+
expect(function () {fs.accessSync(util.next());}).to.throw(Error);
28+
var db = new Database(util.current());
29+
expect(db.name).to.equal(util.current());
30+
expect(db.memory).to.be.false;
2831
expect(db.open).to.be.false;
2932
db.on('open', function () {
30-
fs.accessSync('temp/10.5.db');
33+
fs.accessSync(util.current());
3134
expect(db.open).to.be.true;
3235
done();
3336
});
3437
});
3538
it('should allow in-memory databases to be created', function (done) {
36-
expect(function () {fs.accessSync('temp/10.6.db');}).to.throw(Error);
37-
var db = new Database('temp/10.6.db', {memory: true});
39+
expect(function () {fs.accessSync(util.next());}).to.throw(Error);
40+
var db = new Database(util.current(), {memory: true});
41+
expect(db.name).to.equal(util.current());
42+
expect(db.memory).to.be.true;
3843
expect(db.open).to.be.false;
3944
db.on('open', function () {
40-
expect(function () {fs.accessSync('temp/10.6.db');}).to.throw(Error);
45+
expect(function () {fs.accessSync(util.current());}).to.throw(Error);
46+
expect(db.open).to.be.true;
47+
done();
48+
});
49+
});
50+
it('should not allow the database to be used before it opens', function (done) {
51+
var db = new Database(util.next());
52+
expect(db.open).to.be.false;
53+
expect(function () {db.prepare('CREATE TABLE people (name TEXT)');}).to.throw(Error);
54+
expect(function () {db.transaction(['CREATE TABLE people (name TEXT)']);}).to.throw(Error);
55+
expect(function () {db.pragma('cache_size');}).to.throw(Error);
56+
expect(function () {db.checkpoint();}).to.throw(Error);
57+
db.on('open', function () {
4158
expect(db.open).to.be.true;
4259
done();
4360
});
4461
});
4562
it('should have a proper prototype chain', function () {
46-
var db = new Database('temp/10.7.db');
63+
var db = new Database(util.next());
4764
expect(db).to.be.an.instanceof(Database);
4865
expect(db.constructor).to.equal(Database);
4966
expect(Database.prototype.constructor).to.equal(Database);

test/11.database.close.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
var expect = require('chai').expect;
22
var Database = require('../.');
3+
var util = require('../tools/test-util.js');
34

45
describe('Database#close()', function () {
56
describe('before opening it', function () {
67
it('should never emit the "open" event', function (done) {
7-
var db = new Database('temp/11.1.db');
8+
var db = new Database(util.next());
89
expect(db.open).to.be.false;
910
db.on('open', function () {
1011
done(new Error('This event should not have been fired.'));
@@ -20,7 +21,8 @@ describe('Database#close()', function () {
2021
});
2122
describe('because of a failed open attempt', function () {
2223
it('should have an Error object as its first parameter', function (done) {
23-
var db = new Database('temp/nonexistent/abcfoobar123/11.2.db');
24+
var db = new Database('temp/nonexistent/abcfoobar123/' + util.next());
25+
expect(db.open).to.be.false;
2426
db.on('open', function () {
2527
done(new Error('This event should not have been fired.'));
2628
});
@@ -33,7 +35,7 @@ describe('Database#close()', function () {
3335
});
3436
describe('after opening it', function () {
3537
it('should emit the close event as normal', function (done) {
36-
var db = new Database('temp/11.3.db');
38+
var db = new Database(util.next());
3739
expect(db.open).to.be.false;
3840
db.on('open', function () {
3941
expect(db.open).to.be.true;
@@ -46,5 +48,21 @@ describe('Database#close()', function () {
4648
});
4749
});
4850
});
51+
it('should prevent any further database operations', function (done) {
52+
var db = new Database(util.next());
53+
db.on('open', function () {
54+
expect(db.open).to.be.true;
55+
db.close();
56+
db.on('close', function (err) {
57+
expect(err).to.be.null;
58+
expect(db.open).to.be.false;
59+
expect(function () {db.prepare('CREATE TABLE people (name TEXT)');}).to.throw(Error);
60+
expect(function () {db.transaction(['CREATE TABLE people (name TEXT)']);}).to.throw(Error);
61+
expect(function () {db.pragma('cache_size');}).to.throw(Error);
62+
expect(function () {db.checkpoint();}).to.throw(Error);
63+
done();
64+
});
65+
});
66+
});
4967
});
5068
});

test/12.database.pragma.js

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
var expect = require('chai').expect;
22
var Database = require('../.');
3+
var util = require('../tools/test-util.js');
34

45
describe('Database#pragma()', function () {
5-
it('should throw an exception if used before the database is open', function () {
6-
var db = new Database('temp/12.1.db');
7-
expect(function () {db.pragma('cache_size');}).to.throw(Error);
8-
});
96
it('should throw an exception if a string is not provided', function (done) {
10-
var db = new Database('temp/12.2.db');
7+
var db = new Database(util.next());
118
db.on('open', function () {
129
expect(function () {db.pragma(123);}).to.throw(TypeError);
1310
expect(function () {db.pragma(0);}).to.throw(TypeError);
@@ -18,41 +15,51 @@ describe('Database#pragma()', function () {
1815
});
1916
});
2017
it('should throw an exception if invalid/redundant SQL is provided', function (done) {
21-
var db = new Database('temp/12.3.db');
18+
var db = new Database(util.next());
2219
db.on('open', function () {
2320
expect(function () {db.pragma('PRAGMA cache_size');}).to.throw(Error);
2421
done();
2522
});
2623
});
2724
it('should execute the pragma, returning rows of strings', function (done) {
28-
var db = new Database('temp/12.4.db');
25+
var db = new Database(util.next());
2926
db.on('open', function () {
3027
var rows = db.pragma('cache_size');
3128
expect(rows[0].cache_size).to.be.a('string');
32-
expect(+rows[0].cache_size).to.equal(-16000);
29+
expect(rows[0].cache_size).to.equal('-16000');
3330
done();
3431
});
3532
});
3633
it('should optionally return simpler results', function (done) {
37-
var db = new Database('temp/12.5.db');
34+
var db = new Database(util.next());
3835
db.on('open', function () {
3936
var cache_size = db.pragma('cache_size', true);
4037
expect(cache_size).to.be.a('string');
4138
expect(cache_size).to.equal('-16000');
4239
done();
4340
});
4441
});
42+
it('should accept any truthy value to simplify results', function (done) {
43+
var db = new Database(util.next());
44+
db.on('open', function () {
45+
expect(db.pragma('cache_size', {})).to.equal('-16000');
46+
expect(db.pragma('cache_size', 123)).to.equal('-16000');
47+
expect(db.pragma('cache_size', function () {})).to.equal('-16000');
48+
expect(db.pragma('cache_size', NaN)).to.deep.equal([{cache_size: '-16000'}]);
49+
done();
50+
});
51+
});
4552
it('should obey PRAGMA changes', function (done) {
46-
var db = new Database('temp/12.6.db');
53+
var db = new Database(util.next());
4754
db.on('open', function () {
4855
expect(db.pragma('cache_size', true)).to.equal('-16000');
4956
db.pragma('cache_size = -8000');
5057
expect(db.pragma('cache_size', true)).to.equal('-8000');
5158
done();
5259
});
5360
});
54-
it('should return undefined when no rows exist and simpler results are desired', function (done) {
55-
var db = new Database('temp/12.7.db');
61+
it('should return undefined if no rows exist and simpler results are desired', function (done) {
62+
var db = new Database(util.next());
5663
db.on('open', function () {
5764
expect(db.pragma('table_info', true)).to.be.undefined;
5865
done();

test/13.database.checkpoint.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
var expect = require('chai').expect;
22
var Database = require('../.');
3+
var util = require('../tools/test-util.js');
34

45
describe('Database#checkpoint()', function () {
56

tools/test-util.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'use strict';
2+
var path = require('path');
3+
var dbId = 0;
4+
5+
exports.current = function () {
6+
return 'temp/' + path.basename(__filename).split('.')[0] + '.' + dbId + '.db';
7+
};
8+
9+
exports.next = function () {
10+
++dbId;
11+
return exports.current();
12+
};
13+
14+

0 commit comments

Comments
 (0)