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
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
|
from rpds import HashTrieMap
import pytest
from referencing import Anchor, Registry, Resource, Specification, exceptions
from referencing.jsonschema import DRAFT202012
ID_AND_CHILDREN = Specification(
name="id-and-children",
id_of=lambda contents: contents.get("ID"),
subresources_of=lambda contents: contents.get("children", []),
anchors_in=lambda specification, contents: [
Anchor(
name=name,
resource=specification.create_resource(contents=each),
)
for name, each in contents.get("anchors", {}).items()
],
maybe_in_subresource=lambda segments, resolver, subresource: (
resolver.in_subresource(subresource)
if not len(segments) % 2
and all(each == "children" for each in segments[::2])
else resolver
),
)
def blow_up(uri): # pragma: no cover
"""
A retriever suitable for use in tests which expect it never to be used.
"""
raise RuntimeError("This retrieve function expects to never be called!")
class TestRegistry:
def test_with_resource(self):
"""
Adding a resource to the registry then allows re-retrieving it.
"""
resource = Resource.opaque(contents={"foo": "bar"})
uri = "urn:example"
registry = Registry().with_resource(uri=uri, resource=resource)
assert registry[uri] is resource
def test_with_resources(self):
"""
Adding multiple resources to the registry is like adding each one.
"""
one = Resource.opaque(contents={})
two = Resource(contents={"foo": "bar"}, specification=ID_AND_CHILDREN)
registry = Registry().with_resources(
[
("http://example.com/1", one),
("http://example.com/foo/bar", two),
],
)
assert registry == Registry().with_resource(
uri="http://example.com/1",
resource=one,
).with_resource(
uri="http://example.com/foo/bar",
resource=two,
)
def test_matmul_resource(self):
uri = "urn:example:resource"
resource = ID_AND_CHILDREN.create_resource({"ID": uri, "foo": 12})
registry = resource @ Registry()
assert registry == Registry().with_resource(uri, resource)
def test_matmul_many_resources(self):
one_uri = "urn:example:one"
one = ID_AND_CHILDREN.create_resource({"ID": one_uri, "foo": 12})
two_uri = "urn:example:two"
two = ID_AND_CHILDREN.create_resource({"ID": two_uri, "foo": 12})
registry = [one, two] @ Registry()
assert registry == Registry().with_resources(
[(one_uri, one), (two_uri, two)],
)
def test_matmul_resource_without_id(self):
resource = Resource.opaque(contents={"foo": "bar"})
with pytest.raises(exceptions.NoInternalID) as e:
resource @ Registry()
assert e.value == exceptions.NoInternalID(resource=resource)
def test_with_contents_from_json_schema(self):
uri = "urn:example"
schema = {"$schema": "https://json-schema.org/draft/2020-12/schema"}
registry = Registry().with_contents([(uri, schema)])
expected = Resource(contents=schema, specification=DRAFT202012)
assert registry[uri] == expected
def test_with_contents_and_default_specification(self):
uri = "urn:example"
registry = Registry().with_contents(
[(uri, {"foo": "bar"})],
default_specification=Specification.OPAQUE,
)
assert registry[uri] == Resource.opaque({"foo": "bar"})
def test_len(self):
total = 5
registry = Registry().with_contents(
[(str(i), {"foo": "bar"}) for i in range(total)],
default_specification=Specification.OPAQUE,
)
assert len(registry) == total
def test_bool_empty(self):
assert not Registry()
def test_bool_not_empty(self):
registry = Registry().with_contents(
[(str(i), {"foo": "bar"}) for i in range(3)],
default_specification=Specification.OPAQUE,
)
assert registry
def test_iter(self):
registry = Registry().with_contents(
[(str(i), {"foo": "bar"}) for i in range(8)],
default_specification=Specification.OPAQUE,
)
assert set(registry) == {str(i) for i in range(8)}
def test_crawl_still_has_top_level_resource(self):
resource = Resource.opaque({"foo": "bar"})
uri = "urn:example"
registry = Registry({uri: resource}).crawl()
assert registry[uri] is resource
def test_crawl_finds_a_subresource(self):
child_id = "urn:child"
root = ID_AND_CHILDREN.create_resource(
{"ID": "urn:root", "children": [{"ID": child_id, "foo": 12}]},
)
registry = root @ Registry()
with pytest.raises(LookupError):
registry[child_id]
expected = ID_AND_CHILDREN.create_resource({"ID": child_id, "foo": 12})
assert registry.crawl()[child_id] == expected
def test_crawl_finds_anchors_with_id(self):
resource = ID_AND_CHILDREN.create_resource(
{"ID": "urn:bar", "anchors": {"foo": 12}},
)
registry = resource @ Registry()
assert registry.crawl().anchor(resource.id(), "foo").value == Anchor(
name="foo",
resource=ID_AND_CHILDREN.create_resource(12),
)
def test_crawl_finds_anchors_no_id(self):
resource = ID_AND_CHILDREN.create_resource({"anchors": {"foo": 12}})
registry = Registry().with_resource("urn:root", resource)
assert registry.crawl().anchor("urn:root", "foo").value == Anchor(
name="foo",
resource=ID_AND_CHILDREN.create_resource(12),
)
def test_contents(self):
resource = Resource.opaque({"foo": "bar"})
uri = "urn:example"
registry = Registry().with_resource(uri, resource)
assert registry.contents(uri) == {"foo": "bar"}
def test_getitem_strips_empty_fragments(self):
uri = "http://example.com/"
resource = ID_AND_CHILDREN.create_resource({"ID": uri + "#"})
registry = resource @ Registry()
assert registry[uri] == registry[uri + "#"] == resource
def test_contents_strips_empty_fragments(self):
uri = "http://example.com/"
resource = ID_AND_CHILDREN.create_resource({"ID": uri + "#"})
registry = resource @ Registry()
assert (
registry.contents(uri)
== registry.contents(uri + "#")
== {"ID": uri + "#"}
)
def test_contents_nonexistent_resource(self):
registry = Registry()
with pytest.raises(exceptions.NoSuchResource) as e:
registry.contents("urn:example")
assert e.value == exceptions.NoSuchResource(ref="urn:example")
def test_crawled_anchor(self):
resource = ID_AND_CHILDREN.create_resource({"anchors": {"foo": "bar"}})
registry = Registry().with_resource("urn:example", resource)
retrieved = registry.anchor("urn:example", "foo")
assert retrieved.value == Anchor(
name="foo",
resource=ID_AND_CHILDREN.create_resource("bar"),
)
assert retrieved.registry == registry.crawl()
def test_anchor_in_nonexistent_resource(self):
registry = Registry()
with pytest.raises(exceptions.NoSuchResource) as e:
registry.anchor("urn:example", "foo")
assert e.value == exceptions.NoSuchResource(ref="urn:example")
def test_init(self):
one = Resource.opaque(contents={})
two = ID_AND_CHILDREN.create_resource({"foo": "bar"})
registry = Registry(
{
"http://example.com/1": one,
"http://example.com/foo/bar": two,
},
)
assert (
registry
== Registry()
.with_resources(
[
("http://example.com/1", one),
("http://example.com/foo/bar", two),
],
)
.crawl()
)
def test_dict_conversion(self):
"""
Passing a `dict` to `Registry` gets converted to a `HashTrieMap`.
So continuing to use the registry works.
"""
one = Resource.opaque(contents={})
two = ID_AND_CHILDREN.create_resource({"foo": "bar"})
registry = Registry(
{"http://example.com/1": one},
).with_resource("http://example.com/foo/bar", two)
assert (
registry.crawl()
== Registry()
.with_resources(
[
("http://example.com/1", one),
("http://example.com/foo/bar", two),
],
)
.crawl()
)
def test_no_such_resource(self):
registry = Registry()
with pytest.raises(exceptions.NoSuchResource) as e:
registry["urn:bigboom"]
assert e.value == exceptions.NoSuchResource(ref="urn:bigboom")
def test_combine(self):
one = Resource.opaque(contents={})
two = ID_AND_CHILDREN.create_resource({"foo": "bar"})
three = ID_AND_CHILDREN.create_resource({"baz": "quux"})
four = ID_AND_CHILDREN.create_resource({"anchors": {"foo": 12}})
first = Registry({"http://example.com/1": one})
second = Registry().with_resource("http://example.com/foo/bar", two)
third = Registry(
{
"http://example.com/1": one,
"http://example.com/baz": three,
},
)
fourth = (
Registry()
.with_resource(
"http://example.com/foo/quux",
four,
)
.crawl()
)
assert first.combine(second, third, fourth) == Registry(
[
("http://example.com/1", one),
("http://example.com/baz", three),
("http://example.com/foo/quux", four),
],
anchors=HashTrieMap(
{
("http://example.com/foo/quux", "foo"): Anchor(
name="foo",
resource=ID_AND_CHILDREN.create_resource(12),
),
},
),
).with_resource("http://example.com/foo/bar", two)
def test_combine_self(self):
"""
Combining a registry with itself short-circuits.
This is a performance optimization -- otherwise we do lots more work
(in jsonschema this seems to correspond to making the test suite take
*3x* longer).
"""
registry = Registry({"urn:foo": "bar"})
assert registry.combine(registry) is registry
def test_combine_with_uncrawled_resources(self):
one = Resource.opaque(contents={})
two = ID_AND_CHILDREN.create_resource({"foo": "bar"})
three = ID_AND_CHILDREN.create_resource({"baz": "quux"})
first = Registry().with_resource("http://example.com/1", one)
second = Registry().with_resource("http://example.com/foo/bar", two)
third = Registry(
{
"http://example.com/1": one,
"http://example.com/baz": three,
},
)
expected = Registry(
[
("http://example.com/1", one),
("http://example.com/foo/bar", two),
("http://example.com/baz", three),
],
)
combined = first.combine(second, third)
assert combined != expected
assert combined.crawl() == expected
def test_combine_with_single_retrieve(self):
one = Resource.opaque(contents={})
two = ID_AND_CHILDREN.create_resource({"foo": "bar"})
three = ID_AND_CHILDREN.create_resource({"baz": "quux"})
def retrieve(uri): # pragma: no cover
pass
first = Registry().with_resource("http://example.com/1", one)
second = Registry(
retrieve=retrieve,
).with_resource("http://example.com/2", two)
third = Registry().with_resource("http://example.com/3", three)
assert first.combine(second, third) == Registry(
retrieve=retrieve,
).with_resources(
[
("http://example.com/1", one),
("http://example.com/2", two),
("http://example.com/3", three),
],
)
assert second.combine(first, third) == Registry(
retrieve=retrieve,
).with_resources(
[
("http://example.com/1", one),
("http://example.com/2", two),
("http://example.com/3", three),
],
)
def test_combine_with_common_retrieve(self):
one = Resource.opaque(contents={})
two = ID_AND_CHILDREN.create_resource({"foo": "bar"})
three = ID_AND_CHILDREN.create_resource({"baz": "quux"})
def retrieve(uri): # pragma: no cover
pass
first = Registry(retrieve=retrieve).with_resource(
"http://example.com/1",
one,
)
second = Registry(
retrieve=retrieve,
).with_resource("http://example.com/2", two)
third = Registry(retrieve=retrieve).with_resource(
"http://example.com/3",
three,
)
assert first.combine(second, third) == Registry(
retrieve=retrieve,
).with_resources(
[
("http://example.com/1", one),
("http://example.com/2", two),
("http://example.com/3", three),
],
)
assert second.combine(first, third) == Registry(
retrieve=retrieve,
).with_resources(
[
("http://example.com/1", one),
("http://example.com/2", two),
("http://example.com/3", three),
],
)
def test_combine_conflicting_retrieve(self):
one = Resource.opaque(contents={})
two = ID_AND_CHILDREN.create_resource({"foo": "bar"})
three = ID_AND_CHILDREN.create_resource({"baz": "quux"})
def foo_retrieve(uri): # pragma: no cover
pass
def bar_retrieve(uri): # pragma: no cover
pass
first = Registry(retrieve=foo_retrieve).with_resource(
"http://example.com/1",
one,
)
second = Registry().with_resource("http://example.com/2", two)
third = Registry(retrieve=bar_retrieve).with_resource(
"http://example.com/3",
three,
)
with pytest.raises(Exception, match="conflict.*retriev"):
first.combine(second, third)
def test_remove(self):
one = Resource.opaque(contents={})
two = ID_AND_CHILDREN.create_resource({"foo": "bar"})
registry = Registry({"urn:foo": one, "urn:bar": two})
assert registry.remove("urn:foo") == Registry({"urn:bar": two})
def test_remove_uncrawled(self):
one = Resource.opaque(contents={})
two = ID_AND_CHILDREN.create_resource({"foo": "bar"})
registry = Registry().with_resources(
[("urn:foo", one), ("urn:bar", two)],
)
assert registry.remove("urn:foo") == Registry().with_resource(
"urn:bar",
two,
)
def test_remove_with_anchors(self):
one = Resource.opaque(contents={})
two = ID_AND_CHILDREN.create_resource({"anchors": {"foo": "bar"}})
registry = (
Registry()
.with_resources(
[("urn:foo", one), ("urn:bar", two)],
)
.crawl()
)
assert (
registry.remove("urn:bar")
== Registry()
.with_resource(
"urn:foo",
one,
)
.crawl()
)
def test_remove_nonexistent_uri(self):
with pytest.raises(exceptions.NoSuchResource) as e:
Registry().remove("urn:doesNotExist")
assert e.value == exceptions.NoSuchResource(ref="urn:doesNotExist")
def test_retrieve(self):
foo = Resource.opaque({"foo": "bar"})
registry = Registry(retrieve=lambda uri: foo)
assert registry.get_or_retrieve("urn:example").value == foo
def test_retrieve_arbitrary_exception(self):
foo = Resource.opaque({"foo": "bar"})
def retrieve(uri):
if uri == "urn:succeed":
return foo
raise Exception("Oh no!")
registry = Registry(retrieve=retrieve)
assert registry.get_or_retrieve("urn:succeed").value == foo
with pytest.raises(exceptions.Unretrievable):
registry.get_or_retrieve("urn:uhoh")
def test_retrieve_no_such_resource(self):
foo = Resource.opaque({"foo": "bar"})
def retrieve(uri):
if uri == "urn:succeed":
return foo
raise exceptions.NoSuchResource(ref=uri)
registry = Registry(retrieve=retrieve)
assert registry.get_or_retrieve("urn:succeed").value == foo
with pytest.raises(exceptions.NoSuchResource):
registry.get_or_retrieve("urn:uhoh")
def test_retrieve_cannot_determine_specification(self):
def retrieve(uri):
return Resource.from_contents({})
registry = Registry(retrieve=retrieve)
with pytest.raises(exceptions.CannotDetermineSpecification):
registry.get_or_retrieve("urn:uhoh")
def test_retrieve_already_available_resource(self):
foo = Resource.opaque({"foo": "bar"})
registry = Registry({"urn:example": foo}, retrieve=blow_up)
assert registry["urn:example"] == foo
assert registry.get_or_retrieve("urn:example").value == foo
def test_retrieve_first_checks_crawlable_resource(self):
child = ID_AND_CHILDREN.create_resource({"ID": "urn:child", "foo": 12})
root = ID_AND_CHILDREN.create_resource({"children": [child.contents]})
registry = Registry(retrieve=blow_up).with_resource("urn:root", root)
assert registry.crawl()["urn:child"] == child
def test_resolver(self):
one = Resource.opaque(contents={})
registry = Registry({"http://example.com": one})
resolver = registry.resolver(base_uri="http://example.com")
assert resolver.lookup("#").contents == {}
def test_resolver_with_root_identified(self):
root = ID_AND_CHILDREN.create_resource({"ID": "http://example.com"})
resolver = Registry().resolver_with_root(root)
assert resolver.lookup("http://example.com").contents == root.contents
assert resolver.lookup("#").contents == root.contents
def test_resolver_with_root_unidentified(self):
root = Resource.opaque(contents={})
resolver = Registry().resolver_with_root(root)
assert resolver.lookup("#").contents == root.contents
def test_repr(self):
one = Resource.opaque(contents={})
two = ID_AND_CHILDREN.create_resource({"foo": "bar"})
registry = Registry().with_resources(
[
("http://example.com/1", one),
("http://example.com/foo/bar", two),
],
)
assert repr(registry) == "<Registry (2 uncrawled resources)>"
assert repr(registry.crawl()) == "<Registry (2 resources)>"
def test_repr_mixed_crawled(self):
one = Resource.opaque(contents={})
two = ID_AND_CHILDREN.create_resource({"foo": "bar"})
registry = (
Registry(
{"http://example.com/1": one},
)
.crawl()
.with_resource(uri="http://example.com/foo/bar", resource=two)
)
assert repr(registry) == "<Registry (2 resources, 1 uncrawled)>"
def test_repr_one_resource(self):
registry = Registry().with_resource(
uri="http://example.com/1",
resource=Resource.opaque(contents={}),
)
assert repr(registry) == "<Registry (1 uncrawled resource)>"
def test_repr_empty(self):
assert repr(Registry()) == "<Registry (0 resources)>"
class TestResource:
def test_from_contents_from_json_schema(self):
schema = {"$schema": "https://json-schema.org/draft/2020-12/schema"}
resource = Resource.from_contents(schema)
assert resource == Resource(contents=schema, specification=DRAFT202012)
def test_from_contents_with_no_discernible_information(self):
"""
Creating a resource with no discernible way to see what
specification it belongs to (e.g. no ``$schema`` keyword for JSON
Schema) raises an error.
"""
with pytest.raises(exceptions.CannotDetermineSpecification):
Resource.from_contents({"foo": "bar"})
def test_from_contents_with_no_discernible_information_and_default(self):
resource = Resource.from_contents(
{"foo": "bar"},
default_specification=Specification.OPAQUE,
)
assert resource == Resource.opaque(contents={"foo": "bar"})
def test_from_contents_unneeded_default(self):
schema = {"$schema": "https://json-schema.org/draft/2020-12/schema"}
resource = Resource.from_contents(
schema,
default_specification=Specification.OPAQUE,
)
assert resource == Resource(
contents=schema,
specification=DRAFT202012,
)
def test_non_mapping_from_contents(self):
resource = Resource.from_contents(
True,
default_specification=ID_AND_CHILDREN,
)
assert resource == Resource(
contents=True,
specification=ID_AND_CHILDREN,
)
def test_from_contents_with_fallback(self):
resource = Resource.from_contents(
{"foo": "bar"},
default_specification=Specification.OPAQUE,
)
assert resource == Resource.opaque(contents={"foo": "bar"})
def test_id_delegates_to_specification(self):
specification = Specification(
name="",
id_of=lambda contents: "urn:fixedID",
subresources_of=lambda contents: [],
anchors_in=lambda specification, contents: [],
maybe_in_subresource=(
lambda segments, resolver, subresource: resolver
),
)
resource = Resource(
contents={"foo": "baz"},
specification=specification,
)
assert resource.id() == "urn:fixedID"
def test_id_strips_empty_fragment(self):
uri = "http://example.com/"
root = ID_AND_CHILDREN.create_resource({"ID": uri + "#"})
assert root.id() == uri
def test_subresources_delegates_to_specification(self):
resource = ID_AND_CHILDREN.create_resource({"children": [{}, 12]})
assert list(resource.subresources()) == [
ID_AND_CHILDREN.create_resource(each) for each in [{}, 12]
]
def test_subresource_with_different_specification(self):
schema = {"$schema": "https://json-schema.org/draft/2020-12/schema"}
resource = ID_AND_CHILDREN.create_resource({"children": [schema]})
assert list(resource.subresources()) == [
DRAFT202012.create_resource(schema),
]
def test_anchors_delegates_to_specification(self):
resource = ID_AND_CHILDREN.create_resource(
{"anchors": {"foo": {}, "bar": 1, "baz": ""}},
)
assert list(resource.anchors()) == [
Anchor(name="foo", resource=ID_AND_CHILDREN.create_resource({})),
Anchor(name="bar", resource=ID_AND_CHILDREN.create_resource(1)),
Anchor(name="baz", resource=ID_AND_CHILDREN.create_resource("")),
]
def test_pointer_to_mapping(self):
resource = Resource.opaque(contents={"foo": "baz"})
resolver = Registry().resolver()
assert resource.pointer("/foo", resolver=resolver).contents == "baz"
def test_pointer_to_array(self):
resource = Resource.opaque(contents={"foo": {"bar": [3]}})
resolver = Registry().resolver()
assert resource.pointer("/foo/bar/0", resolver=resolver).contents == 3
def test_root_pointer(self):
contents = {"foo": "baz"}
resource = Resource.opaque(contents=contents)
resolver = Registry().resolver()
assert resource.pointer("", resolver=resolver).contents == contents
def test_opaque(self):
contents = {"foo": "bar"}
assert Resource.opaque(contents) == Resource(
contents=contents,
specification=Specification.OPAQUE,
)
class TestResolver:
def test_lookup_exact_uri(self):
resource = Resource.opaque(contents={"foo": "baz"})
resolver = Registry({"http://example.com/1": resource}).resolver()
resolved = resolver.lookup("http://example.com/1")
assert resolved.contents == resource.contents
def test_lookup_subresource(self):
root = ID_AND_CHILDREN.create_resource(
{
"ID": "http://example.com/",
"children": [
{"ID": "http://example.com/a", "foo": 12},
],
},
)
registry = root @ Registry()
resolved = registry.resolver().lookup("http://example.com/a")
assert resolved.contents == {"ID": "http://example.com/a", "foo": 12}
def test_lookup_anchor_with_id(self):
root = ID_AND_CHILDREN.create_resource(
{
"ID": "http://example.com/",
"anchors": {"foo": 12},
},
)
registry = root @ Registry()
resolved = registry.resolver().lookup("http://example.com/#foo")
assert resolved.contents == 12
def test_lookup_anchor_without_id(self):
root = ID_AND_CHILDREN.create_resource({"anchors": {"foo": 12}})
resolver = Registry().with_resource("urn:example", root).resolver()
resolved = resolver.lookup("urn:example#foo")
assert resolved.contents == 12
def test_lookup_unknown_reference(self):
resolver = Registry().resolver()
ref = "http://example.com/does/not/exist"
with pytest.raises(exceptions.Unresolvable) as e:
resolver.lookup(ref)
assert e.value == exceptions.Unresolvable(ref=ref)
def test_lookup_non_existent_pointer(self):
resource = Resource.opaque({"foo": {}})
resolver = Registry({"http://example.com/1": resource}).resolver()
ref = "http://example.com/1#/foo/bar"
with pytest.raises(exceptions.Unresolvable) as e:
resolver.lookup(ref)
assert e.value == exceptions.PointerToNowhere(
ref="/foo/bar",
resource=resource,
)
assert str(e.value) == "'/foo/bar' does not exist within {'foo': {}}"
def test_lookup_non_existent_pointer_to_array_index(self):
resource = Resource.opaque([1, 2, 4, 8])
resolver = Registry({"http://example.com/1": resource}).resolver()
ref = "http://example.com/1#/10"
with pytest.raises(exceptions.Unresolvable) as e:
resolver.lookup(ref)
assert e.value == exceptions.PointerToNowhere(
ref="/10",
resource=resource,
)
def test_lookup_pointer_to_empty_string(self):
resolver = Registry().resolver_with_root(Resource.opaque({"": {}}))
assert resolver.lookup("#/").contents == {}
def test_lookup_non_existent_pointer_to_empty_string(self):
resource = Resource.opaque({"foo": {}})
resolver = Registry().resolver_with_root(resource)
with pytest.raises(
exceptions.Unresolvable,
match="^'/' does not exist within {'foo': {}}.*'#'",
) as e:
resolver.lookup("#/")
assert e.value == exceptions.PointerToNowhere(
ref="/",
resource=resource,
)
def test_lookup_non_existent_anchor(self):
root = ID_AND_CHILDREN.create_resource({"anchors": {}})
resolver = Registry().with_resource("urn:example", root).resolver()
resolved = resolver.lookup("urn:example")
assert resolved.contents == root.contents
ref = "urn:example#noSuchAnchor"
with pytest.raises(exceptions.Unresolvable) as e:
resolver.lookup(ref)
assert "'noSuchAnchor' does not exist" in str(e.value)
assert e.value == exceptions.NoSuchAnchor(
ref="urn:example",
resource=root,
anchor="noSuchAnchor",
)
def test_lookup_invalid_JSON_pointerish_anchor(self):
resolver = Registry().resolver_with_root(
ID_AND_CHILDREN.create_resource(
{
"ID": "http://example.com/",
"foo": {"bar": 12},
},
),
)
valid = resolver.lookup("#/foo/bar")
assert valid.contents == 12
with pytest.raises(exceptions.InvalidAnchor) as e:
resolver.lookup("#foo/bar")
assert " '#/foo/bar'" in str(e.value)
def test_lookup_retrieved_resource(self):
resource = Resource.opaque(contents={"foo": "baz"})
resolver = Registry(retrieve=lambda uri: resource).resolver()
resolved = resolver.lookup("http://example.com/")
assert resolved.contents == resource.contents
def test_lookup_failed_retrieved_resource(self):
"""
Unretrievable exceptions are also wrapped in Unresolvable.
"""
uri = "http://example.com/"
registry = Registry(retrieve=blow_up)
with pytest.raises(exceptions.Unretrievable):
registry.get_or_retrieve(uri)
resolver = registry.resolver()
with pytest.raises(exceptions.Unresolvable):
resolver.lookup(uri)
def test_repeated_lookup_from_retrieved_resource(self):
"""
A (custom-)retrieved resource is added to the registry returned by
looking it up.
"""
resource = Resource.opaque(contents={"foo": "baz"})
once = [resource]
def retrieve(uri):
return once.pop()
resolver = Registry(retrieve=retrieve).resolver()
resolved = resolver.lookup("http://example.com/")
assert resolved.contents == resource.contents
resolved = resolved.resolver.lookup("http://example.com/")
assert resolved.contents == resource.contents
def test_repeated_anchor_lookup_from_retrieved_resource(self):
resource = Resource.opaque(contents={"foo": "baz"})
once = [resource]
def retrieve(uri):
return once.pop()
resolver = Registry(retrieve=retrieve).resolver()
resolved = resolver.lookup("http://example.com/")
assert resolved.contents == resource.contents
resolved = resolved.resolver.lookup("#")
assert resolved.contents == resource.contents
# FIXME: The tests below aren't really representable in the current
# suite, though we should probably think of ways to do so.
def test_in_subresource(self):
root = ID_AND_CHILDREN.create_resource(
{
"ID": "http://example.com/",
"children": [
{
"ID": "child/",
"children": [{"ID": "grandchild"}],
},
],
},
)
registry = root @ Registry()
resolver = registry.resolver()
first = resolver.lookup("http://example.com/")
assert first.contents == root.contents
with pytest.raises(exceptions.Unresolvable):
first.resolver.lookup("grandchild")
sub = first.resolver.in_subresource(
ID_AND_CHILDREN.create_resource(first.contents["children"][0]),
)
second = sub.lookup("grandchild")
assert second.contents == {"ID": "grandchild"}
def test_in_pointer_subresource(self):
root = ID_AND_CHILDREN.create_resource(
{
"ID": "http://example.com/",
"children": [
{
"ID": "child/",
"children": [{"ID": "grandchild"}],
},
],
},
)
registry = root @ Registry()
resolver = registry.resolver()
first = resolver.lookup("http://example.com/")
assert first.contents == root.contents
with pytest.raises(exceptions.Unresolvable):
first.resolver.lookup("grandchild")
second = first.resolver.lookup("#/children/0")
third = second.resolver.lookup("grandchild")
assert third.contents == {"ID": "grandchild"}
def test_dynamic_scope(self):
one = ID_AND_CHILDREN.create_resource(
{
"ID": "http://example.com/",
"children": [
{
"ID": "child/",
"children": [{"ID": "grandchild"}],
},
],
},
)
two = ID_AND_CHILDREN.create_resource(
{
"ID": "http://example.com/two",
"children": [{"ID": "two-child/"}],
},
)
registry = [one, two] @ Registry()
resolver = registry.resolver()
first = resolver.lookup("http://example.com/")
second = first.resolver.lookup("#/children/0")
third = second.resolver.lookup("grandchild")
fourth = third.resolver.lookup("http://example.com/two")
assert list(fourth.resolver.dynamic_scope()) == [
("http://example.com/child/grandchild", fourth.resolver._registry),
("http://example.com/child/", fourth.resolver._registry),
("http://example.com/", fourth.resolver._registry),
]
assert list(third.resolver.dynamic_scope()) == [
("http://example.com/child/", third.resolver._registry),
("http://example.com/", third.resolver._registry),
]
assert list(second.resolver.dynamic_scope()) == [
("http://example.com/", second.resolver._registry),
]
assert list(first.resolver.dynamic_scope()) == []
class TestSpecification:
def test_create_resource(self):
specification = Specification(
name="",
id_of=lambda contents: "urn:fixedID",
subresources_of=lambda contents: [],
anchors_in=lambda specification, contents: [],
maybe_in_subresource=(
lambda segments, resolver, subresource: resolver
),
)
resource = specification.create_resource(contents={"foo": "baz"})
assert resource == Resource(
contents={"foo": "baz"},
specification=specification,
)
assert resource.id() == "urn:fixedID"
def test_detect_from_json_schema(self):
schema = {"$schema": "https://json-schema.org/draft/2020-12/schema"}
specification = Specification.detect(schema)
assert specification == DRAFT202012
def test_detect_with_no_discernible_information(self):
with pytest.raises(exceptions.CannotDetermineSpecification):
Specification.detect({"foo": "bar"})
def test_detect_with_non_URI_schema(self):
with pytest.raises(exceptions.CannotDetermineSpecification):
Specification.detect({"$schema": 37})
def test_detect_with_no_discernible_information_and_default(self):
specification = Specification.OPAQUE.detect({"foo": "bar"})
assert specification is Specification.OPAQUE
def test_detect_unneeded_default(self):
schema = {"$schema": "https://json-schema.org/draft/2020-12/schema"}
specification = Specification.OPAQUE.detect(schema)
assert specification == DRAFT202012
def test_non_mapping_detect(self):
with pytest.raises(exceptions.CannotDetermineSpecification):
Specification.detect(True)
def test_non_mapping_detect_with_default(self):
specification = ID_AND_CHILDREN.detect(True)
assert specification is ID_AND_CHILDREN
def test_detect_with_fallback(self):
specification = Specification.OPAQUE.detect({"foo": "bar"})
assert specification is Specification.OPAQUE
def test_repr(self):
assert (
repr(ID_AND_CHILDREN) == "<Specification name='id-and-children'>"
)
class TestOpaqueSpecification:
THINGS = [{"foo": "bar"}, True, 37, "foo", object()]
@pytest.mark.parametrize("thing", THINGS)
def test_no_id(self, thing):
"""
An arbitrary thing has no ID.
"""
assert Specification.OPAQUE.id_of(thing) is None
@pytest.mark.parametrize("thing", THINGS)
def test_no_subresources(self, thing):
"""
An arbitrary thing has no subresources.
"""
assert list(Specification.OPAQUE.subresources_of(thing)) == []
@pytest.mark.parametrize("thing", THINGS)
def test_no_anchors(self, thing):
"""
An arbitrary thing has no anchors.
"""
assert list(Specification.OPAQUE.anchors_in(thing)) == []
@pytest.mark.parametrize(
"cls",
[Anchor, Registry, Resource, Specification, exceptions.PointerToNowhere],
)
def test_nonsubclassable(cls):
with pytest.raises(Exception, match="(?i)subclassing"):
class Boom(cls): # pragma: no cover
pass
|