Skip to content

Commit 950f426

Browse files
committed
Avoid having to reference JSON Schema in some more tests.
1 parent 59602f0 commit 950f426

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

referencing/tests/test_core.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
from referencing import Registry, Resource, Specification, exceptions
44
from referencing.jsonschema import DRAFT202012
55

6+
ID_AND_CHILDREN = Specification(
7+
id_of=lambda contents: contents.get("ID"),
8+
subresources_of=lambda contents: contents.get("children", []),
9+
)
10+
611

712
class TestRegistry:
813
def test_with_resource(self):
@@ -21,7 +26,7 @@ def test_with_resources(self):
2126
"""
2227

2328
one = Resource.opaque(contents={})
24-
two = Resource(contents={"foo": "bar"}, specification=DRAFT202012)
29+
two = Resource(contents={"foo": "bar"}, specification=ID_AND_CHILDREN)
2530
registry = Registry().with_resources(
2631
[
2732
("http://example.com/1", one),
@@ -36,7 +41,7 @@ def test_with_resources(self):
3641
resource=two,
3742
)
3843

39-
def test_with_contents(self):
44+
def test_with_contents_from_json_schema(self):
4045
uri = "urn:example"
4146
schema = {"$schema": "https://json-schema.org/draft/2020-12/schema"}
4247
registry = Registry().with_contents([(uri, schema)])
@@ -81,7 +86,7 @@ def test_contents(self):
8186

8287
def test_init(self):
8388
one = Resource.opaque(contents={})
84-
two = Resource(contents={"foo": "bar"}, specification=DRAFT202012)
89+
two = ID_AND_CHILDREN.create_resource({"foo": "bar"})
8590
registry = Registry(
8691
{
8792
"http://example.com/1": one,
@@ -103,7 +108,7 @@ def test_dict_conversion(self):
103108
"""
104109

105110
one = Resource.opaque(contents={})
106-
two = Resource(contents={"foo": "bar"}, specification=DRAFT202012)
111+
two = ID_AND_CHILDREN.create_resource({"foo": "bar"})
107112
registry = Registry(
108113
{"http://example.com/1": one},
109114
).with_resources([("http://example.com/foo/bar", two)])
@@ -116,8 +121,8 @@ def test_dict_conversion(self):
116121

117122
def test_combine(self):
118123
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"})
121126

122127
first = Registry({"http://example.com/1": one})
123128
second = Registry({"http://example.com/foo/bar": two})
@@ -137,7 +142,7 @@ def test_combine(self):
137142

138143
def test_repr(self):
139144
one = Resource.opaque(contents={})
140-
two = Resource(contents={"foo": "bar"}, specification=DRAFT202012)
145+
two = ID_AND_CHILDREN.create_resource({"foo": "bar"})
141146
registry = Registry().with_resources(
142147
[
143148
("http://example.com/1", one),
@@ -194,9 +199,12 @@ def test_from_contents_unneeded_default(self):
194199
def test_non_mapping_from_contents(self):
195200
resource = Resource.from_contents(
196201
True,
197-
default_specification=DRAFT202012,
202+
default_specification=ID_AND_CHILDREN,
203+
)
204+
assert resource == Resource(
205+
contents=True,
206+
specification=ID_AND_CHILDREN,
198207
)
199-
assert resource == Resource(contents=True, specification=DRAFT202012)
200208

201209
def test_from_contents_with_fallback(self):
202210
resource = Resource.from_contents(

0 commit comments

Comments
 (0)