-
-
Notifications
You must be signed in to change notification settings - Fork 828
/
Copy pathopen_close.test.js
187 lines (152 loc) · 5.98 KB
/
open_close.test.js
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
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');
});
});
describe('open and close non-existant shared database', function() {
before(function() {
helper.deleteFile('test/tmp/test_create_shared.db');
});
var db;
it('should open the database', function(done) {
db = new sqlite3.Database('file:./test/tmp/test_create_shared.db', sqlite3.OPEN_URI | sqlite3.OPEN_SHAREDCACHE | sqlite3.OPEN_READWRITE | sqlite3.OPEN_CREATE, done);
});
it('should close the database', function(done) {
db.close(done);
});
it('should have created the file', function() {
assert.fileExists('test/tmp/test_create_shared.db');
});
after(function() {
helper.deleteFile('test/tmp/test_create_shared.db');
});
});
(sqlite3.VERSION_NUMBER < 3008000 ? describe.skip : describe)('open and close shared memory database', function() {
var db1;
var db2;
it('should open the first database', function(done) {
db1 = new sqlite3.Database('file:./test/tmp/test_memory.db?mode=memory', sqlite3.OPEN_URI | sqlite3.OPEN_SHAREDCACHE | sqlite3.OPEN_READWRITE | sqlite3.OPEN_CREATE, done);
});
it('should open the second database', function(done) {
db2 = new sqlite3.Database('file:./test/tmp/test_memory.db?mode=memory', sqlite3.OPEN_URI | sqlite3.OPEN_SHAREDCACHE | sqlite3.OPEN_READWRITE | sqlite3.OPEN_CREATE, done);
});
it('first database should set the user_version', function(done) {
db1.exec('PRAGMA user_version=42', done);
});
it('second database should get the user_version', function(done) {
db2.get('PRAGMA user_version', function(err, row) {
if (err) throw err;
assert.equal(row.user_version, 42);
done();
});
});
it('should close the first database', function(done) {
db1.close(done);
});
it('should close the second database', function(done) {
db2.close(done);
});
});
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);
});
});
});
});