This repository was archived by the owner on May 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
473 lines (418 loc) · 16 KB
/
test.js
File metadata and controls
473 lines (418 loc) · 16 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
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
'use strict';
let assert = require('chai').assert;
let _ = require('lodash');
let util = require('kinda-util').create();
let KindaDB = require('./src');
suite('KindaDB', function() {
let catchError = async function(fn) {
let err;
try {
await fn();
} catch (e) {
err = e;
}
return err;
};
suite('migrations', function() {
test('one empty table', async function() {
let db, stats;
try {
db = KindaDB.create({
name: 'Test',
url: 'mysql://test@localhost/test',
tables: [{ name: 'Table1' }]
});
stats = await db.getStatistics();
assert.strictEqual(stats.store.pairsCount, 0);
await db.initializeDatabase();
stats = await db.getStatistics();
assert.strictEqual(stats.store.pairsCount, 1);
} finally {
await db.destroyDatabase();
stats = await db.getStatistics();
assert.strictEqual(stats.store.pairsCount, 0);
}
});
test('one item in a table', async function() {
let db, stats;
try {
db = KindaDB.create({
name: 'Test',
url: 'mysql://test@localhost/test',
tables: [{ name: 'Table1' }]
});
await db.putItem('Table1', 'aaa', { property1: 'value1' });
stats = await db.getStatistics();
assert.strictEqual(stats.store.pairsCount, 2);
} finally {
await db.destroyDatabase();
}
});
test('one table added afterwards then removed', async function() {
let db, stats;
try {
db = KindaDB.create({
name: 'Test',
url: 'mysql://test@localhost/test',
tables: [{ name: 'Table1' }]
});
await db.initializeDatabase();
stats = await db.getStatistics();
assert.strictEqual(stats.tablesCount, 1);
db = KindaDB.create({
name: 'Test',
url: 'mysql://test@localhost/test',
tables: [{ name: 'Table1' }, { name: 'Table2' }]
});
await db.initializeDatabase();
stats = await db.getStatistics();
assert.strictEqual(stats.tablesCount, 2);
db = KindaDB.create({
name: 'Test',
url: 'mysql://test@localhost/test',
tables: [{ name: 'Table2' }]
});
await db.initializeDatabase();
stats = await db.getStatistics();
assert.strictEqual(stats.tablesCount, 1);
assert.strictEqual(stats.removedTablesCount, 1);
await db.removeTablesMarkedAsRemoved();
stats = await db.getStatistics();
assert.strictEqual(stats.tablesCount, 1);
assert.strictEqual(stats.removedTablesCount, 0);
} finally {
await db.destroyDatabase();
}
});
test('one index added afterwards then removed', async function() {
let db, stats;
try {
db = KindaDB.create({
name: 'Test',
url: 'mysql://test@localhost/test',
tables: [{ name: 'Table1' }]
});
await db.putItem('Table1', 'aaa', { property1: 'value1' });
stats = await db.getStatistics();
assert.strictEqual(stats.indexesCount, 0);
assert.strictEqual(stats.store.pairsCount, 2);
db = KindaDB.create({
name: 'Test',
url: 'mysql://test@localhost/test',
tables: [{ name: 'Table1', indexes: ['property1'] }]
});
await db.initializeDatabase();
stats = await db.getStatistics();
assert.strictEqual(stats.indexesCount, 1);
assert.strictEqual(stats.store.pairsCount, 3);
await db.putItem('Table1', 'bbb', { property1: 'value2' });
stats = await db.getStatistics();
assert.strictEqual(stats.store.pairsCount, 5);
db = KindaDB.create({
name: 'Test',
url: 'mysql://test@localhost/test',
tables: [{ name: 'Table1' }]
});
await db.initializeDatabase();
stats = await db.getStatistics();
assert.strictEqual(stats.indexesCount, 0);
assert.strictEqual(stats.store.pairsCount, 3);
} finally {
await db.destroyDatabase();
}
});
}); // migrations suite
suite('simple database', function() {
let db;
suiteSetup(async function() {
db = KindaDB.create({
name: 'Test',
url: 'mysql://test@localhost/test',
tables: [{ name: 'Users' }]
});
});
suiteTeardown(async function() {
await db.destroyDatabase();
});
test('tables definition', async function() {
assert.strictEqual(db.tables.length, 1);
let table = db.tables[0];
assert.strictEqual(table.name, 'Users');
assert.strictEqual(table.indexes.length, 0);
});
test('put, get and delete some items', async function() {
await db.putItem('Users', 'mvila', { firstName: 'Manu', age: 42 });
let user = await db.getItem('Users', 'mvila');
assert.deepEqual(user, { firstName: 'Manu', age: 42 });
let hasBeenDeleted = await db.deleteItem('Users', 'mvila');
assert.isTrue(hasBeenDeleted);
user = await db.getItem('Users', 'mvila', { errorIfMissing: false });
assert.isUndefined(user);
hasBeenDeleted = await db.deleteItem('Users', 'mvila', { errorIfMissing: false });
assert.isFalse(hasBeenDeleted);
});
}); // simple database suite
suite('rich database', function() {
let db;
suiteSetup(async function() {
db = KindaDB.create({
name: 'Test',
url: 'mysql://test@localhost/test',
tables: [
{
name: 'People',
indexes: [
['lastName', 'firstName'],
'age',
['country', 'city'],
{
properties: 'country',
projection: ['firstName', 'lastName']
},
function fullNameSortKey(item) {
return util.makeSortKey(item.lastName, item.firstName);
}
]
}
]
});
});
suiteTeardown(async function() {
await db.destroyDatabase();
});
setup(async function() {
await db.putItem('People', 'aaa', {
firstName: 'Manuel', lastName: 'Vila',
age: 42, city: 'Paris', country: 'France'
});
await db.putItem('People', 'bbb', {
firstName: 'Jack', lastName: 'Daniel',
age: 60, city: 'New York', country: 'USA'
});
await db.putItem('People', 'ccc', {
firstName: 'Bob', lastName: 'Cracker',
age: 20, city: 'Los Angeles', country: 'USA'
});
await db.putItem('People', 'ddd', {
firstName: 'Vincent', lastName: 'Vila',
age: 43, city: 'Céret', country: 'France'
});
await db.putItem('People', 'eee', {
firstName: 'Pierre', lastName: 'Dupont',
age: 39, city: 'Lyon', country: 'France'
});
await db.putItem('People', 'fff', {
firstName: 'Jacques', lastName: 'Fleur',
age: 39, city: 'San Francisco', country: 'USA'
});
});
teardown(async function() {
await db.deleteItem('People', 'aaa', { errorIfMissing: false });
await db.deleteItem('People', 'bbb', { errorIfMissing: false });
await db.deleteItem('People', 'ccc', { errorIfMissing: false });
await db.deleteItem('People', 'ddd', { errorIfMissing: false });
await db.deleteItem('People', 'eee', { errorIfMissing: false });
await db.deleteItem('People', 'fff', { errorIfMissing: false });
});
test('tables definition', async function() {
assert.strictEqual(db.tables.length, 1);
let table = db.tables[0];
assert.strictEqual(table.name, 'People');
assert.strictEqual(table.indexes.length, 5);
assert.strictEqual(table.indexes[0].properties.length, 2);
assert.strictEqual(table.indexes[0].properties[0].key, 'lastName');
});
test('get many items', async function() {
let items = await db.getItems('People', ['aaa', 'ccc']);
assert.strictEqual(items.length, 2);
assert.strictEqual(items[0].key, 'aaa');
assert.strictEqual(items[0].value.firstName, 'Manuel');
assert.strictEqual(items[1].key, 'ccc');
assert.strictEqual(items[1].value.firstName, 'Bob');
});
test('find all items in a table', async function() {
let items = await db.findItems('People');
assert.strictEqual(items.length, 6);
assert.strictEqual(items[0].key, 'aaa');
assert.strictEqual(items[0].value.firstName, 'Manuel');
assert.strictEqual(items[5].key, 'fff');
assert.strictEqual(items[5].value.firstName, 'Jacques');
});
test('find and order items', async function() {
let items = await db.findItems('People', { order: 'age' });
assert.strictEqual(items.length, 6);
assert.strictEqual(items[0].key, 'ccc');
assert.strictEqual(items[0].value.age, 20);
assert.strictEqual(items[5].key, 'bbb');
assert.strictEqual(items[5].value.age, 60);
items = await db.findItems('People', { order: 'age', reverse: true });
assert.strictEqual(items.length, 6);
assert.strictEqual(items[0].key, 'bbb');
assert.strictEqual(items[0].value.age, 60);
assert.strictEqual(items[5].key, 'ccc');
assert.strictEqual(items[5].value.age, 20);
let err = await catchError(async function() {
await db.findItems('People', { order: 'missingProperty' });
});
assert.instanceOf(err, Error);
});
test('find items with a query', async function() {
let items = await db.findItems('People', { query: { country: 'France' } });
let keys = _.pluck(items, 'key');
assert.deepEqual(keys, ['aaa', 'ddd', 'eee']);
items = await db.findItems('People', { query: { country: 'USA' } });
keys = _.pluck(items, 'key');
assert.deepEqual(keys, ['bbb', 'ccc', 'fff']);
items = await db.findItems('People', { query: { city: 'New York', country: 'USA' } });
keys = _.pluck(items, 'key');
assert.deepEqual(keys, ['bbb']);
items = await db.findItems('People', { query: { country: 'Japan' } });
assert.strictEqual(items.length, 0);
});
test('find items with a query and an order', async function() {
let items = await db.findItems('People', {
query: { country: 'USA' }, order: 'city'
});
let keys = _.pluck(items, 'key');
assert.deepEqual(keys, ['ccc', 'bbb', 'fff']);
items = await db.findItems('People', {
query: { country: 'USA' }, order: 'city', reverse: true
});
keys = _.pluck(items, 'key');
assert.deepEqual(keys, ['fff', 'bbb', 'ccc']);
});
test('find items after a specific item', async function() {
let items = await db.findItems('People', {
query: { country: 'USA' }, order: 'city', start: 'New York'
});
let keys = _.pluck(items, 'key');
assert.deepEqual(keys, ['bbb', 'fff']);
items = await db.findItems('People', {
query: { country: 'USA' }, order: 'city', startAfter: 'New York'
});
keys = _.pluck(items, 'key');
assert.deepEqual(keys, ['fff']);
});
test('find items before a specific item', async function() {
let items = await db.findItems('People', {
query: { country: 'USA' }, order: 'city', end: 'New York'
});
let keys = _.pluck(items, 'key');
assert.deepEqual(keys, ['ccc', 'bbb']);
items = await db.findItems('People', {
query: { country: 'USA' }, order: 'city', endBefore: 'New York'
});
keys = _.pluck(items, 'key');
assert.deepEqual(keys, ['ccc']);
});
test('find a limited number of items', async function() {
let items = await db.findItems('People', {
query: { country: 'France' }, limit: 2
});
let keys = _.pluck(items, 'key');
assert.deepEqual(keys, ['aaa', 'ddd']);
});
test('find items using an index projection', async function() {
let items = await db.findItems('People', {
query: { country: 'France' }, properties: ['firstName', 'lastName']
});
let keys = _.pluck(items, 'key');
assert.deepEqual(keys, ['aaa', 'ddd', 'eee']);
assert.deepEqual(items[0].value, { firstName: 'Manuel', lastName: 'Vila' });
items = await db.findItems('People', { // will not use projection
query: { country: 'France' }, properties: ['firstName', 'lastName', 'age']
});
keys = _.pluck(items, 'key');
assert.deepEqual(keys, ['aaa', 'ddd', 'eee']);
assert.deepEqual(items[0].value, {
firstName: 'Manuel', lastName: 'Vila',
age: 42, city: 'Paris', country: 'France'
});
});
test('find items using a computed index', async function() {
let items = await db.findItems('People', { order: 'fullNameSortKey' });
let keys = _.pluck(items, 'key');
assert.deepEqual(keys, ['ccc', 'bbb', 'eee', 'fff', 'aaa', 'ddd']);
});
test('count all items in a table', async function() {
let count = await db.countItems('People');
assert.strictEqual(count, 6);
});
test('count items with a query', async function() {
let count = await db.countItems('People', {
query: { age: 39 }
});
assert.strictEqual(count, 2);
count = await db.countItems('People', {
query: { country: 'France' }
});
assert.strictEqual(count, 3);
count = await db.countItems('People', {
query: { country: 'France', city: 'Paris' }
});
assert.strictEqual(count, 1);
count = await db.countItems('People', {
query: { country: 'Japan', city: 'Tokyo' }
});
assert.strictEqual(count, 0);
});
test('iterate over items', async function() {
let keys = [];
await db.forEachItems('People', { batchSize: 2 }, async function(value, key) {
keys.push(key);
});
assert.deepEqual(keys, ['aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff']);
});
test('iterate over items in a specific order', async function() {
let keys = [];
let options = { order: ['lastName', 'firstName'], batchSize: 2 };
await db.forEachItems('People', options, async function(value, key) {
keys.push(key);
});
assert.deepEqual(keys, ['ccc', 'bbb', 'eee', 'fff', 'aaa', 'ddd']);
});
test('find and delete items', async function() {
let options = { query: { country: 'France' }, batchSize: 2 };
let deletedItemsCount = await db.findAndDeleteItems('People', options);
assert.strictEqual(deletedItemsCount, 3);
let items = await db.findItems('People');
let keys = _.pluck(items, 'key');
assert.deepEqual(keys, ['bbb', 'ccc', 'fff']);
deletedItemsCount = await db.findAndDeleteItems('People', options);
assert.strictEqual(deletedItemsCount, 0);
});
test('change an item inside a transaction', async function() {
assert.isFalse(db.isInsideTransaction);
await db.transaction(async function(tr) {
assert.isTrue(tr.isInsideTransaction);
let innerItem = await tr.getItem('People', 'aaa');
assert.strictEqual(innerItem.firstName, 'Manuel');
innerItem.firstName = 'Manu';
await tr.putItem('People', 'aaa', innerItem);
innerItem = await tr.getItem('People', 'aaa');
assert.strictEqual(innerItem.firstName, 'Manu');
});
let item = await db.getItem('People', 'aaa');
assert.strictEqual(item.firstName, 'Manu');
});
test('change an item inside an aborted transaction', async function() {
try {
assert.isFalse(db.isInsideTransaction);
await db.transaction(async function(tr) {
assert.isTrue(tr.isInsideTransaction);
let innerItem = await tr.getItem('People', 'aaa');
assert.strictEqual(innerItem.firstName, 'Manuel');
innerItem.firstName = 'Manu';
await tr.putItem('People', 'aaa', innerItem);
innerItem = await tr.getItem('People', 'aaa');
assert.strictEqual(innerItem.firstName, 'Manu');
throw new Error('something wrong');
});
} catch (err) {
// noop
}
let item = await db.getItem('People', 'aaa');
assert.strictEqual(item.firstName, 'Manuel');
});
}); // rich database suite
});