Mercurial > code > home > repos > streamed-graph
annotate src/layout/Layout.test.ts @ 118:c2923b20bf5c
support multi labels per column
author | drewp@bigasterisk.com |
---|---|
date | Sun, 20 Mar 2022 00:54:19 -0700 |
parents | dd3325cc023e |
children | 8715633f5213 |
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(` | |
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()); | |
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 @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . | |
78 | |
79 <> a :View; :table [ :primaryType :T1 ] .`); | |
80 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
|
81 lr = layout.plan(await typedStatements()); |
1aea03d306af
WIP Layout. tests are passing but they're a little wrong
drewp@bigasterisk.com
parents:
103
diff
changeset
|
82 }); |
108
5e6840229a05
rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents:
106
diff
changeset
|
83 it("returns 2 sections", () => { |
103 | 84 expect(lr.sections).toHaveLength(2); |
108
5e6840229a05
rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents:
106
diff
changeset
|
85 }); |
103 | 86 it("puts the right type in the table", async () => { |
118 | 87 const section0 = lr.sections[0] as AlignedTable; |
88 expect(section0.columnHeaders).toEqual([ | |
89 { rdfTypes: [EX("T1")], pred: EX("color") }, | |
90 { 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
|
91 // 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
|
92 ]); |
118 | 93 expect(section0.rowHeaders).toEqual([EX("a"), EX("b"), EX("c"), EX("e")]); |
94 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
|
95 [[EX("red")], []], |
4b33a479dc2f
fix layout test to match new layout return types. clean up UriPairMap
drewp@bigasterisk.com
parents:
109
diff
changeset
|
96 [[EX("blue")], []], |
4b33a479dc2f
fix layout test to match new layout return types. clean up UriPairMap
drewp@bigasterisk.com
parents:
109
diff
changeset
|
97 [[], []], |
4b33a479dc2f
fix layout test to match new layout return types. clean up UriPairMap
drewp@bigasterisk.com
parents:
109
diff
changeset
|
98 [[], [EX("small")]], |
108
5e6840229a05
rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents:
106
diff
changeset
|
99 ]); |
103 | 100 }); |
101 it("leaves the rest ungrouped", async () => { | |
102 expect(lr.sections[1]).toEqual({ | |
108
5e6840229a05
rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents:
106
diff
changeset
|
103 subjRows: [ |
5e6840229a05
rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents:
106
diff
changeset
|
104 { |
5e6840229a05
rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents:
106
diff
changeset
|
105 subj: EX("d"), |
5e6840229a05
rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents:
106
diff
changeset
|
106 predRows: [ |
5e6840229a05
rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents:
106
diff
changeset
|
107 { pred: EX("size"), objs: [EX("big")] }, |
5e6840229a05
rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents:
106
diff
changeset
|
108 { pred: rdf.type, objs: [EX("T2")] }, |
5e6840229a05
rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents:
106
diff
changeset
|
109 ], |
5e6840229a05
rewrite freeStatements rendering to put more planning in layout
drewp@bigasterisk.com
parents:
106
diff
changeset
|
110 }, |
103 | 111 ], |
112 }); | |
113 }); | |
114 }); | |
116 | 115 it("makes two tables", async () => { |
116 const vc = new ViewConfig(); | |
117 await vc.readFromGraph(` | |
118 @prefix ex: <http://example.com/> . | |
119 @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . | |
120 @prefix : <http://example.com/> . | |
121 | |
122 <> a :View; :table [ :primaryType :T1 ], [ :primaryType :T2 ] .`); | |
123 const layout = new Layout(vc); | |
124 const lr = layout.plan(await typedStatements()); | |
125 expect(lr.sections).toHaveLength(2); | |
118 | 126 const section0 = lr.sections[0] as AlignedTable; |
127 expect(section0.columnHeaders).toEqual([ | |
128 { rdfTypes: [EX("T1")], pred: EX("color") }, | |
129 { rdfTypes: [EX("T1"), EX("T2")], pred: EX("size") }, | |
130 ]); | |
131 expect(section0.rowHeaders).toEqual([EX("a"), EX("b"), EX("c"), EX("e")]); | |
132 expect(section0.rows).toEqual([ | |
133 [[EX("red")], []], | |
134 [[EX("blue")], []], | |
135 [[], []], | |
136 [[], [EX("small")]], | |
137 ]); | |
138 const section1 = lr.sections[1] as AlignedTable; | |
139 expect(section1.columnHeaders).toEqual([ | |
140 { rdfTypes: [EX("T1"), EX("T2")], pred: EX("size") }, | |
141 ]); | |
142 expect(section1.rowHeaders).toEqual([EX("d"), EX("e")]); | |
143 expect(section1.rows).toEqual([ | |
144 [[EX("big")]], // | |
145 [[EX("small")]], | |
146 ]); | |
147 }); | |
148 describe("joins multiple types into one table", () => { | |
149 it("can simply merge types", async () => { | |
150 const vc = new ViewConfig(); | |
151 await vc.readFromGraph(` | |
152 @prefix ex: <http://example.com/> . | |
153 @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . | |
154 @prefix : <http://example.com/> . | |
155 | |
156 <> a :View; :table [ :primaryType :T1; :joinType :T2 ] . | |
157 `); | |
158 vc.graph.forEach((q) => console.log("vc", q), null, null, null, null); | |
159 const layout = new Layout(vc); | |
160 const lr = layout.plan( | |
161 await n3Graph(` | |
162 @prefix : <http://example.com/> . | |
163 :g1 { | |
164 :a a :T1 ; :color :red . | |
165 :b a :T2 ; :size :big . | |
166 } | |
167 `) | |
168 ); | |
169 expect(lr.sections).toHaveLength(1); | |
170 const section0 = lr.sections[0] as AlignedTable; | |
171 expect(section0.columnHeaders).toEqual([ | |
172 { rdfTypes: [EX("T1")], pred: EX("color") }, | |
173 { rdfTypes: [EX("T2")], pred: EX("size") }, | |
174 ]); | |
175 expect(section0.rowHeaders).toEqual([EX("a"), EX("b")]); | |
176 expect(section0.rows).toEqual([ | |
116 | 177 [[EX("red")], []], |
118 | 178 [[], [EX("big")]], |
179 ]); | |
116 | 180 }); |
181 }); | |
114
4b33a479dc2f
fix layout test to match new layout return types. clean up UriPairMap
drewp@bigasterisk.com
parents:
109
diff
changeset
|
182 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
|
183 }); |