Skip to content

Commit e6cd4b1

Browse files
committed
reuse hosts definition in Zenodo tests
1 parent 0e4029b commit e6cd4b1

File tree

2 files changed

+31
-93
lines changed

2 files changed

+31
-93
lines changed

repo2docker/contentproviders/zenodo.py

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,36 +14,36 @@
1414
class Zenodo(DoiProvider):
1515
"""Provide contents of a Zenodo deposit."""
1616

17+
# We need the hostname (url where records are), api url (for metadata),
18+
# filepath (path to files in metadata), filename (path to filename in
19+
# metadata), download (path to file download URL), and type (path to item type in metadata)
20+
hosts = [
21+
{
22+
"hostname": ["https://zenodo.org/record/", "http://zenodo.org/record/"],
23+
"api": "https://zenodo.org/api/records/",
24+
"filepath": "files",
25+
"filename": "filename",
26+
"download": "links.download",
27+
"type": "metadata.upload_type",
28+
},
29+
{
30+
"hostname": [
31+
"https://data.caltech.edu/records/",
32+
"http://data.caltech.edu/records/",
33+
],
34+
"api": "https://data.caltech.edu/api/record/",
35+
"filepath": "metadata.electronic_location_and_access",
36+
"filename": "electronic_name.0",
37+
"download": "uniform_resource_identifier",
38+
"type": "metadata.resourceType.resourceTypeGeneral",
39+
},
40+
]
41+
1742
def detect(self, doi, ref=None, extra_args=None):
1843
"""Trigger this provider for things that resolve to a Zenodo/Invenio record"""
19-
# We need the hostname (url where records are), api url (for metadata),
20-
# filepath (path to files in metadata), filename (path to filename in
21-
# metadata), download (path to file download URL), and type (path to item type in metadata)
22-
hosts = [
23-
{
24-
"hostname": ["https://zenodo.org/record/", "http://zenodo.org/record/"],
25-
"api": "https://zenodo.org/api/records/",
26-
"filepath": "files",
27-
"filename": "filename",
28-
"download": "links.download",
29-
"type": "metadata.upload_type",
30-
},
31-
{
32-
"hostname": [
33-
"https://data.caltech.edu/records/",
34-
"http://data.caltech.edu/records/",
35-
],
36-
"api": "https://data.caltech.edu/api/record/",
37-
"filepath": "metadata.electronic_location_and_access",
38-
"filename": "electronic_name.0",
39-
"download": "uniform_resource_identifier",
40-
"type": "metadata.resourceType.resourceTypeGeneral",
41-
},
42-
]
43-
4444
url = self.doi2url(doi)
4545

46-
for host in hosts:
46+
for host in self.hosts:
4747
if any([url.startswith(s) for s in host["hostname"]]):
4848
self.record_id = url.rsplit("/", maxsplit=1)[1]
4949
return {"record": self.record_id, "host": host}

tests/unit/contentproviders/test_zenodo.py

Lines changed: 5 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -28,38 +28,15 @@ def test_content_id():
2828
"10.5281/zenodo.3232985",
2929
"https://doi.org/10.5281/zenodo.3232985",
3030
],
31-
{
32-
"host": {
33-
"hostname": ["https://zenodo.org/record/", "http://zenodo.org/record/"],
34-
"api": "https://zenodo.org/api/records/",
35-
"filepath": "files",
36-
"filename": "filename",
37-
"download": "links.download",
38-
"type": "metadata.upload_type",
39-
},
40-
"record": "3232985",
41-
},
31+
{"host": Zenodo.hosts[0], "record": "3232985"},
4232
),
4333
(
4434
[
4535
"https://data.caltech.edu/records/1235",
4636
"10.22002/d1.1235",
4737
"https://doi.org/10.22002/d1.1235",
4838
],
49-
{
50-
"host": {
51-
"hostname": [
52-
"https://data.caltech.edu/records/",
53-
"http://data.caltech.edu/records/",
54-
],
55-
"api": "https://data.caltech.edu/api/record/",
56-
"filepath": "metadata.electronic_location_and_access",
57-
"filename": "electronic_name.0",
58-
"download": "uniform_resource_identifier",
59-
"type": "metadata.resourceType.resourceTypeGeneral",
60-
},
61-
"record": "1235",
62-
},
39+
{"host": Zenodo.hosts[1], "record": "1235"},
6340
),
6441
]
6542

@@ -122,20 +99,7 @@ def mock_urlopen(self, req):
12299

123100
with patch.object(Zenodo, "urlopen", new=mock_urlopen):
124101
zen = Zenodo()
125-
spec = {
126-
"host": {
127-
"hostname": [
128-
"https://zenodo.org/record/",
129-
"http://zenodo.org/record/",
130-
],
131-
"api": "https://zenodo.org/api/records/",
132-
"filepath": "files",
133-
"filename": "filename",
134-
"download": "links.download",
135-
"type": "metadata.upload_type",
136-
},
137-
"record": "1234",
138-
}
102+
spec = {"host": Zenodo.hosts[0], "record": "1234"}
139103

140104
with TemporaryDirectory() as d:
141105
output = []
@@ -176,20 +140,7 @@ def mock_urlopen(self, req):
176140
with patch.object(Zenodo, "urlopen", new=mock_urlopen):
177141
with TemporaryDirectory() as d:
178142
zen = Zenodo()
179-
spec = spec = {
180-
"host": {
181-
"hostname": [
182-
"https://zenodo.org/record/",
183-
"http://zenodo.org/record/",
184-
],
185-
"api": "https://zenodo.org/api/records/",
186-
"filepath": "files",
187-
"filename": "filename",
188-
"download": "links.download",
189-
"type": "metadata.upload_type",
190-
},
191-
"record": "1234",
192-
}
143+
spec = spec = {"host": Zenodo.hosts[0], "record": "1234"}
193144
output = []
194145
for l in zen.fetch(spec, d):
195146
output.append(l)
@@ -230,20 +181,7 @@ def mock_urlopen(self, req):
230181
with patch.object(Zenodo, "urlopen", new=mock_urlopen):
231182
with TemporaryDirectory() as d:
232183
zen = Zenodo()
233-
spec = {
234-
"host": {
235-
"hostname": [
236-
"https://zenodo.org/record/",
237-
"http://zenodo.org/record/",
238-
],
239-
"api": "https://zenodo.org/api/records/",
240-
"filepath": "files",
241-
"filename": "filename",
242-
"download": "links.download",
243-
"type": "metadata.upload_type",
244-
},
245-
"record": "1234",
246-
}
184+
spec = {"host": Zenodo.hosts[0], "record": "1234"}
247185
output = []
248186
for l in zen.fetch(spec, d):
249187
output.append(l)

0 commit comments

Comments
 (0)