From 2d38a8d6beaeb79de4bc8388662d2b69bceab418 Mon Sep 17 00:00:00 2001 From: pubkey <8926560+pubkey@users.noreply.github.com> Date: Thu, 27 Jan 2022 02:06:14 +0100 Subject: [PATCH] FIX(dexie) all tests green --- package.json | 3 +- src/plugins/dexie/rx-storage-dexie.ts | 8 +++-- .../dexie/rx-storage-instance-dexie.ts | 16 ++++----- src/plugins/local-documents.ts | 2 ++ test/unit/data-migration.test.ts | 4 ++- test/unit/primary.test.ts | 12 ++++++- test/unit/rx-collection.test.ts | 2 +- test/unit/rx-query.test.ts | 35 ++++++++++--------- test/unit/rx-storage-dexie.test.ts | 11 +----- test/unit/rx-storage-implementations.test.ts | 3 -- test/unit/rx-storage-lokijs.test.ts | 3 ++ 11 files changed, 55 insertions(+), 44 deletions(-) diff --git a/package.json b/package.json index 25e2637a469..8276a1dc219 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,8 @@ "// test:fast": "run tests in the fast-mode. Most of them will run in parrallel, skips tests that are known slow", "test:fast": "npm run test:fast:pouchdb && npm run test:fast:lokijs && npm run test:fast:lokijs-worker", "test:fast:pouchdb": "npm run pretest && rimraf -rf pouch__all_dbs__ && cross-env DEFAULT_STORAGE=pouchdb NODE_ENV=fast mocha --config ./config/.mocharc.js ./test_tmp/unit.test.js", - "test:fast:lokijs": "npm run pretest && rimraf -rf pouch__all_dbs__ && cross-env DEFAULT_STORAGE=lokijs NODE_ENV=fast mocha --config ./config/.mocharc.js ./test_tmp/unit.test.js", + "test:fast:lokijs": "npm run pretest && cross-env DEFAULT_STORAGE=lokijs NODE_ENV=fast mocha --config ./config/.mocharc.js ./test_tmp/unit.test.js", + "test:fast:dexie": "npm run pretest && cross-env DEFAULT_STORAGE=dexie NODE_ENV=fast mocha --config ./config/.mocharc.js ./test_tmp/unit.test.js", "test:fast:lokijs-worker": "npm run pretest && rimraf -rf pouch__all_dbs__ && cross-env DEFAULT_STORAGE=lokijs-worker NODE_ENV=fast mocha --config ./config/.mocharc.js ./test_tmp/unit.test.js", "// test:fast:loop": "runs tests in the fast-mode in a loop. Use this to debug tests that only fail sometimes", "test:fast:loop": "npm run test:fast && npm run test:fast:loop", diff --git a/src/plugins/dexie/rx-storage-dexie.ts b/src/plugins/dexie/rx-storage-dexie.ts index 26350a1ce68..3992bbdbcd2 100644 --- a/src/plugins/dexie/rx-storage-dexie.ts +++ b/src/plugins/dexie/rx-storage-dexie.ts @@ -41,14 +41,18 @@ export const RxStorageDexieStatics: RxStorageStatics = { mutateableQuery.selector = { $and: [ { - _deleted: false + _deleted: { + $ne: true + } }, mutateableQuery.selector ] }; } else { mutateableQuery.selector = { - _deleted: false + _deleted: { + $ne: true + } }; } diff --git a/src/plugins/dexie/rx-storage-instance-dexie.ts b/src/plugins/dexie/rx-storage-instance-dexie.ts index 915df462596..0d679bdc4a8 100644 --- a/src/plugins/dexie/rx-storage-instance-dexie.ts +++ b/src/plugins/dexie/rx-storage-instance-dexie.ts @@ -74,13 +74,10 @@ export class RxStorageInstanceDexie implements RxStorageInstance< */ private async addChangeDocumentsMeta(ids: string[]) { const addDocs = ids.map(id => ({ id })); - console.log('addChangeDocumentsMeta():'); - console.dir(addDocs); return this.internals.dexieChangesTable.bulkPut(addDocs); } async bulkWrite(documentWrites: BulkWriteRow[]): Promise> { - console.log('bulkWrite()'); const ret: RxStorageBulkWriteResponse = { success: {}, error: {} @@ -96,8 +93,6 @@ export class RxStorageInstanceDexie implements RxStorageInstance< this.internals.dexieTable, this.internals.dexieChangesTable, async () => { - console.log('bulkWrite() doc keys (' + this.collectionName + '):'); - console.dir(documentKeys); const docsInDb = await this.internals.dexieTable.bulkGet(documentKeys); const bulkPutData: any[] = []; const changesIds: string[] = []; @@ -383,12 +378,17 @@ export class RxStorageInstanceDexie implements RxStorageInstance< ); const sortComparator = RxStorageDexieStatics.getSortComparator(this.schema, preparedQuery); const docsInDb = await this.internals.dexieTable.filter(queryMatcher).toArray(); - const documents = docsInDb + let documents = docsInDb .map(docData => stripDexieKey(docData)) .sort(sortComparator); - console.log('query result:'); - console.dir(documents); + if (preparedQuery.skip) { + documents = documents.slice(preparedQuery.skip); + } + if (preparedQuery.limit && documents.length > preparedQuery.limit) { + documents = documents.slice(0, preparedQuery.limit); + } + return { documents }; diff --git a/src/plugins/local-documents.ts b/src/plugins/local-documents.ts index d44b6096b10..fe944a9a3c8 100644 --- a/src/plugins/local-documents.ts +++ b/src/plugins/local-documents.ts @@ -215,6 +215,7 @@ const RxLocalDocumentPrototype: any = { if (!docResult) { throw getFromObjectOrThrow(res.error, newData._id); } + newData = flatClone(newData); newData._rev = docResult._rev; }); }, @@ -314,6 +315,7 @@ function insertLocal( document: docData } ).then(res => { + docData = flatClone(docData); docData._rev = res._rev; const newDoc = RxLocalDocument.create(id, docData, this); return newDoc; diff --git a/test/unit/data-migration.test.ts b/test/unit/data-migration.test.ts index 40c3d2a86ae..0d09e17de11 100644 --- a/test/unit/data-migration.test.ts +++ b/test/unit/data-migration.test.ts @@ -50,7 +50,9 @@ config.parallel('data-migration.test.js', () => { */ if ( config.storage.name === 'lokijs' || - config.storage.name === 'lokijs-worker' + config.storage.name === 'lokijs-worker' || + // Same goes for the dexie.js 'fake-indexeddb'. + config.storage.name === 'dexie' ) { return; } diff --git a/test/unit/primary.test.ts b/test/unit/primary.test.ts index 9861a229b04..80209db87b9 100644 --- a/test/unit/primary.test.ts +++ b/test/unit/primary.test.ts @@ -273,7 +273,17 @@ config.parallel('primary.test.js', () => { docs = newDocs; }); await c.insert(schemaObjects.simpleHuman()); - await AsyncTestUtil.waitUntil(() => docs && docs.length === 1); + await AsyncTestUtil.waitUntil(() => { + if (docs) { + if (docs.length === 1) { + return true; + } + if (docs.length > 1) { + throw new Error('too many documents'); + } + } + return false; + }); sub.unsubscribe(); c.database.destroy(); }); diff --git a/test/unit/rx-collection.test.ts b/test/unit/rx-collection.test.ts index 88be8d62db3..6071a3752bf 100644 --- a/test/unit/rx-collection.test.ts +++ b/test/unit/rx-collection.test.ts @@ -663,7 +663,7 @@ config.parallel('rx-collection.test.js', () => { const query = c.find({ selector: { age: { - $gt: null + $gt: 0 } } }).sort({ age: 'desc' }); diff --git a/test/unit/rx-query.test.ts b/test/unit/rx-query.test.ts index b465aca4b9d..cd03c42f288 100644 --- a/test/unit/rx-query.test.ts +++ b/test/unit/rx-query.test.ts @@ -328,6 +328,7 @@ config.parallel('rx-query.test.js', () => { const q = col.find(); const docData = { + passportId: 'foobar', color: 'green', hp: 100, maxHP: 767, @@ -837,16 +838,18 @@ config.parallel('rx-query.test.js', () => { c.database.destroy(); }); it('#278 queryCache breaks when pointer out of bounds', async () => { - if (!config.platform.isNode()) return; // dont do this on browsers because firefox takes too long + if (!config.platform.isNode()) { + // dont do this on browsers because firefox takes too long + return; + } const c = await humansCollection.createPrimary(0); // insert 100 - await Promise.all( + await c.bulkInsert( new Array(100) .fill(0) .map(() => schemaObjects.human()) - .map(data => c.insert(data)) ); // make and exec query @@ -855,13 +858,13 @@ config.parallel('rx-query.test.js', () => { assert.strictEqual(docs.length, 100); // produces changeEvents - await Promise.all( + await c.bulkInsert( new Array(300) // higher than ChangeEventBuffer.limit .fill(0) .map(() => schemaObjects.human()) - .map(data => c.insert(data)) ); + // re-exec query const docs2 = await query.exec(); assert.strictEqual(docs2.length, 400); @@ -870,19 +873,18 @@ config.parallel('rx-query.test.js', () => { const docData = new Array(200) .fill(0) .map(() => schemaObjects.human()); - for (const doc of docData) { - await c.insert(doc); - } + await c.bulkInsert(docData); const docs3 = await query.exec(); assert.strictEqual(docs3.length, 600); - const docData2 = clone(docData); + let docData2 = clone(docData); + // because we have no bulkUpsert, we only upsert 10 docs to speed up the test. + docData2 = docData2.slice(0, 10); docData2.forEach((doc: any) => doc.lastName = doc.lastName + '1'); - - for (const doc of docData2) { - await c.upsert(doc); - } + await Promise.all( + docData2.map(doc => c.upsert(doc)) + ); const docs4 = await query.exec(); assert.strictEqual(docs4.length, 600); @@ -1071,7 +1073,7 @@ config.parallel('rx-query.test.js', () => { }, { created_at: { - $gt: null + $gt: 0 } }, { user_id: { @@ -1080,7 +1082,7 @@ config.parallel('rx-query.test.js', () => { }, { created_at: { - $gt: null + $gt: 0 } } ] @@ -1101,14 +1103,13 @@ config.parallel('rx-query.test.js', () => { .find() .where('event_id').eq(2) .where('user_id').eq('6') - .where('created_at').gt(null) + .where('created_at').gt(0) .sort({ created_at: 'desc' }) .exec(); const resultData2 = resultDocs2.map(doc => doc.toJSON()); - assert.strictEqual(resultData1.length, 1); assert.strictEqual(resultData1[0]['event_id'], 2); assert.deepStrictEqual(resultData1, resultData2); diff --git a/test/unit/rx-storage-dexie.test.ts b/test/unit/rx-storage-dexie.test.ts index e844280244d..84efe8829e2 100644 --- a/test/unit/rx-storage-dexie.test.ts +++ b/test/unit/rx-storage-dexie.test.ts @@ -3,30 +3,21 @@ import assert from 'assert'; import config from './config'; import { addRxPlugin, - ensureNotFalsy, - getPseudoSchemaForVersion, MangoQuery, - now, randomCouchString } from '../../plugins/core'; import { - getDexieSortComparator, RxStorageDexieStatics } from '../../plugins/dexie'; -import * as humansCollections from '../helper/humans-collection'; import * as schemaObjects from '../helper/schema-objects'; import { RxDBKeyCompressionPlugin } from '../../plugins/key-compression'; addRxPlugin(RxDBKeyCompressionPlugin); import { RxDBValidatePlugin } from '../../plugins/validate'; -import { waitUntil } from 'async-test-util'; addRxPlugin(RxDBValidatePlugin); -import * as path from 'path'; -import * as fs from 'fs'; -import { LeaderElector } from 'broadcast-channel'; -import { HumanDocumentType, humanMinimal } from '../helper/schemas'; +import { humanMinimal } from '../helper/schemas'; /** * RxStoragePouch specific tests diff --git a/test/unit/rx-storage-implementations.test.ts b/test/unit/rx-storage-implementations.test.ts index 4a62f0c9791..4abc22afefb 100644 --- a/test/unit/rx-storage-implementations.test.ts +++ b/test/unit/rx-storage-implementations.test.ts @@ -1995,9 +1995,6 @@ config.parallel('rx-storage-implementations.test.js (implementation: ' + config. } describe('RxStorageInstance', () => { it('should be able to write and read documents', async () => { - - console.log('#################################'); - const instances = await getMultiInstanceRxStorageInstance(); const emittedB: EventBulk>>[] = []; diff --git a/test/unit/rx-storage-lokijs.test.ts b/test/unit/rx-storage-lokijs.test.ts index 8e569900acd..b03b5bfed75 100644 --- a/test/unit/rx-storage-lokijs.test.ts +++ b/test/unit/rx-storage-lokijs.test.ts @@ -32,6 +32,9 @@ import { HumanDocumentType } from '../helper/schemas'; * RxStorageLokiJS specific tests */ config.parallel('rx-storage-lokijs.test.js', () => { + if (config.storage.name !== 'lokijs') { + return; + } describe('RxDatabase', () => { it('create/write/remove', async () => { const collection = await humansCollections.create(