2021-12-17 11:33:09 +01:00
|
|
|
import test from "ava";
|
2023-11-11 11:17:43 +01:00
|
|
|
import { Any, Query, Variable } from "./query";
|
2021-12-17 11:33:09 +01:00
|
|
|
|
2023-10-09 21:53:44 +02:00
|
|
|
test("query matches simple", (t) => {
|
2023-11-11 10:59:52 +01:00
|
|
|
const query = Query.matches("entity", "attribute", "value");
|
2023-10-09 21:53:44 +02:00
|
|
|
t.is(query.toString(), '(matches entity "attribute" "value")');
|
|
|
|
});
|
2021-12-17 11:33:09 +01:00
|
|
|
|
2023-10-09 21:53:44 +02:00
|
|
|
test("query matches anything", (t) => {
|
2023-11-11 10:59:52 +01:00
|
|
|
const query = Query.matches(Any, Any, Any);
|
2023-10-09 21:53:44 +02:00
|
|
|
t.is(query.toString(), "(matches ? ? ?)");
|
|
|
|
});
|
2021-12-18 15:12:22 +01:00
|
|
|
|
2023-10-09 21:53:44 +02:00
|
|
|
test("query matches array", (t) => {
|
2023-11-11 10:59:52 +01:00
|
|
|
const query = Query.matches("entity", "attribute", ["value1", "value2"]);
|
2023-10-09 21:53:44 +02:00
|
|
|
t.is(query.toString(), '(matches entity "attribute" (in "value1" "value2"))');
|
|
|
|
});
|
2021-12-18 15:12:22 +01:00
|
|
|
|
2023-10-09 21:53:44 +02:00
|
|
|
test("query matches addresses", (t) => {
|
2023-11-11 10:59:52 +01:00
|
|
|
const query = Query.matches("entity", "attribute", [
|
2023-10-09 21:53:44 +02:00
|
|
|
"@address1",
|
|
|
|
"@address2",
|
|
|
|
]);
|
|
|
|
t.is(
|
|
|
|
query.toString(),
|
|
|
|
'(matches entity "attribute" (in @address1 @address2))'
|
|
|
|
);
|
|
|
|
});
|
2021-12-17 11:33:09 +01:00
|
|
|
|
2023-10-09 21:53:44 +02:00
|
|
|
test("query matches numbers", (t) => {
|
2023-11-11 10:59:52 +01:00
|
|
|
const query = Query.matches("entity", "attribute", [1, 2]);
|
2023-10-09 21:53:44 +02:00
|
|
|
t.is(query.toString(), '(matches entity "attribute" (in 1 2))');
|
2021-12-17 11:33:09 +01:00
|
|
|
});
|
2023-11-11 11:17:43 +01:00
|
|
|
|
|
|
|
test("query matches variables", (t) => {
|
|
|
|
const query = Query.matches("entity", "attribute", Variable("a"));
|
|
|
|
t.is(query.toString(), '(matches entity "attribute" ?a)');
|
|
|
|
});
|
2023-11-18 13:51:21 +01:00
|
|
|
|
|
|
|
test("OR queries", (t) => {
|
|
|
|
const query = Query.or(
|
|
|
|
Query.matches("entity", "attribute1", "value2"),
|
|
|
|
Query.matches("entity", "attribute2", "value2")
|
|
|
|
);
|
|
|
|
t.is(
|
|
|
|
query.toString(),
|
|
|
|
'(or (matches entity "attribute1" "value2") (matches entity "attribute2" "value2"))'
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
test("AND queries", (t) => {
|
|
|
|
const query = Query.and(
|
|
|
|
Query.matches("entity", "attribute1", "value2"),
|
|
|
|
Query.matches("entity", "attribute2", "value2")
|
|
|
|
);
|
|
|
|
t.is(
|
|
|
|
query.toString(),
|
|
|
|
'(and (matches entity "attribute1" "value2") (matches entity "attribute2" "value2"))'
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
test("NOT query", (t) => {
|
|
|
|
const query = Query.not(Query.matches("entity", "attribute1", "value2"));
|
|
|
|
t.is(query.toString(), '(not (matches entity "attribute1" "value2"))');
|
|
|
|
});
|
|
|
|
|
|
|
|
test("JOIN queries", (t) => {
|
|
|
|
const query = Query.join(
|
|
|
|
Query.matches("entity", "attribute1", "value2"),
|
|
|
|
Query.matches("entity", "attribute2", "value2")
|
|
|
|
);
|
|
|
|
t.is(
|
|
|
|
query.toString(),
|
|
|
|
'(join (matches entity "attribute1" "value2") (matches entity "attribute2" "value2"))'
|
|
|
|
);
|
|
|
|
});
|