Skip to content

Commit 58554fa

Browse files
committed
add tests validating service version and required links
1 parent 78a2648 commit 58554fa

File tree

2 files changed

+69
-1
lines changed

2 files changed

+69
-1
lines changed

node_registry.schema.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@
126126
"properties": {
127127
"rel": {
128128
"type": "string",
129-
"const": "service"
129+
"const": "service-doc"
130130
}
131131
}
132132
}

tests/test_registry.py

+68
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import copy
12
import os
23

34
import json
@@ -26,6 +27,33 @@ def registry_content(request):
2627
return content
2728

2829

30+
@pytest.fixture
31+
def registry_content_with_services(registry_content):
32+
any_svc_key = list(registry_content)[0]
33+
registry_content["test"] = copy.deepcopy(registry_content[any_svc_key])
34+
registry_content["test"]["services"] = [
35+
{
36+
"name": "test-service",
37+
"keywords": ["other"],
38+
"description": "test service",
39+
"version": "1.2.3",
40+
"links": [
41+
{
42+
"rel": "service",
43+
"href": "https://example.com/test",
44+
"type": "application/json"
45+
},
46+
{
47+
"rel": "service-doc",
48+
"href": "https://readthedocs.com/example",
49+
"type": "text/html"
50+
}
51+
]
52+
}
53+
]
54+
return registry_content
55+
56+
2957
@pytest.fixture
3058
def schema_content(request):
3159
"""Return the content contained in node_registry.schema.json"""
@@ -38,3 +66,43 @@ def schema_content(request):
3866

3967
def test_valid_registry(registry_content, schema_content):
4068
jsonschema.validate(registry_content, schema_content)
69+
70+
71+
def test_services_good_version(registry_content_with_services, schema_content):
72+
jsonschema.validate(registry_content_with_services, schema_content)
73+
74+
75+
def test_services_bad_version(registry_content_with_services, schema_content):
76+
registry_content_with_services["test"]["services"][0]["version"] = "bad_version"
77+
with pytest.raises(jsonschema.exceptions.ValidationError) as exc:
78+
jsonschema.validate(registry_content_with_services, schema_content)
79+
assert "bad_version" in exc.value.message
80+
assert list(exc.value.path) == ["test", "services", 0, "version"]
81+
assert list(exc.value.schema_path)[-6:] == ["properties", "services", "items", "properties", "version", "pattern"]
82+
83+
84+
def test_services_bad_links(registry_content_with_services, schema_content):
85+
registry_content_with_services["test"]["services"][0].pop("links")
86+
with pytest.raises(jsonschema.exceptions.ValidationError) as exc:
87+
jsonschema.validate(registry_content_with_services, schema_content)
88+
assert exc.value.message == "'links' is a required property"
89+
assert list(exc.value.path) == ["test", "services", 0]
90+
assert list(exc.value.schema_path)[-4:] == ["properties", "services", "items", "required"]
91+
92+
93+
@pytest.mark.parametrize(
94+
["required_link_rel", "expected_link_pos"],
95+
[
96+
("service", 0),
97+
("service-doc", 1),
98+
]
99+
)
100+
def test_services_missing_link(registry_content_with_services, schema_content, required_link_rel, expected_link_pos):
101+
links = registry_content_with_services["test"]["services"][0]["links"]
102+
links = [link for link in links if link["rel"] != required_link_rel]
103+
registry_content_with_services["test"]["services"][0]["links"] = links
104+
with pytest.raises(jsonschema.exceptions.ValidationError) as exc:
105+
jsonschema.validate(registry_content_with_services, schema_content)
106+
assert "does not contain" in exc.value.message
107+
assert list(exc.value.path) == ["test", "services", 0, "links"]
108+
assert list(exc.value.schema_path)[-4:] == ["links", "allOf", expected_link_pos, "contains"]

0 commit comments

Comments
 (0)