-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathspec_test.cljc
More file actions
669 lines (626 loc) · 24.1 KB
/
spec_test.cljc
File metadata and controls
669 lines (626 loc) · 24.1 KB
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
(ns xapi-schema.spec-test
(:require [clojure.test :refer [deftest is testing] :include-macros true]
[clojure.spec.alpha :as s :include-macros true]
[xapi-schema.spec :as xs :include-macros true]
[xapi-schema.support.spec :refer [should-satisfy
should-not-satisfy
should-satisfy+
key-should-satisfy+]]
[xapi-schema.support.data :as d :refer [simple-statement
long-statement]]))
(deftest double-conformer-test
(testing "conforms to double"
(is (= 1.0
(s/conform xs/double-conformer 1))))
(testing "unform is a no-op"
(is (= 1.0
(->> 1
(s/conform xs/double-conformer)
(s/unform xs/double-conformer))))))
(deftest conform-unform-test
(is (= long-statement
(s/unform ::xs/statement (s/conform ::xs/statement long-statement)))))
(deftest conform-ns-map-test
(is (= (name (xs/conform-ns-map "foo/bar" []))
"invalid")))
(deftest language-tag-test
(testing "is a valid RFC 5646 Language Tag"
(should-satisfy+ ::xs/language-tag
"en-US"
:bad
"not a tag!")))
(deftest language-map-test
(testing "has LanguageTags for keys"
(should-satisfy+ ::xs/language-map
{"en-US" "foo"}
{"es" "hola mundo"}
{"zh-cmn" "你好世界"}
{} ; empty lang maps are allowed by spec
:bad
{"hey there" "foo"}
{"en" 2})))
(deftest iri-test
(testing "must be a valid url with scheme"
(should-satisfy+ ::xs/iri
"http://foo.com"
:bad
"foo.com")))
(deftest mail-to-iri-test
(testing "must be a valid foaf mbox"
(should-satisfy+ ::xs/mailto-iri
"mailto:milt@yetanalytics.com"
:bad
"mailto:user%@example.com"
"milt@yetanalytics.com")))
(deftest irl-test
(testing "must be a valid URL"
(should-satisfy+ ::xs/irl
"http://foo.com"
:bad
"not an IRL")))
(deftest extensions-test
(testing "is a map with IRI keys"
(should-satisfy+ ::xs/extensions
{"http://www.foo.bar" {"arbitrary" "data"}}
:bad
{"foo.bar" {"arbitrary" "data"}})))
(deftest open-id-test
(testing "is a valid URL"
(should-satisfy+ ::xs/openid
"http://foo.bar/baz"
:bad
"some other crap")))
(deftest uuid-test
(testing "is a valid v1-8 UUID"
(should-satisfy+ ::xs/uuid
"f47ac10b-58cc-4372-a567-0e02b2c3d479"
"12345678-1234-1234-1234-123456789012"
"017b4f9f-2a7e-84f1-80e9-7b788a5baba4"
:bad
;; 9 is not a valid version number
"12345678-1234-9234-1234-123456789012")))
(deftest timestamp-test
(testing "is a valid ISO 8601 DateTime"
(should-satisfy+ ::xs/timestamp
"2014-09-10T14:12:05Z"
"2014-10-31T14:12:05Z" ; october 31
"2015-06-30T23:59:60Z" ; leap second
"2020-02-29T01:01:01Z" ; leap year
"2000-02-29T01:01:01Z" ; 2000 is a leap year
:bad
"09-10-2014T14:12:00+500"
"2014-09-32T14:12:05Z" ; september 32
"2014-09-31T14:12:05Z" ; september 31
"2014-10-32T14:12:05Z" ; october 32
"2014-09-12T03:47:40" ; no time zone
"2021-02-30T01:01:01Z" ; february 30
"2021-02-29T01:01:01Z" ; february 29, non leap year
"1900-02-29T01:01:01Z" ; 1900 is not a leap year
"2014-09-10T14:12:05.22.33Z")))
(deftest duration-test
(testing "is a valid ISO 8601 Duration"
(should-satisfy+ ::xs/duration
"P3Y6M4DT12H30M5S"
"P3Y6M4DT12H30M5.2S" ;; good fractional
:bad
"2 hours"
"P"
"PT"
"P3Y6M4DT12H30.1M5S"))) ;; bad fractional
(deftest version-test
(testing "is a valid xAPI 1.0.X version"
(should-satisfy+ ::xs/version
"1.0.0"
"1.0.1"
"1.0.2"
"1.0.3"
"1.0.3-rc1"
"1.0.0-alpha"
"1.0.0-alpha.1"
"1.0.0-0.3.7"
"1.0.0-x.7.z.92"
:bad
"0.9.5"
"1.0." ;; bad semver
"1.0.0-.123"
"1.0.0-..."
"1.0.0-123."
"1.0.0-+"
"1.0.0-+123"
"1.0.0-"
"what's going on?")))
(deftest sha2-test
(testing "is a Base64 encoded string"
(should-satisfy+ ::xs/sha2
"672fa5fa658017f1b72d65036f13379c6ab05d4ab3b6664908d8acf0b6a0c634"
:bad
123
"Q3lxN0R1NQ==")))
(deftest sha1sum-test
(testing "is a SHA-1 string of 40 hex chars"
(should-satisfy+
::xs/sha1sum
"2fd4e1c67a2d28fced849ee1bb76e7391b93eb12"
:bad
"2fd4e1c67a2d28fced849ee1bb76e7391" ;; >40
"12345"
"wat"
2)))
(deftest interaction-component-test
(testing "must have an ID"
(should-satisfy+ ::xs/interaction-component
{"id" "foo"}
:bad
{})))
(deftest interaction-components-test
(testing
"each component"
(testing "must have a unique ID"
(should-satisfy+ ::xs/interaction-components
[{"id" "1"
"description" {"en-US" "foo"}}
{"id" "2"
"description" {"en-US" "bar"}}]
:bad
[{"id" "1"
"description" {"en-US" "foo"}}
{"id" "1"
"description" {"en-US" "bar"}}]))))
(deftest definition-test
(let [definition d/definition
definition-with-interaction-type d/definition-with-interaction-type]
(testing "should be satisfied by a valid definition"
(should-satisfy+ :activity/definition
definition))
(testing
"correctResponsesPattern"
(testing "is an array of strings"
(key-should-satisfy+
:activity/definition
definition-with-interaction-type
"correctResponsesPattern"
["foo" "bar" "baz"]
:bad
"foo bar baz")))
(testing
"interactionType"
(testing "is one of true-false, choice, fill-in, long-fill-in, matching,
performance, sequencing, likert, numeric, other"
(key-should-satisfy+ :activity/definition
definition
"interactionType"
"true-false" "choice" "fill-in" "long-fill-in" "matching"
"performance" "sequencing" "likert" "numeric" "other"
:bad "foo")))
(testing "when the activity is an interaction activity"
(testing "is satisfied by all interaction types"
(apply should-satisfy+ :activity/definition
(vals d/interaction-activity-defs))))))
(deftest activity-test
(testing
"id"
(testing "is required"
(should-satisfy+ ::xs/activity
{"id" "http://foo.com/bar"}
:bad
{})))
(testing
"objectType"
(testing "must be Activity if present"
(should-satisfy+ ::xs/activity
{"id" "http://foo.com/bar"}
{"id" "http://foo.com/bar"
"objectType" "Activity"}
:bad
{"id" "http://foo.com/bar"
"objectType" "foo"}))))
(deftest account-test
(testing
"name"
(testing "is required"
(should-satisfy+ ::xs/account
{"name" "bob"
"homePage" "http://foo.com/bar"}
:bad
{"homePage" "http://foo.com/bar"})))
(testing
"homePage"
(testing "is required"
(should-satisfy+ ::xs/account
{"name" "bob"
"homePage" "http://foo.com/bar"}
:bad
{"name" "bob"}))))
(deftest agent-test
(testing "must have one and only one IFI"
(should-satisfy+ ::xs/agent
{"mbox" "mailto:milt@yetanalytics.com"}
:bad
{}
{"mbox" "mailto:milt@yetanalytics.com"
"openid" "https://some.site.com/foo"}))
(testing
"objectType"
(testing "must be Agent if provided"
(key-should-satisfy+ ::xs/agent
{"mbox" "mailto:milt@yetanalytics.com"}
"objectType"
"Agent"
:bad
"Group"))))
(deftest group-test
(testing
"Anonymous Groups"
(testing "must have a member property"
(should-satisfy+ ::xs/group
{"member" [{"mbox" "mailto:milt@yetanalytics.com"}]
"objectType" "Group"}
:bad
{"objectType" "Group"}
{"member" []
"objectType" "Group"})))
(testing
"Identified Group"
(testing "must have one or no IFI"
(should-satisfy+ ::xs/group
{"mbox" "mailto:milt@yetanalytics.com"
"objectType" "Group"}
:bad
{"objectType" "Group"}
{"mbox" "mailto:milt@yetanalytics.com"
"openid" "https://some.site.com/foo"
"objectType" "Group"})))
(testing
"objectType"
(testing "must be present and be Group"
(should-satisfy+ ::xs/group
{"mbox" "mailto:somegroup@yetanalytics.com"
"objectType" "Group"}
:bad
{"mbox" "mailto:so@yetanalytics.com"}
{"mbox" "mailto:somegroup@yetanalytics.com"
"objectType" "Agent"}))))
(deftest verb-test
(testing "id"
(testing "is required"
(should-satisfy+ ::xs/verb
{"id" "http://foo.bar/baz"}
:bad
{}))))
(deftest score-test
(testing "validates score properties"
(should-satisfy+ :result/score
{"raw" 5
"min" 1
"max" 10}
:bad
{"raw" 5
"max" 1}
{"raw" 100
"min" 99
"max" 1}))
(testing "can be empty"
(should-satisfy :result/score {}))
(testing "can be conformed/unformed, per https://github.com/yetanalytics/xapi-schema/issues/61"
(is (= {"scaled" 0.5}
(->> {"scaled" 0.5}
(s/conform :result/score)
(s/unform :result/score))))))
(deftest result-test
(testing "can be empty"
(should-satisfy ::xs/result {})))
(deftest statement-ref
(testing
"id"
(testing "is required"
(should-satisfy+ ::xs/statement-ref
{"objectType" "StatementRef"
"id" "f47ac10b-58cc-4372-a567-0e02b2c3d479"}
:bad
{"objectType" "StatementRef"})))
(testing
"objectType"
(testing "is required and must be StatementRef"
(should-satisfy+ ::xs/statement-ref
{"objectType" "StatementRef"
"id" "f47ac10b-58cc-4372-a567-0e02b2c3d479"}
:bad
{"objectType" "foo"
"id" "f47ac10b-58cc-4372-a567-0e02b2c3d479"}
{"id" "f47ac10b-58cc-4372-a567-0e02b2c3d479"}))))
(deftest context-activities-test
(testing "is an array of 1+ activities or single activity"
(should-satisfy+ ::xs/context-activities
[{"id" "http://foo.bar/baz"
"objectType" "Activity"}]
[{"id" "http://foo.bar/baz"
"objectType" "Activity"}
{"id" "http://foo.bar/biz"
"objectType" "Activity"}]
{"id" "http://foo.bar/baz"
"objectType" "Activity"}
:bad
[]
["foo"])))
(deftest context-activities-map-test
(testing "can be empty"
(should-satisfy :context/contextActivities {})))
(deftest context-test
(testing "can be empty"
(should-satisfy ::xs/context {}))
(testing
"team"
(testing "must be a group"
(should-satisfy+ ::xs/context
{"team" {"mbox" "mailto:a@b.com"
"objectType" "Group"}}
:bad
{"team" {"mbox" "mailto:a@b.com"}}
{"team" {"mbox" "mailto:a@b.com"
"objectType" "Agent"}})))
(testing "xAPI 2.0.0"
(binding [xs/*xapi-version* "2.0.0"]
(testing "contextAgents"
(should-satisfy+
::xs/context
{"contextAgents"
[{:objectType "contextAgent"
:agent {"mbox" "mailto:a@b.com"
"objectType" "Agent"}}]}
:bad
{"contextAgents" [{"mbox" "mailto:a@b.com"
"objectType" "Agent"}]}
{"contextAgents"
[{:objectType "contextGroup"
:group {"mbox" "mailto:a@b.com"
"objectType" "Group"}}]}))
(testing "contextGroups"
(should-satisfy+
::xs/context
{"contextGroups"
[{:objectType "contextGroup"
:group {"mbox" "mailto:a@b.com"
"objectType" "Group"}}]}
:bad
{"contextGroups" [{"mbox" "mailto:a@b.com"
"objectType" "Group"}]}
{"contextGroups"
[{:objectType "contextAgent"
:agent {"mbox" "mailto:a@b.com"
"objectType" "Agent"}}]})))))
(deftest attachment-test
(testing
"usageType, display, contentType, length, sha2"
(testing "are required"
(should-satisfy+ ::xs/attachment
{"usageType" "http://foo.bar/baz"
"display" {"en-US" "foo"}
"contentType" "application/json"
"length" 1024
"sha2" "672fa5fa658017f1b72d65036f13379c6ab05d4ab3b6664908d8acf0b6a0c634"}
:bad
{}
{"usageType" "http://foo.bar/baz"}))))
(deftest url-attachment-test
(testing
"usageType, display, contentType, length, sha2, fileUrl"
(testing "are required"
(should-satisfy+ ::xs/attachment
{"usageType" "http://foo.bar/baz"
"display" {"en-US" "foo"}
"contentType" "application/json"
"length" 1024
"sha2" "672fa5fa658017f1b72d65036f13379c6ab05d4ab3b6664908d8acf0b6a0c634"
"fileUrl" "http://foo.bar/baz"}
:bad
{}
{"usageType" "http://foo.bar/baz"}))))
(deftest attachments-test
(testing "is an array of at least one attachment"
(should-satisfy+ ::xs/attachments
[{"usageType" "http://foo.bar/baz"
"display" {"en-US" "foo"}
"contentType" "application/json"
"length" 1024
"sha2" "672fa5fa658017f1b72d65036f13379c6ab05d4ab3b6664908d8acf0b6a0c634"}]
:bad
[]
{})))
(deftest sub-statement-test
(let [minimal-sub-statement d/sub-statement]
(testing "must not have the id, stored, version, or authority properties"
(should-satisfy+ ::xs/sub-statement
minimal-sub-statement
:bad
(assoc minimal-sub-statement "id" d/uuid-str)
(assoc minimal-sub-statement "stored" d/timestamp)
(assoc minimal-sub-statement "version" d/version)
(assoc minimal-sub-statement "authority" d/agente)))
(testing
"actor"
(testing "is required"
(should-not-satisfy ::xs/sub-statement (dissoc minimal-sub-statement "actor")))
(testing "is an agent or group"
(key-should-satisfy+ ::xs/sub-statement
minimal-sub-statement
"actor"
d/agente
d/group
d/anon-group
:bad
"foo"
{})))
(testing
"verb"
(testing "is required"
(should-not-satisfy ::xs/sub-statement (dissoc minimal-sub-statement "verb")))
(testing "is a Verb"
(key-should-satisfy+ ::xs/sub-statement
minimal-sub-statement
"verb"
d/verb
:bad
[]
{}
d/activity
d/agente)))
(testing
"object"
(testing "is required"
(should-not-satisfy ::xs/sub-statement
(dissoc minimal-sub-statement "object")))
(testing "is an Activity, Agent, Group, or StatementRef"
(key-should-satisfy+ ::xs/sub-statement
minimal-sub-statement
"object"
;; Activity is the default
(dissoc d/activity "objectType")
;; explicit Activity ObjectType
d/activity
d/agente
d/group
d/anon-group
d/statement-ref
:bad
[]
{})))
(testing
"objectType"
(testing "is required"
(should-not-satisfy ::xs/sub-statement (dissoc minimal-sub-statement "objectType"))))))
(deftest oauth-consumer-test
(testing "must be identified by account"
(should-satisfy+
::xs/oauth-consumer
{"account" {"name" "oauth_consumer_x75db"
"homePage" "http://example.com/xAPI/OAuth/Token"}}
:bad
{"mbox" "mailto:milt@yetanalytics.com"})))
(deftest three-legged-oauth-group-test
(testing "must be a group with two agents"
(should-satisfy+
::xs/tlo-group
d/authority-group
:bad
(update-in d/authority-group ["member"] (comp vector first))))
(testing "must have an OAuthConsumer for the first member"
(should-satisfy+
::xs/tlo-group
d/authority-group
:bad
(assoc d/authority-group "member" [d/agente d/agente]))))
(deftest authority-test
(testing "must be an agent"
(should-satisfy
:statement/authority
d/agente))
(testing
"except in the case of three-legged-oauth, when"
(testing "can be a group with two agents"
(should-satisfy
:statement/authority
d/authority-group))))
(deftest statement-object
(testing "is an Agent, Group, SubStatement, StatementRef or Activity"
(should-satisfy+ :statement/object
d/agente
d/group
d/anon-group
d/sub-statement
d/statement-ref
d/activity
(dissoc d/activity "objectType")
:bad
[]
{}
d/verb)))
(deftest statement-test
(testing
"actor"
(testing "is required"
(should-not-satisfy ::xs/statement (dissoc long-statement "actor")))
(testing "is an agent or group"
(key-should-satisfy+ ::xs/statement
long-statement
"actor"
d/agente
d/group
d/anon-group
:bad
"foo"
{})))
(testing
"verb"
(testing "is required"
(should-not-satisfy ::xs/statement (dissoc long-statement "verb")))
(testing "is a Verb"
(key-should-satisfy+ ::xs/statement
long-statement
"verb"
d/verb
:bad
[]
{}
d/activity
d/agente)))
(testing
"object"
(testing "is required"
(should-not-satisfy ::xs/statement
(dissoc long-statement "object")))
(testing "is an Activity, Agent, Group, StatementRef, or sub-statement"
(key-should-satisfy+ ::xs/statement
;; use one without context so we can swap non-activity objects
(dissoc long-statement "context")
"object"
;; Activity is the default
(dissoc d/activity "objectType")
;; explicit Activity ObjectType
d/activity
d/agente
d/group
d/anon-group
d/sub-statement
:bad
[]
{})))
(testing
"context"
(testing "when the statement object is an activity"
(let [statement (assoc-in long-statement ["context" "revision"] "whatevs")]
(testing "can have the platform and revision properties"
(should-satisfy ::xs/statement statement))))
(testing "when the statement object is not an activity"
(let [statement d/void-statement]
(testing "cannot have the platform and revision properties"
(should-satisfy+ ::xs/statement
statement
:bad
(assoc statement "context" {"platform" "Apple Newton"})
(assoc statement "context" {"revision" "whatevs"}))))))
(testing "is satisfied by all ADL example statements"
(should-satisfy+ ::xs/statement
simple-statement
long-statement
d/completion-statement
d/void-statement))
(testing "checks for the proper object objectType on voiding statements"
(should-satisfy+ ::xs/statement
d/void-statement
:bad
{"actor" {"objectType" "Agent"
"name" "Example Admin"
"mbox" "mailto:admin@example.adlnet.gov"}
"verb" {"id" "http://adlnet.gov/expapi/verbs/voided"
"display" {"en-US" "voided"}}
"object" {"id" "http://example.com/activities/1"}})))
(deftest statements-test
(testing "generic statememt batch"
(should-satisfy+ ::xs/statements
[simple-statement]
[simple-statement long-statement]
[]))
(testing "LRS retrieval statement batch"
(should-satisfy+ ::xs/lrs-statements
[d/statement] ; This statement has ID and other required fields
[])))