Mercurial > code > home > repos > streamed-graph
annotate src/layout/Layout.test.ts @ 121:3584f24becf4
cleanup
author | drewp@bigasterisk.com |
---|---|
date | Sun, 20 Mar 2022 14:10:56 -0700 |
parents | 8715633f5213 |
children | 2e8fa3fec0c8 |
rev | line source |
---|---|
104
1aea03d306af
WIP Layout. tests are passing but they're a little wrong
drewp@bigasterisk.com
parents:
103
diff
changeset
|
1 import { Quad, Store, Term } from "n3"; |
1aea03d306af
WIP Layout. tests are passing but they're a little wrong
drewp@bigasterisk.com
parents:
103
diff
changeset
|
2 import { n3Graph } from "./fetchAndParse"; |
1aea03d306af
WIP Layout. tests are passing but they're a little wrong
drewp@bigasterisk.com
parents:
103
diff
changeset
|
3 import { AlignedTable, Layout, LayoutResult } from "./Layout"; |
1aea03d306af
WIP Layout. tests are passing but they're a little wrong
drewp@bigasterisk.com
parents:
103
diff
changeset
|
4 import { EX, rdf } from "./namespaces"; |
103 | 5 import { ViewConfig } from "./ViewConfig"; |
88
ac7ad087d474
graph view rewrites and fixes for the multi-subject table
drewp@bigasterisk.com
parents:
diff
changeset
|
6 |
103 | 7 const twoStatements = async (): Promise<Store> => { |
8 return n3Graph(` | |
9 @prefix : <http://example.com/> . | |
10 :g1 { | |
104
1aea03d306af
WIP Layout. tests are passing but they're a little wrong
drewp@bigasterisk.com
parents:
103
diff
changeset
|
11 :a0 :b0 :c0 . |
1aea03d306af
WIP Layout. tests are passing but they're a little wrong
drewp@bigasterisk.com
parents:
103
diff
changeset
|
12 :d0 :e0 :f0 . |
103 | 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(` | |
121 | 37 @prefix : <http://example.com/> . |
103 | 38 @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . |
39 | |
121 | 40 <> a :View; rdfs:label "repos" .`); |
103 | 41 const layout = new Layout(vc); |
42 const lr = layout.plan(await twoStatements()); | |
88
ac7ad087d474
graph view rewrites and fixes for the multi-subject table
drewp@bigasterisk.com
parents:
diff
changeset
|
43 }); |
116 | 44 it("returns no sections for empty graph", () => { |
45 const vc = new ViewConfig(); | |
46 const layout = new Layout(vc); | |
47 const lr = layout.plan(new Store()); | |
48 expect(lr.sections).toHaveLength(0); | |
49 }); | |
103 | 50 it("defaults to putting all triples in the ungrouped list", async () => { |
51 const layout = new Layout(); | |
52 const lr = layout.plan(await twoStatements()); | |
53 expect(lr).toEqual({ | |
54 sections: [ | |
55 { | |
108
5e6840229a05
rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents:
106
diff
changeset
|
56 subjRows: [ |
109 | 57 { |
58 subj: EX("a0"), | |
59 predRows: [{ pred: EX("b0"), objs: [EX("c0")] }], | |
60 }, | |
61 { | |
62 subj: EX("d0"), | |
63 predRows: [{ pred: EX("e0"), objs: [EX("f0")] }], | |
64 }, | |
103 | 65 ], |
66 }, | |
67 ], | |
68 }); | |
69 }); | |
70 describe("makes a table as requested by ViewConfig", () => { | |
71 let lr: LayoutResult; | |
72 | |
73 beforeAll(async () => { | |
74 const vc = new ViewConfig(); | |
75 await vc.readFromGraph(` | |
76 @prefix : <http://example.com/> . | |
77 | |
78 <> a :View; :table [ :primaryType :T1 ] .`); | |
79 const layout = new Layout(vc); | |
104
1aea03d306af
WIP Layout. tests are passing but they're a little wrong
drewp@bigasterisk.com
parents:
103
diff
changeset
|
80 lr = layout.plan(await typedStatements()); |
1aea03d306af
WIP Layout. tests are passing but they're a little wrong
drewp@bigasterisk.com
parents:
103
diff
changeset
|
81 }); |
108
5e6840229a05
rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents:
106
diff
changeset
|
82 it("returns 2 sections", () => { |
103 | 83 expect(lr.sections).toHaveLength(2); |
108
5e6840229a05
rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents:
106
diff
changeset
|
84 }); |
103 | 85 it("puts the right type in the table", async () => { |
118 | 86 const section0 = lr.sections[0] as AlignedTable; |
87 expect(section0.columnHeaders).toEqual([ | |
88 { rdfTypes: [EX("T1")], pred: EX("color") }, | |
89 { rdfTypes: [EX("T1"), EX("T2")], pred: EX("size") }, | |
114
4b33a479dc2f
fix layout test to match new layout return types. clean up UriPairMap
drewp@bigasterisk.com
parents:
109
diff
changeset
|
90 // and doesn't include rdf:type as a column header here |
108
5e6840229a05
rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents:
106
diff
changeset
|
91 ]); |
118 | 92 expect(section0.rowHeaders).toEqual([EX("a"), EX("b"), EX("c"), EX("e")]); |
93 expect(section0.rows).toEqual([ | |
114
4b33a479dc2f
fix layout test to match new layout return types. clean up UriPairMap
drewp@bigasterisk.com
parents:
109
diff
changeset
|
94 [[EX("red")], []], |
4b33a479dc2f
fix layout test to match new layout return types. clean up UriPairMap
drewp@bigasterisk.com
parents:
109
diff
changeset
|
95 [[EX("blue")], []], |
4b33a479dc2f
fix layout test to match new layout return types. clean up UriPairMap
drewp@bigasterisk.com
parents:
109
diff
changeset
|
96 [[], []], |
4b33a479dc2f
fix layout test to match new layout return types. clean up UriPairMap
drewp@bigasterisk.com
parents:
109
diff
changeset
|
97 [[], [EX("small")]], |
108
5e6840229a05
rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents:
106
diff
changeset
|
98 ]); |
103 | 99 }); |
100 it("leaves the rest ungrouped", async () => { | |
101 expect(lr.sections[1]).toEqual({ | |
108
5e6840229a05
rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents:
106
diff
changeset
|
102 subjRows: [ |
5e6840229a05
rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents:
106
diff
changeset
|
103 { |
5e6840229a05
rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents:
106
diff
changeset
|
104 subj: EX("d"), |
5e6840229a05
rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents:
106
diff
changeset
|
105 predRows: [ |
5e6840229a05
rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents:
106
diff
changeset
|
106 { pred: EX("size"), objs: [EX("big")] }, |
5e6840229a05
rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents:
106
diff
changeset
|
107 { pred: rdf.type, objs: [EX("T2")] }, |
5e6840229a05
rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents:
106
diff
changeset
|
108 ], |
5e6840229a05
rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents:
106
diff
changeset
|
109 }, |
103 | 110 ], |
111 }); | |
112 }); | |
113 }); | |
116 | 114 it("makes two tables", async () => { |
115 const vc = new ViewConfig(); | |
116 await vc.readFromGraph(` | |
117 @prefix : <http://example.com/> . | |
118 | |
119 <> a :View; :table [ :primaryType :T1 ], [ :primaryType :T2 ] .`); | |
120 const layout = new Layout(vc); | |
121 const lr = layout.plan(await typedStatements()); | |
122 expect(lr.sections).toHaveLength(2); | |
118 | 123 const section0 = lr.sections[0] as AlignedTable; |
124 expect(section0.columnHeaders).toEqual([ | |
125 { rdfTypes: [EX("T1")], pred: EX("color") }, | |
126 { rdfTypes: [EX("T1"), EX("T2")], pred: EX("size") }, | |
127 ]); | |
128 expect(section0.rowHeaders).toEqual([EX("a"), EX("b"), EX("c"), EX("e")]); | |
129 expect(section0.rows).toEqual([ | |
130 [[EX("red")], []], | |
131 [[EX("blue")], []], | |
132 [[], []], | |
133 [[], [EX("small")]], | |
134 ]); | |
135 const section1 = lr.sections[1] as AlignedTable; | |
136 expect(section1.columnHeaders).toEqual([ | |
137 { rdfTypes: [EX("T1"), EX("T2")], pred: EX("size") }, | |
138 ]); | |
139 expect(section1.rowHeaders).toEqual([EX("d"), EX("e")]); | |
140 expect(section1.rows).toEqual([ | |
141 [[EX("big")]], // | |
142 [[EX("small")]], | |
143 ]); | |
144 }); | |
145 describe("joins multiple types into one table", () => { | |
121 | 146 it("can simply display multiple types merged together", async () => { |
118 | 147 const vc = new ViewConfig(); |
148 await vc.readFromGraph(` | |
149 @prefix ex: <http://example.com/> . | |
150 @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . | |
151 @prefix : <http://example.com/> . | |
152 | |
153 <> a :View; :table [ :primaryType :T1; :joinType :T2 ] . | |
154 `); | |
155 const layout = new Layout(vc); | |
156 const lr = layout.plan( | |
157 await n3Graph(` | |
158 @prefix : <http://example.com/> . | |
159 :g1 { | |
160 :a a :T1 ; :color :red . | |
161 :b a :T2 ; :size :big . | |
162 } | |
163 `) | |
164 ); | |
165 expect(lr.sections).toHaveLength(1); | |
166 const section0 = lr.sections[0] as AlignedTable; | |
167 expect(section0.columnHeaders).toEqual([ | |
168 { rdfTypes: [EX("T1")], pred: EX("color") }, | |
169 { rdfTypes: [EX("T2")], pred: EX("size") }, | |
170 ]); | |
171 expect(section0.rowHeaders).toEqual([EX("a"), EX("b")]); | |
172 expect(section0.rows).toEqual([ | |
116 | 173 [[EX("red")], []], |
118 | 174 [[], [EX("big")]], |
175 ]); | |
116 | 176 }); |
121 | 177 it("groups columns by type", async () => { |
119
8715633f5213
fancier column sorting to bring preds from the same type together
drewp@bigasterisk.com
parents:
118
diff
changeset
|
178 const vc = new ViewConfig(); |
8715633f5213
fancier column sorting to bring preds from the same type together
drewp@bigasterisk.com
parents:
118
diff
changeset
|
179 await vc.readFromGraph(` |
8715633f5213
fancier column sorting to bring preds from the same type together
drewp@bigasterisk.com
parents:
118
diff
changeset
|
180 @prefix ex: <http://example.com/> . |
8715633f5213
fancier column sorting to bring preds from the same type together
drewp@bigasterisk.com
parents:
118
diff
changeset
|
181 @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . |
8715633f5213
fancier column sorting to bring preds from the same type together
drewp@bigasterisk.com
parents:
118
diff
changeset
|
182 @prefix : <http://example.com/> . |
8715633f5213
fancier column sorting to bring preds from the same type together
drewp@bigasterisk.com
parents:
118
diff
changeset
|
183 |
8715633f5213
fancier column sorting to bring preds from the same type together
drewp@bigasterisk.com
parents:
118
diff
changeset
|
184 <> a :View; :table [ :primaryType :T1; :joinType :T2, :T3 ] . |
8715633f5213
fancier column sorting to bring preds from the same type together
drewp@bigasterisk.com
parents:
118
diff
changeset
|
185 `); |
8715633f5213
fancier column sorting to bring preds from the same type together
drewp@bigasterisk.com
parents:
118
diff
changeset
|
186 const layout = new Layout(vc); |
8715633f5213
fancier column sorting to bring preds from the same type together
drewp@bigasterisk.com
parents:
118
diff
changeset
|
187 const lr = layout.plan( |
8715633f5213
fancier column sorting to bring preds from the same type together
drewp@bigasterisk.com
parents:
118
diff
changeset
|
188 await n3Graph(` |
8715633f5213
fancier column sorting to bring preds from the same type together
drewp@bigasterisk.com
parents:
118
diff
changeset
|
189 @prefix : <http://example.com/> . |
8715633f5213
fancier column sorting to bring preds from the same type together
drewp@bigasterisk.com
parents:
118
diff
changeset
|
190 :g1 { |
8715633f5213
fancier column sorting to bring preds from the same type together
drewp@bigasterisk.com
parents:
118
diff
changeset
|
191 :a a :T1; :p1 :red; :p3 :green . |
8715633f5213
fancier column sorting to bring preds from the same type together
drewp@bigasterisk.com
parents:
118
diff
changeset
|
192 :b a :T2; :p2 :blue . |
8715633f5213
fancier column sorting to bring preds from the same type together
drewp@bigasterisk.com
parents:
118
diff
changeset
|
193 } |
8715633f5213
fancier column sorting to bring preds from the same type together
drewp@bigasterisk.com
parents:
118
diff
changeset
|
194 `) |
8715633f5213
fancier column sorting to bring preds from the same type together
drewp@bigasterisk.com
parents:
118
diff
changeset
|
195 ); |
8715633f5213
fancier column sorting to bring preds from the same type together
drewp@bigasterisk.com
parents:
118
diff
changeset
|
196 const section0 = lr.sections[0] as AlignedTable; |
8715633f5213
fancier column sorting to bring preds from the same type together
drewp@bigasterisk.com
parents:
118
diff
changeset
|
197 expect(section0.columnHeaders).toEqual([ |
8715633f5213
fancier column sorting to bring preds from the same type together
drewp@bigasterisk.com
parents:
118
diff
changeset
|
198 { rdfTypes: [EX("T1")], pred: EX("p1") }, |
8715633f5213
fancier column sorting to bring preds from the same type together
drewp@bigasterisk.com
parents:
118
diff
changeset
|
199 { rdfTypes: [EX("T1")], pred: EX("p3") }, |
8715633f5213
fancier column sorting to bring preds from the same type together
drewp@bigasterisk.com
parents:
118
diff
changeset
|
200 { rdfTypes: [EX("T2")], pred: EX("p2") }, |
8715633f5213
fancier column sorting to bring preds from the same type together
drewp@bigasterisk.com
parents:
118
diff
changeset
|
201 ]); |
8715633f5213
fancier column sorting to bring preds from the same type together
drewp@bigasterisk.com
parents:
118
diff
changeset
|
202 }) |
116 | 203 }); |
114
4b33a479dc2f
fix layout test to match new layout return types. clean up UriPairMap
drewp@bigasterisk.com
parents:
109
diff
changeset
|
204 it.skip("makes a table out of ungrouped triples with the same type", async () => {}); |
88
ac7ad087d474
graph view rewrites and fixes for the multi-subject table
drewp@bigasterisk.com
parents:
diff
changeset
|
205 }); |