Skip to content

Commit

Permalink
FIX(dexie) all tests green
Browse files Browse the repository at this point in the history
  • Loading branch information
pubkey committed Jan 27, 2022
1 parent 04e2465 commit 2d38a8d
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 44 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 6 additions & 2 deletions src/plugins/dexie/rx-storage-dexie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
};
}

Expand Down
16 changes: 8 additions & 8 deletions src/plugins/dexie/rx-storage-instance-dexie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,10 @@ export class RxStorageInstanceDexie<RxDocType> 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<RxDocType>[]): Promise<RxStorageBulkWriteResponse<RxDocType>> {
console.log('bulkWrite()');
const ret: RxStorageBulkWriteResponse<RxDocType> = {
success: {},
error: {}
Expand All @@ -96,8 +93,6 @@ export class RxStorageInstanceDexie<RxDocType> 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[] = [];
Expand Down Expand Up @@ -383,12 +378,17 @@ export class RxStorageInstanceDexie<RxDocType> 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
};
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/local-documents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ const RxLocalDocumentPrototype: any = {
if (!docResult) {
throw getFromObjectOrThrow(res.error, newData._id);
}
newData = flatClone(newData);
newData._rev = docResult._rev;
});
},
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion test/unit/data-migration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
12 changes: 11 additions & 1 deletion test/unit/primary.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand Down
2 changes: 1 addition & 1 deletion test/unit/rx-collection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,7 @@ config.parallel('rx-collection.test.js', () => {
const query = c.find({
selector: {
age: {
$gt: null
$gt: 0
}
}
}).sort({ age: 'desc' });
Expand Down
35 changes: 18 additions & 17 deletions test/unit/rx-query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ config.parallel('rx-query.test.js', () => {
const q = col.find();

const docData = {
passportId: 'foobar',
color: 'green',
hp: 100,
maxHP: 767,
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -1071,7 +1073,7 @@ config.parallel('rx-query.test.js', () => {
},
{
created_at: {
$gt: null
$gt: 0
}
}, {
user_id: {
Expand All @@ -1080,7 +1082,7 @@ config.parallel('rx-query.test.js', () => {
},
{
created_at: {
$gt: null
$gt: 0
}
}
]
Expand All @@ -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);
Expand Down
11 changes: 1 addition & 10 deletions test/unit/rx-storage-dexie.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 0 additions & 3 deletions test/unit/rx-storage-implementations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<RxStorageChangeEvent<RxDocumentData<TestDocType>>>[] = [];
Expand Down
3 changes: 3 additions & 0 deletions test/unit/rx-storage-lokijs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 2d38a8d

Please sign in to comment.