@naturalcycles/db-lib 10.0.0 → 10.0.2
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 +6 -6
- package/dist/adapter/cachedb/cache.db.js +6 -5
- package/dist/adapter/cachedb/cache.db.model.d.ts +3 -3
- package/dist/adapter/cachedb/index.d.ts +1 -1
- package/dist/adapter/file/file.db.d.ts +8 -7
- package/dist/adapter/file/file.db.js +11 -10
- package/dist/adapter/file/file.db.model.d.ts +2 -2
- package/dist/adapter/file/inMemory.persistence.plugin.d.ts +3 -3
- package/dist/adapter/file/inMemory.persistence.plugin.js +1 -3
- package/dist/adapter/file/index.d.ts +1 -1
- package/dist/adapter/file/localFile.persistence.plugin.d.ts +3 -3
- package/dist/adapter/file/localFile.persistence.plugin.js +1 -0
- package/dist/adapter/file/noop.persistence.plugin.d.ts +3 -3
- package/dist/adapter/inmemory/inMemory.db.d.ts +6 -5
- package/dist/adapter/inmemory/inMemory.db.js +13 -10
- package/dist/adapter/inmemory/inMemoryKeyValueDB.d.ts +4 -4
- package/dist/adapter/inmemory/inMemoryKeyValueDB.js +6 -5
- package/dist/adapter/inmemory/queryInMemory.d.ts +2 -2
- package/dist/base.common.db.d.ts +6 -5
- package/dist/base.common.db.js +2 -4
- package/dist/common.db.d.ts +3 -3
- package/dist/commondao/common.dao.d.ts +9 -6
- package/dist/commondao/common.dao.js +6 -2
- package/dist/commondao/common.dao.model.d.ts +5 -4
- package/dist/db.model.d.ts +2 -2
- package/dist/kv/commonKeyValueDB.d.ts +3 -3
- package/dist/kv/commonKeyValueDao.d.ts +5 -5
- package/dist/kv/commonKeyValueDao.js +1 -0
- package/dist/kv/commonKeyValueDaoMemoCache.d.ts +3 -2
- package/dist/kv/commonKeyValueDaoMemoCache.js +1 -0
- package/dist/model.util.d.ts +1 -1
- package/dist/pipeline/dbPipelineBackup.d.ts +5 -3
- package/dist/pipeline/dbPipelineCopy.d.ts +6 -4
- package/dist/pipeline/dbPipelineRestore.d.ts +6 -4
- package/dist/query/dbQuery.d.ts +5 -5
- package/dist/query/dbQuery.js +15 -5
- package/dist/testing/daoTest.d.ts +3 -3
- package/dist/testing/daoTest.js +89 -74
- package/dist/testing/dbTest.d.ts +2 -3
- package/dist/testing/dbTest.js +68 -68
- package/dist/testing/index.d.ts +4 -2
- package/dist/testing/keyValueDBTest.d.ts +2 -2
- package/dist/testing/keyValueDBTest.js +37 -37
- package/dist/testing/keyValueDaoTest.d.ts +2 -2
- package/dist/testing/keyValueDaoTest.js +33 -33
- package/dist/testing/test.model.d.ts +1 -1
- package/dist/testing/timeSeriesTest.util.d.ts +1 -1
- package/dist/timeseries/commonTimeSeriesDao.d.ts +1 -1
- package/dist/timeseries/commonTimeSeriesDao.js +1 -0
- package/dist/timeseries/timeSeries.model.d.ts +1 -1
- package/dist/transaction/dbTransaction.util.d.ts +2 -2
- package/dist/transaction/dbTransaction.util.js +1 -0
- package/dist/validation/index.d.ts +2 -2
- package/package.json +3 -2
- package/src/adapter/cachedb/cache.db.model.ts +3 -3
- package/src/adapter/cachedb/cache.db.ts +8 -7
- package/src/adapter/cachedb/index.ts +1 -1
- package/src/adapter/file/file.db.model.ts +2 -2
- package/src/adapter/file/file.db.ts +9 -15
- package/src/adapter/file/inMemory.persistence.plugin.ts +4 -3
- package/src/adapter/file/index.ts +1 -1
- package/src/adapter/file/localFile.persistence.plugin.ts +4 -3
- package/src/adapter/file/noop.persistence.plugin.ts +3 -3
- package/src/adapter/inmemory/inMemory.db.ts +14 -20
- package/src/adapter/inmemory/inMemoryKeyValueDB.ts +5 -9
- package/src/adapter/inmemory/queryInMemory.ts +3 -2
- package/src/base.common.db.ts +6 -5
- package/src/common.db.ts +3 -3
- package/src/commondao/common.dao.model.ts +5 -6
- package/src/commondao/common.dao.ts +25 -21
- package/src/db.model.ts +2 -2
- package/src/kv/commonKeyValueDB.ts +3 -3
- package/src/kv/commonKeyValueDao.ts +7 -5
- package/src/kv/commonKeyValueDaoMemoCache.ts +3 -8
- package/src/model.util.ts +2 -1
- package/src/pipeline/dbPipelineBackup.ts +4 -13
- package/src/pipeline/dbPipelineCopy.ts +5 -13
- package/src/pipeline/dbPipelineRestore.ts +9 -8
- package/src/query/dbQuery.ts +6 -11
- package/src/testing/daoTest.ts +28 -6
- package/src/testing/dbTest.ts +24 -22
- package/src/testing/index.ts +3 -4
- package/src/testing/keyValueDBTest.ts +6 -4
- package/src/testing/keyValueDaoTest.ts +4 -3
- package/src/testing/test.model.ts +2 -7
- package/src/testing/timeSeriesTest.util.ts +1 -1
- package/src/timeseries/commonTimeSeriesDao.ts +3 -2
- package/src/timeseries/timeSeries.model.ts +1 -1
- package/src/transaction/dbTransaction.util.ts +2 -2
- package/src/validation/index.ts +3 -8
package/dist/testing/dbTest.js
CHANGED
|
@@ -1,62 +1,62 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.runCommonDBTest = runCommonDBTest;
|
|
4
|
-
exports.expectMatch = expectMatch;
|
|
5
4
|
const js_lib_1 = require("@naturalcycles/js-lib");
|
|
6
|
-
const vitest_1 = require("vitest");
|
|
7
5
|
const common_db_1 = require("../common.db");
|
|
8
6
|
const dbQuery_1 = require("../query/dbQuery");
|
|
9
7
|
const test_model_1 = require("./test.model");
|
|
10
|
-
function runCommonDBTest(db, quirks = {}) {
|
|
8
|
+
async function runCommonDBTest(db, quirks = {}) {
|
|
9
|
+
// this is because vitest cannot be "required" from cjs
|
|
10
|
+
const { test, expect } = await import('vitest');
|
|
11
11
|
const { support } = db;
|
|
12
12
|
const items = (0, test_model_1.createTestItemsDBM)(3);
|
|
13
13
|
(0, js_lib_1._deepFreeze)(items);
|
|
14
14
|
const item1 = items[0];
|
|
15
15
|
const queryAll = () => dbQuery_1.DBQuery.create(test_model_1.TEST_TABLE);
|
|
16
|
-
|
|
16
|
+
test('ping', async () => {
|
|
17
17
|
await db.ping();
|
|
18
18
|
});
|
|
19
19
|
// CREATE TABLE, DROP
|
|
20
20
|
if (support.createTable) {
|
|
21
|
-
|
|
21
|
+
test('createTable, dropIfExists=true', async () => {
|
|
22
22
|
await db.createTable(test_model_1.TEST_TABLE, test_model_1.testItemBMJsonSchema, { dropIfExists: true });
|
|
23
23
|
});
|
|
24
24
|
}
|
|
25
25
|
if (support.queries) {
|
|
26
26
|
// DELETE ALL initially
|
|
27
|
-
|
|
27
|
+
test('deleteByIds test items', async () => {
|
|
28
28
|
const { rows } = await db.runQuery(queryAll().select(['id']));
|
|
29
29
|
await db.deleteByQuery(queryAll().filterIn('id', rows.map(i => i.id)));
|
|
30
30
|
});
|
|
31
31
|
// QUERY empty
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
32
|
+
test('runQuery(all), runQueryCount should return empty', async () => {
|
|
33
|
+
expect((await db.runQuery(queryAll())).rows).toEqual([]);
|
|
34
|
+
expect(await db.runQueryCount(queryAll())).toBe(0);
|
|
35
35
|
});
|
|
36
36
|
}
|
|
37
37
|
// GET empty
|
|
38
|
-
|
|
38
|
+
test('getByIds(item1.id) should return empty', async () => {
|
|
39
39
|
const [item1Loaded] = await db.getByIds(test_model_1.TEST_TABLE, [item1.id]);
|
|
40
40
|
// console.log(a)
|
|
41
|
-
|
|
41
|
+
expect(item1Loaded).toBeUndefined();
|
|
42
42
|
});
|
|
43
|
-
|
|
44
|
-
|
|
43
|
+
test('getByIds([]) should return []', async () => {
|
|
44
|
+
expect(await db.getByIds(test_model_1.TEST_TABLE, [])).toEqual([]);
|
|
45
45
|
});
|
|
46
|
-
|
|
47
|
-
|
|
46
|
+
test('getByIds(...) should return empty', async () => {
|
|
47
|
+
expect(await db.getByIds(test_model_1.TEST_TABLE, ['abc', 'abcd'])).toEqual([]);
|
|
48
48
|
});
|
|
49
49
|
// TimeMachine
|
|
50
50
|
if (support.timeMachine) {
|
|
51
|
-
|
|
52
|
-
|
|
51
|
+
test('getByIds(...) 10 minutes ago should return []', async () => {
|
|
52
|
+
expect(await db.getByIds(test_model_1.TEST_TABLE, [item1.id, 'abc'], {
|
|
53
53
|
readAt: js_lib_1.localTime.now().minus(10, 'minute').unix,
|
|
54
54
|
})).toEqual([]);
|
|
55
55
|
});
|
|
56
56
|
}
|
|
57
57
|
// SAVE
|
|
58
58
|
if (support.nullValues) {
|
|
59
|
-
|
|
59
|
+
test('should allow to save and load null values', async () => {
|
|
60
60
|
const item3 = {
|
|
61
61
|
...(0, test_model_1.createTestItemDBM)(3),
|
|
62
62
|
k2: null,
|
|
@@ -65,11 +65,11 @@ function runCommonDBTest(db, quirks = {}) {
|
|
|
65
65
|
await db.saveBatch(test_model_1.TEST_TABLE, [item3]);
|
|
66
66
|
const item3Loaded = (await db.getByIds(test_model_1.TEST_TABLE, [item3.id]))[0];
|
|
67
67
|
expectMatch([item3], [item3Loaded], quirks);
|
|
68
|
-
|
|
68
|
+
expect(item3Loaded.k2).toBeNull();
|
|
69
69
|
});
|
|
70
70
|
}
|
|
71
71
|
if (db.dbType === common_db_1.CommonDBType.document) {
|
|
72
|
-
|
|
72
|
+
test('undefined values should not be saved/loaded', async () => {
|
|
73
73
|
const item3 = {
|
|
74
74
|
...(0, test_model_1.createTestItemDBM)(3),
|
|
75
75
|
k2: undefined,
|
|
@@ -80,45 +80,45 @@ function runCommonDBTest(db, quirks = {}) {
|
|
|
80
80
|
await db.saveBatch(test_model_1.TEST_TABLE, [item3]);
|
|
81
81
|
const item3Loaded = (await db.getByIds(test_model_1.TEST_TABLE, [item3.id]))[0];
|
|
82
82
|
expectMatch([expected], [item3Loaded], quirks);
|
|
83
|
-
|
|
84
|
-
|
|
83
|
+
expect(item3Loaded.k2).toBeUndefined();
|
|
84
|
+
expect(Object.keys(item3Loaded)).not.toContain('k2');
|
|
85
85
|
});
|
|
86
86
|
}
|
|
87
87
|
if (support.updateSaveMethod) {
|
|
88
|
-
|
|
89
|
-
await
|
|
88
|
+
test('saveBatch UPDATE method should throw', async () => {
|
|
89
|
+
await expect(db.saveBatch(test_model_1.TEST_TABLE, items, { saveMethod: 'update' })).rejects.toThrow();
|
|
90
90
|
});
|
|
91
91
|
}
|
|
92
|
-
|
|
92
|
+
test('saveBatch test items', async () => {
|
|
93
93
|
await db.saveBatch(test_model_1.TEST_TABLE, items);
|
|
94
94
|
});
|
|
95
|
-
|
|
96
|
-
await
|
|
95
|
+
test('saveBatch should throw on null id', async () => {
|
|
96
|
+
await expect(db.saveBatch(test_model_1.TEST_TABLE, [{ ...item1, id: null }])).rejects.toThrow();
|
|
97
97
|
});
|
|
98
98
|
if (support.insertSaveMethod) {
|
|
99
|
-
|
|
100
|
-
await
|
|
99
|
+
test('saveBatch INSERT method should throw', async () => {
|
|
100
|
+
await expect(db.saveBatch(test_model_1.TEST_TABLE, items, { saveMethod: 'insert' })).rejects.toThrow();
|
|
101
101
|
});
|
|
102
102
|
}
|
|
103
103
|
if (support.updateSaveMethod) {
|
|
104
|
-
|
|
104
|
+
test('saveBatch UPDATE method should pass', async () => {
|
|
105
105
|
await db.saveBatch(test_model_1.TEST_TABLE, items, { saveMethod: 'update' });
|
|
106
106
|
});
|
|
107
107
|
}
|
|
108
108
|
// GET not empty
|
|
109
|
-
|
|
109
|
+
test('getByIds all items', async () => {
|
|
110
110
|
const rows = await db.getByIds(test_model_1.TEST_TABLE, items.map(i => i.id).concat('abcd'));
|
|
111
111
|
expectMatch(items, (0, js_lib_1._sortBy)(rows, r => r.id), quirks);
|
|
112
112
|
});
|
|
113
113
|
// QUERY
|
|
114
114
|
if (support.queries) {
|
|
115
|
-
|
|
115
|
+
test('runQuery(all) should return all items', async () => {
|
|
116
116
|
let { rows } = await db.runQuery(queryAll());
|
|
117
117
|
rows = (0, js_lib_1._sortBy)(rows, r => r.id); // because query doesn't specify order here
|
|
118
118
|
expectMatch(items, rows, quirks);
|
|
119
119
|
});
|
|
120
120
|
if (support.dbQueryFilter) {
|
|
121
|
-
|
|
121
|
+
test('query even=true', async () => {
|
|
122
122
|
const q = new dbQuery_1.DBQuery(test_model_1.TEST_TABLE).filter('even', '==', true);
|
|
123
123
|
let { rows } = await db.runQuery(q);
|
|
124
124
|
if (!support.dbQueryOrder)
|
|
@@ -127,67 +127,67 @@ function runCommonDBTest(db, quirks = {}) {
|
|
|
127
127
|
});
|
|
128
128
|
}
|
|
129
129
|
if (support.dbQueryOrder) {
|
|
130
|
-
|
|
130
|
+
test('query order by k1 desc', async () => {
|
|
131
131
|
const q = new dbQuery_1.DBQuery(test_model_1.TEST_TABLE).order('k1', true);
|
|
132
132
|
const { rows } = await db.runQuery(q);
|
|
133
133
|
expectMatch([...items].reverse(), rows, quirks);
|
|
134
134
|
});
|
|
135
135
|
}
|
|
136
136
|
if (support.dbQuerySelectFields) {
|
|
137
|
-
|
|
137
|
+
test('projection query with only ids', async () => {
|
|
138
138
|
const q = new dbQuery_1.DBQuery(test_model_1.TEST_TABLE).select(['id']);
|
|
139
139
|
let { rows } = await db.runQuery(q);
|
|
140
140
|
rows = (0, js_lib_1._sortBy)(rows, r => r.id); // cause order is not specified
|
|
141
141
|
expectMatch(items.map(item => (0, js_lib_1._pick)(item, ['id'])), rows, quirks);
|
|
142
142
|
});
|
|
143
|
-
|
|
143
|
+
test('projection query without ids', async () => {
|
|
144
144
|
const q = new dbQuery_1.DBQuery(test_model_1.TEST_TABLE).select(['k1']);
|
|
145
145
|
let { rows } = await db.runQuery(q);
|
|
146
146
|
rows = (0, js_lib_1._sortBy)(rows, r => r.k1); // cause order is not specified
|
|
147
147
|
expectMatch(items.map(item => (0, js_lib_1._pick)(item, ['k1'])), rows, quirks);
|
|
148
148
|
});
|
|
149
|
-
|
|
149
|
+
test('projection query empty fields (edge case)', async () => {
|
|
150
150
|
const q = new dbQuery_1.DBQuery(test_model_1.TEST_TABLE).select([]);
|
|
151
151
|
const { rows } = await db.runQuery(q);
|
|
152
152
|
expectMatch(items.map(() => ({})), rows, quirks);
|
|
153
153
|
});
|
|
154
154
|
}
|
|
155
|
-
|
|
156
|
-
|
|
155
|
+
test('runQueryCount should return 3', async () => {
|
|
156
|
+
expect(await db.runQueryCount(new dbQuery_1.DBQuery(test_model_1.TEST_TABLE))).toBe(3);
|
|
157
157
|
});
|
|
158
158
|
}
|
|
159
159
|
// STREAM
|
|
160
160
|
if (support.streaming) {
|
|
161
|
-
|
|
161
|
+
test('streamQuery all', async () => {
|
|
162
162
|
let rows = await db.streamQuery(queryAll()).toArray();
|
|
163
163
|
rows = (0, js_lib_1._sortBy)(rows, r => r.id); // cause order is not specified in DBQuery
|
|
164
164
|
expectMatch(items, rows, quirks);
|
|
165
165
|
});
|
|
166
166
|
}
|
|
167
167
|
// getTables
|
|
168
|
-
|
|
168
|
+
test('getTables, getTableSchema (if supported)', async () => {
|
|
169
169
|
const tables = await db.getTables();
|
|
170
170
|
// console.log({ tables })
|
|
171
171
|
if (support.tableSchemas) {
|
|
172
172
|
await (0, js_lib_1.pMap)(tables, async (table) => {
|
|
173
173
|
const schema = await db.getTableSchema(table);
|
|
174
174
|
// console.log(schema)
|
|
175
|
-
|
|
175
|
+
expect(schema.$id).toBe(`${table}.schema.json`);
|
|
176
176
|
});
|
|
177
177
|
}
|
|
178
178
|
});
|
|
179
179
|
// DELETE BY
|
|
180
180
|
if (support.queries && support.dbQueryFilter) {
|
|
181
|
-
|
|
181
|
+
test('deleteByQuery even=false', async () => {
|
|
182
182
|
const q = new dbQuery_1.DBQuery(test_model_1.TEST_TABLE).filter('even', '==', false);
|
|
183
183
|
const deleted = await db.deleteByQuery(q);
|
|
184
|
-
|
|
185
|
-
|
|
184
|
+
expect(deleted).toBe(items.filter(item => !item.even).length);
|
|
185
|
+
expect(await db.runQueryCount(queryAll())).toBe(1);
|
|
186
186
|
});
|
|
187
187
|
}
|
|
188
188
|
// BUFFER
|
|
189
189
|
if (support.bufferValues) {
|
|
190
|
-
|
|
190
|
+
test('buffer values', async () => {
|
|
191
191
|
const s = 'helloWorld 1';
|
|
192
192
|
const b1 = Buffer.from(s);
|
|
193
193
|
const item = {
|
|
@@ -205,12 +205,12 @@ function runCommonDBTest(db, quirks = {}) {
|
|
|
205
205
|
// b1,
|
|
206
206
|
// b1Loaded,
|
|
207
207
|
// })
|
|
208
|
-
|
|
209
|
-
|
|
208
|
+
expect(b1Loaded).toEqual(b1);
|
|
209
|
+
expect(b1Loaded.toString()).toBe(s);
|
|
210
210
|
});
|
|
211
211
|
}
|
|
212
212
|
if (support.transactions) {
|
|
213
|
-
|
|
213
|
+
test('transaction happy path', async () => {
|
|
214
214
|
// cleanup
|
|
215
215
|
await db.deleteByQuery(queryAll());
|
|
216
216
|
// saveBatch [item1, 2, 3]
|
|
@@ -226,7 +226,7 @@ function runCommonDBTest(db, quirks = {}) {
|
|
|
226
226
|
const expected = [items[0], { ...items[2], k1: 'k1_mod' }];
|
|
227
227
|
expectMatch(expected, rows, quirks);
|
|
228
228
|
});
|
|
229
|
-
|
|
229
|
+
test('transaction rollback', async () => {
|
|
230
230
|
let err;
|
|
231
231
|
try {
|
|
232
232
|
await db.runInTransaction(async (tx) => {
|
|
@@ -238,14 +238,14 @@ function runCommonDBTest(db, quirks = {}) {
|
|
|
238
238
|
catch (err_) {
|
|
239
239
|
err = err_;
|
|
240
240
|
}
|
|
241
|
-
|
|
241
|
+
expect(err).toBeDefined();
|
|
242
242
|
const { rows } = await db.runQuery(queryAll());
|
|
243
243
|
const expected = [items[0], { ...items[2], k1: 'k1_mod' }];
|
|
244
244
|
expectMatch(expected, rows, quirks);
|
|
245
245
|
});
|
|
246
246
|
}
|
|
247
247
|
if (support.patchByQuery) {
|
|
248
|
-
|
|
248
|
+
test('patchByQuery', async () => {
|
|
249
249
|
// cleanup, reset initial data
|
|
250
250
|
await db.deleteByQuery(queryAll());
|
|
251
251
|
await db.saveBatch(test_model_1.TEST_TABLE, items);
|
|
@@ -265,7 +265,7 @@ function runCommonDBTest(db, quirks = {}) {
|
|
|
265
265
|
});
|
|
266
266
|
}
|
|
267
267
|
if (support.increment) {
|
|
268
|
-
|
|
268
|
+
test('incrementBatch', async () => {
|
|
269
269
|
// cleanup, reset initial data
|
|
270
270
|
await db.deleteByQuery(queryAll());
|
|
271
271
|
await db.saveBatch(test_model_1.TEST_TABLE, items);
|
|
@@ -284,24 +284,24 @@ function runCommonDBTest(db, quirks = {}) {
|
|
|
284
284
|
});
|
|
285
285
|
}
|
|
286
286
|
if (support.queries) {
|
|
287
|
-
|
|
287
|
+
test('cleanup', async () => {
|
|
288
288
|
// CLEAN UP
|
|
289
289
|
await db.deleteByQuery(queryAll());
|
|
290
290
|
});
|
|
291
291
|
}
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
292
|
+
function expectMatch(expected, actual, quirks) {
|
|
293
|
+
// const expectedSorted = sortObjectDeep(expected)
|
|
294
|
+
// const actualSorted = sortObjectDeep(actual)
|
|
295
|
+
if (quirks.allowBooleansAsUndefined) {
|
|
296
|
+
expected = expected.map(r => {
|
|
297
|
+
return typeof r !== 'object' ? r : (0, js_lib_1._filterObject)(r, (_k, v) => v !== false);
|
|
298
|
+
});
|
|
299
|
+
}
|
|
300
|
+
if (quirks.allowExtraPropertiesInResponse) {
|
|
301
|
+
expect(actual).toMatchObject(expected);
|
|
302
|
+
}
|
|
303
|
+
else {
|
|
304
|
+
expect(actual).toEqual(expected);
|
|
305
|
+
}
|
|
306
306
|
}
|
|
307
307
|
}
|
package/dist/testing/index.d.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { runCommonDaoTest } from './daoTest';
|
|
2
|
-
import { CommonDBImplementationQuirks
|
|
2
|
+
import type { CommonDBImplementationQuirks } from './dbTest';
|
|
3
|
+
import { runCommonDBTest } from './dbTest';
|
|
3
4
|
import { runCommonKeyValueDaoTest } from './keyValueDaoTest';
|
|
4
5
|
import { runCommonKeyValueDBTest } from './keyValueDBTest';
|
|
5
|
-
import {
|
|
6
|
+
import type { TestItemBM, TestItemDBM, TestItemTM } from './test.model';
|
|
7
|
+
import { createTestItemBM, createTestItemDBM, createTestItemsBM, createTestItemsDBM, TEST_TABLE, testItemBMJsonSchema, testItemBMSchema, testItemTMSchema } from './test.model';
|
|
6
8
|
export type { CommonDBImplementationQuirks, TestItemBM, TestItemDBM, TestItemTM };
|
|
7
9
|
export { createTestItemBM, createTestItemDBM, createTestItemsBM, createTestItemsDBM, runCommonDaoTest, runCommonDBTest, runCommonKeyValueDaoTest, runCommonKeyValueDBTest, TEST_TABLE, testItemBMJsonSchema, testItemBMSchema, testItemTMSchema, };
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { CommonKeyValueDB } from '../kv/commonKeyValueDB';
|
|
2
|
-
export declare function runCommonKeyValueDBTest(db: CommonKeyValueDB): void
|
|
1
|
+
import type { CommonKeyValueDB } from '../kv/commonKeyValueDB';
|
|
2
|
+
export declare function runCommonKeyValueDBTest(db: CommonKeyValueDB): Promise<void>;
|
|
@@ -2,15 +2,15 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.runCommonKeyValueDBTest = runCommonKeyValueDBTest;
|
|
4
4
|
const js_lib_1 = require("@naturalcycles/js-lib");
|
|
5
|
-
const vitest_1 = require("vitest");
|
|
6
5
|
const test_model_1 = require("./test.model");
|
|
7
6
|
const testIds = (0, js_lib_1._range)(1, 4).map(n => `id${n}`);
|
|
8
7
|
const testEntries = testIds.map(id => [
|
|
9
8
|
id,
|
|
10
9
|
Buffer.from(`${id}value`),
|
|
11
10
|
]);
|
|
12
|
-
function runCommonKeyValueDBTest(db) {
|
|
13
|
-
|
|
11
|
+
async function runCommonKeyValueDBTest(db) {
|
|
12
|
+
const { afterAll, beforeAll, expect, test } = await import('vitest');
|
|
13
|
+
beforeAll(async () => {
|
|
14
14
|
// Tests in this suite are not isolated,
|
|
15
15
|
// and failing tests can leave the DB in an unexpected state for other tests,
|
|
16
16
|
// including the following test run.
|
|
@@ -18,98 +18,98 @@ function runCommonKeyValueDBTest(db) {
|
|
|
18
18
|
const ids = await db.streamIds(test_model_1.TEST_TABLE).toArray();
|
|
19
19
|
await db.deleteByIds(test_model_1.TEST_TABLE, ids);
|
|
20
20
|
});
|
|
21
|
-
|
|
21
|
+
afterAll(async () => {
|
|
22
22
|
const ids = await db.streamIds(test_model_1.TEST_TABLE).toArray();
|
|
23
23
|
await db.deleteByIds(test_model_1.TEST_TABLE, ids);
|
|
24
24
|
});
|
|
25
25
|
const { support } = db;
|
|
26
|
-
|
|
26
|
+
test('ping', async () => {
|
|
27
27
|
await db.ping();
|
|
28
28
|
});
|
|
29
|
-
|
|
29
|
+
test('createTable', async () => {
|
|
30
30
|
await db.createTable(test_model_1.TEST_TABLE, { dropIfExists: true });
|
|
31
31
|
});
|
|
32
|
-
|
|
32
|
+
test('deleteByIds non existing', async () => {
|
|
33
33
|
await db.deleteByIds(test_model_1.TEST_TABLE, testIds);
|
|
34
34
|
});
|
|
35
|
-
|
|
35
|
+
test('getByIds should return empty', async () => {
|
|
36
36
|
const results = await db.getByIds(test_model_1.TEST_TABLE, testIds);
|
|
37
|
-
|
|
37
|
+
expect(results).toEqual([]);
|
|
38
38
|
});
|
|
39
|
-
|
|
40
|
-
|
|
39
|
+
test('count should be 0', async () => {
|
|
40
|
+
expect(await db.count(test_model_1.TEST_TABLE)).toBe(0);
|
|
41
41
|
});
|
|
42
|
-
|
|
42
|
+
test('saveBatch, then getByIds', async () => {
|
|
43
43
|
await db.saveBatch(test_model_1.TEST_TABLE, testEntries);
|
|
44
44
|
const entries = await db.getByIds(test_model_1.TEST_TABLE, testIds);
|
|
45
45
|
(0, js_lib_1._sortBy)(entries, e => e[0], true);
|
|
46
|
-
|
|
46
|
+
expect(entries).toEqual(testEntries);
|
|
47
47
|
});
|
|
48
48
|
if (support.count) {
|
|
49
|
-
|
|
50
|
-
|
|
49
|
+
test('count should be 3', async () => {
|
|
50
|
+
expect(await db.count(test_model_1.TEST_TABLE)).toBe(3);
|
|
51
51
|
});
|
|
52
52
|
}
|
|
53
|
-
|
|
53
|
+
test('streamIds', async () => {
|
|
54
54
|
const ids = await db.streamIds(test_model_1.TEST_TABLE).toArray();
|
|
55
55
|
ids.sort();
|
|
56
|
-
|
|
56
|
+
expect(ids).toEqual(testIds);
|
|
57
57
|
});
|
|
58
|
-
|
|
58
|
+
test('streamIds limited', async () => {
|
|
59
59
|
const idsLimited = await db.streamIds(test_model_1.TEST_TABLE, 2).toArray();
|
|
60
60
|
// Order is non-deterministic, so, cannot compare values
|
|
61
61
|
// idsLimited.sort()
|
|
62
62
|
// expect(idsLimited).toEqual(testIds.slice(0, 2))
|
|
63
|
-
|
|
63
|
+
expect(idsLimited.length).toBe(2);
|
|
64
64
|
});
|
|
65
|
-
|
|
65
|
+
test('streamValues', async () => {
|
|
66
66
|
const values = await db.streamValues(test_model_1.TEST_TABLE).toArray();
|
|
67
67
|
values.sort();
|
|
68
|
-
|
|
68
|
+
expect(values).toEqual(testEntries.map(e => e[1]));
|
|
69
69
|
});
|
|
70
|
-
|
|
70
|
+
test('streamValues limited', async () => {
|
|
71
71
|
const valuesLimited = await db.streamValues(test_model_1.TEST_TABLE, 2).toArray();
|
|
72
72
|
// valuesLimited.sort()
|
|
73
73
|
// expect(valuesLimited).toEqual(testEntries.map(e => e[1]).slice(0, 2))
|
|
74
|
-
|
|
74
|
+
expect(valuesLimited.length).toBe(2);
|
|
75
75
|
});
|
|
76
|
-
|
|
76
|
+
test('streamEntries', async () => {
|
|
77
77
|
const entries = await db.streamEntries(test_model_1.TEST_TABLE).toArray();
|
|
78
78
|
entries.sort();
|
|
79
|
-
|
|
79
|
+
expect(entries).toEqual(testEntries);
|
|
80
80
|
});
|
|
81
|
-
|
|
81
|
+
test('streamEntries limited', async () => {
|
|
82
82
|
const entriesLimited = await db.streamEntries(test_model_1.TEST_TABLE, 2).toArray();
|
|
83
83
|
// entriesLimited.sort()
|
|
84
84
|
// expect(entriesLimited).toEqual(testEntries.slice(0, 2))
|
|
85
|
-
|
|
85
|
+
expect(entriesLimited.length).toBe(2);
|
|
86
86
|
});
|
|
87
|
-
|
|
87
|
+
test('deleteByIds should clear', async () => {
|
|
88
88
|
await db.deleteByIds(test_model_1.TEST_TABLE, testIds);
|
|
89
89
|
const results = await db.getByIds(test_model_1.TEST_TABLE, testIds);
|
|
90
|
-
|
|
90
|
+
expect(results).toEqual([]);
|
|
91
91
|
});
|
|
92
92
|
if (support.increment) {
|
|
93
93
|
const id = 'nonExistingField';
|
|
94
94
|
const id2 = 'nonExistingField2';
|
|
95
|
-
|
|
95
|
+
test('increment on a non-existing field should set the value to 1', async () => {
|
|
96
96
|
const result = await db.incrementBatch(test_model_1.TEST_TABLE, [[id, 1]]);
|
|
97
|
-
|
|
97
|
+
expect(result).toEqual([[id, 1]]);
|
|
98
98
|
});
|
|
99
|
-
|
|
99
|
+
test('increment on a existing field should increase the value by one', async () => {
|
|
100
100
|
const result = await db.incrementBatch(test_model_1.TEST_TABLE, [[id, 1]]);
|
|
101
|
-
|
|
101
|
+
expect(result).toEqual([[id, 2]]);
|
|
102
102
|
});
|
|
103
|
-
|
|
103
|
+
test('increment should increase the value by the specified amount', async () => {
|
|
104
104
|
const result = await db.incrementBatch(test_model_1.TEST_TABLE, [[id, 2]]);
|
|
105
|
-
|
|
105
|
+
expect(result).toEqual([[id, 4]]);
|
|
106
106
|
});
|
|
107
|
-
|
|
107
|
+
test('increment 2 ids at the same time', async () => {
|
|
108
108
|
const result = await db.incrementBatch(test_model_1.TEST_TABLE, [
|
|
109
109
|
[id, 1],
|
|
110
110
|
[id2, 2],
|
|
111
111
|
]);
|
|
112
|
-
|
|
112
|
+
expect(Object.fromEntries(result)).toEqual({
|
|
113
113
|
[id]: 5,
|
|
114
114
|
[id2]: 2,
|
|
115
115
|
});
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { CommonKeyValueDB } from '../kv/commonKeyValueDB';
|
|
2
|
-
export declare function runCommonKeyValueDaoTest(db: CommonKeyValueDB): void
|
|
1
|
+
import type { CommonKeyValueDB } from '../kv/commonKeyValueDB';
|
|
2
|
+
export declare function runCommonKeyValueDaoTest(db: CommonKeyValueDB): Promise<void>;
|