From 173caf41d0dd80d7525db074fc68b2411ce1a4ab Mon Sep 17 00:00:00 2001 From: Abdullah Atta Date: Tue, 6 May 2025 13:11:01 +0500 Subject: [PATCH] core: fix query-transformer tests --- packages/core/src/api/lookup.ts | 6 +- .../utils/__tests__/query-transformer.test.ts | 125 ++++++++---------- packages/core/src/utils/query-transformer.ts | 4 +- 3 files changed, 62 insertions(+), 73 deletions(-) diff --git a/packages/core/src/api/lookup.ts b/packages/core/src/api/lookup.ts index 83b040c3d..dc002ec4a 100644 --- a/packages/core/src/api/lookup.ts +++ b/packages/core/src/api/lookup.ts @@ -106,8 +106,10 @@ export default class Lookup { }) .then((r) => r.map((r) => r.id)); - const smallTokens = tokens.filter( - (token) => token.length < 3 && token !== "OR" + const smallTokens = Array.from( + new Set( + tokens.filter((token) => token.length < 3 && token !== "OR") + ).values() ); if (smallTokens.length === 0) return resultsA; diff --git a/packages/core/src/utils/__tests__/query-transformer.test.ts b/packages/core/src/utils/__tests__/query-transformer.test.ts index 74112dc76..492170d23 100644 --- a/packages/core/src/utils/__tests__/query-transformer.test.ts +++ b/packages/core/src/utils/__tests__/query-transformer.test.ts @@ -17,75 +17,60 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . */ -// import { expect, test } from "vitest"; -// import { transformQuery } from "../query-transformer.js"; +import { expect, test } from "vitest"; +import { transformQuery } from "../query-transformer.js"; -// function lt2(str: string) { -// return str; //`(">${str}" OR "${str}" OR "${str}<")`; -// } +const TRANSFORM_QUERY_TESTS: [string, string][] = [ + ["hello world", `hello AND world`], + ["hello world OR bar", `hello AND world OR bar`], + ["hello world OR bar NOT baz", `hello AND world OR bar NOT baz`], + ["hello world OR NOT AND", `hello AND world`], + ["hello world OR NOT AND something", `hello AND world AND something`], + ["hello world -foo", `hello AND world AND "-foo"`], + ["hello world phrase-with-dash", `hello AND world AND "phrase-with-dash"`], + ["hello world phrase-with-dash*", 'hello AND world AND "phrase-with-dash*"'], + ["example + foo + bar", `example AND foo AND bar`], + ["example OR foo NOT bar", `example OR foo NOT bar`], + [ + 'example "quoted phrase" "another quoted phrase"', + `example AND "quoted phrase" AND "another quoted phrase"` + ], + ['"phrase-with-dash*"', `"phrase-with-dash*"`], + ['-foo + bar OR "quoted-phrase"', `"-foo" AND bar OR "quoted-phrase"`], + [ + 'phrase-with-dash* + "quoted-phrase"', + `"phrase-with-dash*" AND "quoted-phrase"` + ], + [ + 'example -foo + bar + "quoted-dash-phrase*" OR "another-quoted-phrase"', + `example AND "-foo" AND bar AND "quoted-dash-phrase*" OR "another-quoted-phrase"` + ], + ["", ""], + ["foo", `foo`], + ['"quoted"', '"quoted"'], + ["-foo -bar", `"-foo" AND "-bar"`], + ["foo + + bar", `foo AND bar`], + ["foo + OR", `foo`], + ['"special -phrase*"', '"special -phrase*"'], + ["foo* + bar*", `"foo*" AND "bar*"`], + ["(foo + bar) -baz", `"(foo" AND "bar)" AND "-baz"`], + ['"phrase with "quotes""', '"phrase with ""quotes"""'], + ['foo + "bar -baz" OR "qux*"', `foo AND "bar -baz" OR "qux*"`], + ["foo + bar + ", `foo AND bar`], + ["+foo bar", `"+foo" AND bar`], + ["foo*bar*", `"foo*bar*"`], + ['"escaped "quotes""', '"escaped ""quotes"""'], + ["-hello-world", `"-hello-world"`], + ["-hello-world*", '"-hello-world*"'], + ["*helo*", `"*helo*"`], + [">he", `">he"`], + ["something", ``] +]; -// const TRANSFORM_QUERY_TESTS = [ -// ["hello world", `hello AND world`], -// ["hello world OR bar", `hello AND world OR bar`], -// ["hello world OR bar NOT baz", `hello AND world OR bar NOT baz`], -// ["hello world OR NOT AND", `hello AND world`], -// ["hello world OR NOT AND something", `hello AND world AND something`], -// ["hello world -foo", `hello AND world AND "-foo"`], -// ["hello world phrase-with-dash", `hello AND world AND "phrase-with-dash"`], -// ["hello world phrase-with-dash*", 'hello AND world AND "phrase-with-dash*"'], -// [ -// "example + foo + bar", -// `example AND ${lt2("+")} AND foo AND ${lt2("+")} AND bar` -// ], -// ["example OR foo NOT bar", `example OR foo NOT bar`], -// [ -// 'example "quoted phrase" "another quoted phrase"', -// `example AND "quoted phrase" AND "another quoted phrase"` -// ], -// ['"phrase-with-dash*"', `"phrase-with-dash*"`], -// [ -// '-foo + bar OR "quoted-phrase"', -// `"-foo" AND ${lt2("+")} AND bar OR "quoted-phrase"` -// ], -// [ -// 'phrase-with-dash* + "quoted-phrase"', -// `"phrase-with-dash*" AND ${lt2("+")} AND "quoted-phrase"` -// ], -// [ -// 'example -foo + bar + "quoted-dash-phrase*" OR "another-quoted-phrase"', -// `example AND "-foo" AND ${lt2("+")} AND bar AND ${lt2( -// "+" -// )} AND "quoted-dash-phrase*" OR "another-quoted-phrase"` -// ], -// ["", ""], -// ["foo", `foo`], -// ['"quoted"', '"quoted"'], -// ["-foo -bar", `"-foo" AND "-bar"`], -// ["foo + + bar", `foo AND ${lt2("+")} AND ${lt2("+")} AND bar`], -// ["foo + OR", `foo AND ${lt2("+")}`], -// ['"special -phrase*"', '"special -phrase*"'], -// ["foo* + bar*", `"foo*" AND ${lt2("+")} AND "bar*"`], -// ["(foo + bar) -baz", `"(foo" AND ${lt2("+")} AND "bar)" AND "-baz"`], -// ['"phrase with "quotes""', '"phrase with ""quotes"""'], -// [ -// 'foo + "bar -baz" OR "qux*"', -// `foo AND ${lt2("+")} AND "bar -baz" OR "qux*"` -// ], -// ["foo + bar + ", `foo AND ${lt2("+")} AND bar AND ${lt2("+")}`], -// ["+foo bar", `"+foo" AND bar`], -// ["foo*bar*", `"foo*bar*"`], -// ['"escaped "quotes""', '"escaped ""quotes"""'], -// ["-hello-world", `"-hello-world"`], -// ["-hello-world*", '"-hello-world*"'], -// ["*helo*", `"*helo*"`], -// [">he", `">he"`], -// ["something", `">"`] -// ]; - -// for (const [input, expectedOutput] of TRANSFORM_QUERY_TESTS) { -// test(`should transform "${input}" into a valid SQL query`, () => { -// expect(transformQuery(input)).toBe(expectedOutput); -// }); -// } +for (const [input, expectedOutput] of TRANSFORM_QUERY_TESTS) { + test(`should transform "${input}" into a valid SQL query`, () => { + expect(transformQuery(input).query).toBe(expectedOutput); + }); +} diff --git a/packages/core/src/utils/query-transformer.ts b/packages/core/src/utils/query-transformer.ts index d6f929432..81fb4c34c 100644 --- a/packages/core/src/utils/query-transformer.ts +++ b/packages/core/src/utils/query-transformer.ts @@ -162,7 +162,9 @@ function generateSQL(ast: QueryNode): string { export function transformQuery(query: string) { const tokens = tokenize(query); - const largeTokens = tokens.filter((token) => token.length >= 3); + const largeTokens = tokens.filter( + (token) => token.length >= 3 || token === "OR" + ); return { query: generateSQL(transformAST(parseTokens(largeTokens))), tokens