/
test_codemeta.py
115 lines (92 loc) · 3.34 KB
/
test_codemeta.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import json
import sys
import pytest
import rdflib
from somesy.cff.writer import CFF
from somesy.codemeta import update_codemeta
from somesy.codemeta.utils import (
_codemeta_context,
_graph_from_cm_dict,
_graph_from_cm_file,
_localize_codemetapy_context,
)
from somesy.core.models import SomesyConfig
cm_dict = {
"@context": list(_codemeta_context),
"@type": "SoftwareSourceCode",
"applicationCategory": "Software Development > Libraries > Application Frameworks",
"audience": [
{"@type": "Audience", "audienceType": "Developers"},
],
"author": [
{
"@id": "https://orcid.org/0123-4567-8910-1112",
"@type": "Person",
"familyName": "Doe",
"givenName": "Jane",
},
],
"codeRepository": "https://example.com/my-software",
"description": "Amazing software",
"identifier": "awesome-tool",
"keywords": ["amazing", "software"],
"license": "http://spdx.org/licenses/MIT",
"name": "awesome-tool",
"operatingSystem": "POSIX > Linux",
"runtimePlatform": "Python 3",
"url": "https://example.com/my-software",
"version": "0.1.0",
}
num_triples = 20 # empirically observed from successful rdflib parse
def test_localized_graph():
# no context -> do nothing
assert _localize_codemetapy_context({}) == {}
# unexpected context -> exception
with pytest.raises(RuntimeError):
_localize_codemetapy_context({"@context": "invalid"})
# correct context -> replace
ret = _localize_codemetapy_context({"@context": _codemeta_context})
assert ret["@context"] != _codemeta_context
def rdflib_audit_hook(name: str, args) -> None:
"""An audit hook that blocks access when an attempt is made to open an URL.
See https://rdflib.readthedocs.io/en/stable/_modules/examples/secure_with_audit.html#audit_hook
"""
if name == "urllib.Request":
raise PermissionError("Permission denied for URL")
return None
def test_graph_from_dict():
sys.addaudithook(rdflib_audit_hook)
# test without localized graph
# -> rdflib will attempt network access
g = rdflib.Graph()
with pytest.raises(PermissionError):
g.parse(data=json.dumps(cm_dict), format="json-ld")
# now use the localized graph with local context
# -> no network access, but still able to load triples
ret = _graph_from_cm_dict(cm_dict)
assert len(ret) == num_triples
def test_graph_from_file(tmp_path):
filepath = tmp_path / "my_codemeta.json"
# no file -> None
assert _graph_from_cm_file(filepath) is None
# create file
with open(filepath, "w") as f:
json.dump(cm_dict, f)
# file exists -> returns correct graph
ret = _graph_from_cm_file(filepath)
assert len(ret) == num_triples
def test_update_codemeta(tmp_path):
file_path = tmp_path / "my_codemeta.json"
cff_path = tmp_path / "CITATION.cff"
cff = CFF(cff_path)
cff.save()
assert cff_path.is_file()
# first time, no file -> codemeta.json is created
conf = SomesyConfig(codemeta_file=file_path, cff_file=cff_path)
assert update_codemeta(conf)
# second time, no changes -> codemeta.json is not overwritten
assert not update_codemeta(conf)
cff.description = "Changed description"
cff.save()
# second time, changes -> codemeta.json is overwritten
assert update_codemeta(conf)