1- import unittest
1+ import os
2+ import pytest
23
34from jsonasobj2 import JsonObj , loads
5+ from rdflib import URIRef
6+
7+ from linkml_runtime .linkml_model .meta import ClassDefinition , Prefix , SchemaDefinition
8+ from linkml_runtime .utils .context_utils import map_import , merge_contexts
9+ from linkml_runtime .utils .namespaces import Namespaces
410
5- from linkml_runtime .utils .context_utils import merge_contexts
611from tests .test_utils import METAMODEL_CONTEXT_URI , META_BASE_URI
712
813json_1 = '{ "ex": "http://example.org/test/", "ex2": "http://example.org/test2/" }'
2631}"""
2732
2833
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 ]))
34+ def test_merge_contexts ():
35+ assert merge_contexts () is None
36+ assert "file://local.jsonld" == merge_contexts ("local.jsonld" )["@context" ]
37+ assert "file://local.jsonld" == merge_contexts (["local.jsonld" ])["@context" ]
38+ assert METAMODEL_CONTEXT_URI == merge_contexts (METAMODEL_CONTEXT_URI )["@context" ]
39+ assert METAMODEL_CONTEXT_URI == merge_contexts ([METAMODEL_CONTEXT_URI ])["@context" ]
40+ assert JsonObj (ex = "http://example.org/test/" , ex2 = "http://example.org/test2/" ) == merge_contexts (json_1 )["@context" ]
41+ assert (
42+ JsonObj (ex = "http://example.org/test/" , ex2 = "http://example.org/test2/" ) == merge_contexts ([json_1 ])["@context" ]
43+ )
44+ assert (
45+ JsonObj (ex = "http://example.org/test3/" , ex2 = JsonObj (** {"@id" : "http://example.org/test4/" }))
46+ == merge_contexts (json_2 )["@context" ]
47+ )
48+ assert (
49+ JsonObj (ex = "http://example.org/test3/" , ex2 = JsonObj (** {"@id" : "http://example.org/test4/" }))
50+ == merge_contexts ([json_2 ])["@context" ]
51+ )
52+ assert [
53+ f"file://local.jsonld" ,
54+ "https://w3id.org/linkml/meta.context.jsonld" ,
55+ JsonObj (ex = "http://example.org/test/" , ex2 = "http://example.org/test2/" ),
56+ JsonObj (ex = "http://example.org/test3/" , ex2 = JsonObj (** {"@id" : "http://example.org/test4/" })),
57+ ] == merge_contexts (["local.jsonld" , METAMODEL_CONTEXT_URI , json_1 , json_2 ])["@context" ]
58+ assert loads (context_output ) == merge_contexts (["local.jsonld" , METAMODEL_CONTEXT_URI , json_1 , json_2 ])
59+ # Dups are not removed
60+ assert JsonObj (
61+ ** {
62+ "@context" : [
63+ JsonObj (ex = "http://example.org/test/" , ex2 = "http://example.org/test2/" ),
64+ JsonObj (ex = "http://example.org/test/" , ex2 = "http://example.org/test2/" ),
65+ ]
66+ }
67+ ) == merge_contexts ([json_1 , json_1 ])
68+ assert "file://local.jsonld" , merge_contexts ("local.jsonld" )["@context" ]
5169
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' ])
5870
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 ("""
71+ def test_merge_contexts_base ():
72+ assert JsonObj (** {"@context" : JsonObj (** {"@base" : "file://relloc" })}) == merge_contexts (base = "file://relloc" )
73+ assert loads (f'{{"@context": {{"@base": "{ META_BASE_URI } "}}}}' ) == merge_contexts (base = META_BASE_URI )
74+ assert loads ("""
6675{"@context": [
6776 "https://w3id.org/linkml/meta.context.jsonld",
6877 {
@@ -79,8 +88,29 @@ def test_merge_contexts_base(self):
7988 "@base": "https://w3id.org/linkml/"
8089 }
8190 ]
82- }""" ), merge_contexts ([METAMODEL_CONTEXT_URI , json_1 , json_2 ], base = META_BASE_URI ))
91+ }""" ) == merge_contexts ([METAMODEL_CONTEXT_URI , json_1 , json_2 ], base = META_BASE_URI )
92+
93+
94+ @pytest .mark .parametrize (
95+ ("imp" , "result" ),
96+ [
97+ ("linkml:types" , f"C:\\ temp\\ linkml_model\\ model\\ schema{ os .sep } types" ),
98+ ("ex_file" , "/tmp/example/schema" ),
99+ ("_types" , "https://w3id.org/linkml/types" ),
100+ ],
101+ )
102+ def test_map_import (imp , result ):
103+ importmap = {
104+ "linkml:" : "C:\\ temp\\ linkml_model\\ model\\ schema" ,
105+ "ex_file" : "/tmp/example/schema" ,
106+ "_types" : "linkml:types" ,
107+ }
83108
109+ def namespaces ():
110+ ns = Namespaces ()
111+ ns ["linkml" ] = URIRef ("https://w3id.org/linkml/" )
112+ ns ["ex_file" ] = URIRef ("https://example.org/file/" )
113+ ns ["_types" ] = URIRef ("https://w3id.org/linkml/types/" )
114+ return ns
84115
85- if __name__ == '__main__' :
86- unittest .main ()
116+ assert map_import (importmap , namespaces , imp ) == result
0 commit comments