comparison src/layout/Layout.test.ts @ 106:2468f2227d22

make src/layout/ and src/render/ separation
author drewp@bigasterisk.com
date Sun, 13 Mar 2022 22:00:30 -0700
parents src/Layout.test.ts@1aea03d306af
children 5e6840229a05
comparison
equal deleted inserted replaced
105:4bb8c7775c83 106:2468f2227d22
1 import { Quad, Store, Term } from "n3";
2 import { n3Graph } from "./fetchAndParse";
3 import { AlignedTable, Layout, LayoutResult } from "./Layout";
4 import { EX, rdf } from "./namespaces";
5 import { ViewConfig } from "./ViewConfig";
6
7 const twoStatements = async (): Promise<Store> => {
8 return n3Graph(`
9 @prefix : <http://example.com/> .
10 :g1 {
11 :a0 :b0 :c0 .
12 :d0 :e0 :f0 .
13 }
14 `);
15 };
16
17 const typedStatements = async (): Promise<Store> => {
18 return n3Graph(`
19 @prefix : <http://example.com/> .
20 :g1 {
21 :a a :T1 ; :color :red .
22 :b a :T1 ; :color :blue .
23 :c a :T1 .
24 :d a :T2 ; :size :big .
25 :e a :T1,:T2; :size :small
26 }
27 `);
28 };
29 function G1(s: Term, p: Term, o: Term): Quad {
30 return new Quad(s, p, o, EX("g1"));
31 }
32
33 describe("Layout", () => {
34 it("accepts a ViewConfig", async () => {
35 const vc = new ViewConfig();
36 await vc.readFromGraph(`
37 @prefix ex: <http://example.com/> .
38 @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
39
40 <> a ex:View; rdfs:label "repos" .`);
41 const layout = new Layout(vc);
42 const lr = layout.plan(await twoStatements());
43 });
44 it("defaults to putting all triples in the ungrouped list", async () => {
45 const layout = new Layout();
46 const lr = layout.plan(await twoStatements());
47 expect(lr).toEqual({
48 sections: [
49 {
50 statements: [
51 G1(EX("a0"), EX("b0"), EX("c0")),
52 G1(EX("d0"), EX("e0"), EX("f0")),
53 ],
54 },
55 ],
56 });
57 });
58 describe("makes a table as requested by ViewConfig", () => {
59 let lr: LayoutResult;
60
61 beforeAll(async () => {
62 const vc = new ViewConfig();
63 await vc.readFromGraph(`
64 @prefix : <http://example.com/> .
65 @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
66
67 <> a :View; :table [ :primaryType :T1 ] .`);
68 const layout = new Layout(vc);
69 lr = layout.plan(await typedStatements());
70 });
71 it("returns 2 sections", ()=>{
72 expect(lr.sections).toHaveLength(2);
73 })
74 it("puts the right type in the table", async () => {
75 const sec0 = lr.sections[0] as AlignedTable;
76 expect(sec0.columnHeaders).toEqual([
77 { rdfType: EX("T1"), pred: EX("color") },
78 { rdfType: EX("T1"), pred: EX("size") }
79 ])
80 expect(sec0.rows).toEqual([
81 [EX("a"), EX("red"), null],
82 [EX("b"), EX("blue"),null],
83 [EX("c"), null, null],
84 [EX("e"), null, EX('small')],
85 ]);
86 });
87 it("leaves the rest ungrouped", async () => {
88 expect(lr.sections[1]).toEqual({
89 statements: [
90 G1(EX("d"), rdf.type, EX("T2")),
91 G1(EX("d"), EX("size"), EX("big")),
92 ],
93 });
94 });
95 });
96 it("makes a table out of ungrouped triples with the same type", async () => {});
97 });
98
99 // describe("equality", () => {
100 // test("investigation of https://github.com/rdfjs/N3.js/issues/265", () => {
101 // const x = namedNode("x");
102 // const x2 = namedNode("x");
103 // // (NamedNode.prototype as any).hashCode = () => 0;
104 // // expect((x as any).hashCode()).toEqual((x2 as any).hashCode())
105 // expect(x === x2).toBeFalsy();
106 // expect(x == x2).toBeFalsy();
107 // expect(x.equals(x2)).toBeTruthy();
108 // let imap = Immutable.Map();
109 // imap = imap.set(x, 11);
110 // imap = imap.set(x, 22);
111 // imap = imap.set(x2, 33);
112 // expect(imap.has(x)).toBeTruthy();
113 // expect(imap.has(x2)).toBeTruthy();
114 // expect(imap.size).toEqual(1);
115 // });
116 // });
117
118 // describe("groupByRdfType", () => {
119 // test("finds multiple graphs", () => {});
120 // test("works", async () => {
121 // const store = new Store();
122
123 // const parser = new Parser();
124 // await new Promise((res, rej) => {
125 // parser.parse(
126 // `PREFIX : <urn:>
127 // :rs1 a :Foo; :pred1 "obj1" .
128 // :rs2 a :Foo; :pred1 "obj2" .
129 // :rs3 a :Bar .
130 // :rs4 :pred1 "obj4" .
131 // `,
132 // (error, quad: Quad, prefixes: Prefixes) => {
133 // if (quad) {
134 // store.addQuad(quad);
135 // } else {
136 // res(undefined);
137 // }
138 // }
139 // );
140 // });
141 // const grouped = groupByRdfType(store);
142 // expect(Array.from(grouped.byType.keys())).toHaveLength(2);
143 // expect(grouped.byType.get(namedNode("urn:Foo"))).toEqual(
144 // Immutable.Set([namedNode("urn:rs1"), namedNode("urn:rs2")])
145 // );
146 // expect(grouped.byType.get(namedNode("urn:Bar"))).toEqual(
147 // Immutable.Set([namedNode("urn:rs3")])
148 // );
149 // expect(grouped.untypedSubjs).toEqual([namedNode("urn:rs4")]);
150 // });
151
152 // describe("MultiSubjsTypeBlockLayout", () => {
153 // test("gathers subjs", () => {
154
155 // });
156 // test("gathers preds", () => {
157
158 // });
159 // test("cells reports filled cells", () => {
160
161 // });
162 // });
163 // });