Added unit tests for internalSearch

This commit is contained in:
Mel O'Hagan 2022-08-15 14:11:15 +01:00
parent ae9ae26bd4
commit f810d1f134
2 changed files with 26 additions and 3 deletions

View File

@ -184,9 +184,12 @@ class QueryBuilder {
}
const contains = (key, value, mode = "AND") => {
if (!Array.isArray(value) || value.length === 0) {
if (Array.isArray(value) && value.length === 0) {
return null
}
if (!Array.isArray(value)) {
return `${key}:${value}`
}
let statement = `${builder.preprocess(value[0], { escape: true })}`
for (let i = 1; i < value.length; i++) {
statement += ` ${mode} ${builder.preprocess(value[i], {
@ -197,7 +200,8 @@ class QueryBuilder {
}
const notContains = (key, value) => {
return "*:* AND NOT " + contains(key, value)
const allPrefix = allOr === "" ? "*:* AND" : ""
return allPrefix + "NOT " + contains(key, value)
}
const containsAny = (key, value) => {

View File

@ -129,9 +129,10 @@ describe("internal search", () => {
const response = await search.paginatedSearch({
contains: {
"column": "a",
"colArr": [1, 2, 3],
},
}, PARAMS)
checkLucene(response, `*:* AND column:a`, PARAMS)
checkLucene(response, `*:* AND column:a AND colArr:(1 AND 2 AND 3)`, PARAMS)
})
it("test multiple of same column", async () => {
@ -154,4 +155,22 @@ describe("internal search", () => {
}, PARAMS)
checkLucene(response, `*:* AND 1\\:column:"a"`, PARAMS)
})
it("test containsAny query", async () => {
const response = await search.paginatedSearch({
containsAny: {
"column": ["a", "b", "c"]
},
}, PARAMS)
checkLucene(response, `*:* AND column:(a OR b OR c)`, PARAMS)
})
it("test notContains query", async () => {
const response = await search.paginatedSearch({
notContains: {
"column": ["a", "b", "c"]
},
}, PARAMS)
checkLucene(response, `*:* AND NOT column:(a AND b AND c)`, PARAMS)
})
})