diff src/Layout.test.ts @ 103:f12feced00ce

WIP rewriting Layout
author drewp@bigasterisk.com
date Sat, 12 Mar 2022 00:42:00 -0800
parents src/tabulate.test.ts@ac7ad087d474
children 1aea03d306af
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/Layout.test.ts	Sat Mar 12 00:42:00 2022 -0800
@@ -0,0 +1,169 @@
+import { Layout, LayoutResult } from "./Layout";
+import { ViewConfig } from "./ViewConfig";
+import {
+  DataFactory,
+  Store,
+  Prefixes,
+  Parser,
+  Quad,
+  NamedNode,
+  Term,
+} from "n3";
+import { EX, rdf } from "./namespaces";
+import Immutable from "immutable";
+import { n3Graph } from "./fetchAndParse";
+const { namedNode } = DataFactory;
+
+const twoStatements = async (): Promise<Store> => {
+  return n3Graph(`
+  @prefix : <http://example.com/> .
+  :g1 {
+    :a :b :c .
+    :d :e :f .
+  }
+  `);
+};
+
+const typedStatements = async (): Promise<Store> => {
+  return n3Graph(`
+  @prefix : <http://example.com/> .
+  :g1 {
+    :a a :T1 ; :color :red .
+    :b a :T1 ; :color :blue .
+    :c a :T1 .
+    :d a :T2 ; :size :big .
+    :e a :T1,:T2; :size :small
+  }
+  `);
+};
+function G1(s: Term, p: Term, o: Term): Quad {
+  return new Quad(s, p, o, EX("g1"));
+}
+
+describe("Layout", () => {
+  it("accepts a ViewConfig", async () => {
+    const vc = new ViewConfig();
+    await vc.readFromGraph(`
+      @prefix ex: <http://example.com/> .
+      @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
+
+      <> a ex:View; rdfs:label "repos" .`);
+    const layout = new Layout(vc);
+    const lr = layout.plan(await twoStatements());
+  });
+  it("defaults to putting all triples in the ungrouped list", async () => {
+    const layout = new Layout();
+    const lr = layout.plan(await twoStatements());
+    expect(lr).toEqual({
+      sections: [
+        {
+          statements: [
+            G1(EX("a"), EX("b"), EX("c")),
+            G1(EX("d"), EX("e"), EX("f")),
+          ],
+        },
+      ],
+    });
+  });
+  describe("makes a table as requested by ViewConfig", () => {
+    let lr: LayoutResult;
+
+    beforeAll(async () => {
+      const vc = new ViewConfig();
+      await vc.readFromGraph(`
+        @prefix : <http://example.com/> .
+        @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
+  
+        <> a :View; :table [ :primaryType :T1 ] .`);
+      const layout = new Layout(vc);
+      const lr = layout.plan(await typedStatements());
+      expect(lr.sections).toHaveLength(2);
+    });
+    it("puts the right type in the table", async () => {
+      expect(lr.sections[0]).toEqual({
+        columnHeaders: [{ rdfType: EX("T1"), pred: EX("color") }],
+        rows: [
+          [EX("a"), EX("red")],
+          [EX("b"), EX("blue")],
+          [EX("c"), null],
+          [EX("e"), null],
+        ],
+      });
+    });
+    it("leaves the rest ungrouped", async () => {
+      expect(lr.sections[1]).toEqual({
+        statements: [
+          G1(EX("d"), rdf.type, EX("T1")),
+          G1(EX("d"), EX("size"), EX("big")),
+        ],
+      });
+    });
+  });
+  it("makes a table out of ungrouped triples with the same type", async () => {});
+});
+
+// describe("equality", () => {
+//   test("investigation of https://github.com/rdfjs/N3.js/issues/265", () => {
+//     const x = namedNode("x");
+//     const x2 = namedNode("x");
+//     // (NamedNode.prototype as any).hashCode = () => 0;
+//     // expect((x as any).hashCode()).toEqual((x2 as any).hashCode())
+//     expect(x === x2).toBeFalsy();
+//     expect(x == x2).toBeFalsy();
+//     expect(x.equals(x2)).toBeTruthy();
+//     let imap = Immutable.Map();
+//     imap = imap.set(x, 11);
+//     imap = imap.set(x, 22);
+//     imap = imap.set(x2, 33);
+//     expect(imap.has(x)).toBeTruthy();
+//     expect(imap.has(x2)).toBeTruthy();
+//     expect(imap.size).toEqual(1);
+//   });
+// });
+
+// describe("groupByRdfType", () => {
+//   test("finds multiple graphs", () => {});
+//   test("works", async () => {
+//     const store = new Store();
+
+//     const parser = new Parser();
+//     await new Promise((res, rej) => {
+//       parser.parse(
+//         `PREFIX : <urn:>
+//         :rs1 a :Foo; :pred1 "obj1" .
+//         :rs2 a :Foo; :pred1 "obj2" .
+//         :rs3 a :Bar .
+//         :rs4 :pred1 "obj4" .
+// `,
+//         (error, quad: Quad, prefixes: Prefixes) => {
+//           if (quad) {
+//             store.addQuad(quad);
+//           } else {
+//             res(undefined);
+//           }
+//         }
+//       );
+//     });
+//     const grouped = groupByRdfType(store);
+//     expect(Array.from(grouped.byType.keys())).toHaveLength(2);
+//     expect(grouped.byType.get(namedNode("urn:Foo"))).toEqual(
+//       Immutable.Set([namedNode("urn:rs1"), namedNode("urn:rs2")])
+//     );
+//     expect(grouped.byType.get(namedNode("urn:Bar"))).toEqual(
+//       Immutable.Set([namedNode("urn:rs3")])
+//     );
+//     expect(grouped.untypedSubjs).toEqual([namedNode("urn:rs4")]);
+//   });
+
+//   describe("MultiSubjsTypeBlockLayout", () => {
+//     test("gathers subjs", () => {
+
+//     });
+//     test("gathers preds", () => {
+
+//     });
+//     test("cells reports filled cells", () => {
+
+//     });
+//   });
+// });