From 30c66748af62f4ef3d3cb79f7946658163b06cdc Mon Sep 17 00:00:00 2001 From: adrinr Date: Thu, 16 Mar 2023 13:09:11 +0100 Subject: [PATCH] Add skip tests --- .../backend-core/src/db/tests/lucene.spec.ts | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/packages/backend-core/src/db/tests/lucene.spec.ts b/packages/backend-core/src/db/tests/lucene.spec.ts index 52017cc94c..3ac9b5febe 100644 --- a/packages/backend-core/src/db/tests/lucene.spec.ts +++ b/packages/backend-core/src/db/tests/lucene.spec.ts @@ -136,6 +136,67 @@ describe("lucene", () => { const resp = await builder.run() expect(resp.rows.length).toBe(2) }) + + describe("skip", () => { + const skipDbName = `db-${newid()}` + let docs: { + _id: string + property: string + array: string[] + }[] + + beforeAll(async () => { + const db = getDB(skipDbName) + + docs = Array(1500) + .fill(0) + .map((_, i) => ({ + _id: i.toString().padStart(4, "0"), + property: `value${i}`, + array: [], + })) + await db.bulkDocs(docs) + + await db.put({ + _id: "_design/database", + indexes: { + [INDEX_NAME]: { + index: index, + analyzer: "standard", + }, + }, + }) + }) + + it("should be able to apply skip", async () => { + const builder = new QueryBuilder(skipDbName, INDEX_NAME) + const firstResponse = await builder.run() + builder.setSkip(40) + const secondResponse = await builder.run() + + // Return the default limit + expect(firstResponse.rows.length).toBe(50) + expect(secondResponse.rows.length).toBe(50) + + // Should have the expected overlap + expect(firstResponse.rows.slice(40)).toEqual( + secondResponse.rows.slice(0, 10) + ) + }) + + it("should handle limits", async () => { + const builder = new QueryBuilder(skipDbName, INDEX_NAME) + builder.setLimit(10) + builder.setSkip(50) + builder.setSort("_id") + + const resp = await builder.run() + expect(resp.rows.length).toBe(10) + expect(resp.rows).toEqual( + docs.slice(50, 60).map(expect.objectContaining) + ) + }) + }) }) describe("paginated search", () => {