This repository was archived by the owner on Jun 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest-node.js
381 lines (352 loc) · 13.1 KB
/
test-node.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
'use strict'
import test from 'tape'
import ns from 'rdf-ns'
import { Map, Set, Seq, Iterable, List, Collection, fromJS } from 'immutable'
import Cursor from 'immutable/contrib/cursor'
import { JSONLDNode, JSONLDValue, fromExpandedJSONLD} from '../ImmutableJSONLD'
import event from '../../test/data/event-expanded.json'
import product from '../../test/data/product-expanded.json'
import stupid from '../../test/data/stupid-expanded.json'
import stupid_children from '../../test/data/stupid-expanded-children.json'
import stupid_descends from '../../test/data/stupid-expanded-descendants.json'
const rdfs = ns('http://www.w3.org/2000/01/rdf-schema#')
, skos = ns('http://www.w3.org/2004/02/skos/core#')
test('test JSONLDNode construction via factory method', t => {
const node = JSONLDNode()
t.plan(9)
t.ok(node instanceof JSONLDNode,
'is a JSONLDNode')
t.ok(node instanceof Map,
'is an Map')
t.ok(node instanceof Collection.Keyed,
'is an Collection.Keyed')
t.ok(node instanceof Collection,
'is an Collection')
t.ok(node instanceof Iterable,
'is an Iterable')
t.ok(JSONLDNode.isJSONLDNode(node), 'isJSONLDNode()')
t.ok(Map.isMap(node), 'isMap()')
t.ok(Iterable.isIterable(node), 'isIterable()')
t.ok(Iterable.isKeyed(node), 'isKeyed()')
})
test('test JSONLDNode.toString()', t => {
const empty = new JSONLDNode()
, node = fromExpandedJSONLD(
{"http://purl.org/dc/terms/title": [{"@value": "Moby Dick"}]}
).first()
t.plan(2)
t.equals(empty.toString(), 'JSONLDNode {}',
'works for empty node')
t.equals(node.toString(),
'JSONLDNode { "http://purl.org/dc/terms/title": '
+ 'List [ JSONLDValue { "@value": "Moby Dick" } ] }',
'works for non-empty node')
})
test('test JSONLDNode.getIn([])', t => {
const node = fromExpandedJSONLD(event).first()
let value = node.getIn([])
t.plan(2)
t.ok(value instanceof JSONLDNode, 'that is a JSONLDNode')
t.deepEqual(value.toJS(), event[0], 'round-trips OK')
})
test('test JSONLDNode.getIn([predicate])', t => {
const pred = 'http://www.w3.org/2002/12/cal/ical#dtstart'
, node = fromExpandedJSONLD(event).first()
let values = node.getIn([pred])
t.plan(4)
t.ok(values instanceof List, 'returns an List')
t.equal(values.size, 1, 'with one value')
t.ok(values.first() instanceof JSONLDValue, 'that is a JSONLDValue')
t.deepEqual(values.first().toJS(), event[0][pred][0], 'round-trips OK')
})
test('test JSONLDNode.getIn([predicate, 0])', t => {
const pred = 'http://www.w3.org/2002/12/cal/ical#dtstart'
, node = fromExpandedJSONLD(event).first()
let value = node.getIn([pred, 0])
t.plan(2)
t.ok(value instanceof JSONLDValue, 'that is a JSONLDValue')
t.deepEqual(value.toJS(), event[0][pred][0], 'round-trips OK')
})
test('test JSONLDNode.getAt([])', t => {
const node = fromExpandedJSONLD(event).first()
let values = node.getAt([])
t.plan(4)
t.ok(values instanceof Set, 'returns an Set')
t.equal(values.size, 1, 'with one value')
t.ok(values.first() instanceof JSONLDNode, 'that is a JSONLDNode')
t.deepEqual(values.first().toJS(), event[0], 'round-trips OK')
})
test('test JSONLDNode.getAt([predicate])', t => {
const pred = 'http://www.w3.org/2002/12/cal/ical#dtstart'
, node = fromExpandedJSONLD(event).first()
let values = node.getAt([pred])
t.plan(4)
t.ok(values instanceof Set, 'returns an Set')
t.equal(values.size, 1, 'with one value')
t.ok(values.first() instanceof JSONLDValue, 'that is a JSONLDValue')
t.deepEqual(values.first().toJS(), event[0][pred][0], 'round-trips OK')
})
test('test JSONLDNode.getAt([predicate, predicate])', t => {
const path = ['http://stupid.com/wheels', 'http://stupid.com/hubcap']
, node = fromExpandedJSONLD(stupid).first()
let values = node.getAt(path)
t.plan(6)
t.ok(values instanceof Set, 'returns an Set')
t.equal(values.size, 2, 'with two values')
t.ok(values.first() instanceof JSONLDNode, 'first is a JSONLDNode')
t.deepEqual(values.first().toJS(), stupid[0][path[0]][0][path[1]][0],
'first round-trips OK')
t.ok(values.last() instanceof JSONLDNode, 'last is a JSONLDNode')
t.deepEqual(values.last().toJS(), stupid[0][path[0]][1][path[1]][0],
'last round-trips OK')
})
test('test JSONLDNode.getAt([predicate, predicate, predicate])', t => {
const path =
[ 'http://stupid.com/wheels'
, 'http://stupid.com/hubcap'
, 'http://stupid.com/color'
]
, node = fromExpandedJSONLD(stupid).first()
let values = node.getAt(path)
t.plan(6)
t.ok(values instanceof Set, 'returns an Set')
t.equal(values.size, 2, 'with two values')
t.ok(values.first() instanceof JSONLDValue, 'first is a JSONLDValue')
t.deepEqual(values.first().toJS(),
stupid[0][path[0]][0][path[1]][0][path[2]][0],
'first round-trips OK')
t.ok(values.last() instanceof JSONLDValue, 'last is a JSONLDValue')
t.deepEqual(values.last().toJS(),
stupid[0][path[0]][1][path[1]][0][path[2]][0],
'last round-trips OK')
})
test('test cursor from list of JSONLDNodes', t => {
const nodes = fromExpandedJSONLD(stupid)
let path = [ 0
, 'http://stupid.com/wheels', 1
, 'http://stupid.com/hubcap', 0
, 'http://stupid.com/color', 0
]
let cursor = Cursor.from(nodes, path, newData => {
t.equal(newData.getIn(path.concat(['@value'])), 'pink')
t.end()
}
)
cursor.update('@value', () => 'pink')
})
test('test JSONLDNode.types', t => {
t.plan(7)
t.ok(JSONLDNode().types instanceof Set, 'types is a set')
t.ok(JSONLDNode().types.equals(Set()), 'no types')
t.ok(JSONLDNode(
{'@type': Set()}).types.equals(Set()),
'no types explicit')
t.ok(JSONLDNode(
{'@type': Set.of('http://schema.org/Person')}).types.equals(
Set.of('http://schema.org/Person')), 'one type')
try {JSONLDNode().types = null} catch (e) {
t.ok(e instanceof TypeError, 'set throws TypeError')
t.ok(/^Cannot set property types/.test(e.message), 'with message')
}
t.ok(fromExpandedJSONLD(stupid).first().types.equals(
Set.of('http://stupid.com/Car')), 'this works too')
})
test('test JSONLDNode.id', t => {
t.plan(4)
t.equal(JSONLDNode().id, undefined, 'undefined by default')
try {JSONLDNode().id = 1} catch (e) {
t.ok(e instanceof TypeError, 'set throws TypeError')
t.ok(/^Cannot set property id/.test(e.message), 'with message')
}
t.equal(fromExpandedJSONLD(product).first().id,
'http://example.org/cars/for-sale#tesla')
})
test('test JSONLDNode.propertySeq()', t => {
const allkeywords = JSONLDNode(
{ '@id': null
, '@type': []
, '@reverse': null
, '@index': null
})
t.plan(2)
t.ok(allkeywords.propertySeq().equals(Seq()),
'keywords are skipped')
t.ok(fromExpandedJSONLD(event).first()
.propertySeq()
.map(([predicate, ]) => predicate)
.equals(Seq.of(
'http://www.w3.org/2002/12/cal/ical#dtstart',
'http://www.w3.org/2002/12/cal/ical#location',
'http://www.w3.org/2002/12/cal/ical#summary'
)), 'others terms are not')
})
test('test JSONLDNode.childNodes()', t => {
const node = fromExpandedJSONLD(stupid).first()
, expected = fromJS(stupid_children)
t.plan(2)
t.ok(node.childNodes().equals(expected), 'returns expected child nodes')
t.ok(fromExpandedJSONLD(
{'http://stupid.com/color': [{'@value': 'black'}]}).first()
.childNodes().equals(List()), 'returns no child nodes')
})
test('test JSONLDNode.descendantNodes()', t => {
const node = fromExpandedJSONLD(stupid).first()
, expected = fromJS(stupid_descends)
, leafnode = fromExpandedJSONLD(
{'http://stupid.com/color': [{'@value': 'black'}]}).first()
t.plan(2)
t.ok(node.descendantNodes().equals(expected), 'returns expected nodes')
t.ok(leafnode.descendantNodes().equals(
List.of(List.of(List(), leafnode))),
'returns itself')
})
test('test JSONLDNode.delete()', t => {
const node = JSONLDNode({'@id': 'http://stupid.com/1'})
, expected = JSONLDNode()
t.plan(1)
t.ok(node.delete('@id').equals(expected), 'returns expected node')
})
test('test JSONLDNode.remove()', t => {
const node = JSONLDNode({'@id': 'http://stupid.com/1'})
, expected = JSONLDNode()
t.plan(1)
t.ok(node.remove('@id').equals(expected), 'returns expected node')
})
test('test JSONLDNode.push()', t => {
const node = JSONLDNode()
, predicate = 'http://stupid.com/prop'
, value1 = JSONLDValue({'@value': 'val1'})
, value2 = JSONLDValue({'@value': 'val2'})
, expected1 = JSONLDNode({[predicate]: List.of(value1)})
, expected2 = JSONLDNode({[predicate]: List.of(value1, value2)})
t.plan(2)
t.ok(node.push(predicate, value1).equals(expected1))
t.ok(node.push(predicate, value1).push(predicate, value2).equals(expected2))
})
test('test JSONLDNode.push(@type)', t => {
const node = JSONLDNode()
, class1 = 'http://stupid.com/Fish'
, class2 = 'http://stupid.com/Bicycle'
, expected1 = JSONLDNode({'@type': List.of(class1)})
, expected2 = JSONLDNode({'@type': List.of(class1, class2)})
t.plan(2)
t.ok(node.push('@type', class1).equals(expected1))
t.ok(node.push('@type', class1).push('@type', class2).equals(expected2))
})
test('test JSONLDNode.preferredLabel()', t => {
const label1 = JSONLDValue({'@value': 'foo'})
, label2 = JSONLDValue({'@value': 'bar', '@language': 'en'})
t.plan(4)
t.equal(JSONLDNode().preferredLabel(), undefined)
t.ok(JSONLDNode()
.push(rdfs('label'), label1)
.preferredLabel()
.equals(label1), 'rdfs:label is default label predicate')
t.ok(JSONLDNode()
.push(skos('prefLabel'), label2)
.preferredLabel()
.equals(label2), 'skos:prefLabel is default label predicate')
t.ok(JSONLDNode()
.push(rdfs('label'), label1)
.push(skos('prefLabel'), label2)
.preferredLabel()
.equals(label2), 'skos:prefLabel is preferred to rdfs:label')
})
test('test JSONLDNode.preferredLabel(language)', t => {
const label_en = JSONLDValue({'@value': 'English', '@language': 'en'})
, label_ja = JSONLDValue({'@value': '日本語', '@language': 'ja'})
, label_none = JSONLDValue({'@value': 'None'})
t.plan(6)
t.equal(JSONLDNode().preferredLabel('ja'), undefined)
t.equal(JSONLDNode()
.push(rdfs('label'), label_en)
.preferredLabel('ja'), undefined)
t.ok(JSONLDNode()
.push(rdfs('label'), label_en)
.preferredLabel('en')
.equals(label_en), 'gets English label')
t.ok(JSONLDNode()
.push(rdfs('label'), label_en)
.push(rdfs('label'), label_ja)
.preferredLabel('en')
.equals(label_en), 'gets English label')
t.ok(JSONLDNode()
.push(rdfs('label'), label_en)
.push(rdfs('label'), label_ja)
.preferredLabel('ja')
.equals(label_ja), 'gets Japanese label')
t.ok(JSONLDNode()
.push(rdfs('label'), label_en)
.push(rdfs('label'), label_ja)
.push(rdfs('label'), label_none)
.preferredLabel()
.equals(label_none), 'gets label with no language specified')
})
test('test JSONLDNode.preferredLabel(labelPredicates)', t => {
const label1 = JSONLDValue({'@value': 'foo'})
, label2 = JSONLDValue({'@value': 'bar'})
, fake = ns('http://fake.net/')
, labelPredicate1 = fake('label')
, labelPredicate2 = fake('altLabel')
t.plan(7)
t.equal(JSONLDNode().preferredLabel(), undefined)
t.ok(JSONLDNode()
.push(fake('label'), label1)
.preferredLabel('', List([labelPredicate1]))
.equals(label1))
t.equal(JSONLDNode()
.push(fake('label'), label1)
.preferredLabel('', List([labelPredicate2])), undefined)
t.ok(JSONLDNode()
.push(fake('label'), label1)
.preferredLabel('', List([labelPredicate2, labelPredicate1]))
.equals(label1))
t.ok(JSONLDNode()
.push(fake('altLabel'), label2)
.preferredLabel('', List([labelPredicate2]))
.equals(label2))
t.ok(JSONLDNode()
.push(fake('label'), label1)
.push(fake('altLabel'), label2)
.preferredLabel('', List([labelPredicate2]))
.equals(label2))
t.ok(JSONLDNode()
.push(fake('label'), label1)
.push(fake('altLabel'), label2)
.preferredLabel('', List([labelPredicate1]))
.equals(label1))
})
test('test JSONLDNode.set(@value, blah)', t => {
let node = JSONLDNode()
t.plan(1)
t.throws(() => node.set('@value', 'blah'),
/invalid node object keypath: \[ @value \]/)
})
test('test JSONLDNode.setIn([@type, 0])', t => {
let node = JSONLDNode()
t.plan(2)
t.throws(() => node.setIn(['@type', 0], 'blah'),
/no Immutable\.List exists at keypath: \[ @type \]/)
node = node.set('@type', List())
t.doesNotThrow(() => node.setIn(['@type', 0], 'blah'))
})
test('test JSONLDNode.setIn(invalidKeypath)', t => {
t.plan(9)
const node = JSONLDNode()
t.throws(() => node.setIn([0], 'x'))
t.throws(() => node.setIn([true], 'x'))
t.throws(() => node.setIn([{}], 'x'))
t.throws(() => node.setIn([[]], 'x'))
t.throws(() => node.setIn(['a', 'b'], 'x'))
t.throws(() => node.setIn(['a', 0, 0], 'x'))
t.throws(() => node.setIn([0, 'a', 0], 'x'))
t.throws(() => node.setIn(['@id', 0], 'x'))
t.throws(() => node.setIn(['@type', 0, 'foo'], 'x'))
})
test('invalid keyPath does not throw in production', t => {
let node_env = process.env['NODE_ENV']
process.env['NODE_ENV'] = 'production'
t.plan(1)
t.doesNotThrow(() => JSONLDNode().setIn(List(), 'blah'))
process.env['NODE_ENV'] = node_env
})