@naturalcycles/db-lib 10.0.2 → 10.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/adapter/cachedb/cache.db.d.ts +5 -5
- package/dist/adapter/cachedb/cache.db.js +8 -12
- package/dist/adapter/cachedb/cache.db.model.d.ts +2 -2
- package/dist/adapter/cachedb/cache.db.model.js +1 -2
- package/dist/adapter/cachedb/index.d.ts +2 -2
- package/dist/adapter/cachedb/index.js +2 -5
- package/dist/adapter/file/file.db.d.ts +5 -6
- package/dist/adapter/file/file.db.js +25 -27
- package/dist/adapter/file/file.db.model.d.ts +2 -2
- package/dist/adapter/file/file.db.model.js +1 -2
- package/dist/adapter/file/inMemory.persistence.plugin.d.ts +2 -2
- package/dist/adapter/file/inMemory.persistence.plugin.js +3 -7
- package/dist/adapter/file/index.d.ts +3 -3
- package/dist/adapter/file/index.js +3 -7
- package/dist/adapter/file/localFile.persistence.plugin.d.ts +2 -2
- package/dist/adapter/file/localFile.persistence.plugin.js +11 -15
- package/dist/adapter/file/noop.persistence.plugin.d.ts +2 -2
- package/dist/adapter/file/noop.persistence.plugin.js +1 -5
- package/dist/adapter/inmemory/inMemory.db.d.ts +5 -4
- package/dist/adapter/inmemory/inMemory.db.js +43 -41
- package/dist/adapter/inmemory/inMemoryKeyValueDB.d.ts +2 -2
- package/dist/adapter/inmemory/inMemoryKeyValueDB.js +7 -11
- package/dist/adapter/inmemory/queryInMemory.d.ts +1 -1
- package/dist/adapter/inmemory/queryInMemory.js +4 -7
- package/dist/base.common.db.d.ts +6 -4
- package/dist/base.common.db.js +8 -9
- package/dist/cnst.js +2 -5
- package/dist/common.db.d.ts +8 -2
- package/dist/common.db.js +3 -6
- package/dist/commondao/common.dao.d.ts +11 -4
- package/dist/commondao/common.dao.js +120 -113
- package/dist/commondao/common.dao.model.d.ts +2 -2
- package/dist/commondao/common.dao.model.js +2 -5
- package/dist/db.model.d.ts +6 -1
- package/dist/db.model.js +4 -7
- package/dist/index.d.ts +17 -17
- package/dist/index.js +17 -20
- package/dist/kv/commonKeyValueDB.d.ts +1 -1
- package/dist/kv/commonKeyValueDB.js +1 -4
- package/dist/kv/commonKeyValueDao.d.ts +3 -3
- package/dist/kv/commonKeyValueDao.js +10 -14
- package/dist/kv/commonKeyValueDaoMemoCache.d.ts +1 -1
- package/dist/kv/commonKeyValueDaoMemoCache.js +4 -8
- package/dist/model.util.js +9 -15
- package/dist/pipeline/dbPipelineBackup.d.ts +2 -2
- package/dist/pipeline/dbPipelineBackup.js +27 -30
- package/dist/pipeline/dbPipelineCopy.d.ts +2 -2
- package/dist/pipeline/dbPipelineCopy.js +20 -23
- package/dist/pipeline/dbPipelineRestore.d.ts +2 -2
- package/dist/pipeline/dbPipelineRestore.js +27 -30
- package/dist/query/dbQuery.d.ts +3 -3
- package/dist/query/dbQuery.js +7 -12
- package/dist/testing/{dbTest.d.ts → commonDBTest.d.ts} +1 -1
- package/dist/testing/{dbTest.js → commonDBTest.js} +81 -62
- package/dist/testing/commonDaoTest.d.ts +3 -0
- package/dist/testing/{daoTest.js → commonDaoTest.js} +101 -38
- package/dist/testing/index.d.ts +7 -7
- package/dist/testing/index.js +6 -20
- package/dist/testing/keyValueDBTest.d.ts +1 -1
- package/dist/testing/keyValueDBTest.js +28 -31
- package/dist/testing/keyValueDaoTest.d.ts +1 -1
- package/dist/testing/keyValueDaoTest.js +8 -11
- package/dist/testing/test.model.js +30 -37
- package/dist/testing/timeSeriesTest.util.d.ts +1 -1
- package/dist/testing/timeSeriesTest.util.js +3 -6
- package/dist/timeseries/commonTimeSeriesDao.d.ts +1 -1
- package/dist/timeseries/commonTimeSeriesDao.js +5 -9
- package/dist/timeseries/timeSeries.model.d.ts +1 -1
- package/dist/timeseries/timeSeries.model.js +1 -2
- package/dist/transaction/dbTransaction.util.d.ts +3 -2
- package/dist/transaction/dbTransaction.util.js +2 -5
- package/dist/validation/index.d.ts +2 -2
- package/dist/validation/index.js +25 -28
- package/package.json +3 -3
- package/src/adapter/cachedb/cache.db.model.ts +2 -2
- package/src/adapter/cachedb/cache.db.ts +6 -6
- package/src/adapter/cachedb/index.ts +2 -2
- package/src/adapter/file/file.db.model.ts +2 -2
- package/src/adapter/file/file.db.ts +8 -6
- package/src/adapter/file/inMemory.persistence.plugin.ts +2 -2
- package/src/adapter/file/index.ts +3 -3
- package/src/adapter/file/localFile.persistence.plugin.ts +2 -2
- package/src/adapter/file/noop.persistence.plugin.ts +2 -2
- package/src/adapter/inmemory/inMemory.db.ts +15 -10
- package/src/adapter/inmemory/inMemoryKeyValueDB.ts +7 -3
- package/src/adapter/inmemory/queryInMemory.ts +1 -1
- package/src/base.common.db.ts +9 -5
- package/src/common.db.ts +10 -2
- package/src/commondao/common.dao.model.ts +2 -2
- package/src/commondao/common.dao.ts +20 -6
- package/src/db.model.ts +7 -1
- package/src/index.ts +17 -17
- package/src/kv/commonKeyValueDB.ts +1 -1
- package/src/kv/commonKeyValueDao.ts +3 -3
- package/src/kv/commonKeyValueDaoMemoCache.ts +1 -1
- package/src/pipeline/dbPipelineBackup.ts +2 -2
- package/src/pipeline/dbPipelineCopy.ts +3 -3
- package/src/pipeline/dbPipelineRestore.ts +2 -2
- package/src/query/dbQuery.ts +3 -3
- package/src/testing/{dbTest.ts → commonDBTest.ts} +34 -6
- package/src/testing/{daoTest.ts → commonDaoTest.ts} +89 -11
- package/src/testing/index.ts +7 -7
- package/src/testing/keyValueDBTest.ts +2 -2
- package/src/testing/keyValueDaoTest.ts +3 -3
- package/src/testing/timeSeriesTest.util.ts +1 -1
- package/src/timeseries/commonTimeSeriesDao.ts +2 -2
- package/src/timeseries/timeSeries.model.ts +1 -1
- package/src/transaction/dbTransaction.util.ts +3 -2
- package/src/validation/index.ts +8 -3
- package/dist/testing/daoTest.d.ts +0 -3
|
@@ -1,25 +1,23 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
const test_model_1 = require("./test.model");
|
|
10
|
-
async function runCommonDaoTest(db, quirks = {}) {
|
|
1
|
+
import { Readable } from 'node:stream';
|
|
2
|
+
import { _deepCopy, _filterObject, _omit, _pick, _sortBy, localTime } from '@naturalcycles/js-lib';
|
|
3
|
+
import { _pipeline } from '@naturalcycles/nodejs-lib';
|
|
4
|
+
import { CommonDao } from '../commondao/common.dao.js';
|
|
5
|
+
import { CommonDaoLogLevel } from '../commondao/common.dao.model.js';
|
|
6
|
+
import { DBQuery } from '../query/dbQuery.js';
|
|
7
|
+
import { createTestItemBM, createTestItemsBM, TEST_TABLE, testItemBMJsonSchema, testItemBMSchema, } from './test.model.js';
|
|
8
|
+
export async function runCommonDaoTest(db, quirks = {}) {
|
|
11
9
|
// this is because vitest cannot be "required" from cjs
|
|
12
10
|
const { test, expect } = await import('vitest');
|
|
13
11
|
const { support } = db;
|
|
14
|
-
const dao = new
|
|
15
|
-
table:
|
|
12
|
+
const dao = new CommonDao({
|
|
13
|
+
table: TEST_TABLE,
|
|
16
14
|
db,
|
|
17
|
-
bmSchema:
|
|
15
|
+
bmSchema: testItemBMSchema,
|
|
18
16
|
logStarted: true,
|
|
19
|
-
logLevel:
|
|
17
|
+
logLevel: CommonDaoLogLevel.DATA_FULL,
|
|
20
18
|
});
|
|
21
|
-
const items =
|
|
22
|
-
const itemsClone =
|
|
19
|
+
const items = createTestItemsBM(3);
|
|
20
|
+
const itemsClone = _deepCopy(items);
|
|
23
21
|
// deepFreeze(items) // mutation of id/created/updated is allowed now! (even expected)
|
|
24
22
|
const item1 = items[0];
|
|
25
23
|
const expectedItems = items.map(i => ({
|
|
@@ -32,14 +30,14 @@ async function runCommonDaoTest(db, quirks = {}) {
|
|
|
32
30
|
// CREATE TABLE, DROP
|
|
33
31
|
if (support.createTable) {
|
|
34
32
|
test('createTable, dropIfExists=true', async () => {
|
|
35
|
-
await dao.createTable(
|
|
33
|
+
await dao.createTable(testItemBMJsonSchema, { dropIfExists: true });
|
|
36
34
|
});
|
|
37
35
|
}
|
|
38
36
|
if (support.queries) {
|
|
39
37
|
// DELETE ALL initially
|
|
40
38
|
test('deleteByIds test items', async () => {
|
|
41
39
|
const rows = await dao.query().select(['id']).runQuery();
|
|
42
|
-
await db.deleteByQuery(
|
|
40
|
+
await db.deleteByQuery(DBQuery.create(TEST_TABLE).filter('id', 'in', rows.map(r => r.id)));
|
|
43
41
|
});
|
|
44
42
|
// QUERY empty
|
|
45
43
|
test('runQuery(all), runQueryCount should return empty', async () => {
|
|
@@ -63,7 +61,7 @@ async function runCommonDaoTest(db, quirks = {}) {
|
|
|
63
61
|
if (support.timeMachine) {
|
|
64
62
|
test('getByIds(...) 10 minutes ago should return []', async () => {
|
|
65
63
|
expect(await dao.getByIds([item1.id, 'abc'], {
|
|
66
|
-
readAt:
|
|
64
|
+
readAt: localTime.now().minus(10, 'minute').unix,
|
|
67
65
|
})).toEqual([]);
|
|
68
66
|
});
|
|
69
67
|
}
|
|
@@ -71,7 +69,7 @@ async function runCommonDaoTest(db, quirks = {}) {
|
|
|
71
69
|
if (support.nullValues) {
|
|
72
70
|
test('should allow to save and load null values', async () => {
|
|
73
71
|
const item3 = {
|
|
74
|
-
...
|
|
72
|
+
...createTestItemBM(3),
|
|
75
73
|
k2: null,
|
|
76
74
|
};
|
|
77
75
|
// deepFreeze(item3) // no, Dao is expected to mutate object to add id, created, updated
|
|
@@ -85,7 +83,7 @@ async function runCommonDaoTest(db, quirks = {}) {
|
|
|
85
83
|
}
|
|
86
84
|
test('undefined values should not be saved/loaded', async () => {
|
|
87
85
|
const item3 = {
|
|
88
|
-
...
|
|
86
|
+
...createTestItemBM(3),
|
|
89
87
|
k2: undefined,
|
|
90
88
|
};
|
|
91
89
|
// deepFreeze(item3) // no, Dao is expected to mutate object to add id, created, updated
|
|
@@ -112,7 +110,7 @@ async function runCommonDaoTest(db, quirks = {}) {
|
|
|
112
110
|
test('increment', async () => {
|
|
113
111
|
await dao.incrementBatch('k3', { id1: 1, id2: 2 });
|
|
114
112
|
let rows = await dao.query().runQuery();
|
|
115
|
-
rows =
|
|
113
|
+
rows = _sortBy(rows, r => r.id);
|
|
116
114
|
const expected = expectedItems.map(r => {
|
|
117
115
|
if (r.id === 'id1') {
|
|
118
116
|
return {
|
|
@@ -137,19 +135,19 @@ async function runCommonDaoTest(db, quirks = {}) {
|
|
|
137
135
|
// GET not empty
|
|
138
136
|
test('getByIds all items', async () => {
|
|
139
137
|
const rows = await dao.getByIds(items.map(i => i.id).concat('abcd'));
|
|
140
|
-
expectMatch(expectedItems,
|
|
138
|
+
expectMatch(expectedItems, _sortBy(rows, r => r.id), quirks);
|
|
141
139
|
});
|
|
142
140
|
// QUERY
|
|
143
141
|
if (support.queries) {
|
|
144
142
|
test('runQuery(all) should return all items', async () => {
|
|
145
143
|
let rows = await dao.query().runQuery();
|
|
146
|
-
rows =
|
|
144
|
+
rows = _sortBy(rows, r => r.id);
|
|
147
145
|
expectMatch(expectedItems, rows, quirks);
|
|
148
146
|
});
|
|
149
147
|
if (support.dbQueryFilter) {
|
|
150
148
|
test('query even=true', async () => {
|
|
151
149
|
let rows = await dao.query().filter('even', '==', true).runQuery();
|
|
152
|
-
rows =
|
|
150
|
+
rows = _sortBy(rows, r => r.id);
|
|
153
151
|
expectMatch(expectedItems.filter(i => i.even), rows, quirks);
|
|
154
152
|
});
|
|
155
153
|
test('query nested property', async () => {
|
|
@@ -157,7 +155,7 @@ async function runCommonDaoTest(db, quirks = {}) {
|
|
|
157
155
|
.query()
|
|
158
156
|
.filter('nested.foo', '==', 1)
|
|
159
157
|
.runQuery();
|
|
160
|
-
rows =
|
|
158
|
+
rows = _sortBy(rows, r => r.id);
|
|
161
159
|
expectMatch(expectedItems.filter(i => i.nested?.foo === 1), rows, quirks);
|
|
162
160
|
});
|
|
163
161
|
}
|
|
@@ -170,8 +168,8 @@ async function runCommonDaoTest(db, quirks = {}) {
|
|
|
170
168
|
if (support.dbQuerySelectFields) {
|
|
171
169
|
test('projection query with only ids', async () => {
|
|
172
170
|
let rows = await dao.query().select(['id']).runQuery();
|
|
173
|
-
rows =
|
|
174
|
-
expectMatch(expectedItems.map(item =>
|
|
171
|
+
rows = _sortBy(rows, r => r.id);
|
|
172
|
+
expectMatch(expectedItems.map(item => _pick(item, ['id'])), rows, quirks);
|
|
175
173
|
});
|
|
176
174
|
}
|
|
177
175
|
test('runQueryCount should return 3', async () => {
|
|
@@ -183,12 +181,12 @@ async function runCommonDaoTest(db, quirks = {}) {
|
|
|
183
181
|
test('streamQueryForEach all', async () => {
|
|
184
182
|
let rows = [];
|
|
185
183
|
await dao.query().streamQueryForEach(bm => void rows.push(bm));
|
|
186
|
-
rows =
|
|
184
|
+
rows = _sortBy(rows, r => r.id);
|
|
187
185
|
expectMatch(expectedItems, rows, quirks);
|
|
188
186
|
});
|
|
189
187
|
test('streamQuery all', async () => {
|
|
190
188
|
let rows = await dao.query().streamQuery().toArray();
|
|
191
|
-
rows =
|
|
189
|
+
rows = _sortBy(rows, r => r.id);
|
|
192
190
|
expectMatch(expectedItems, rows, quirks);
|
|
193
191
|
});
|
|
194
192
|
test('streamQueryIdsForEach all', async () => {
|
|
@@ -203,9 +201,9 @@ async function runCommonDaoTest(db, quirks = {}) {
|
|
|
203
201
|
expectMatch(expectedItems.map(i => i.id), ids, quirks);
|
|
204
202
|
});
|
|
205
203
|
test('streamSaveTransform', async () => {
|
|
206
|
-
const items2 =
|
|
204
|
+
const items2 = createTestItemsBM(2).map(i => ({ ...i, id: i.id + '_str' }));
|
|
207
205
|
const ids = items2.map(i => i.id);
|
|
208
|
-
await
|
|
206
|
+
await _pipeline([Readable.from(items2), ...dao.streamSaveTransform()]);
|
|
209
207
|
const items2Loaded = await dao.getByIds(ids);
|
|
210
208
|
expectMatch(items2, items2Loaded, quirks);
|
|
211
209
|
// cleanup
|
|
@@ -225,13 +223,23 @@ async function runCommonDaoTest(db, quirks = {}) {
|
|
|
225
223
|
});
|
|
226
224
|
}
|
|
227
225
|
if (support.transactions) {
|
|
226
|
+
/**
|
|
227
|
+
* Returns expected items in the DB after the preparation.
|
|
228
|
+
*/
|
|
229
|
+
async function prepare() {
|
|
230
|
+
// cleanup
|
|
231
|
+
await dao.query().deleteByQuery();
|
|
232
|
+
const itemsToSave = [items[0], { ...items[2], k1: 'k1_mod' }];
|
|
233
|
+
await dao.saveBatch(itemsToSave);
|
|
234
|
+
return itemsToSave;
|
|
235
|
+
}
|
|
228
236
|
test('transaction happy path', async () => {
|
|
229
237
|
// cleanup
|
|
230
238
|
await dao.query().deleteByQuery();
|
|
231
239
|
// Test that id, created, updated are created
|
|
232
|
-
const now =
|
|
240
|
+
const now = localTime.nowUnix();
|
|
233
241
|
await dao.runInTransaction(async (tx) => {
|
|
234
|
-
const row =
|
|
242
|
+
const row = _omit(item1, ['id', 'created', 'updated']);
|
|
235
243
|
await tx.save(dao, row);
|
|
236
244
|
});
|
|
237
245
|
const loaded = await dao.query().runQuery();
|
|
@@ -255,13 +263,68 @@ async function runCommonDaoTest(db, quirks = {}) {
|
|
|
255
263
|
const expected = [items[0], { ...items[2], k1: 'k1_mod' }];
|
|
256
264
|
expectMatch(expected, rows, quirks);
|
|
257
265
|
});
|
|
266
|
+
test('createTransaction happy path', async () => {
|
|
267
|
+
// cleanup
|
|
268
|
+
await dao.query().deleteByQuery();
|
|
269
|
+
// Test that id, created, updated are created
|
|
270
|
+
const now = localTime.nowUnix();
|
|
271
|
+
const row = _omit(item1, ['id', 'created', 'updated']);
|
|
272
|
+
let tx = await dao.createTransaction();
|
|
273
|
+
await tx.save(dao, row);
|
|
274
|
+
await tx.commit();
|
|
275
|
+
const loaded = await dao.query().runQuery();
|
|
276
|
+
expect(loaded.length).toBe(1);
|
|
277
|
+
expect(loaded[0].id).toBeDefined();
|
|
278
|
+
expect(loaded[0].created).toBeGreaterThanOrEqual(now);
|
|
279
|
+
expect(loaded[0].updated).toBe(loaded[0].created);
|
|
280
|
+
tx = await dao.createTransaction();
|
|
281
|
+
await tx.deleteById(dao, loaded[0].id);
|
|
282
|
+
await tx.commit();
|
|
283
|
+
// saveBatch [item1, 2, 3]
|
|
284
|
+
// save item3 with k1: k1_mod
|
|
285
|
+
// delete item2
|
|
286
|
+
// remaining: item1, item3_with_k1_mod
|
|
287
|
+
tx = await dao.createTransaction();
|
|
288
|
+
await tx.saveBatch(dao, items);
|
|
289
|
+
await tx.save(dao, { ...items[2], k1: 'k1_mod' });
|
|
290
|
+
await tx.deleteById(dao, items[1].id);
|
|
291
|
+
await tx.commit();
|
|
292
|
+
const rows = await dao.query().runQuery();
|
|
293
|
+
const expected = [items[0], { ...items[2], k1: 'k1_mod' }];
|
|
294
|
+
expectMatch(expected, rows, quirks);
|
|
295
|
+
});
|
|
258
296
|
test('transaction rollback', async () => {
|
|
259
|
-
await
|
|
297
|
+
const expected = await prepare();
|
|
298
|
+
let err;
|
|
299
|
+
try {
|
|
300
|
+
await dao.runInTransaction(async (tx) => {
|
|
301
|
+
await tx.deleteById(dao, items[2].id);
|
|
302
|
+
await tx.save(dao, { ...items[0], k1: 5 }); // it should fail here
|
|
303
|
+
});
|
|
304
|
+
}
|
|
305
|
+
catch (err_) {
|
|
306
|
+
err = err_;
|
|
307
|
+
}
|
|
308
|
+
expect(err).toBeDefined();
|
|
309
|
+
expect(err).toBeInstanceOf(Error);
|
|
310
|
+
const rows = await dao.query().runQuery();
|
|
311
|
+
expectMatch(expected, rows, quirks);
|
|
312
|
+
});
|
|
313
|
+
test('createTransaction rollback', async () => {
|
|
314
|
+
const expected = await prepare();
|
|
315
|
+
let err;
|
|
316
|
+
try {
|
|
317
|
+
const tx = await dao.createTransaction();
|
|
260
318
|
await tx.deleteById(dao, items[2].id);
|
|
261
319
|
await tx.save(dao, { ...items[0], k1: 5 }); // it should fail here
|
|
262
|
-
|
|
320
|
+
await tx.commit();
|
|
321
|
+
}
|
|
322
|
+
catch (err_) {
|
|
323
|
+
err = err_;
|
|
324
|
+
}
|
|
325
|
+
expect(err).toBeDefined();
|
|
326
|
+
expect(err).toBeInstanceOf(Error);
|
|
263
327
|
const rows = await dao.query().runQuery();
|
|
264
|
-
const expected = [items[0], { ...items[2], k1: 'k1_mod' }];
|
|
265
328
|
expectMatch(expected, rows, quirks);
|
|
266
329
|
});
|
|
267
330
|
if (support.queries) {
|
|
@@ -275,7 +338,7 @@ async function runCommonDaoTest(db, quirks = {}) {
|
|
|
275
338
|
// const actualSorted = sortObjectDeep(actual)
|
|
276
339
|
if (quirks.allowBooleansAsUndefined) {
|
|
277
340
|
expected = expected.map(r => {
|
|
278
|
-
return typeof r !== 'object' ? r :
|
|
341
|
+
return typeof r !== 'object' ? r : _filterObject(r, (_k, v) => v !== false);
|
|
279
342
|
});
|
|
280
343
|
}
|
|
281
344
|
if (quirks.allowExtraPropertiesInResponse) {
|
package/dist/testing/index.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { runCommonDaoTest } from './
|
|
2
|
-
import type { CommonDBImplementationQuirks } from './
|
|
3
|
-
import { runCommonDBTest } from './
|
|
4
|
-
import { runCommonKeyValueDaoTest } from './keyValueDaoTest';
|
|
5
|
-
import { runCommonKeyValueDBTest } from './keyValueDBTest';
|
|
6
|
-
import type { TestItemBM, TestItemDBM, TestItemTM } from './test.model';
|
|
7
|
-
import { createTestItemBM, createTestItemDBM, createTestItemsBM, createTestItemsDBM, TEST_TABLE, testItemBMJsonSchema, testItemBMSchema, testItemTMSchema } from './test.model';
|
|
1
|
+
import { runCommonDaoTest } from './commonDaoTest.js';
|
|
2
|
+
import type { CommonDBImplementationQuirks } from './commonDBTest.js';
|
|
3
|
+
import { runCommonDBTest } from './commonDBTest.js';
|
|
4
|
+
import { runCommonKeyValueDaoTest } from './keyValueDaoTest.js';
|
|
5
|
+
import { runCommonKeyValueDBTest } from './keyValueDBTest.js';
|
|
6
|
+
import type { TestItemBM, TestItemDBM, TestItemTM } from './test.model.js';
|
|
7
|
+
import { createTestItemBM, createTestItemDBM, createTestItemsBM, createTestItemsDBM, TEST_TABLE, testItemBMJsonSchema, testItemBMSchema, testItemTMSchema } from './test.model.js';
|
|
8
8
|
export type { CommonDBImplementationQuirks, TestItemBM, TestItemDBM, TestItemTM };
|
|
9
9
|
export { createTestItemBM, createTestItemDBM, createTestItemsBM, createTestItemsDBM, runCommonDaoTest, runCommonDBTest, runCommonKeyValueDaoTest, runCommonKeyValueDBTest, TEST_TABLE, testItemBMJsonSchema, testItemBMSchema, testItemTMSchema, };
|
package/dist/testing/index.js
CHANGED
|
@@ -1,20 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
Object.defineProperty(exports, "runCommonDBTest", { enumerable: true, get: function () { return dbTest_1.runCommonDBTest; } });
|
|
8
|
-
const keyValueDaoTest_1 = require("./keyValueDaoTest");
|
|
9
|
-
Object.defineProperty(exports, "runCommonKeyValueDaoTest", { enumerable: true, get: function () { return keyValueDaoTest_1.runCommonKeyValueDaoTest; } });
|
|
10
|
-
const keyValueDBTest_1 = require("./keyValueDBTest");
|
|
11
|
-
Object.defineProperty(exports, "runCommonKeyValueDBTest", { enumerable: true, get: function () { return keyValueDBTest_1.runCommonKeyValueDBTest; } });
|
|
12
|
-
const test_model_1 = require("./test.model");
|
|
13
|
-
Object.defineProperty(exports, "createTestItemBM", { enumerable: true, get: function () { return test_model_1.createTestItemBM; } });
|
|
14
|
-
Object.defineProperty(exports, "createTestItemDBM", { enumerable: true, get: function () { return test_model_1.createTestItemDBM; } });
|
|
15
|
-
Object.defineProperty(exports, "createTestItemsBM", { enumerable: true, get: function () { return test_model_1.createTestItemsBM; } });
|
|
16
|
-
Object.defineProperty(exports, "createTestItemsDBM", { enumerable: true, get: function () { return test_model_1.createTestItemsDBM; } });
|
|
17
|
-
Object.defineProperty(exports, "TEST_TABLE", { enumerable: true, get: function () { return test_model_1.TEST_TABLE; } });
|
|
18
|
-
Object.defineProperty(exports, "testItemBMJsonSchema", { enumerable: true, get: function () { return test_model_1.testItemBMJsonSchema; } });
|
|
19
|
-
Object.defineProperty(exports, "testItemBMSchema", { enumerable: true, get: function () { return test_model_1.testItemBMSchema; } });
|
|
20
|
-
Object.defineProperty(exports, "testItemTMSchema", { enumerable: true, get: function () { return test_model_1.testItemTMSchema; } });
|
|
1
|
+
import { runCommonDaoTest } from './commonDaoTest.js';
|
|
2
|
+
import { runCommonDBTest } from './commonDBTest.js';
|
|
3
|
+
import { runCommonKeyValueDaoTest } from './keyValueDaoTest.js';
|
|
4
|
+
import { runCommonKeyValueDBTest } from './keyValueDBTest.js';
|
|
5
|
+
import { createTestItemBM, createTestItemDBM, createTestItemsBM, createTestItemsDBM, TEST_TABLE, testItemBMJsonSchema, testItemBMSchema, testItemTMSchema, } from './test.model.js';
|
|
6
|
+
export { createTestItemBM, createTestItemDBM, createTestItemsBM, createTestItemsDBM, runCommonDaoTest, runCommonDBTest, runCommonKeyValueDaoTest, runCommonKeyValueDBTest, TEST_TABLE, testItemBMJsonSchema, testItemBMSchema, testItemTMSchema, };
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import type { CommonKeyValueDB } from '../kv/commonKeyValueDB';
|
|
1
|
+
import type { CommonKeyValueDB } from '../kv/commonKeyValueDB.js';
|
|
2
2
|
export declare function runCommonKeyValueDBTest(db: CommonKeyValueDB): Promise<void>;
|
|
@@ -1,111 +1,108 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const js_lib_1 = require("@naturalcycles/js-lib");
|
|
5
|
-
const test_model_1 = require("./test.model");
|
|
6
|
-
const testIds = (0, js_lib_1._range)(1, 4).map(n => `id${n}`);
|
|
1
|
+
import { _range, _sortBy } from '@naturalcycles/js-lib';
|
|
2
|
+
import { TEST_TABLE } from './test.model.js';
|
|
3
|
+
const testIds = _range(1, 4).map(n => `id${n}`);
|
|
7
4
|
const testEntries = testIds.map(id => [
|
|
8
5
|
id,
|
|
9
6
|
Buffer.from(`${id}value`),
|
|
10
7
|
]);
|
|
11
|
-
async function runCommonKeyValueDBTest(db) {
|
|
8
|
+
export async function runCommonKeyValueDBTest(db) {
|
|
12
9
|
const { afterAll, beforeAll, expect, test } = await import('vitest');
|
|
13
10
|
beforeAll(async () => {
|
|
14
11
|
// Tests in this suite are not isolated,
|
|
15
12
|
// and failing tests can leave the DB in an unexpected state for other tests,
|
|
16
13
|
// including the following test run.
|
|
17
14
|
// Here we clear the table before running the tests.
|
|
18
|
-
const ids = await db.streamIds(
|
|
19
|
-
await db.deleteByIds(
|
|
15
|
+
const ids = await db.streamIds(TEST_TABLE).toArray();
|
|
16
|
+
await db.deleteByIds(TEST_TABLE, ids);
|
|
20
17
|
});
|
|
21
18
|
afterAll(async () => {
|
|
22
|
-
const ids = await db.streamIds(
|
|
23
|
-
await db.deleteByIds(
|
|
19
|
+
const ids = await db.streamIds(TEST_TABLE).toArray();
|
|
20
|
+
await db.deleteByIds(TEST_TABLE, ids);
|
|
24
21
|
});
|
|
25
22
|
const { support } = db;
|
|
26
23
|
test('ping', async () => {
|
|
27
24
|
await db.ping();
|
|
28
25
|
});
|
|
29
26
|
test('createTable', async () => {
|
|
30
|
-
await db.createTable(
|
|
27
|
+
await db.createTable(TEST_TABLE, { dropIfExists: true });
|
|
31
28
|
});
|
|
32
29
|
test('deleteByIds non existing', async () => {
|
|
33
|
-
await db.deleteByIds(
|
|
30
|
+
await db.deleteByIds(TEST_TABLE, testIds);
|
|
34
31
|
});
|
|
35
32
|
test('getByIds should return empty', async () => {
|
|
36
|
-
const results = await db.getByIds(
|
|
33
|
+
const results = await db.getByIds(TEST_TABLE, testIds);
|
|
37
34
|
expect(results).toEqual([]);
|
|
38
35
|
});
|
|
39
36
|
test('count should be 0', async () => {
|
|
40
|
-
expect(await db.count(
|
|
37
|
+
expect(await db.count(TEST_TABLE)).toBe(0);
|
|
41
38
|
});
|
|
42
39
|
test('saveBatch, then getByIds', async () => {
|
|
43
|
-
await db.saveBatch(
|
|
44
|
-
const entries = await db.getByIds(
|
|
45
|
-
|
|
40
|
+
await db.saveBatch(TEST_TABLE, testEntries);
|
|
41
|
+
const entries = await db.getByIds(TEST_TABLE, testIds);
|
|
42
|
+
_sortBy(entries, e => e[0], true);
|
|
46
43
|
expect(entries).toEqual(testEntries);
|
|
47
44
|
});
|
|
48
45
|
if (support.count) {
|
|
49
46
|
test('count should be 3', async () => {
|
|
50
|
-
expect(await db.count(
|
|
47
|
+
expect(await db.count(TEST_TABLE)).toBe(3);
|
|
51
48
|
});
|
|
52
49
|
}
|
|
53
50
|
test('streamIds', async () => {
|
|
54
|
-
const ids = await db.streamIds(
|
|
51
|
+
const ids = await db.streamIds(TEST_TABLE).toArray();
|
|
55
52
|
ids.sort();
|
|
56
53
|
expect(ids).toEqual(testIds);
|
|
57
54
|
});
|
|
58
55
|
test('streamIds limited', async () => {
|
|
59
|
-
const idsLimited = await db.streamIds(
|
|
56
|
+
const idsLimited = await db.streamIds(TEST_TABLE, 2).toArray();
|
|
60
57
|
// Order is non-deterministic, so, cannot compare values
|
|
61
58
|
// idsLimited.sort()
|
|
62
59
|
// expect(idsLimited).toEqual(testIds.slice(0, 2))
|
|
63
60
|
expect(idsLimited.length).toBe(2);
|
|
64
61
|
});
|
|
65
62
|
test('streamValues', async () => {
|
|
66
|
-
const values = await db.streamValues(
|
|
63
|
+
const values = await db.streamValues(TEST_TABLE).toArray();
|
|
67
64
|
values.sort();
|
|
68
65
|
expect(values).toEqual(testEntries.map(e => e[1]));
|
|
69
66
|
});
|
|
70
67
|
test('streamValues limited', async () => {
|
|
71
|
-
const valuesLimited = await db.streamValues(
|
|
68
|
+
const valuesLimited = await db.streamValues(TEST_TABLE, 2).toArray();
|
|
72
69
|
// valuesLimited.sort()
|
|
73
70
|
// expect(valuesLimited).toEqual(testEntries.map(e => e[1]).slice(0, 2))
|
|
74
71
|
expect(valuesLimited.length).toBe(2);
|
|
75
72
|
});
|
|
76
73
|
test('streamEntries', async () => {
|
|
77
|
-
const entries = await db.streamEntries(
|
|
74
|
+
const entries = await db.streamEntries(TEST_TABLE).toArray();
|
|
78
75
|
entries.sort();
|
|
79
76
|
expect(entries).toEqual(testEntries);
|
|
80
77
|
});
|
|
81
78
|
test('streamEntries limited', async () => {
|
|
82
|
-
const entriesLimited = await db.streamEntries(
|
|
79
|
+
const entriesLimited = await db.streamEntries(TEST_TABLE, 2).toArray();
|
|
83
80
|
// entriesLimited.sort()
|
|
84
81
|
// expect(entriesLimited).toEqual(testEntries.slice(0, 2))
|
|
85
82
|
expect(entriesLimited.length).toBe(2);
|
|
86
83
|
});
|
|
87
84
|
test('deleteByIds should clear', async () => {
|
|
88
|
-
await db.deleteByIds(
|
|
89
|
-
const results = await db.getByIds(
|
|
85
|
+
await db.deleteByIds(TEST_TABLE, testIds);
|
|
86
|
+
const results = await db.getByIds(TEST_TABLE, testIds);
|
|
90
87
|
expect(results).toEqual([]);
|
|
91
88
|
});
|
|
92
89
|
if (support.increment) {
|
|
93
90
|
const id = 'nonExistingField';
|
|
94
91
|
const id2 = 'nonExistingField2';
|
|
95
92
|
test('increment on a non-existing field should set the value to 1', async () => {
|
|
96
|
-
const result = await db.incrementBatch(
|
|
93
|
+
const result = await db.incrementBatch(TEST_TABLE, [[id, 1]]);
|
|
97
94
|
expect(result).toEqual([[id, 1]]);
|
|
98
95
|
});
|
|
99
96
|
test('increment on a existing field should increase the value by one', async () => {
|
|
100
|
-
const result = await db.incrementBatch(
|
|
97
|
+
const result = await db.incrementBatch(TEST_TABLE, [[id, 1]]);
|
|
101
98
|
expect(result).toEqual([[id, 2]]);
|
|
102
99
|
});
|
|
103
100
|
test('increment should increase the value by the specified amount', async () => {
|
|
104
|
-
const result = await db.incrementBatch(
|
|
101
|
+
const result = await db.incrementBatch(TEST_TABLE, [[id, 2]]);
|
|
105
102
|
expect(result).toEqual([[id, 4]]);
|
|
106
103
|
});
|
|
107
104
|
test('increment 2 ids at the same time', async () => {
|
|
108
|
-
const result = await db.incrementBatch(
|
|
105
|
+
const result = await db.incrementBatch(TEST_TABLE, [
|
|
109
106
|
[id, 1],
|
|
110
107
|
[id2, 2],
|
|
111
108
|
]);
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import type { CommonKeyValueDB } from '../kv/commonKeyValueDB';
|
|
1
|
+
import type { CommonKeyValueDB } from '../kv/commonKeyValueDB.js';
|
|
2
2
|
export declare function runCommonKeyValueDaoTest(db: CommonKeyValueDB): Promise<void>;
|
|
@@ -1,17 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const
|
|
5
|
-
const commonKeyValueDao_1 = require("../kv/commonKeyValueDao");
|
|
6
|
-
const test_model_1 = require("./test.model");
|
|
7
|
-
const testItems = (0, test_model_1.createTestItemsBM)(4);
|
|
1
|
+
import { _sortBy } from '@naturalcycles/js-lib';
|
|
2
|
+
import { CommonKeyValueDao } from '../kv/commonKeyValueDao.js';
|
|
3
|
+
import { createTestItemsBM, TEST_TABLE } from './test.model.js';
|
|
4
|
+
const testItems = createTestItemsBM(4);
|
|
8
5
|
const testIds = testItems.map(e => e.id);
|
|
9
6
|
const testEntries = testItems.map(e => [e.id, Buffer.from(`${e.id}value`)]);
|
|
10
|
-
async function runCommonKeyValueDaoTest(db) {
|
|
7
|
+
export async function runCommonKeyValueDaoTest(db) {
|
|
11
8
|
const { afterAll, beforeAll, expect, test } = await import('vitest');
|
|
12
|
-
const dao = new
|
|
9
|
+
const dao = new CommonKeyValueDao({
|
|
13
10
|
db,
|
|
14
|
-
table:
|
|
11
|
+
table: TEST_TABLE,
|
|
15
12
|
// todo: make this test support "deflatedJson" transformer
|
|
16
13
|
});
|
|
17
14
|
const { support } = db;
|
|
@@ -44,7 +41,7 @@ async function runCommonKeyValueDaoTest(db) {
|
|
|
44
41
|
await dao.saveBatch(testEntries);
|
|
45
42
|
const entries = await dao.getByIds(testIds);
|
|
46
43
|
// console.log(typeof entries[0]![1], entries[0]![1])
|
|
47
|
-
|
|
44
|
+
_sortBy(entries, e => e[0], true);
|
|
48
45
|
expect(entries).toEqual(testEntries); // Jest doesn't allow to compare Buffers directly
|
|
49
46
|
// expect(entries.map(e => e[0])).toEqual(testEntries.map(e => e[0]))
|
|
50
47
|
// expect(entries.map(e => e[1].toString())).toEqual(testEntries.map(e => e[1].toString()))
|
|
@@ -1,43 +1,36 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.testItemBMJsonSchema = exports.testItemTMSchema = exports.testItemBMSchema = exports.TEST_TABLE = void 0;
|
|
4
|
-
exports.createTestItemDBM = createTestItemDBM;
|
|
5
|
-
exports.createTestItemBM = createTestItemBM;
|
|
6
|
-
exports.createTestItemsDBM = createTestItemsDBM;
|
|
7
|
-
exports.createTestItemsBM = createTestItemsBM;
|
|
8
|
-
const js_lib_1 = require("@naturalcycles/js-lib");
|
|
9
|
-
const nodejs_lib_1 = require("@naturalcycles/nodejs-lib");
|
|
1
|
+
import { _range, jsonSchema } from '@naturalcycles/js-lib';
|
|
2
|
+
import { baseDBEntitySchema, binarySchema, booleanSchema, numberSchema, objectSchema, stringSchema, } from '@naturalcycles/nodejs-lib';
|
|
10
3
|
const MOCK_TS_2018_06_21 = 1529539200;
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
k1:
|
|
14
|
-
k2:
|
|
15
|
-
k3:
|
|
16
|
-
even:
|
|
17
|
-
b1:
|
|
18
|
-
nested:
|
|
19
|
-
foo:
|
|
4
|
+
export const TEST_TABLE = 'TEST_TABLE';
|
|
5
|
+
export const testItemBMSchema = objectSchema({
|
|
6
|
+
k1: stringSchema,
|
|
7
|
+
k2: stringSchema.allow(null).optional(),
|
|
8
|
+
k3: numberSchema.optional(),
|
|
9
|
+
even: booleanSchema.optional(),
|
|
10
|
+
b1: binarySchema.optional(),
|
|
11
|
+
nested: objectSchema({
|
|
12
|
+
foo: numberSchema,
|
|
20
13
|
}).optional(),
|
|
21
|
-
}).concat(
|
|
22
|
-
|
|
23
|
-
k1:
|
|
24
|
-
even:
|
|
14
|
+
}).concat(baseDBEntitySchema);
|
|
15
|
+
export const testItemTMSchema = objectSchema({
|
|
16
|
+
k1: stringSchema,
|
|
17
|
+
even: booleanSchema.optional(),
|
|
25
18
|
});
|
|
26
|
-
|
|
19
|
+
export const testItemBMJsonSchema = jsonSchema
|
|
27
20
|
.rootObject({
|
|
28
21
|
// todo: figure out how to not copy-paste these 3 fields
|
|
29
|
-
id:
|
|
30
|
-
created:
|
|
31
|
-
updated:
|
|
32
|
-
k1:
|
|
33
|
-
k2:
|
|
34
|
-
k3:
|
|
35
|
-
even:
|
|
36
|
-
b1:
|
|
22
|
+
id: jsonSchema.string(), // todo: not strictly needed here
|
|
23
|
+
created: jsonSchema.unixTimestamp(),
|
|
24
|
+
updated: jsonSchema.unixTimestamp(),
|
|
25
|
+
k1: jsonSchema.string(),
|
|
26
|
+
k2: jsonSchema.oneOf([jsonSchema.string(), jsonSchema.null()]).optional(),
|
|
27
|
+
k3: jsonSchema.number().optional(),
|
|
28
|
+
even: jsonSchema.boolean().optional(),
|
|
29
|
+
b1: jsonSchema.buffer().optional(),
|
|
37
30
|
})
|
|
38
31
|
.baseDBEntity()
|
|
39
32
|
.build();
|
|
40
|
-
function createTestItemDBM(num = 1) {
|
|
33
|
+
export function createTestItemDBM(num = 1) {
|
|
41
34
|
return {
|
|
42
35
|
id: `id${num}`,
|
|
43
36
|
k1: `v${num}`,
|
|
@@ -49,12 +42,12 @@ function createTestItemDBM(num = 1) {
|
|
|
49
42
|
updated: MOCK_TS_2018_06_21,
|
|
50
43
|
};
|
|
51
44
|
}
|
|
52
|
-
function createTestItemBM(num = 1) {
|
|
45
|
+
export function createTestItemBM(num = 1) {
|
|
53
46
|
return createTestItemDBM(num);
|
|
54
47
|
}
|
|
55
|
-
function createTestItemsDBM(count = 1) {
|
|
56
|
-
return
|
|
48
|
+
export function createTestItemsDBM(count = 1) {
|
|
49
|
+
return _range(1, count + 1).map(num => createTestItemDBM(num));
|
|
57
50
|
}
|
|
58
|
-
function createTestItemsBM(count = 1) {
|
|
59
|
-
return
|
|
51
|
+
export function createTestItemsBM(count = 1) {
|
|
52
|
+
return _range(1, count + 1).map(num => createTestItemBM(num));
|
|
60
53
|
}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import type { TimeSeriesDataPoint } from '../timeseries/timeSeries.model';
|
|
1
|
+
import type { TimeSeriesDataPoint } from '../timeseries/timeSeries.model.js';
|
|
2
2
|
export declare function createTestTimeSeries(count?: number): TimeSeriesDataPoint[];
|
|
@@ -1,8 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.createTestTimeSeries = createTestTimeSeries;
|
|
4
|
-
const js_lib_1 = require("@naturalcycles/js-lib");
|
|
5
|
-
function createTestTimeSeries(count = 10) {
|
|
1
|
+
import { _randomInt, _range } from '@naturalcycles/js-lib';
|
|
2
|
+
export function createTestTimeSeries(count = 10) {
|
|
6
3
|
const ts = Date.now();
|
|
7
|
-
return
|
|
4
|
+
return _range(1, count + 1).map(i => [ts - i * 60_000, _randomInt(10, 20)]);
|
|
8
5
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { CommonTimeSeriesDaoCfg, TimeSeriesDataPoint, TimeSeriesQuery, TimeSeriesSaveBatchOp } from './timeSeries.model';
|
|
1
|
+
import type { CommonTimeSeriesDaoCfg, TimeSeriesDataPoint, TimeSeriesQuery, TimeSeriesSaveBatchOp } from './timeSeries.model.js';
|
|
2
2
|
/**
|
|
3
3
|
* TimeSeries DB implementation based on provided CommonDB database.
|
|
4
4
|
* Turns any CommonDB database into TimeSeries DB. Kind of.
|