|
1 | 1 | var expect = require('chai').expect; |
2 | 2 | var fs = require('fs'); |
3 | 3 | var Database = require('../.'); |
| 4 | +var util = require('../tools/test-util.js'); |
4 | 5 |
|
5 | 6 | describe('new Database()', function () { |
6 | 7 | it('should throw an exception when file path is not a string', function () { |
7 | 8 | expect(function () {new Database();}).to.throw(TypeError); |
8 | 9 | expect(function () {new Database(null);}).to.throw(TypeError); |
9 | 10 | expect(function () {new Database(0);}).to.throw(TypeError); |
10 | 11 | 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); |
12 | 13 | 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); |
14 | 15 | }); |
15 | 16 | it('should throw an exception when file path is empty', function () { |
16 | 17 | expect(function () {new Database('');}).to.throw(TypeError); |
17 | 18 | }); |
18 | 19 | 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); |
21 | 22 | }); |
22 | 23 | it('should not allow ":memory:" databases', function () { |
23 | 24 | expect(function () {new Database(':memory:');}).to.throw(TypeError); |
24 | 25 | }); |
25 | 26 | 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; |
28 | 31 | expect(db.open).to.be.false; |
29 | 32 | db.on('open', function () { |
30 | | - fs.accessSync('temp/10.5.db'); |
| 33 | + fs.accessSync(util.current()); |
31 | 34 | expect(db.open).to.be.true; |
32 | 35 | done(); |
33 | 36 | }); |
34 | 37 | }); |
35 | 38 | 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; |
38 | 43 | expect(db.open).to.be.false; |
39 | 44 | 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 () { |
41 | 58 | expect(db.open).to.be.true; |
42 | 59 | done(); |
43 | 60 | }); |
44 | 61 | }); |
45 | 62 | it('should have a proper prototype chain', function () { |
46 | | - var db = new Database('temp/10.7.db'); |
| 63 | + var db = new Database(util.next()); |
47 | 64 | expect(db).to.be.an.instanceof(Database); |
48 | 65 | expect(db.constructor).to.equal(Database); |
49 | 66 | expect(Database.prototype.constructor).to.equal(Database); |
|
0 commit comments