forked from chenqing/co-sqlite3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
230 lines (163 loc) · 5.05 KB
/
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/**
* promise based node-sqlite3 for co or koa
*/
var db = require('./');
var chai = require('chai');
var should = chai.should();
var expect = chai.expect;
var assert = chai.assert;
var fs = require('fs');
chai.use(require('chai-as-promised'));
describe('New sqlite3.Database test', function() {
// after this test ,delete test.db
it('should create success if filename is writeable', function() {
db('test2.db').then(function(data) {
expect(data.db).to.have.property('filename').with.equal('test2.db');
}, function(err) {
should.not.exist(err);
});
});
it('should create fail if filename is unwriteable', function() {
db('/somepaththatnotexist/test.db').then(function(data) {
should.not.exist(data);
}, function(err) {
should.exist(err);
});
});
});
// db.run test
describe('Database#run API test', function() {
var sqlCreateTable = 'CREATE TABLE IF NOT EXISTS testtable2( id INT NOT NULL)';
var sqlInsert = 'INSERT INTO testtable2 (id) VALUES(1)';
var sqlCreateTableWrong = 'INSERT INTO testta VALUES("string")';
it('should create table success if sql right', function() {
db('test2.db').then(function(data) {
return data.run(sqlCreateTable).should.eventually.have.property('lastID');
});
});
it('should insert data success if sql right', function() {
db('test2.db').then(function(data) {
return data.run(sqlInsert).should.eventually.have.property('lastID');
});
});
it('should run sql fail if sql wrong', function() {
db('test2.db').then(function(data) {
return data.run(sqlCreateTableWrong).should.be.rejected;
});
});
});
// db.close test
describe('Database#close API test', function() {
// after this test ,delete test2.db
it('should close success have opened connection', function() {
db('test2.db').then(function(data) {
return data.close().should.eventually.resolve;
});
});
});
// db.get test
describe('Database#get API test', function() {
var selectSQL = 'SELECT * FROM testtable2;';
// after this test ,delete test2.db
it('should get one record with id ', function() {
db('test2.db').then(function(data) {
data.get(selectSQL).then(function(d){
console.log(d);
})
});
});
});
// db.all test
describe('Database#all API test', function() {
var selectSQL = 'SELECT * FROM testtable2;';
after(function(){
//fs.unlinkSync('test2.db');
});
it('should get more than one record ', function() {
db('test2.db').then(function(data) {
return expect(data.all(selectSQL)).to.eventually.be.a('array');
});
});
});
// db.each test
describe('Database#each API test', function() {
var selectSQL = 'SELECT * FROM testtable2;';
it('should get a num ', function() {
db('test2.db').then(function(data) {
return expect(data.each(selectSQL)).to.eventually.be.a('number');
});
});
});
// db.exec test
describe('Database#exec API test', function() {
var selectSQL = 'SELECT * FROM testtable2;';
it('should resolve successful ', function() {
db('test2.db').then(function(data) {
return data.exec(selectSQL).should.eventually.resolve;
});
});
});
// db.exec test
describe('Database#prepare API test', function() {
var selectSQL = 'SELECT * FROM testtable2;';
it('should resolve successful ', function() {
db('test2.db').then(function(data) {
return data.prepare(selectSQL).should.eventually.resolve;
});
});
});
// statement.bind test
describe('Statement#bind API test', function() {
var selectSQL = 'SELECT * FROM testtable2;';
it('should resolve successful ', function() {
db('test2.db').then(function(data) {
data.prepare(selectSQL).then(function(statement){
return statement.bind().should.eventually.resolve;
});
});
});
});
// statement.reset test
describe('Statement#reset API test', function() {
var selectSQL = 'SELECT * FROM testtable2;';
it('should resolve successful ', function() {
db('test2.db').then(function(data) {
data.prepare(selectSQL).then(function(statement){
return statement.reset().should.eventually.resolve;
});
});
});
});
// statement.finalize test
describe('Statement#finalize API test', function() {
var selectSQL = 'SELECT * FROM testtable2;';
it('should resolve successful ', function() {
db('test2.db').then(function(data) {
data.prepare(selectSQL).then(function(statement){
return statement.finalize().should.eventually.resolve;
});
});
});
});
// statement.run test
describe('Statement#run API test', function() {
var selectSQL = 'SELECT * FROM testtable2;';
it('should resolve successful and have property lastID ', function() {
db('test2.db').then(function(data) {
data.prepare(selectSQL).then(function(statement){
return statement.run().should.eventually.have.property('lastID');
});
});
});
});
// statement.each test
describe('Statement#run API test', function() {
var selectSQL = 'SELECT * FROM testtable2;';
it('should resolve successful and got a num ', function() {
db('test2.db').then(function(data) {
data.prepare(selectSQL).then(function(statement){
return expect(statement.each()).to.eventually.be.a('number');
});
});
});
});