Skip to content

Commit

Permalink
test that shows persistent query cache can be incorrect
Browse files Browse the repository at this point in the history
  • Loading branch information
TristanH committed Dec 2, 2023
1 parent 6db4b0c commit 99c2926
Showing 1 changed file with 39 additions and 1 deletion.
40 changes: 39 additions & 1 deletion test/unit/rx-query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
promiseWait,
randomCouchString,
ensureNotFalsy,
now,
now, uncacheRxQuery,
} from '../../';

import { firstValueFrom } from 'rxjs';
Expand Down Expand Up @@ -1833,5 +1833,43 @@ describe('rx-query.test.ts', () => {

collection.database.destroy();
});

it('gives correct limit results when items were removed', async () => {
const {collection} = await setUpPersistentQueryCacheCollection();
const human1 = schemaObjects.human('1', 30);
const human2 = schemaObjects.human('2', 40);
const human3 = schemaObjects.human('3', 50);
await collection.bulkInsert([human1, human2, human3]);

// wait 1 second so that not all docs are included in lwt
await new Promise((resolve) => {
setTimeout(resolve, 1000);
return;
});

// Cache a limited query:
const query = collection.find({ limit: 2, sort: [{age: 'asc'}], selector: { age: { $gt: 10 } } });
const cache = new Cache();
await query.enablePersistentQueryCache(cache);
const originalResults = await query.exec();
assert.deepStrictEqual(originalResults.map(h => h.passportId), ['1', '2']);

// Now, get into a state where that query is no longer in memory (eg new tab)
// (but, the query should still be cached)
uncacheRxQuery(collection._queryCache, query);
assert.strictEqual(cache.size, 2);

// while the query is not in memory, remove one of the items from the query results
await collection.find({selector: { passportId: '1'}}).update({
$set: {
age: 1,
}
});

const queryAgain = collection.find({ limit: 2, sort: [{age: 'asc'}], selector: { age: { $gt: 10 } } });
await queryAgain.enablePersistentQueryCache(cache);
const updatedResults = await queryAgain.exec();
assert.deepStrictEqual(updatedResults.map(h => h.passportId), ['2', '3']);
});
});
});

0 comments on commit 99c2926

Please sign in to comment.