Skip to content

Commit 4920090

Browse files
author
notactuallyfinn
committed
added more tests
1 parent 95288a2 commit 4920090

2 files changed

Lines changed: 125 additions & 8 deletions

File tree

src/hermes/commands/harvest/codemeta.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,16 @@
88
import glob
99
import json
1010
import pathlib
11-
import typing as t
11+
from typing import Union
1212

1313
from hermes.commands.harvest.base import HermesHarvestCommand, HermesHarvestPlugin
1414
from hermes.commands.harvest.util.validate_codemeta import validate_codemeta
15-
from hermes.model.errors import HermesValidationError
15+
from hermes.model.error import HermesValidationError
16+
from hermes.model import SoftwareMetadata
1617

1718

1819
class CodeMetaHarvestPlugin(HermesHarvestPlugin):
19-
def __call__(self, command: HermesHarvestCommand) -> t.Tuple[t.Dict, t.Dict]:
20+
def __call__(self, command: HermesHarvestCommand) -> tuple[SoftwareMetadata, dict]:
2021
"""
2122
Implementation of a harvester that provides data from a codemeta.json file format.
2223
@@ -39,7 +40,7 @@ def __call__(self, command: HermesHarvestCommand) -> t.Tuple[t.Dict, t.Dict]:
3940
raise HermesValidationError(codemeta_file)
4041

4142
codemeta = json.loads(codemeta_str)
42-
return codemeta, {'local_path': str(codemeta_file)}
43+
return SoftwareMetadata(codemeta), {'local_path': str(codemeta_file)}
4344

4445
def _validate(self, codemeta_file: pathlib.Path) -> bool:
4546
with open(codemeta_file, "r") as fi:
@@ -55,7 +56,7 @@ def _validate(self, codemeta_file: pathlib.Path) -> bool:
5556

5657
return True
5758

58-
def _get_single_codemeta(self, path: pathlib.Path) -> t.Optional[pathlib.Path]:
59+
def _get_single_codemeta(self, path: pathlib.Path) -> Union[pathlib.Path, None]:
5960
# Find CodeMeta files in directories and subdirectories
6061
# TODO: Do we really want to search recursive? Maybe add another option to enable pointing to a single file?
6162
# (So this stays "convention over configuration")

test/hermes_test/model/test_api_e2e.py

Lines changed: 119 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1+
# SPDX-FileCopyrightText: 2026 German Aerospace Center (DLR)
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
# SPDX-FileContributor: Michael Fritzsche
6+
17
import pytest
28
from hermes.commands.harvest.cff import CffHarvestPlugin, CffHarvestSettings
9+
from hermes.commands.harvest.codemeta import CodeMetaHarvestPlugin
310
from hermes.model import SoftwareMetadata
411

512

@@ -18,15 +25,88 @@
1825
email: max@muster.mann""",
1926
SoftwareMetadata({
2027
"@type": "SoftwareSourceCode",
21-
"schema:author": {
28+
"author": {
2229
"@list": [{
2330
"@type": "Person",
2431
"email": ["max@muster.mann"],
2532
"familyName": ["Mustermann"],
2633
"givenName": ["Max"]
2734
}]
2835
},
29-
"schema:name": ["Temp"]
36+
"name": ["Temp"]
37+
})
38+
),
39+
(
40+
"""# SPDX-FileCopyrightText: 2022 German Aerospace Center (DLR), Helmholtz-Zentrum Dresden-Rossendorf
41+
#
42+
# SPDX-License-Identifier: CC0-1.0
43+
44+
# SPDX-FileContributor: Michael Meinel
45+
46+
cff-version: 1.2.0
47+
title: hermes
48+
message: >-
49+
If you use this software, please cite it using the
50+
metadata from this file.
51+
version: 0.9.0
52+
license: "Apache-2.0"
53+
abstract: "Tool to automate software publication. Not stable yet."
54+
type: software
55+
authors:
56+
- given-names: Michael
57+
family-names: Meinel
58+
email: michael.meinel@dlr.de
59+
affiliation: German Aerospace Center (DLR)
60+
orcid: "https://orcid.org/0000-0001-6372-3853"
61+
- given-names: Stephan
62+
family-names: Druskat
63+
email: stephan.druskat@dlr.de
64+
affiliation: German Aerospace Center (DLR)
65+
orcid: "https://orcid.org/0000-0003-4925-7248"
66+
identifiers:
67+
- type: doi
68+
value: 10.5281/zenodo.13221384
69+
description: Version 0.8.1b1
70+
""",
71+
SoftwareMetadata({
72+
"@type": ["http://schema.org/SoftwareSourceCode"],
73+
"http://schema.org/author": [
74+
{
75+
"@list": [
76+
{
77+
"@id": "https://orcid.org/0000-0001-6372-3853",
78+
"@type": ["http://schema.org/Person"],
79+
"http://schema.org/affiliation": [
80+
{
81+
"@type": ["http://schema.org/Organization"],
82+
"http://schema.org/name": [{"@value": "German Aerospace Center (DLR)"}]
83+
}
84+
],
85+
"http://schema.org/email": [{"@value": "michael.meinel@dlr.de"}],
86+
"http://schema.org/familyName": [{"@value": "Meinel"}],
87+
"http://schema.org/givenName": [{"@value": "Michael"}]
88+
},
89+
{
90+
"@id": "https://orcid.org/0000-0003-4925-7248",
91+
"@type": ["http://schema.org/Person"],
92+
"http://schema.org/affiliation": [
93+
{
94+
"@type": ["http://schema.org/Organization"],
95+
"http://schema.org/name": [{"@value": "German Aerospace Center (DLR)"}]
96+
}
97+
],
98+
"http://schema.org/email": [{"@value": "stephan.druskat@dlr.de"}],
99+
"http://schema.org/familyName": [{"@value": "Druskat"}],
100+
"http://schema.org/givenName": [{"@value": "Stephan"}]
101+
}
102+
]
103+
}
104+
],
105+
"http://schema.org/description": [{"@value": "Tool to automate software publication. Not stable yet."}],
106+
"http://schema.org/identifier": [{"@id": "https://doi.org/10.5281/zenodo.13221384"}],
107+
"http://schema.org/license": [{"@id": "https://spdx.org/licenses/Apache-2.0"}],
108+
"http://schema.org/name": [{"@value": "hermes"}],
109+
"http://schema.org/version": [{"@value": "0.9.0"}]
30110
})
31111
)
32112
]
@@ -51,6 +131,42 @@ def __init__(self, args, settings):
51131
cff_file.write_text(cff)
52132

53133
result = CffHarvestPlugin().__call__(command)
54-
# FIXME: update to compare the SoftwareMetadata objects instead of the data_dicts
134+
# FIXME: update to compare the SoftwareMetadata objects instead of the data_dicts (in multiple places)
55135
# after merge with refactor/data-model and/or refactor/423-implement-public-api
56136
assert result[0].data_dict == res.data_dict
137+
138+
139+
@pytest.mark.parametrize(
140+
"codemeta, res",
141+
[
142+
(
143+
"""{
144+
"@context": "https://doi.org/10.5063/schema/codemeta-2.0",
145+
"type": "SoftwareSourceCode",
146+
"description": "for testing",
147+
"name": "Test"
148+
}""",
149+
SoftwareMetadata({
150+
"@type": ["http://schema.org/SoftwareSourceCode"],
151+
"http://schema.org/description": [{"@value": "for testing"}],
152+
"http://schema.org/name": [{"@value": "Test"}]
153+
})
154+
)
155+
]
156+
)
157+
def test_codemeta_harvest(tmp_path, codemeta, res):
158+
class Args:
159+
def __init__(self, path):
160+
self.path = path
161+
162+
class Command:
163+
def __init__(self, args):
164+
self.args = args
165+
166+
command = Command(Args(tmp_path))
167+
168+
codemeta_file = tmp_path / "codemeta.json"
169+
codemeta_file.write_text(codemeta)
170+
171+
result = CodeMetaHarvestPlugin().__call__(command)
172+
assert result[0].data_dict == res.data_dict

0 commit comments

Comments
 (0)