3
3
import schema_salad .main
4
4
import schema_salad .schema
5
5
from schema_salad .jsonld_context import makerdf
6
+ from pkg_resources import Requirement , resource_filename , ResolutionError # type: ignore
6
7
import rdflib
7
8
import ruamel .yaml as yaml
8
9
import json
11
12
try :
12
13
from ruamel .yaml import CSafeLoader as SafeLoader
13
14
except ImportError :
14
- from ruamel .yaml import SafeLoader
15
+ from ruamel .yaml import SafeLoader # type: ignore
16
+
17
+
18
+ def get_data (filename ):
19
+ filepath = None
20
+ try :
21
+ filepath = resource_filename (
22
+ Requirement .parse ("schema-salad" ), filename )
23
+ except ResolutionError :
24
+ pass
25
+ if not filepath or not os .path .isfile (filepath ):
26
+ filepath = os .path .join (os .path .dirname (__file__ ), os .pardir , filename )
27
+ return filepath
15
28
16
29
17
30
class TestSchemas (unittest .TestCase ):
18
31
def test_schemas (self ):
19
32
l = schema_salad .ref_resolver .Loader ({})
20
33
21
34
ra , _ = l .resolve_all ({
22
- u"$schemas" : [u "tests/EDAM.owl" ],
35
+ u"$schemas" : [get_data ( "tests/EDAM.owl" ) ],
23
36
u"$namespaces" : {u"edam" : u"http://edamontology.org/" },
24
37
u"edam:has_format" : u"edam:format_1915"
25
38
}, "" )
26
39
27
40
self .assertEqual ({
28
- u"$schemas" : [u "tests/EDAM.owl" ],
41
+ u"$schemas" : [get_data ( "tests/EDAM.owl" ) ],
29
42
u"$namespaces" : {u"edam" : u"http://edamontology.org/" },
30
43
u'http://edamontology.org/has_format' : u'http://edamontology.org/format_1915'
31
44
}, ra )
@@ -50,12 +63,15 @@ def test_schemas(self):
50
63
# })
51
64
52
65
def test_self_validate (self ):
53
- self .assertEqual (0 , schema_salad .main .main (argsl = ["schema_salad/metaschema/metaschema.yml" ]))
54
- self .assertEqual (0 , schema_salad .main .main (argsl = ["schema_salad/metaschema/metaschema.yml" ,
55
- "schema_salad/metaschema/metaschema.yml" ]))
66
+ self .assertEqual (0 , schema_salad .main .main (
67
+ argsl = [get_data ("metaschema/metaschema.yml" )]))
68
+ self .assertEqual (0 , schema_salad .main .main (
69
+ argsl = [get_data ("metaschema/metaschema.yml" ),
70
+ get_data ("metaschema/metaschema.yml" )]))
56
71
57
72
def test_avro_regression (self ):
58
- self .assertEqual (0 , schema_salad .main .main (argsl = ["tests/Process.yml" ]))
73
+ self .assertEqual (0 , schema_salad .main .main (
74
+ argsl = [get_data ("tests/Process.yml" )]))
59
75
60
76
def test_jsonld_ctx (self ):
61
77
ldr , _ , _ , _ = schema_salad .schema .load_schema ({
@@ -76,8 +92,6 @@ def test_jsonld_ctx(self):
76
92
'http://example.com/foo#bar' : 'asym'
77
93
})
78
94
79
- maxDiff = None
80
-
81
95
def test_idmap (self ):
82
96
ldr = schema_salad .ref_resolver .Loader ({})
83
97
ldr .add_context ({
@@ -107,7 +121,8 @@ def test_idmap(self):
107
121
self .assertEqual ("http://example2.com/#stuff" , ra ["id" ])
108
122
for item in ra ["inputs" ]:
109
123
if item ["a" ] == 2 :
110
- self .assertEquals ('http://example2.com/#stuff/zing' , item ["id" ])
124
+ self .assertEquals (
125
+ 'http://example2.com/#stuff/zing' , item ["id" ])
111
126
else :
112
127
self .assertEquals ('http://example2.com/#stuff/zip' , item ["id" ])
113
128
self .assertEquals (['http://example2.com/#stuff/out' ], ra ['outputs' ])
@@ -183,46 +198,44 @@ def test_scoped_ref(self):
183
198
'id' : 'http://example2.com/#inp2' ,
184
199
'type' : 'string'
185
200
}],
186
- 'outputs' : [{
187
- 'id' : 'http://example2.com/#out' ,
188
- 'type' : 'string' ,
189
- 'source' : 'http://example2.com/#step2/out'
190
- }],
191
- 'steps' : [{
201
+ 'outputs' : [{
202
+ 'id' : 'http://example2.com/#out' ,
203
+ 'type' : 'string' ,
204
+ 'source' : 'http://example2.com/#step2/out'
205
+ }],
206
+ 'steps' : [{
192
207
'id' : 'http://example2.com/#step1' ,
193
208
'scatter' : 'http://example2.com/#step1/inp' ,
194
209
'in' : [{
195
- 'id' : 'http://example2.com/#step1/inp' ,
196
- 'source' : 'http://example2.com/#inp'
210
+ 'id' : 'http://example2.com/#step1/inp' ,
211
+ 'source' : 'http://example2.com/#inp'
197
212
}, {
198
- 'id' : 'http://example2.com/#step1/inp2' ,
199
- 'source' : 'http://example2.com/#inp2'
213
+ 'id' : 'http://example2.com/#step1/inp2' ,
214
+ 'source' : 'http://example2.com/#inp2'
200
215
}, {
201
- 'id' : 'http://example2.com/#step1/inp3' ,
202
- 'source' : ['http://example2.com/#inp' , 'http://example2.com/#inp2' ]
216
+ 'id' : 'http://example2.com/#step1/inp3' ,
217
+ 'source' : ['http://example2.com/#inp' , 'http://example2.com/#inp2' ]
203
218
}],
204
219
"out" : ["http://example2.com/#step1/out" ],
205
- }, {
220
+ }, {
206
221
'id' : 'http://example2.com/#step2' ,
207
222
'scatter' : 'http://example2.com/#step2/inp' ,
208
223
'in' : [{
209
- 'id' : 'http://example2.com/#step2/inp' ,
210
- 'source' : 'http://example2.com/#step1/out'
224
+ 'id' : 'http://example2.com/#step2/inp' ,
225
+ 'source' : 'http://example2.com/#step1/out'
211
226
}],
212
227
"out" : ["http://example2.com/#step2/out" ],
213
228
}]
214
- }, ra )
215
-
229
+ }, ra )
216
230
217
231
def test_examples (self ):
218
- self .maxDiff = None
219
232
for a in ["field_name" , "ident_res" , "link_res" , "vocab_res" ]:
220
233
ldr , _ , _ , _ = schema_salad .schema .load_schema (
221
- "schema_salad/ metaschema/%s_schema.yml" % a )
222
- with open ("schema_salad/ metaschema/%s_src.yml" % a ) as src_fp :
234
+ get_data ( " metaschema/%s_schema.yml" % a ) )
235
+ with open (get_data ( " metaschema/%s_src.yml" % a ) ) as src_fp :
223
236
src = ldr .resolve_all (
224
237
yaml .load (src_fp , Loader = SafeLoader ), "" , checklinks = False )[0 ]
225
- with open ("schema_salad/ metaschema/%s_proc.yml" % a ) as src_proc :
238
+ with open (get_data ( " metaschema/%s_proc.yml" % a ) ) as src_proc :
226
239
proc = yaml .load (src_proc , Loader = SafeLoader )
227
240
self .assertEqual (proc , src )
228
241
@@ -251,7 +264,8 @@ def test_typedsl_ref(self):
251
264
self .assertEqual ({'type' : {'items' : 'File' , 'type' : 'array' }}, ra )
252
265
253
266
ra , _ = ldr .resolve_all ({"type" : "File[]?" }, "" )
254
- self .assertEqual ({'type' : ['null' , {'items' : 'File' , 'type' : 'array' }]}, ra )
267
+ self .assertEqual (
268
+ {'type' : ['null' , {'items' : 'File' , 'type' : 'array' }]}, ra )
255
269
256
270
def test_scoped_id (self ):
257
271
ldr = schema_salad .ref_resolver .Loader ({})
@@ -275,7 +289,7 @@ def test_scoped_id(self):
275
289
self .assertEqual ({'id' : 'http://example.com/#foo' ,
276
290
'bar' : {
277
291
'id' : 'http://example.com/#foo/baz' },
278
- }, ra )
292
+ }, ra )
279
293
280
294
g = makerdf (None , ra , ctx )
281
295
print (g .serialize (format = "n3" ))
@@ -289,7 +303,7 @@ def test_scoped_id(self):
289
303
self .assertEqual ({'location' : 'http://example.com/foo' ,
290
304
'bar' : {
291
305
'location' : 'http://example.com/baz' },
292
- }, ra )
306
+ }, ra )
293
307
294
308
g = makerdf (None , ra , ctx )
295
309
print (g .serialize (format = "n3" ))
@@ -303,7 +317,7 @@ def test_scoped_id(self):
303
317
self .assertEqual ({'id' : 'http://example.com/#foo' ,
304
318
'bar' : {
305
319
'location' : 'http://example.com/baz' },
306
- }, ra )
320
+ }, ra )
307
321
308
322
g = makerdf (None , ra , ctx )
309
323
print (g .serialize (format = "n3" ))
@@ -317,37 +331,36 @@ def test_scoped_id(self):
317
331
self .assertEqual ({'location' : 'http://example.com/foo' ,
318
332
'bar' : {
319
333
'id' : 'http://example.com/#baz' },
320
- }, ra )
334
+ }, ra )
321
335
322
336
g = makerdf (None , ra , ctx )
323
337
print (g .serialize (format = "n3" ))
324
338
325
-
326
339
def test_mixin (self ):
327
340
ldr = schema_salad .ref_resolver .Loader ({})
328
- ra = ldr .resolve_ref ({"$mixin" : " mixin.yml" , "one" : "five" },
329
- base_url = "file://" + os .getcwd ()+ "/tests/" )
341
+ ra = ldr .resolve_ref ({"$mixin" : get_data ( "tests/ mixin.yml") , "one" : "five" },
342
+ base_url = "file://" + os .getcwd () + "/tests/" )
330
343
self .assertEqual ({'id' : 'four' , 'one' : 'five' }, ra [0 ])
331
344
332
345
ldr = schema_salad .ref_resolver .Loader ({"id" : "@id" })
333
- base_url = "file://" + os .getcwd ()+ "/tests/"
346
+ base_url = "file://" + os .getcwd () + "/tests/"
334
347
ra = ldr .resolve_all ([{
335
348
"id" : "a" ,
336
- "m" : {"$mixin" : " mixin.yml" }
349
+ "m" : {"$mixin" : get_data ( "tests/ mixin.yml") }
337
350
}, {
338
351
"id" : "b" ,
339
- "m" : {"$mixin" : " mixin.yml" }
352
+ "m" : {"$mixin" : get_data ( "tests/ mixin.yml") }
340
353
}], base_url = base_url )
341
354
self .assertEqual ([{
342
- 'id' : base_url + '#a' ,
355
+ 'id' : base_url + '#a' ,
343
356
'm' : {
344
- 'id' : base_url + u'#a/four' ,
357
+ 'id' : base_url + u'#a/four' ,
345
358
'one' : 'two'
346
359
},
347
360
}, {
348
- 'id' : base_url + '#b' ,
361
+ 'id' : base_url + '#b' ,
349
362
'm' : {
350
- 'id' : base_url + u'#b/four' ,
363
+ 'id' : base_url + u'#b/four' ,
351
364
'one' : 'two' }
352
365
}], ra [0 ])
353
366
0 commit comments