From 4e3cb88b48825e400c26658e258ae9d9ab44e460 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 | 141 +++++++++++++---------------------- 1 file changed, 51 insertions(+), 90 deletions(-) diff --git a/test/unit/bug-report.test.ts b/test/unit/bug-report.test.ts index f85fdff4529..8cce8dcca57 100644 --- a/test/unit/bug-report.test.ts +++ b/test/unit/bug-report.test.ts @@ -8,44 +8,30 @@ * - '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 { 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 { - 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 }, firstName: { type: 'string' @@ -53,89 +39,64 @@ describe('bug-report.test.js', () => { lastName: { type: 'string' }, - age: { - type: 'integer', - minimum: 0, - maximum: 150 + adult: { + type: 'boolean' } - } - }; + }, + required: [ 'passportId', 'adult' ], + indexes: [ + 'firstName', + 'lastName', + 'adult', + [ 'firstName', 'lastName', 'adult' ] + ] + } as const satisfies RxJsonSchema; - /** - * Always generate a random database-name - * to ensure that different test runs do not affect each other. - */ - const name = randomCouchString(10); + const name = (Math.random() + 1).toString(36).substring(7); + const storage = wrappedKeyCompressionStorage({ storage: getRxStorageDexie() }); - // 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 + testCollection: { + 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.testCollection.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.testCollection.find(queryObject).exec() ).length === 2 ); - // clean up afterwards - sub.unsubscribe(); db.destroy(); - dbInOtherTab.destroy(); }); });