-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathtest_metadata.py
185 lines (172 loc) · 6.89 KB
/
test_metadata.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# Copyright 2019-2025 The University of Manchester, UK
# Copyright 2020-2025 Vlaams Instituut voor Biotechnologie (VIB), BE
# Copyright 2020-2025 Barcelona Supercomputing Center (BSC), ES
# Copyright 2020-2025 Center for Advanced Studies, Research and Development in Sardinia (CRS4), IT
# Copyright 2022-2025 École Polytechnique Fédérale de Lausanne, CH
# Copyright 2024-2025 Data Centre, SciLifeLab, SE
# Copyright 2024-2025 National Institute of Informatics (NII), JP
# Copyright 2025 Senckenberg Society for Nature Research (SGN), DE
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import pytest
from copy import deepcopy
from rocrate.metadata import find_root_entity_id
@pytest.mark.parametrize("root,basename", [
("", "ro-crate-metadata.json"),
("", "ro-crate-metadata.jsonld"),
("https://example.org/crate/", "ro-crate-metadata.json"),
("https://example.org/crate/", "ro-crate-metadata.jsonld"),
("", "bad-name.json"),
])
def test_find_root(root, basename):
metadata_id = root + basename
root_id = root or "./"
entities = {_["@id"]: _ for _ in [
{
"@id": metadata_id,
"@type": "CreativeWork",
"about": {"@id": root_id},
"conformsTo": [
{"@id": "https://w3id.org/ro/crate/1.1"},
{"@id": "https://example.org/fancy-ro-crate/1.0"},
]
},
{
"@id": root_id,
"@type": "Dataset",
},
]}
if basename not in {"ro-crate-metadata.json", "ro-crate-metadata.jsonld"}:
with pytest.raises(KeyError):
find_root_entity_id(entities)
else:
assert find_root_entity_id(entities) == (metadata_id, root_id)
def test_find_root_bad_entities():
orig_entities = {
"ro-crate-metadata.json": {
"@id": "ro-crate-metadata.json",
"@type": "CreativeWork",
"about": {"@id": "./"},
"conformsTo": {"@id": "https://w3id.org/ro/crate/1.1"},
},
"./": {
"@id": "./",
"@type": "Dataset",
},
}
# missing "about"
entities = deepcopy(orig_entities)
del entities["ro-crate-metadata.json"]["about"]
with pytest.raises(ValueError, match="does not reference"):
find_root_entity_id(entities)
# "about" does not reference the root entity
entities = deepcopy(orig_entities)
for about in "http://example.org", {"@id": "http://example.org"}:
entities["ro-crate-metadata.json"]["about"] = about
with pytest.raises(ValueError, match="does not reference"):
find_root_entity_id(entities)
# metadata type is not CreativeWork
entities = deepcopy(orig_entities)
entities["ro-crate-metadata.json"]["@type"] = "Thing"
with pytest.raises(ValueError, match="must be of type"):
find_root_entity_id(entities)
# root type is not Dataset
entities = deepcopy(orig_entities)
entities["./"]["@type"] = "Thing"
with pytest.raises(ValueError, match="must have"):
find_root_entity_id(entities)
@pytest.mark.filterwarnings("ignore")
def test_find_root_multiple_entries():
orig_entities = {
"http://example.org/ro-crate-metadata.json": {
"@id": "http://example.org/ro-crate-metadata.json",
"@type": "CreativeWork",
"about": {"@id": "http://example.org/"},
"conformsTo": {"@id": "https://w3id.org/ro/crate/1.1"},
},
"http://example.org/": {
"@id": "http://example.org/",
"@type": "Dataset",
"hasPart": [
{"@id": "http://example.com/"},
{"@id": "http://example.com/ro-crate-metadata.json"}
]
},
"http://example.com/ro-crate-metadata.json": {
"@id": "http://example.com/ro-crate-metadata.json",
"@type": "CreativeWork",
"about": {"@id": "http://example.com/"},
"conformsTo": {"@id": "https://w3id.com/ro/crate/1.1"},
},
"http://example.com/": {
"@id": "http://example.com/",
"@type": "Dataset",
},
}
def check_finds_org(entities):
m_id, r_id = find_root_entity_id(entities)
assert m_id == "http://example.org/ro-crate-metadata.json"
assert r_id == "http://example.org/"
def check_picks_one(entities):
m_id, r_id = find_root_entity_id(entities)
assert m_id in [f"http://example.{_}/ro-crate-metadata.json" for _ in ("org", "com")]
assert r_id in [f"http://example.{_}/" for _ in ("org", "com")]
check_finds_org(orig_entities)
# no root candidate contains the other one
mod_entities = deepcopy(orig_entities)
del mod_entities["http://example.org/"]["hasPart"]
check_picks_one(mod_entities)
# each root candidate contains the other one
mod_entities = deepcopy(orig_entities)
mod_entities["http://example.com/"]["hasPart"] = [
{"@id": "http://example.org/"},
{"@id": "http://example.org/ro-crate-metadata.json"}
]
check_picks_one(mod_entities)
# "about" does not reference the root entity
mod_entities = deepcopy(orig_entities)
for about in "http://google.com", {"@id": "http://google.com"}:
mod_entities["http://example.com/ro-crate-metadata.json"]["about"] = about
check_finds_org(mod_entities)
# metadata type is not CreativeWork
mod_entities = deepcopy(orig_entities)
mod_entities["http://example.com/ro-crate-metadata.json"]["@type"] = "Thing"
check_finds_org(mod_entities)
# root type is not Dataset
mod_entities = deepcopy(orig_entities)
mod_entities["http://example.com/"]["@type"] = "Thing"
check_finds_org(mod_entities)
def test_find_root_multiple_types():
entities = {_["@id"]: _ for _ in [
{
"@id": "ro-crate-metadata.json",
"@type": "CreativeWork",
"about": {"@id": "./"},
"conformsTo": {"@id": "https://w3id.org/ro/crate/1.1"},
},
{
"@id": "./",
"@type": ["Dataset", "RepositoryCollection"],
},
]}
m_id, r_id = find_root_entity_id(entities)
assert m_id == "ro-crate-metadata.json"
assert r_id == "./"
# "Dataset" not included
del entities["./"]["@type"][0]
with pytest.raises(ValueError):
find_root_entity_id(entities)
# Check we're not trying to be too clever
entities["./"]["@type"] = "NotADataset"
with pytest.raises(ValueError):
find_root_entity_id(entities)