1- import unittest
1+ import pytest
22
33from jsonasobj2 import JsonObj , loads
4+ from rdflib import URIRef
5+
6+ from linkml_runtime .linkml_model .meta import ClassDefinition , Prefix , SchemaDefinition
7+ from linkml_runtime .utils .context_utils import map_import , merge_contexts
8+ from linkml_runtime .utils .namespaces import Namespaces
49
5- from linkml_runtime .utils .context_utils import merge_contexts
610from tests .test_utils import METAMODEL_CONTEXT_URI , META_BASE_URI
711
812json_1 = '{ "ex": "http://example.org/test/", "ex2": "http://example.org/test2/" }'
2630}"""
2731
2832
29- class ContextUtilsTestCase (unittest .TestCase ):
30- def test_merge_contexts (self ):
31- self .assertIsNone (merge_contexts ())
32- self .assertEqual ('file://local.jsonld' , merge_contexts ("local.jsonld" )['@context' ])
33- self .assertEqual ('file://local.jsonld' , merge_contexts (["local.jsonld" ])['@context' ])
34- self .assertEqual (METAMODEL_CONTEXT_URI , merge_contexts (METAMODEL_CONTEXT_URI )['@context' ])
35- self .assertEqual (METAMODEL_CONTEXT_URI , merge_contexts ([METAMODEL_CONTEXT_URI ])['@context' ])
36- self .assertEqual (JsonObj (ex = 'http://example.org/test/' , ex2 = 'http://example.org/test2/' ),
37- merge_contexts (json_1 )['@context' ])
38- self .assertEqual (JsonObj (ex = 'http://example.org/test/' , ex2 = 'http://example.org/test2/' ),
39- merge_contexts ([json_1 ])['@context' ])
40- self .assertEqual (JsonObj (ex = 'http://example.org/test3/' , ex2 = JsonObj (** {'@id' : 'http://example.org/test4/' })),
41- merge_contexts (json_2 )['@context' ])
42- self .assertEqual (JsonObj (ex = 'http://example.org/test3/' , ex2 = JsonObj (** {'@id' : 'http://example.org/test4/' })),
43- merge_contexts ([json_2 ])['@context' ])
44- self .assertEqual ([f'file://local.jsonld' ,
45- 'https://w3id.org/linkml/meta.context.jsonld' ,
46- JsonObj (ex = 'http://example.org/test/' , ex2 = 'http://example.org/test2/' ),
47- JsonObj (ex = 'http://example.org/test3/' , ex2 = JsonObj (** {'@id' : 'http://example.org/test4/' }))],
48- merge_contexts (["local.jsonld" , METAMODEL_CONTEXT_URI , json_1 , json_2 ])['@context' ])
49- self .assertEqual (loads (context_output ),
50- merge_contexts (["local.jsonld" , METAMODEL_CONTEXT_URI , json_1 , json_2 ]))
33+ def test_merge_contexts ():
34+ assert merge_contexts () is None
35+ assert "file://local.jsonld" == merge_contexts ("local.jsonld" )["@context" ]
36+ assert "file://local.jsonld" == merge_contexts (["local.jsonld" ])["@context" ]
37+ assert METAMODEL_CONTEXT_URI == merge_contexts (METAMODEL_CONTEXT_URI )["@context" ]
38+ assert METAMODEL_CONTEXT_URI == merge_contexts ([METAMODEL_CONTEXT_URI ])["@context" ]
39+ assert JsonObj (ex = "http://example.org/test/" , ex2 = "http://example.org/test2/" ) == merge_contexts (json_1 )["@context" ]
40+ assert (
41+ JsonObj (ex = "http://example.org/test/" , ex2 = "http://example.org/test2/" ) == merge_contexts ([json_1 ])["@context" ]
42+ )
43+ assert (
44+ JsonObj (ex = "http://example.org/test3/" , ex2 = JsonObj (** {"@id" : "http://example.org/test4/" }))
45+ == merge_contexts (json_2 )["@context" ]
46+ )
47+ assert (
48+ JsonObj (ex = "http://example.org/test3/" , ex2 = JsonObj (** {"@id" : "http://example.org/test4/" }))
49+ == merge_contexts ([json_2 ])["@context" ]
50+ )
51+ assert [
52+ f"file://local.jsonld" ,
53+ "https://w3id.org/linkml/meta.context.jsonld" ,
54+ JsonObj (ex = "http://example.org/test/" , ex2 = "http://example.org/test2/" ),
55+ JsonObj (ex = "http://example.org/test3/" , ex2 = JsonObj (** {"@id" : "http://example.org/test4/" })),
56+ ] == merge_contexts (["local.jsonld" , METAMODEL_CONTEXT_URI , json_1 , json_2 ])["@context" ]
57+ assert loads (context_output ) == merge_contexts (["local.jsonld" , METAMODEL_CONTEXT_URI , json_1 , json_2 ])
58+ # Dups are not removed
59+ assert JsonObj (
60+ ** {
61+ "@context" : [
62+ JsonObj (ex = "http://example.org/test/" , ex2 = "http://example.org/test2/" ),
63+ JsonObj (ex = "http://example.org/test/" , ex2 = "http://example.org/test2/" ),
64+ ]
65+ }
66+ ) == merge_contexts ([json_1 , json_1 ])
67+ assert "file://local.jsonld" , merge_contexts ("local.jsonld" )["@context" ]
5168
52- # Dups are not removed
53- self .assertEqual (
54- JsonObj (** {'@context' : [JsonObj (ex = 'http://example.org/test/' , ex2 = 'http://example.org/test2/' ),
55- JsonObj (ex = 'http://example.org/test/' , ex2 = 'http://example.org/test2/' )]}),
56- merge_contexts ([json_1 , json_1 ]))
57- self .assertEqual ('file://local.jsonld' , merge_contexts ("local.jsonld" )['@context' ])
5869
59- def test_merge_contexts_base (self ):
60- self .assertEqual (
61- JsonObj (** {'@context' :
62- JsonObj (** {'@base' : 'file://relloc' })}),
63- merge_contexts (base = 'file://relloc' ))
64- self .assertEqual (loads (f'{{"@context": {{"@base": "{ META_BASE_URI } "}}}}' ), merge_contexts (base = META_BASE_URI ))
65- self .assertEqual (loads ("""
70+ def test_merge_contexts_base ():
71+ assert JsonObj (** {"@context" : JsonObj (** {"@base" : "file://relloc" })}) == merge_contexts (base = "file://relloc" )
72+ assert loads (f'{{"@context": {{"@base": "{ META_BASE_URI } "}}}}' ) == merge_contexts (base = META_BASE_URI )
73+ assert loads ("""
6674{"@context": [
6775 "https://w3id.org/linkml/meta.context.jsonld",
6876 {
@@ -79,8 +87,28 @@ def test_merge_contexts_base(self):
7987 "@base": "https://w3id.org/linkml/"
8088 }
8189 ]
82- }""" ), merge_contexts ([METAMODEL_CONTEXT_URI , json_1 , json_2 ], base = META_BASE_URI ))
90+ }""" ) == merge_contexts ([METAMODEL_CONTEXT_URI , json_1 , json_2 ], base = META_BASE_URI )
91+
8392
93+ @pytest .mark .parametrize (
94+ ("imp" , "result" ),
95+ [
96+ ("linkml:types" , r"C:\temp\linkml_model\model\schema\types" ),
97+ ("ex_file" , "C:\\ temp\\ example\\ schema" ),
98+ ("_types" , "https://w3id.org/linkml/types" ),
99+ ],
100+ )
101+ def test_map_import (imp , result ):
102+ importmap = {
103+ "linkml:" : "C:\\ temp\\ linkml_model\\ model\\ schema" ,
104+ "ex_file" : "C:\\ temp\\ example\\ schema" ,
105+ "_types" : "linkml:types" ,
106+ }
107+ def namespaces ():
108+ ns = Namespaces ()
109+ ns ["linkml" ] = URIRef ("https://w3id.org/linkml/" )
110+ ns ["ex_file" ] = URIRef ("https://example.org/file/" )
111+ ns ["_types" ] = URIRef ("https://w3id.org/linkml/types/" )
112+ return ns
84113
85- if __name__ == '__main__' :
86- unittest .main ()
114+ assert map_import (importmap , namespaces , imp ) == result
0 commit comments