Updating lucene test case to improve coverage.

This commit is contained in:
mike12345567 2023-02-27 21:48:05 +00:00
parent 6fac00b3de
commit b47101e1fb
1 changed files with 111 additions and 90 deletions

View File

@ -1,7 +1,7 @@
import { newid } from "../../newid" import { newid } from "../../newid"
import { getDB } from "../db" import { getDB } from "../db"
import { Database } from "@budibase/types" import { Database } from "@budibase/types"
import { QueryBuilder } from "../lucene" import { QueryBuilder, paginatedSearch, fullSearch } from "../lucene"
const INDEX_NAME = "main" const INDEX_NAME = "main"
@ -39,102 +39,123 @@ describe("lucene", () => {
expect(response.ok).toBe(true) expect(response.ok).toBe(true)
}) })
describe("query builder", () => {
it("should be able to perform a basic query", async () => { it("should be able to perform a basic query", async () => {
const builder = new QueryBuilder(dbName, INDEX_NAME) const builder = new QueryBuilder(dbName, INDEX_NAME)
builder.setSort("property")
builder.setSortOrder("desc")
builder.setSortType("string")
const resp = await builder.run() const resp = await builder.run()
expect(resp.rows.length).toBe(3) expect(resp.rows.length).toBe(3)
}) })
it("should be able to perform a string search", async () => { it("should handle limits", async () => {
const builder = new QueryBuilder(dbName, INDEX_NAME, { const builder = new QueryBuilder(dbName, INDEX_NAME)
string: { builder.setLimit(1)
property: "wo", 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() const resp = await builder.run()
expect(resp.rows.length).toBe(3) expect(resp.rows.length).toBe(3)
}) })
it("should be able to perform a range search", async () => { it("should be able to perform a range search", async () => {
const builder = new QueryBuilder(dbName, INDEX_NAME, { const builder = new QueryBuilder(dbName, INDEX_NAME)
range: { builder.addRange("number", 0, 1)
number: {
low: 0,
high: 1,
},
},
})
const resp = await builder.run() const resp = await builder.run()
expect(resp.rows.length).toBe(1) expect(resp.rows.length).toBe(1)
}) })
it("should be able to perform an equal search", async () => { it("should be able to perform an equal search", async () => {
const builder = new QueryBuilder(dbName, INDEX_NAME, { const builder = new QueryBuilder(dbName, INDEX_NAME)
equal: { builder.addEqual("property", "word2")
property: "word2",
},
})
const resp = await builder.run() const resp = await builder.run()
expect(resp.rows.length).toBe(1) expect(resp.rows.length).toBe(1)
}) })
it("should be able to perform a not equal search", async () => { it("should be able to perform a not equal search", async () => {
const builder = new QueryBuilder(dbName, INDEX_NAME, { const builder = new QueryBuilder(dbName, INDEX_NAME)
notEqual: { builder.addNotEqual("property", "word2")
property: "word2",
},
})
const resp = await builder.run() const resp = await builder.run()
expect(resp.rows.length).toBe(2) expect(resp.rows.length).toBe(2)
}) })
it("should be able to perform an empty search", async () => { it("should be able to perform an empty search", async () => {
const builder = new QueryBuilder(dbName, INDEX_NAME, { const builder = new QueryBuilder(dbName, INDEX_NAME)
empty: { builder.addEmpty("number", true)
number: true,
},
})
const resp = await builder.run() const resp = await builder.run()
expect(resp.rows.length).toBe(2) expect(resp.rows.length).toBe(2)
}) })
it("should be able to perform a not empty search", async () => { it("should be able to perform a not empty search", async () => {
const builder = new QueryBuilder(dbName, INDEX_NAME, { const builder = new QueryBuilder(dbName, INDEX_NAME)
notEmpty: { builder.addNotEmpty("number", true)
number: true,
},
})
const resp = await builder.run() const resp = await builder.run()
expect(resp.rows.length).toBe(1) expect(resp.rows.length).toBe(1)
}) })
it("should be able to perform a one of search", async () => { it("should be able to perform a one of search", async () => {
const builder = new QueryBuilder(dbName, INDEX_NAME, { const builder = new QueryBuilder(dbName, INDEX_NAME)
oneOf: { builder.addOneOf("property", ["word", "word2"])
property: ["word", "word2"],
},
})
const resp = await builder.run() const resp = await builder.run()
expect(resp.rows.length).toBe(2) expect(resp.rows.length).toBe(2)
}) })
it("should be able to perform a contains search", async () => { it("should be able to perform a contains search", async () => {
const builder = new QueryBuilder(dbName, INDEX_NAME, { const builder = new QueryBuilder(dbName, INDEX_NAME)
contains: { builder.addContains("property", ["word"])
property: ["word"],
},
})
const resp = await builder.run() const resp = await builder.run()
expect(resp.rows.length).toBe(1) expect(resp.rows.length).toBe(1)
}) })
it("should be able to perform a not contains search", async () => { it("should be able to perform a not contains search", async () => {
const builder = new QueryBuilder(dbName, INDEX_NAME, { const builder = new QueryBuilder(dbName, INDEX_NAME)
notContains: { builder.addNotContains("property", ["word2"])
property: ["word2"],
},
})
const resp = await builder.run() const resp = await builder.run()
expect(resp.rows.length).toBe(2) expect(resp.rows.length).toBe(2)
}) })
}) })
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()
})
})
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)
})
})
})