upend/tools/upend_js/test.ts

88 lines
2.4 KiB
TypeScript

import test from "ava";
import { Any, Query, Variable } from "./query";
import { asAddress, isAddress } from "./types";
test("address check", (t) => {
const address = "@address";
t.is(isAddress(address), true);
});
test("address conversion", (t) => {
t.is(asAddress("address"), "@address");
t.is(asAddress("@address"), "@address");
});
test("query matches simple", (t) => {
const query = Query.matches("entity", "attribute", "value");
t.is(query.toString(), '(matches entity "attribute" "value")');
});
test("query matches anything", (t) => {
const query = Query.matches(Any, Any, Any);
t.is(query.toString(), "(matches ? ? ?)");
});
test("query matches array", (t) => {
const query = Query.matches("entity", "attribute", ["value1", "value2"]);
t.is(query.toString(), '(matches entity "attribute" (in "value1" "value2"))');
});
test("query matches addresses", (t) => {
const query = Query.matches("entity", "attribute", [
"@address1",
"@address2",
]);
t.is(
query.toString(),
'(matches entity "attribute" (in @address1 @address2))'
);
});
test("query matches numbers", (t) => {
const query = Query.matches("entity", "attribute", [1, 2]);
t.is(query.toString(), '(matches entity "attribute" (in 1 2))');
});
test("query matches variables", (t) => {
const query = Query.matches("entity", "attribute", Variable("a"));
t.is(query.toString(), '(matches entity "attribute" ?a)');
});
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"))'
);
});