diff web/patch.test.ts @ 2376:4556eebe5d73

topdir reorgs; let pdm have its src/ dir; separate vite area from light9/
author drewp@bigasterisk.com
date Sun, 12 May 2024 19:02:10 -0700
parents light9/web/patch.test.ts@dd9474bef2a6
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/web/patch.test.ts	Sun May 12 19:02:10 2024 -0700
@@ -0,0 +1,79 @@
+import { assert, describe, expect, it } from "vitest";
+
+import { Quad, NamedNode } from "n3";
+import { Patch, QuadPattern } from "./patch";
+import * as N3 from "n3";
+
+const node1 = new NamedNode("http://example.com/node1");
+const node2 = new NamedNode("http://example.com/node2");
+const node3 = new NamedNode("http://example.com/node3");
+
+const decimalDT = new NamedNode("http://www.w3.org/2001/XMLSchema#decimal");
+
+function QP(
+  subject: N3.Quad_Subject | null, //
+  predicate: N3.Quad_Predicate | null,
+  object: N3.Quad_Object | null,
+  graph: N3.Quad_Graph | null
+): QuadPattern {
+  return { subject, predicate, object, graph };
+}
+
+describe("Patch.matches", () => {
+  it("matches any quads against an open pattern", () => {
+    const quad1 = new Quad(node1, node2, node3);
+    const quad2 = new Quad(node1, node2, node3);
+    const quad3 = new Quad(node1, node2, node3);
+
+    const pattern = QP(null, null, null, null);
+
+    const p = new Patch([quad1, quad2], [quad3]);
+
+    assert.isTrue(p.matches(pattern));
+  });
+  it("doesn't match when the patch is empty", () => {
+    const p = new Patch([], []);
+    assert.isFalse(p.matches(QP(null, null, null, null)));
+  });
+  it("compares terms correctly", () => {
+    assert.isTrue(new Patch([new Quad(node1, node2, node3)], []).matches(QP(node1, null, null, null)));
+    assert.isFalse(new Patch([new Quad(node1, node2, node3)], []).matches(QP(node2, null, null, null)));
+  });
+  it("matches on just one set term", () => {
+    assert.isTrue(new Patch([new Quad(node1, node2, node3)], []).matches(QP(node1, null, null, null)));
+    assert.isTrue(new Patch([new Quad(node1, node2, node3)], []).matches(QP(null, node2, null, null)));
+    assert.isTrue(new Patch([new Quad(node1, node2, node3)], []).matches(QP(null, null, node3, null)));
+  });
+});
+
+describe("Patch.empty", () => {
+  it("works with no quads", () => {
+    const p = new Patch([], []);
+    assert.isTrue(p.isEmpty());
+  });
+  it("works with unmatched quads", () => {
+    const p = new Patch([], [new Quad(node1, node2, node3)]);
+    assert.isFalse(p.isEmpty());
+  });
+  it("understands floats are equal", () => {
+    const p = new Patch(
+      [new Quad(node1, node2, N3.DataFactory.literal((0.12345).toPrecision(3), decimalDT))],
+      [new Quad(node1, node2, N3.DataFactory.literal((0.1234).toPrecision(3), decimalDT))]
+    );
+    assert.isTrue(p.isEmpty());
+  });
+  it("...and when they're not", () => {
+    const p = new Patch(
+      [new Quad(node1, node2, N3.DataFactory.literal(0.123, decimalDT))], //
+      [new Quad(node1, node2, N3.DataFactory.literal(0.124, decimalDT))]
+    );
+    assert.isFalse(p.isEmpty());
+  });
+  it("understands literals are equal", () => {
+    const p = new Patch(
+      [new Quad(node1, node2, node3)], //
+      [new Quad(node1, node2, new NamedNode("http://example.com/node" + "3"))]
+    );
+    assert.isTrue(p.isEmpty());
+  });
+});