generated from ioggstream/draft-polli-foo-media-type
-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_oasld.py
114 lines (97 loc) · 2.95 KB
/
test_oasld.py
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
import json
import logging
from copy import deepcopy
from pathlib import Path
import pytest
import yaml
from oasld import Instance, RefResolver
@pytest.fixture
def resolver(schema_yaml):
return RefResolver(schema_yaml)
log = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)
@pytest.fixture
def schema_yaml():
return schemas()
def schemas():
return yaml.safe_load(Path("schemas.yaml").read_text())
@pytest.mark.parametrize(
"testcase,expected",
[
(
dict(
instance={},
schema={"x-jsonld-context": "http://foo.example", "type": "object"},
context=Instance.NO_CONTEXT,
),
Instance.NO_CONTEXT,
),
(
dict(
instance={},
schema={"x-jsonld-context": "http://foo.example", "type": "object"},
),
"http://foo.example",
),
(
dict(
instance={},
schema={"x-jsonld-context": "http://foo.example", "type": "object"},
context={"foo": "bar"},
),
"http://foo.example",
),
(
dict(
instance={"givenName": "Mario"},
schema={"x-jsonld-context": "http://foo.example", "type": "object"},
context={"@container": "@set"},
parent=Instance({}, {}),
),
{
"@container": "@set",
"@context": "http://foo.example",
},
),
(
dict(
instance={"foo": 1, "biz": 2},
schema={"x-jsonld-context": {"biz": "pop"}, "type": "object"},
context={"@container": "@set"},
parent=Instance({}, {}),
),
{"@container": "@set", "@context": {"biz": "pop"}},
),
],
)
def test_init(testcase, expected):
context = testcase.pop("context", None)
i = Instance(
**testcase,
context=deepcopy(context) if context is not Instance.NO_CONTEXT else context,
)
res = (
i.subentry_context_ref
if i.is_subentry or i.is_decontext()
else i.subentry_context_ref.get("@context")
)
assert res == expected
@pytest.mark.parametrize(
"schema_name",
[x for x in schemas()],
)
def test_edu(resolver, schema_yaml, schema_name):
from pyld import jsonld
schema = schema_yaml[schema_name]
instance = harn_schema(schema, resolver)
with open(f"tmp.{schema_name}.json", "w") as fp:
json.dump(instance.ld, fp=fp, indent=2)
with open(f"tmp.{schema_name}.expanded.json", "w") as fp:
json.dump(jsonld.expand(instance.ld), fp=fp, indent=2)
def harn_schema(schema, resolver):
example = schema["example"]
instance = Instance(example, schema)
instance.safe_mode = False
instance.process_instance(resolver=resolver)
log.info("\n" + json.dumps(instance.ld))
return instance