-
Notifications
You must be signed in to change notification settings - Fork 16
/
test.js
224 lines (185 loc) · 6.58 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
#!/usr/bin/env node
const fs = require('fs'),
child = require('child_process'),
should = require('should'),
sqlite = require('better-sqlite3'),
rimraf = require('rimraf').sync,
mkdirp = require('mkdirp').sync;
const sqliteJSON = require('./');
const data = [
{ name: 'Washington', id: 1 },
{ name: 'Adams', id: 2 },
{ name: 'Jefferson', id: 3 },
{ name: 'Madison', id: 4 },
{ name: 'Monroe', id: 5 },
{ name: 'Adams', id: 6 },
];
const bigInteger = '6500328718134052119';
describe('sqliteToJson', function () {
before(function (done) {
rimraf('./tmp');
mkdirp('./tmp');
const db = new sqlite('./tmp/tmp.db');
this.sqlitejson = sqliteJSON(db);
db.exec("CREATE TABLE presidents (name TEXT, id INT)");
const stmt = db.prepare("INSERT INTO presidents VALUES (?, ?)");
data.forEach(row => stmt.run(row.name, row.id));
db.exec('CREATE TABLE big (id INT);')
db.exec(`INSERT INTO big VALUES (${bigInteger})`);
done(null);
desired = data.reduce(function(o, v) { o[v.name] = v; return o; }, {});
this.command = 'node ./bin/sqlite-json.js';
});
it('accepts a filename', function() {
const dbfile = './tmp/newdb.db'
new sqlite(dbfile);
sqliteJSON(dbfile).should.be.an.instanceOf(sqliteJSON);
rimraf(dbfile);
});
it('calls back with all tables in the specified database', function (done) {
this.sqlitejson.tables(function(err, result) {
result.should.have.length(2);
result.should.be.containDeep(['presidents', 'big']);
done(err);
});
});
it('exports a table to JSON', function (done) {
this.sqlitejson.json({table: 'presidents'}, function (err, json) {
if (!err) should.deepEqual(JSON.parse(json), data);
done(err);
});
});
it('saves a table in a database to a file', function (done) {
var dest = 'tmp/bar';
this.sqlitejson.save({table: 'presidents'}, dest, function (err, data) {
if (!err)
should.deepEqual(
JSON.parse(data),
JSON.parse(fs.readFileSync(dest)),
'data should match file'
);
done(err);
});
});
it('accepts a key option', function (done) {
const desired = data.reduce(function(o, v) { o[v.name] = v; return o; }, {});
this.sqlitejson.json({table: 'presidents', key: "name"}, function (err, json) {
if (!err) should.deepEqual(JSON.parse(json), desired);
done(err);
});
});
it('attaches a key to the output', function (done) {
const desired = data.reduce(function(o, v) { o[v.name] = v; return o; }, {});
this.sqlitejson.json({table: 'presidents', key: "name", columns: ["id"]}, function (err, json) {
if (!err) should.deepEqual(JSON.parse(json), desired);
done(err);
});
});
it('filters with a where option', function (done) {
const desired = data.filter(function(i) { return i.name == 'Adams'; }, {});
this.sqlitejson.json({table: 'presidents', where: "name = 'Adams'"}, function (err, json) {
if (!err) should.deepEqual(json, JSON.stringify(desired));
done(err);
});
});
it('filters with a columns option', function (done) {
const desired = data.map(function(i) { return {"name": i.name}; }, {});
this.sqlitejson.json({table: 'presidents', columns: ["name"]}, function (err, json) {
if (!err) should.deepEqual(JSON.parse(json), desired);
done(err);
});
});
it('returns a bigint properly', function (done) {
const desired = `[{"id":${bigInteger}}]`;
this.sqlitejson.json(
"SELECT id FROM big LIMIT 1",
function (err, json) {
if (!err) should.deepEqual(json, desired);
done(err);
}
);
});
it('accepts SQL with a callback', function (done) {
const desired = data.map(function(i) { return {"name": i.name}; }, {});
this.sqlitejson.json("select name from presidents", function (err, json) {
if (!err) should.deepEqual(JSON.parse(json), desired);
done(err);
});
});
it('accepts where, key, columns simultaneously', function (done) {
const opts = {
table: 'presidents',
columns: ["name"],
key: "name",
where: "id == 1"
},
desired = {"Washington": {"name": "Washington"}};
this.sqlitejson.json(opts, function (err, json) {
if (!err) should.deepEqual(JSON.parse(json), desired);
done(err);
});
});
it('cli works with options', function (done) {
args = [
"./tmp/tmp.db",
'--table',
'presidents'
];
fixture = JSON.stringify();
child.exec(this.command +" "+ args.join(' '), function(e, result, err) {
if (e) throw e;
if (err) {
console.error("");
console.error(err);
}
should.deepEqual(JSON.parse(result),
data,
'Command line matches'
);
done();
});
});
it('cli works with SQL', function (done) {
nodeargs = [
"./tmp/tmp.db",
'"SELECT * FROM presidents"'
];
fixture = JSON.stringify();
child.exec(this.command +" "+ nodeargs.join(' '), function(e, result, err) {
if (e) throw e;
if (err) {
console.error("");
console.error(err);
}
should.deepEqual(JSON.parse(result),
data,
'Command line matches'
);
done();
});
});
it('cli SQL overrides options', function (done) {
nodeargs = [
"./tmp/tmp.db",
'"SELECT * FROM presidents"',
"--where",
"id==1"
];
fixture = JSON.stringify();
child.exec(this.command +" "+ nodeargs.join(' '), function(e, result, err) {
if (e) throw e;
if (err) {
console.error("");
console.error(err);
}
should.deepEqual(JSON.parse(result),
data,
'Command line matches'
);
done();
});
});
after(function(){
rimraf('./tmp');
});
});