Skip to content

Commit 50adf74

Browse files
committed
update codemeta tests
1 parent 7a81fad commit 50adf74

1 file changed

Lines changed: 41 additions & 118 deletions

File tree

tests/output/test_codemeta.py

Lines changed: 41 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -1,121 +1,44 @@
1+
from somesy.codemeta import Codemeta
12
import json
2-
import sys
33

4-
import pytest
5-
import rdflib
64

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

0 commit comments

Comments
 (0)