comparison planemo/lib/python3.7/site-packages/schema_salad/tests/test_examples.py @ 1:56ad4e20f292 draft

"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
author guerler
date Fri, 31 Jul 2020 00:32:28 -0400
parents
children
comparison
equal deleted inserted replaced
0:d30785e31577 1:56ad4e20f292
1 from __future__ import absolute_import, print_function
2
3 import os
4
5 import six
6
7 import ruamel.yaml
8 from ruamel.yaml.comments import CommentedMap, CommentedSeq
9 import schema_salad.main
10 import schema_salad.ref_resolver
11 import schema_salad.schema
12 from schema_salad.jsonld_context import makerdf
13 from schema_salad.sourceline import SourceLine, cmap
14
15 from .util import get_data
16
17
18 def test_schemas():
19 loader = schema_salad.ref_resolver.Loader({})
20
21 ra, _ = loader.resolve_all(
22 cmap(
23 {
24 u"$schemas": [
25 schema_salad.ref_resolver.file_uri(get_data("tests/EDAM.owl"))
26 ],
27 u"$namespaces": {u"edam": u"http://edamontology.org/"},
28 u"edam:has_format": u"edam:format_1915",
29 }
30 ),
31 "",
32 )
33
34 assert {
35 u"$schemas": [schema_salad.ref_resolver.file_uri(get_data("tests/EDAM.owl"))],
36 u"$namespaces": {u"edam": u"http://edamontology.org/"},
37 u"http://edamontology.org/has_format": u"http://edamontology.org/format_1915",
38 } == ra
39
40
41 def test_self_validate():
42 assert 0 == schema_salad.main.main(argsl=[get_data("metaschema/metaschema.yml")])
43 assert 0 == schema_salad.main.main(
44 argsl=[
45 get_data("metaschema/metaschema.yml"),
46 get_data("metaschema/metaschema.yml"),
47 ]
48 )
49
50
51 def test_avro_regression():
52 assert 0 == schema_salad.main.main(argsl=[get_data("tests/Process.yml")])
53
54
55 def test_jsonld_ctx():
56 ldr, _, _, _ = schema_salad.schema.load_schema(
57 cmap(
58 {
59 "$base": "Y",
60 "name": "X",
61 "$namespaces": {"foo": "http://example.com/foo#"},
62 "$graph": [
63 {"name": "ExampleType", "type": "enum", "symbols": ["asym", "bsym"]}
64 ],
65 }
66 )
67 )
68
69 ra, _ = ldr.resolve_all(cmap({"foo:bar": "asym"}), "X")
70
71 assert ra == {"http://example.com/foo#bar": "asym"}
72
73
74 def test_idmap():
75 ldr = schema_salad.ref_resolver.Loader({})
76 ldr.add_context(
77 {
78 "inputs": {
79 "@id": "http://example.com/inputs",
80 "mapSubject": "id",
81 "mapPredicate": "a",
82 },
83 "outputs": {"@type": "@id", "identity": True},
84 "id": "@id",
85 }
86 )
87
88 ra, _ = ldr.resolve_all(
89 cmap(
90 {
91 "id": "stuff",
92 "inputs": {"zip": 1, "zing": 2},
93 "outputs": ["out"],
94 "other": {"n": 9},
95 }
96 ),
97 "http://example2.com/",
98 )
99
100 assert "http://example2.com/#stuff" == ra["id"]
101 for item in ra["inputs"]:
102 if item["a"] == 2:
103 assert "http://example2.com/#stuff/zing" == item["id"]
104 else:
105 assert "http://example2.com/#stuff/zip" == item["id"]
106 assert ["http://example2.com/#stuff/out"] == ra["outputs"]
107 assert {"n": 9} == ra["other"]
108
109
110 def test_scoped_ref():
111 ldr = schema_salad.ref_resolver.Loader({})
112 ldr.add_context(
113 {
114 "scatter": {"@type": "@id", "refScope": 0},
115 "source": {"@type": "@id", "refScope": 2},
116 "in": {"mapSubject": "id", "mapPredicate": "source"},
117 "out": {"@type": "@id", "identity": True},
118 "inputs": {"mapSubject": "id", "mapPredicate": "type"},
119 "outputs": {"mapSubject": "id"},
120 "steps": {"mapSubject": "id"},
121 "id": "@id",
122 }
123 )
124
125 ra, _ = ldr.resolve_all(
126 cmap(
127 {
128 "inputs": {"inp": "string", "inp2": "string"},
129 "outputs": {"out": {"type": "string", "source": "step2/out"}},
130 "steps": {
131 "step1": {
132 "in": {"inp": "inp", "inp2": "#inp2", "inp3": ["inp", "inp2"]},
133 "out": ["out"],
134 "scatter": "inp",
135 },
136 "step2": {
137 "in": {"inp": "step1/out"},
138 "scatter": "inp",
139 "out": ["out"],
140 },
141 },
142 }
143 ),
144 "http://example2.com/",
145 )
146
147 assert {
148 "inputs": [
149 {"id": "http://example2.com/#inp", "type": "string"},
150 {"id": "http://example2.com/#inp2", "type": "string"},
151 ],
152 "outputs": [
153 {
154 "id": "http://example2.com/#out",
155 "type": "string",
156 "source": "http://example2.com/#step2/out",
157 }
158 ],
159 "steps": [
160 {
161 "id": "http://example2.com/#step1",
162 "scatter": "http://example2.com/#step1/inp",
163 "in": [
164 {
165 "id": "http://example2.com/#step1/inp",
166 "source": "http://example2.com/#inp",
167 },
168 {
169 "id": "http://example2.com/#step1/inp2",
170 "source": "http://example2.com/#inp2",
171 },
172 {
173 "id": "http://example2.com/#step1/inp3",
174 "source": [
175 "http://example2.com/#inp",
176 "http://example2.com/#inp2",
177 ],
178 },
179 ],
180 "out": ["http://example2.com/#step1/out"],
181 },
182 {
183 "id": "http://example2.com/#step2",
184 "scatter": "http://example2.com/#step2/inp",
185 "in": [
186 {
187 "id": "http://example2.com/#step2/inp",
188 "source": "http://example2.com/#step1/out",
189 }
190 ],
191 "out": ["http://example2.com/#step2/out"],
192 },
193 ],
194 } == ra
195
196
197 def test_examples():
198 for a in ["field_name", "ident_res", "link_res", "vocab_res"]:
199 ldr, _, _, _ = schema_salad.schema.load_schema(
200 get_data("metaschema/%s_schema.yml" % a)
201 )
202 with open(get_data("metaschema/%s_src.yml" % a)) as src_fp:
203 src = ldr.resolve_all(
204 ruamel.yaml.round_trip_load(src_fp), "", checklinks=False
205 )[0]
206 with open(get_data("metaschema/%s_proc.yml" % a)) as src_proc:
207 proc = ruamel.yaml.safe_load(src_proc)
208 assert proc == src
209
210
211 def test_yaml_float_test():
212 assert ruamel.yaml.safe_load("float-test: 2e-10")["float-test"] == 2e-10
213
214
215 def test_typedsl_ref():
216 ldr = schema_salad.ref_resolver.Loader({})
217 ldr.add_context(
218 {
219 "File": "http://example.com/File",
220 "null": "http://example.com/null",
221 "array": "http://example.com/array",
222 "type": {"@type": "@vocab", "typeDSL": True},
223 }
224 )
225
226 ra, _ = ldr.resolve_all(cmap({"type": "File"}), "")
227 assert {"type": "File"} == ra
228
229 ra, _ = ldr.resolve_all(cmap({"type": "File?"}), "")
230 assert {"type": ["null", "File"]} == ra
231
232 ra, _ = ldr.resolve_all(cmap({"type": "File[]"}), "")
233 assert {"type": {"items": "File", "type": "array"}} == ra
234
235 ra, _ = ldr.resolve_all(cmap({"type": "File[]?"}), "")
236 assert {"type": ["null", {"items": "File", "type": "array"}]} == ra
237
238
239 def test_secondaryFile_dsl_ref():
240 ldr = schema_salad.ref_resolver.Loader({})
241 ldr.add_context({"secondaryFiles": {"secondaryFilesDSL": True}})
242
243 ra, _ = ldr.resolve_all(cmap({"secondaryFiles": ".foo"}), "")
244 assert {"secondaryFiles": {"pattern": ".foo", "required": None}} == ra
245
246 ra, _ = ldr.resolve_all(cmap({"secondaryFiles": ".foo?"}), "")
247 assert {"secondaryFiles": {"pattern": ".foo", "required": False}} == ra
248
249 ra, _ = ldr.resolve_all(cmap({"secondaryFiles": [".foo"]}), "")
250 assert {"secondaryFiles": [{"pattern": ".foo", "required": None}]} == ra
251
252 ra, _ = ldr.resolve_all(cmap({"secondaryFiles": [".foo?"]}), "")
253 assert {"secondaryFiles": [{"pattern": ".foo", "required": False}]} == ra
254
255
256 def test_scoped_id():
257 ldr = schema_salad.ref_resolver.Loader({})
258 ctx = {
259 "id": "@id",
260 "location": {"@id": "@id", "@type": "@id"},
261 "bar": "http://example.com/bar",
262 "ex": "http://example.com/",
263 }
264 ldr.add_context(ctx)
265
266 ra, _ = ldr.resolve_all(
267 cmap({"id": "foo", "bar": {"id": "baz"}}), "http://example.com"
268 )
269 assert {
270 "id": "http://example.com/#foo",
271 "bar": {"id": "http://example.com/#foo/baz"},
272 } == ra
273
274 g = makerdf(None, ra, ctx)
275 print(g.serialize(format="n3"))
276
277 ra, _ = ldr.resolve_all(
278 cmap({"location": "foo", "bar": {"location": "baz"}}),
279 "http://example.com",
280 checklinks=False,
281 )
282 assert {
283 "location": "http://example.com/foo",
284 "bar": {"location": "http://example.com/baz"},
285 } == ra
286
287 g = makerdf(None, ra, ctx)
288 print(g.serialize(format="n3"))
289
290 ra, _ = ldr.resolve_all(
291 cmap({"id": "foo", "bar": {"location": "baz"}}),
292 "http://example.com",
293 checklinks=False,
294 )
295 assert {
296 "id": "http://example.com/#foo",
297 "bar": {"location": "http://example.com/baz"},
298 } == ra
299
300 g = makerdf(None, ra, ctx)
301 print(g.serialize(format="n3"))
302
303 ra, _ = ldr.resolve_all(
304 cmap({"location": "foo", "bar": {"id": "baz"}}),
305 "http://example.com",
306 checklinks=False,
307 )
308 assert {
309 "location": "http://example.com/foo",
310 "bar": {"id": "http://example.com/#baz"},
311 } == ra
312
313 g = makerdf(None, ra, ctx)
314 print(g.serialize(format="n3"))
315
316
317 def test_subscoped_id():
318 ldr = schema_salad.ref_resolver.Loader({})
319 ctx = {"id": "@id", "bar": {"subscope": "bar"}}
320 ldr.add_context(ctx)
321
322 ra, _ = ldr.resolve_all(
323 cmap({"id": "foo", "bar": {"id": "baz"}}), "http://example.com"
324 )
325 assert {
326 "id": "http://example.com/#foo",
327 "bar": {"id": "http://example.com/#foo/bar/baz"},
328 } == ra
329
330
331 def test_mixin():
332 base_url = schema_salad.ref_resolver.file_uri(os.path.join(os.getcwd(), "tests"))
333 ldr = schema_salad.ref_resolver.Loader({})
334 ra = ldr.resolve_ref(
335 cmap({"$mixin": get_data("tests/mixin.yml"), "one": "five"}), base_url=base_url
336 )
337 assert {"id": "four", "one": "five"} == ra[0]
338 ldr = schema_salad.ref_resolver.Loader({"id": "@id"})
339
340 ra = ldr.resolve_all(
341 cmap(
342 [
343 {"id": "a", "m": {"$mixin": get_data("tests/mixin.yml")}},
344 {"id": "b", "m": {"$mixin": get_data("tests/mixin.yml")}},
345 ]
346 ),
347 base_url=base_url,
348 )
349 assert [
350 {"id": base_url + "#a", "m": {"id": base_url + u"#a/four", "one": "two"}},
351 {"id": base_url + "#b", "m": {"id": base_url + u"#b/four", "one": "two"}},
352 ] == ra[0]
353
354
355 def test_fragment():
356 ldr = schema_salad.ref_resolver.Loader({"id": "@id"})
357 b, _ = ldr.resolve_ref(get_data("tests/frag.yml#foo2"))
358 assert {"id": b["id"], "bar": "b2"} == b
359
360
361 def test_file_uri():
362 # Note: this test probably won't pass on Windows. Someone with a
363 # windows box should add an alternate test.
364 assert "file:///foo/bar%20baz/quux" == schema_salad.ref_resolver.file_uri(
365 "/foo/bar baz/quux"
366 )
367 assert os.path.normpath(
368 "/foo/bar baz/quux"
369 ) == schema_salad.ref_resolver.uri_file_path("file:///foo/bar%20baz/quux")
370 assert (
371 "file:///foo/bar%20baz/quux%23zing%20zong"
372 == schema_salad.ref_resolver.file_uri("/foo/bar baz/quux#zing zong")
373 )
374 assert (
375 "file:///foo/bar%20baz/quux#zing%20zong"
376 == schema_salad.ref_resolver.file_uri(
377 "/foo/bar baz/quux#zing zong", split_frag=True
378 )
379 )
380 assert os.path.normpath(
381 "/foo/bar baz/quux#zing zong"
382 ) == schema_salad.ref_resolver.uri_file_path(
383 "file:///foo/bar%20baz/quux#zing%20zong"
384 )
385
386
387 def test_sourceline():
388 ldr = schema_salad.ref_resolver.Loader({"id": "@id"})
389 b, _ = ldr.resolve_ref(get_data("tests/frag.yml"))
390
391 class TestExp(Exception):
392 pass
393
394 try:
395 with SourceLine(b, 1, TestExp, False):
396 raise Exception("Whoops")
397 except TestExp as e:
398 assert str(e).endswith("frag.yml:3:3: Whoops"), e
399 except Exception as exc:
400 assert False, exc
401
402 if six.PY2:
403 try:
404 with SourceLine(b, 1, TestExp, True):
405 raise Exception("Whoops")
406 except TestExp as e:
407 assert (
408 str(e)
409 .splitlines()[0]
410 .endswith("frag.yml:3:3: Traceback (most recent call last):")
411 ), str(e)
412 except Exception as exc:
413 assert False, exc
414
415
416 def test_cmap():
417 # Test bugfix that cmap won't fail when given a CommentedMap with no lc.data
418 cmap(CommentedMap((("foo", "bar"), ("baz", ["quux"]))))
419 cmap(CommentedSeq(("foo", [], "bar")))
420
421
422 def test_blank_node_id():
423 # Test that blank nodes are passed through and not considered
424 # relative paths. Blank nodes (also called anonymous ids) are
425 # URIs starting with "_:". They are randomly generated
426 # placeholders mainly used internally where an id is needed but
427 # was not given.
428
429 ldr = schema_salad.ref_resolver.Loader({})
430 ctx = {"id": "@id"}
431 ldr.add_context(ctx)
432
433 ra, _ = ldr.resolve_all(cmap({"id": "_:foo"}), "http://example.com")
434 assert {"id": "_:foo"} == ra