core: fix query-transformer tests

This commit is contained in:
Abdullah Atta
2025-05-06 13:11:01 +05:00
parent 30e4f55218
commit 173caf41d0
3 changed files with 62 additions and 73 deletions

View File

@@ -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;

View File

@@ -17,75 +17,60 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// 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<hello", `"something<hello"`],
["<", ``],
[">", ``]
];
// 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<hello", `"something<hello"`],
// ["<", `"<"`],
// [">", `">"`]
// ];
// 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);
});
}

View File

@@ -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