From 3d55b60add55e48a4a890d2de591b689d8f564b1 Mon Sep 17 00:00:00 2001 From: Mel O'Hagan Date: Mon, 15 Aug 2022 14:11:15 +0100 Subject: [PATCH] Added unit tests for internalSearch --- .../src/api/controllers/row/internalSearch.js | 8 +++++-- .../api/routes/tests/internalSearch.spec.js | 21 ++++++++++++++++++- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/packages/server/src/api/controllers/row/internalSearch.js b/packages/server/src/api/controllers/row/internalSearch.js index 94b29cb31e..41686c6761 100644 --- a/packages/server/src/api/controllers/row/internalSearch.js +++ b/packages/server/src/api/controllers/row/internalSearch.js @@ -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) => { diff --git a/packages/server/src/api/routes/tests/internalSearch.spec.js b/packages/server/src/api/routes/tests/internalSearch.spec.js index 50ee2b26ac..3b478eda17 100644 --- a/packages/server/src/api/routes/tests/internalSearch.spec.js +++ b/packages/server/src/api/routes/tests/internalSearch.spec.js @@ -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) + }) }) \ No newline at end of file