3
3
from referencing import Registry , Resource , Specification , exceptions
4
4
from referencing .jsonschema import DRAFT202012
5
5
6
+ ID_AND_CHILDREN = Specification (
7
+ id_of = lambda contents : contents .get ("ID" ),
8
+ subresources_of = lambda contents : contents .get ("children" , []),
9
+ )
10
+
6
11
7
12
class TestRegistry :
8
13
def test_with_resource (self ):
@@ -21,7 +26,7 @@ def test_with_resources(self):
21
26
"""
22
27
23
28
one = Resource .opaque (contents = {})
24
- two = Resource (contents = {"foo" : "bar" }, specification = DRAFT202012 )
29
+ two = Resource (contents = {"foo" : "bar" }, specification = ID_AND_CHILDREN )
25
30
registry = Registry ().with_resources (
26
31
[
27
32
("http://example.com/1" , one ),
@@ -36,7 +41,7 @@ def test_with_resources(self):
36
41
resource = two ,
37
42
)
38
43
39
- def test_with_contents (self ):
44
+ def test_with_contents_from_json_schema (self ):
40
45
uri = "urn:example"
41
46
schema = {"$schema" : "https://json-schema.org/draft/2020-12/schema" }
42
47
registry = Registry ().with_contents ([(uri , schema )])
@@ -81,7 +86,7 @@ def test_contents(self):
81
86
82
87
def test_init (self ):
83
88
one = Resource .opaque (contents = {})
84
- two = Resource ( contents = {"foo" : "bar" }, specification = DRAFT202012 )
89
+ two = ID_AND_CHILDREN . create_resource ( {"foo" : "bar" })
85
90
registry = Registry (
86
91
{
87
92
"http://example.com/1" : one ,
@@ -103,7 +108,7 @@ def test_dict_conversion(self):
103
108
"""
104
109
105
110
one = Resource .opaque (contents = {})
106
- two = Resource ( contents = {"foo" : "bar" }, specification = DRAFT202012 )
111
+ two = ID_AND_CHILDREN . create_resource ( {"foo" : "bar" })
107
112
registry = Registry (
108
113
{"http://example.com/1" : one },
109
114
).with_resources ([("http://example.com/foo/bar" , two )])
@@ -116,8 +121,8 @@ def test_dict_conversion(self):
116
121
117
122
def test_combine (self ):
118
123
one = Resource .opaque (contents = {})
119
- two = Resource ( contents = {"foo" : "bar" }, specification = DRAFT202012 )
120
- three = Resource ( contents = {"baz" : "quux" }, specification = DRAFT202012 )
124
+ two = ID_AND_CHILDREN . create_resource ( {"foo" : "bar" })
125
+ three = ID_AND_CHILDREN . create_resource ( {"baz" : "quux" })
121
126
122
127
first = Registry ({"http://example.com/1" : one })
123
128
second = Registry ({"http://example.com/foo/bar" : two })
@@ -137,7 +142,7 @@ def test_combine(self):
137
142
138
143
def test_repr (self ):
139
144
one = Resource .opaque (contents = {})
140
- two = Resource ( contents = {"foo" : "bar" }, specification = DRAFT202012 )
145
+ two = ID_AND_CHILDREN . create_resource ( {"foo" : "bar" })
141
146
registry = Registry ().with_resources (
142
147
[
143
148
("http://example.com/1" , one ),
@@ -194,9 +199,12 @@ def test_from_contents_unneeded_default(self):
194
199
def test_non_mapping_from_contents (self ):
195
200
resource = Resource .from_contents (
196
201
True ,
197
- default_specification = DRAFT202012 ,
202
+ default_specification = ID_AND_CHILDREN ,
203
+ )
204
+ assert resource == Resource (
205
+ contents = True ,
206
+ specification = ID_AND_CHILDREN ,
198
207
)
199
- assert resource == Resource (contents = True , specification = DRAFT202012 )
200
208
201
209
def test_from_contents_with_fallback (self ):
202
210
resource = Resource .from_contents (
0 commit comments