1+ # SPDX-FileCopyrightText: 2025 German Aerospace Center (DLR)
2+ #
3+ # SPDX-License-Identifier: Apache-2.0
4+
5+ # SPDX-FileContributor: Michael Meinel
6+
17import pytest
28
9+ from unittest import mock
10+
11+ from pyld import jsonld
312from hermes .model .types import pyld_util
413
514
@@ -16,3 +25,60 @@ def test_mock_document_compact(ld_proc, mock_document):
1625def test_mock_document_expanded (ld_proc , mock_document ):
1726 expanded_document = ld_proc .expand (mock_document .compact (), {})
1827 assert expanded_document == mock_document .expanded ()
28+
29+
30+ def test_initial_context (ld_proc , httpserver , mock_document ):
31+ with pytest .raises (jsonld .JsonLdError ):
32+ active_ctx = ld_proc .initial_ctx (
33+ [{"s" : "www.spam.de" }],
34+ {"documentLoader" : pyld_util .bundled_loader }
35+ )
36+ url = httpserver .url_for ("/" )
37+ httpserver .expect_request ("/" ).respond_with_json ({"@context" : mock_document .vocabulary (url )})
38+ active_ctx = ld_proc .initial_ctx (
39+ [url ],
40+ {"documentLoader" : pyld_util .bundled_loader }
41+ )
42+ assert "spam" in active_ctx ["mappings" ]
43+ assert active_ctx ["mappings" ]["spam" ]["@id" ] == url + "spam"
44+ assert active_ctx ["mappings" ]["ham" ]["@id" ] == url + "ham"
45+ assert active_ctx ["mappings" ]["use_until" ]["@id" ] == url + "use_until"
46+ assert active_ctx ["mappings" ]["Egg" ]["@id" ] == url + "Egg"
47+ assert active_ctx ["processingMode" ] == "json-ld-1.1"
48+
49+
50+ def test_expand_iri (ld_proc , mock_context ):
51+ active_ctx = {'processingMode' : 'json-ld-1.1' ,
52+ 'mappings' : mock_context }
53+ assert ld_proc .expand_iri (active_ctx , "spam" ) == "http://spam.eggs/" + "spam"
54+
55+
56+ def test_compact_iri (ld_proc , mock_context ):
57+ active_ctx = {'mappings' : {'spam' : {'reverse' : False , 'protected' : False , '_prefix' : False ,
58+ '_term_has_colon' : False , '@id' : 'http://spam.eggs/spam' },
59+ 'ham' : {'reverse' : False , 'protected' : False , '_prefix' : False ,
60+ '_term_has_colon' : False , '@id' : 'http://spam.eggs/ham' , '@type' : '@id' },
61+ },
62+ 'processingMode' : 'json-ld-1.1' , '_uuid' : 'c641b9db-b0e8-11f0-bc68-9cfce89fd5b3' }
63+
64+ assert ld_proc .compact_iri (active_ctx , "http://spam.eggs/spam" ) == "spam"
65+ assert ld_proc .compact_iri (active_ctx , "http://spam.eggs/bacon" ) == "http://spam.eggs/bacon"
66+
67+
68+ def test_register_typemap ():
69+ len_typemap = len (pyld_util .JsonLdProcessor ._type_map )
70+ pyld_util .JsonLdProcessor .register_typemap ("function" , ** dict (spam = "hallo" ))
71+ assert len (pyld_util .JsonLdProcessor ._type_map ) == len_typemap + 1
72+ assert pyld_util .JsonLdProcessor ._type_map ["spam" ] == [("function" , "hallo" )]
73+
74+
75+ def test_apply_typemap ():
76+ pyld_util .JsonLdProcessor ._type_map ["spam" ] = [(lambda c : isinstance (c , list ), lambda c , ** _ : c [0 ]+ "eggs" )]
77+ ld_value , ld_output = pyld_util .JsonLdProcessor .apply_typemap (["ham" ], "spam" )
78+ assert ld_output == "spam"
79+ assert ld_value == "hameggs"
80+ ld_value , ld_output = pyld_util .JsonLdProcessor .apply_typemap (["eggs" , "ham" ], "spam" )
81+ assert ld_output == "spam"
82+ assert ld_value == "eggseggs"
83+ ld_value , ld_output = pyld_util .JsonLdProcessor .apply_typemap ("ham" , "spam" )
84+ assert ld_value == "ham"
0 commit comments