From b47101e1fb725a3d7850f9ea1041277d61ad896a Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Mon, 27 Feb 2023 21:48:05 +0000 Subject: [PATCH] Updating lucene test case to improve coverage. --- .../backend-core/src/db/tests/lucene.spec.ts | 201 ++++++++++-------- 1 file changed, 111 insertions(+), 90 deletions(-) diff --git a/packages/backend-core/src/db/tests/lucene.spec.ts b/packages/backend-core/src/db/tests/lucene.spec.ts index de469ae9e4..23b01e18df 100644 --- a/packages/backend-core/src/db/tests/lucene.spec.ts +++ b/packages/backend-core/src/db/tests/lucene.spec.ts @@ -1,7 +1,7 @@ import { newid } from "../../newid" import { getDB } from "../db" import { Database } from "@budibase/types" -import { QueryBuilder } from "../lucene" +import { QueryBuilder, paginatedSearch, fullSearch } from "../lucene" const INDEX_NAME = "main" @@ -39,102 +39,123 @@ describe("lucene", () => { expect(response.ok).toBe(true) }) - it("should be able to perform a basic query", async () => { - const builder = new QueryBuilder(dbName, INDEX_NAME) - const resp = await builder.run() - expect(resp.rows.length).toBe(3) - }) - - it("should be able to perform a string search", async () => { - const builder = new QueryBuilder(dbName, INDEX_NAME, { - string: { - property: "wo", - }, + describe("query builder", () => { + it("should be able to perform a basic query", async () => { + const builder = new QueryBuilder(dbName, INDEX_NAME) + builder.setSort("property") + builder.setSortOrder("desc") + builder.setSortType("string") + const resp = await builder.run() + expect(resp.rows.length).toBe(3) + }) + + it("should handle limits", async () => { + const builder = new QueryBuilder(dbName, INDEX_NAME) + builder.setLimit(1) + const resp = await builder.run() + expect(resp.rows.length).toBe(1) + }) + + it("should be able to perform a string search", async () => { + const builder = new QueryBuilder(dbName, INDEX_NAME) + builder.addString("property", "wo") + const resp = await builder.run() + expect(resp.rows.length).toBe(3) + }) + + it("should be able to perform a range search", async () => { + const builder = new QueryBuilder(dbName, INDEX_NAME) + builder.addRange("number", 0, 1) + const resp = await builder.run() + expect(resp.rows.length).toBe(1) + }) + + it("should be able to perform an equal search", async () => { + const builder = new QueryBuilder(dbName, INDEX_NAME) + builder.addEqual("property", "word2") + const resp = await builder.run() + expect(resp.rows.length).toBe(1) + }) + + it("should be able to perform a not equal search", async () => { + const builder = new QueryBuilder(dbName, INDEX_NAME) + builder.addNotEqual("property", "word2") + const resp = await builder.run() + expect(resp.rows.length).toBe(2) + }) + + it("should be able to perform an empty search", async () => { + const builder = new QueryBuilder(dbName, INDEX_NAME) + builder.addEmpty("number", true) + const resp = await builder.run() + expect(resp.rows.length).toBe(2) + }) + + it("should be able to perform a not empty search", async () => { + const builder = new QueryBuilder(dbName, INDEX_NAME) + builder.addNotEmpty("number", true) + const resp = await builder.run() + expect(resp.rows.length).toBe(1) + }) + + it("should be able to perform a one of search", async () => { + const builder = new QueryBuilder(dbName, INDEX_NAME) + builder.addOneOf("property", ["word", "word2"]) + const resp = await builder.run() + expect(resp.rows.length).toBe(2) + }) + + it("should be able to perform a contains search", async () => { + const builder = new QueryBuilder(dbName, INDEX_NAME) + builder.addContains("property", ["word"]) + const resp = await builder.run() + expect(resp.rows.length).toBe(1) + }) + + it("should be able to perform a not contains search", async () => { + const builder = new QueryBuilder(dbName, INDEX_NAME) + builder.addNotContains("property", ["word2"]) + const resp = await builder.run() + expect(resp.rows.length).toBe(2) }) - const resp = await builder.run() - expect(resp.rows.length).toBe(3) }) - it("should be able to perform a range search", async () => { - const builder = new QueryBuilder(dbName, INDEX_NAME, { - range: { - number: { - low: 0, - high: 1, + describe("paginated search", () => { + it("should be able to perform a paginated search", async () => { + const page = await paginatedSearch( + dbName, + INDEX_NAME, + { + string: { + property: "wo", + }, }, - }, + { + limit: 1, + sort: "property", + sortType: "string", + sortOrder: "desc", + } + ) + expect(page.rows.length).toBe(1) + expect(page.hasNextPage).toBe(true) + expect(page.bookmark).toBeDefined() }) - const resp = await builder.run() - expect(resp.rows.length).toBe(1) }) - it("should be able to perform an equal search", async () => { - const builder = new QueryBuilder(dbName, INDEX_NAME, { - equal: { - property: "word2", - }, + describe("full search", () => { + it("should be able to perform a full search", async () => { + const page = await fullSearch( + dbName, + INDEX_NAME, + { + string: { + property: "wo", + }, + }, + {} + ) + expect(page.rows.length).toBe(3) }) - const resp = await builder.run() - expect(resp.rows.length).toBe(1) - }) - - it("should be able to perform a not equal search", async () => { - const builder = new QueryBuilder(dbName, INDEX_NAME, { - notEqual: { - property: "word2", - }, - }) - const resp = await builder.run() - expect(resp.rows.length).toBe(2) - }) - - it("should be able to perform an empty search", async () => { - const builder = new QueryBuilder(dbName, INDEX_NAME, { - empty: { - number: true, - }, - }) - const resp = await builder.run() - expect(resp.rows.length).toBe(2) - }) - - it("should be able to perform a not empty search", async () => { - const builder = new QueryBuilder(dbName, INDEX_NAME, { - notEmpty: { - number: true, - }, - }) - const resp = await builder.run() - expect(resp.rows.length).toBe(1) - }) - - it("should be able to perform a one of search", async () => { - const builder = new QueryBuilder(dbName, INDEX_NAME, { - oneOf: { - property: ["word", "word2"], - }, - }) - const resp = await builder.run() - expect(resp.rows.length).toBe(2) - }) - - it("should be able to perform a contains search", async () => { - const builder = new QueryBuilder(dbName, INDEX_NAME, { - contains: { - property: ["word"], - }, - }) - const resp = await builder.run() - expect(resp.rows.length).toBe(1) - }) - - it("should be able to perform a not contains search", async () => { - const builder = new QueryBuilder(dbName, INDEX_NAME, { - notContains: { - property: ["word2"], - }, - }) - const resp = await builder.run() - expect(resp.rows.length).toBe(2) }) })