upend/sdks/js/tests/query.test.ts

80 lines
2.4 KiB
TypeScript

import { Any, Query, Variable } from "../src/query";
describe("query matches", () => {
test("query matches simple", () => {
const query = Query.matches("entity", "attribute", "value");
expect(query.toString()).toBe('(matches entity "attribute" "value")');
});
test("query matches anything", () => {
const query = Query.matches(Any, Any, Any);
expect(query.toString()).toBe("(matches ? ? ?)");
});
test("query matches array", () => {
const query = Query.matches("entity", "attribute", ["value1", "value2"]);
expect(query.toString()).toBe(
'(matches entity "attribute" (in "value1" "value2"))',
);
});
test("query matches addresses", () => {
const query = Query.matches("entity", "attribute", [
"@address1",
"@address2",
]);
expect(query.toString()).toBe(
'(matches entity "attribute" (in @address1 @address2))',
);
});
test("query matches numbers", () => {
const query = Query.matches("entity", "attribute", [1, 2]);
expect(query.toString()).toBe('(matches entity "attribute" (in 1 2))');
});
test("query matches variables", () => {
const query = Query.matches("entity", "attribute", Variable("a"));
expect(query.toString()).toBe('(matches entity "attribute" ?a)');
});
});
describe("compound queries", () => {
test("OR queries", () => {
const query = Query.or(
Query.matches("entity", "attribute1", "value2"),
Query.matches("entity", "attribute2", "value2"),
);
expect(query.toString()).toBe(
'(or (matches entity "attribute1" "value2") (matches entity "attribute2" "value2"))',
);
});
test("AND queries", () => {
const query = Query.and(
Query.matches("entity", "attribute1", "value2"),
Query.matches("entity", "attribute2", "value2"),
);
expect(query.toString()).toBe(
'(and (matches entity "attribute1" "value2") (matches entity "attribute2" "value2"))',
);
});
test("NOT query", () => {
const query = Query.not(Query.matches("entity", "attribute1", "value2"));
expect(query.toString()).toBe(
'(not (matches entity "attribute1" "value2"))',
);
});
test("JOIN queries", () => {
const query = Query.join(
Query.matches("entity", "attribute1", "value2"),
Query.matches("entity", "attribute2", "value2"),
);
expect(query.toString()).toBe(
'(join (matches entity "attribute1" "value2") (matches entity "attribute2" "value2"))',
);
});
});