-
Notifications
You must be signed in to change notification settings - Fork 0
/
rhino.test.js
executable file
·353 lines (349 loc) · 13.7 KB
/
rhino.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
const rhino = require('./rhino.js');
const Query = require('./query.js');
const fs = require('fs');
const path = require('path');
require('dotenv').config();
require('jest');
describe('#ping', () => {
test('returns true when connection is established.', async () => {
let pool = rhino.create();
let results = await pool.ping();
pool.destroy();
expect(results).toBe(true);
});
test('returns true when connection is already established.', async () => {
let pool = rhino.create();
await pool.ping();
let results = await pool.ping();
pool.destroy();
expect(results).toBe(true);
});
test('returns false when a connection cannot be established.', async () => {
let pool = rhino.create({
server: 'idontexist_bad_server'
});
let results = await pool.ping();
pool.destroy();
expect(results).toBe(false);
});
});
describe('#query', () => {
describe('non-data selects', () => {
let db = null;
beforeAll(() => {
db = rhino.create({
options: {
useColumnNames: false
}
});
});
afterAll(() => {
db.destroy();
});
test('throws properly on connection is lost or resets (unstable connection).', async () => {
let db = rhino.create();
expect.assertions(2);
try {
await db.ping();
let conn = db._pool.free[0].resource;
let p = db.query('WAITFOR DELAY \'00:00:05\';');
//simulate a unexpected termination of the connection.
await new Promise((r) => setTimeout(r, 1000));
conn._tdsConnection.socket.end();
await p;
} catch (err) {
expect(err).toBeTruthy();
expect(err.code).toBe('ESOCKET');
} finally {
db.destroy();
}
}, 10000);
test('runs a simple non-data select query returning row objects.', async () => {
let db = rhino.create({
options: {
useColumnNames: true
}
});
try {
let r = await db.query('SELECT 1 AS A, \'hello\' AS B, \'world\' AS C;');
expect(r).toBeTruthy();
expect(r.columns.length).toBe(3);
expect(r.rows.length).toBe(1);
expect(r.rows[0].A).toBe(1);
expect(r.rows[0].B).toBe('hello');
expect(r.rows[0].C).toBe('world');
} finally {
db.destroy();
}
});
test('runs a simple non-data select query returning row value arrays.', async () => {
let r = await db.query('SELECT 1 AS A, \'hello\' AS B, \'world\' AS C;');
expect(r).toBeTruthy();
expect(r.columns.length).toBe(3);
expect(r.rows.length).toBe(1);
expect(r.rows[0][0]).toBe(1);
expect(r.rows[0][1]).toBe('hello');
expect(r.rows[0][2]).toBe('world');
});
test('runs a simple non-data select using parameters.', async () => {
let r = await db
.query('SELECT @a AS A, @b AS B, @c AS C;')
.in('a', 1)
.in('b', 'hello')
.in('c', 'world');
expect(r).toBeTruthy();
expect(r.columns.length).toBe(3);
expect(r.rows.length).toBe(1);
expect(r.rows[0][0]).toBe(1);
expect(r.rows[0][1]).toBe('hello');
expect(r.rows[0][2]).toBe('world');
//test param object approach
r = await db
.query('SELECT @a AS A, @b AS B, @c AS C;', {
a: 1, b: 'hello', c: 'world'
});
expect(r).toBeTruthy();
expect(r.columns.length).toBe(3);
expect(r.rows.length).toBe(1);
expect(r.rows[0][0]).toBe(1);
expect(r.rows[0][1]).toBe('hello');
expect(r.rows[0][2]).toBe('world');
//test param map approach
let m = new Map();
m.set('a', 1);
m.set('b', 'hello');
m.set('c', 'world');
r = await db.query('SELECT @a AS A, @b AS B, @c AS C;', m);
expect(r).toBeTruthy();
expect(r.columns.length).toBe(3);
expect(r.rows.length).toBe(1);
expect(r.rows[0][0]).toBe(1);
expect(r.rows[0][1]).toBe('hello');
expect(r.rows[0][2]).toBe('world');
});
test('runs a simple non-data multi-statement select query.', async () => {
let r = await db.query('SELECT 1 AS A, \'hello\' AS B, \'world\' AS C; SELECT 123; SELECT \'ABC\';');
expect(Array.isArray(r)).toBeTruthy();
expect(r.length).toBe(3);
expect(r[0].columns.length).toBe(3);
expect(r[0].rows.length).toBe(1);
expect(r[0].rows[0][0]).toBe(1);
expect(r[0].rows[0][1]).toBe('hello');
expect(r[0].rows[0][2]).toBe('world');
expect(r[1].columns.length).toBe(1);
expect(r[1].rows.length).toBe(1);
expect(r[1].rows[0][0]).toBe(123);
expect(r[2].columns.length).toBe(1);
expect(r[2].rows.length).toBe(1);
expect(r[2].rows[0][0]).toBe('ABC');
});
test('runs a simple non-data multi-statement select query with parameters.', async () => {
let r = await db
.query('SELECT @a AS A, @b AS B, @c AS C; SELECT 123; SELECT @b + @c;')
.in('a', 1)
.in('b', 'hello')
.in('c', 'world');
expect(Array.isArray(r)).toBeTruthy();
expect(r.length).toBe(3);
expect(r[0].columns.length).toBe(3);
expect(r[0].rows.length).toBe(1);
expect(r[0].rows[0][0]).toBe(1);
expect(r[0].rows[0][1]).toBe('hello');
expect(r[0].rows[0][2]).toBe('world');
expect(r[1].columns.length).toBe(1);
expect(r[1].rows.length).toBe(1);
expect(r[1].rows[0][0]).toBe(123);
expect(r[2].columns.length).toBe(1);
expect(r[2].rows.length).toBe(1);
expect(r[2].rows[0][0]).toBe('helloworld');
});
test('runs multiple non-data queries where some may fail, but will not affect others.', async () => {
let db = rhino.create({
options: {
useColumnNames: false
}
});
expect.assertions(4);
await db.query('SELECT 1 AS A, \'hello\' AS B, \'world\' AS C;');
try {
await db.query('SELECTINVALID X*$)323');
} catch (err) {
expect(err).toBeTruthy();
}
let r = await db.query('SELECT 1');
expect(r).toBeTruthy();
expect(r.rows.length).toBe(1);
expect(r.rows[0][0]).toBe(1);
db.destroy();
});
test('runs a stored procedure that uses out parameters.', async () => {
let db = rhino.create({
options: {
useColumnNames: false
}
});
let r = await db
.query('EXEC dbo.uspLogError')
.out('ErrorLogID', null, 'INT');
expect(r).toBeTruthy();
expect(r.rows.length).toBe(1);
expect(r.columns.length).toBe(1);
expect(r.columns[0].name).toBe('ErrorLogID');
expect(r.columns[0].parameter).toBe(true);
expect(r.rows[0][0]).not.toBeNull();
db.destroy();
});
test('pool releases resources that are unused.', async () => {
let db = rhino.create({
options: {
useColumnNames: false
}
});
await db.query('SELECT 1 AS A, \'hello\' AS B, \'world\' AS C;');
try {
await db.query('SELECTINVALID X*$)323');
} catch (err) {
expect(err).toBeTruthy();
}
for (let x = 0; x < 50; x++) {
await Promise.all([
db.query('SELECT 1'),
db.query('SELECT 2'),
db.query('SELECT 3'),
db.query('SELECT 4')
]);
await db.query('SELECT 123');
}
expect(db._pool.free.length).toBeLessThanOrEqual(4);
db.destroy();
});
});
describe('batch', () => {
let db = null;
beforeAll(() => {
db = rhino.create({
options: {
useColumnNames: false
}
});
});
afterAll(() => {
db.destroy();
});
test('batches a large script from memory.', async () => {
let data = fs.readFileSync(path.resolve(__dirname, 'test/batch-load.sql'));
let q = db.query(data.toString('utf8'));
expect(q.mode).toBe(Query.MODE.BATCH);
let r = await q;
expect(r).toBeTruthy();
r = await db.query('SELECT COUNT(*) FROM Theme;');
expect(r.rows.length).toBe(1);
expect(r.rows[0][0]).toBe(100);
});
});
});
describe('#bulk', () => {
/** @type {rhino} */
let db = null;
beforeAll(() => {
db = rhino.create({
options: {
useColumnNames: false
}
});
});
afterAll(async () => {
await db.destroy();
});
test('performs a bulk load of rows into a table.', async () => {
let bk = db.bulk('Theme', { timeout: 5000 });
await bk.column('Name', Query.TYPE.VarChar, { nullable: false, length: 512 });
await bk.column('HexCode', Query.TYPE.VarChar, { nullable: false, length: 512 });
for (let x = 0; x < 1000; x++) {
bk.add({ Name: `name${x}`, HexCode: `#000${x}${x}${x}` });
}
//add null and undefined rows (should be skipped).
bk.add(null);
bk.add(undefined);
let result = await bk.execute();
expect(result).toBe(1000);
}, 5000);
test('ensures that rows are objects.', async () => {
let bk = db.bulk('Theme');
await expect(bk.add(123)).rejects.toThrow();
await expect(bk.add(true)).rejects.toThrow();
await expect(bk.add('hello')).rejects.toThrow();
});
});
describe('#transaction', () => {
let db = null;
beforeAll(() => {
db = rhino.create({
options: {
useColumnNames: false
}
});
});
afterAll(() => {
db.destroy();
});
test('runs a simple non-data multi-statement select queries.', async () => {
let tx = db.transaction();
tx.query('SELECT 1 AS A, \'hello\' AS B, \'world\' AS C; SELECT 123; SELECT \'ABC\';');
tx.query('SELECT TOP 1 * FROM Production.Culture WHERE CultureID = \'en\';');
let r = await tx.commit();
expect(r.length).toBe(4);
expect(r[0].columns.length).toBe(3);
expect(r[0].rows.length).toBe(1);
expect(r[0].rows[0][0]).toBe(1);
expect(r[0].rows[0][1]).toBe('hello');
expect(r[0].rows[0][2]).toBe('world');
expect(r[1].columns.length).toBe(1);
expect(r[1].rows.length).toBe(1);
expect(r[1].rows[0][0]).toBe(123);
expect(r[2].columns.length).toBe(1);
expect(r[2].rows.length).toBe(1);
expect(r[2].rows[0][0]).toBe('ABC');
expect(r[3].rows.length).toBe(1);
expect(r[3].rows[0][0]).toBe('en ');
expect(r[3].rows[0][1]).toBe('English');
expect(r[3].rows[0][2]).toBeInstanceOf(Date);
});
test('rollback completes when no commit occurred.', async () => {
let tx = db.transaction();
tx.query('INSERT INTO Production.Culture (CultureID, Name) VALUES (\'zz\', @name);', { name: 'TXTEST' });
await tx.rollback();
let r = await db.query('SELECT * FROM Production.Culture WHERE Name = @name', { name: 'TXTEST' });
expect(r.rows.length).toBe(0);
});
test('rollback completes after a commit fails.', async () => {
let tx = db.transaction();
try {
tx.query('INSERT INTO Production.Culture (CultureID, Name) VALUES (\'ca\', @name);', { name: 'TXTEST' });
tx.query('THISWILLFAIL;');
await tx.commit();
} catch (err) {
await tx.rollback();
}
let r = await db.query('SELECT * FROM Production.Culture WHERE CultureID = @culture', { culture: 'ca' });
expect(r.rows.length).toBe(0);
});
test('rollback works up to the last savepoint.', async () => {
await db.query('DELETE FROM Production.Culture WHERE Name = @name', { name: 'SPTEST' });
let tx = db.transaction();
try {
tx.query('INSERT INTO Production.Culture (CultureID, Name) VALUES (\'spt\', @name);', { name: 'SPTEST' });
tx.savePoint('insertsptest');
//insert a duplicate record after the savepoint (would create 2 results with the name if it was committed).
tx.query('INSERT INTO Production.Culture (CultureID, Name) VALUES (\'spt2\', @name);', { name: 'SPTEST' });
//fail (identity insert conflict)
tx.query('INSERT INTO Production.Culture (CultureID, Name) VALUES (\'spt\', \'Shouldnt exist.\');');
await tx.commit();
} catch (err) {
await tx.rollback('insertsptest');
}
let r = await db.query('SELECT * FROM Production.Culture WHERE Name = @name', { name: 'SPTEST' });
expect(r.rows.length).toBe(1);
await db.query('DELETE FROM Production.Culture WHERE Name = @name', { name: 'SPTEST' });
});
});