forked from TryGhost/node-sqlite3
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathopen_close.test.js
More file actions
129 lines (108 loc) · 3.93 KB
/
open_close.test.js
File metadata and controls
129 lines (108 loc) · 3.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
var sqlite3 = require('..');
var assert = require('assert');
var fs = require('fs');
var helper = require('./support/helper');
describe('open/close', function() {
before(function() {
helper.ensureExists('test/tmp');
});
describe('open and close non-existant database', function() {
before(function() {
helper.deleteFile('test/tmp/test_create.db');
});
var db;
it('should open the database', function(done) {
db = new sqlite3.Database('test/tmp/test_create.db', done);
});
it('should close the database', function(done) {
db.close(done);
});
it('should have created the file', function() {
assert.fileExists('test/tmp/test_create.db');
});
after(function() {
helper.deleteFile('test/tmp/test_create.db');
});
});
it('should not be unable to open an inaccessible database', function(done) {
// NOTE: test assumes that the user is not allowed to create new files
// in /usr/bin.
var db = new sqlite3.Database('/test/tmp/directory-does-not-exist/test.db', function(err) {
if (err && err.errno === sqlite3.CANTOPEN) {
done();
} else if (err) {
done(err);
} else {
done('Opened database that should be inaccessible');
}
});
});
describe('creating database without create flag', function() {
before(function() {
helper.deleteFile('test/tmp/test_readonly.db');
});
it('should fail to open the database', function(done) {
new sqlite3.Database('tmp/test_readonly.db', sqlite3.OPEN_READONLY, function(err) {
if (err && err.errno === sqlite3.CANTOPEN) {
done();
} else if (err) {
done(err);
} else {
done('Created database without create flag');
}
});
});
it('should not have created the file', function() {
assert.fileDoesNotExist('test/tmp/test_readonly.db');
});
after(function() {
helper.deleteFile('test/tmp/test_readonly.db');
});
});
describe('open and close memory database queuing', function() {
var db;
it('should open the database', function(done) {
db = new sqlite3.Database(':memory:', done);
});
it('should close the database', function(done) {
db.close(done);
});
it('shouldn\'t close the database again', function(done) {
db.close(function(err) {
assert.ok(err, 'No error object received on second close');
assert.ok(err.errno === sqlite3.MISUSE);
done();
});
});
});
describe('closing with unfinalized statements', function(done) {
var completed = false;
var completedSecond = false;
var closed = false;
var db;
before(function() {
db = new sqlite3.Database(':memory:', done);
});
it('should create a table', function(done) {
db.run("CREATE TABLE foo (id INT, num INT)", done);
});
var stmt;
it('should prepare/run a statement', function(done) {
stmt = db.prepare('INSERT INTO foo VALUES (?, ?)');
stmt.run(1, 2, done);
});
it('should fail to close the database', function(done) {
db.close(function(err) {
assert.ok(err.message,
"SQLITE_BUSY: unable to close due to unfinalised statements");
done();
});
});
it('should succeed to close the database after finalizing', function(done) {
stmt.run(3, 4, function() {
stmt.finalize();
db.close(done);
});
});
});
});