From 5e4a93e2411f5360234157a786007d4897c88069 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mihai=20B=C4=83l=C4=83ceanu?= Date: Tue, 30 Jul 2024 13:17:44 +0300 Subject: [PATCH] bug example key compression enabled with boolean indexes on dexie --- test/unit/bug-report.test.ts | 151 ++++++++++++++--------------------- 1 file changed, 59 insertions(+), 92 deletions(-) diff --git a/test/unit/bug-report.test.ts b/test/unit/bug-report.test.ts index f85fdff4529..96ae88b08f9 100644 --- a/test/unit/bug-report.test.ts +++ b/test/unit/bug-report.test.ts @@ -8,134 +8,101 @@ * - 'npm run test:node' so it runs in nodejs * - 'npm run test:browser' so it runs in the browser */ + import assert from 'assert'; -import AsyncTestUtil from 'async-test-util'; -import config from './config.ts'; +import { addRxPlugin, createRxDatabase, MangoQuery, } from '../../plugins/core/index.mjs'; +import { wrappedKeyCompressionStorage } from '../../plugins/key-compression/index.mts'; +import { getRxStorageDexie } from '../../plugins/storage-dexie/index.mts'; +import { isNode } from '../../plugins/test-utils/index.mjs'; +import { RxJsonSchema } from '../../src/index.ts'; +import { RxDBDevModePlugin } from '../../plugins/dev-mode/index.mts'; -import { - createRxDatabase, - randomCouchString -} from '../../plugins/core/index.mjs'; -import { - isNode -} from '../../plugins/test-utils/index.mjs'; describe('bug-report.test.js', () => { it('should fail because it reproduces the bug', async function () { - /** - * If your test should only run in nodejs or only run in the browser, - * you should comment in the return operator and adapt the if statement. - */ - if ( - !isNode // runs only in node - // isNode // runs only in the browser - ) { - // return; - } - - if (!config.storage.hasMultiInstance) { + if ( isNode ) { return; } // create a schema - const mySchema = { + const schema = { version: 0, + keyCompression: true, primaryKey: 'passportId', type: 'object', properties: { passportId: { type: 'string', - maxLength: 100 + maxLength: 128, }, firstName: { - type: 'string' + type: 'string', + maxLength: 128, }, lastName: { - type: 'string' + type: 'string', + maxLength: 128, }, - age: { - type: 'integer', - minimum: 0, - maximum: 150 + adult: { + type: 'boolean' } - } - }; + }, + required: [ 'passportId', 'adult' ], + indexes: [ + 'firstName', + 'lastName', + 'adult', + [ 'firstName', 'lastName', 'adult' ] + ] + } as const satisfies RxJsonSchema; + + const name = `a${(Math.random() + 1).toString(36).substring(7)}`; + const storage = wrappedKeyCompressionStorage({ storage: getRxStorageDexie() }); - /** - * Always generate a random database-name - * to ensure that different test runs do not affect each other. - */ - const name = randomCouchString(10); + addRxPlugin(RxDBDevModePlugin); - // create a database const db = await createRxDatabase({ name, - /** - * By calling config.storage.getStorage(), - * we can ensure that all variations of RxStorage are tested in the CI. - */ - storage: config.storage.getStorage(), + storage, eventReduce: true, ignoreDuplicate: true }); - // create a collection const collections = await db.addCollections({ - mycollection: { - schema: mySchema + ['test-collection']: { + schema } }); // insert a document - await collections.mycollection.insert({ - passportId: 'foobar', - firstName: 'Bob', - lastName: 'Kelso', - age: 56 - }); - - /** - * to simulate the event-propagation over multiple browser-tabs, - * we create the same database again - */ - const dbInOtherTab = await createRxDatabase({ - name, - storage: config.storage.getStorage(), - eventReduce: true, - ignoreDuplicate: true - }); - // create a collection - const collectionInOtherTab = await dbInOtherTab.addCollections({ - mycollection: { - schema: mySchema + await collections['test-collection'].bulkUpsert([ + { + passportId: '1', + firstName: 'Bob', + lastName: 'Kelso', + adult: true + }, + { + passportId: '2', + firstName: 'Andrew', + lastName: 'Wright', + adult: true + }, + { + passportId: '3', + firstName: 'Tommy', + lastName: 'Little', + adult: false } - }); + ] ); - // find the document in the other tab - const myDocument = await collectionInOtherTab.mycollection - .findOne() - .where('firstName') - .eq('Bob') - .exec(); - - /* - * assert things, - * here your tests should fail to show that there is a bug - */ - assert.strictEqual(myDocument.age, 56); - - - // you can also wait for events - const emitted: any[] = []; - const sub = collectionInOtherTab.mycollection - .findOne().$ - .subscribe(doc => { - emitted.push(doc); - }); - await AsyncTestUtil.waitUntil(() => emitted.length === 1); + const queryObject: MangoQuery = { + selector:{ + adult: true + }, + index: [ 'adult' ] + }; + assert.ok( ( await collections['test-collection'].find(queryObject).exec() ).length === 2 ); - // clean up afterwards - sub.unsubscribe(); db.destroy(); - dbInOtherTab.destroy(); }); });