Skip to content

Commit 460e3d4

Browse files
authored
Merge pull request #374 from softwarepub/refactor/372-test-pyld-util
Test pyld util
2 parents 39d25e9 + d5f64e1 commit 460e3d4

1 file changed

Lines changed: 66 additions & 0 deletions

File tree

test/hermes_test/model/types/test_pyld_util.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
1+
# SPDX-FileCopyrightText: 2025 German Aerospace Center (DLR)
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
# SPDX-FileContributor: Michael Meinel
6+
17
import pytest
28

9+
from unittest import mock
10+
11+
from pyld import jsonld
312
from hermes.model.types import pyld_util
413

514

@@ -16,3 +25,60 @@ def test_mock_document_compact(ld_proc, mock_document):
1625
def 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

Comments
 (0)