Skip to content

Commit 698b8a1

Browse files
committed
try to improve test coverage
1 parent b2b7560 commit 698b8a1

File tree

4 files changed

+56
-11
lines changed

4 files changed

+56
-11
lines changed

repo2docker/contentproviders/doi.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import os
2+
import abc
23
import json
34
import shutil
45
import logging
56

67
from os import makedirs
78
from os import path
8-
from urllib.request import urlopen, Request
9+
from urllib import request # urlopen, Request
910
from urllib.error import HTTPError
1011
from zipfile import ZipFile, is_zipfile
1112

@@ -21,15 +22,15 @@ class DoiProvider(ContentProvider):
2122
def urlopen(self, req, headers=None):
2223
"""A urlopen() helper"""
2324
# someone passed a string, not a request
24-
if not isinstance(req, Request):
25-
req = Request(req)
25+
if not isinstance(req, request.Request):
26+
req = request.Request(req)
2627

2728
req.add_header("User-Agent", "repo2docker {}".format(__version__))
2829
if headers is not None:
2930
for key, value in headers.items():
3031
req.add_header(key, value)
3132

32-
return urlopen(req)
33+
return request.urlopen(req)
3334

3435
def doi2url(self, doi):
3536
# Transform a DOI to a URL
@@ -90,6 +91,6 @@ def fetch_file(self, file_ref, host, output_dir, unzip=False):
9091
yield "Fetched files: {}\n".format(os.listdir(output_dir))
9192

9293
@property
94+
@abc.abstractmethod
9395
def content_id(self):
94-
"""The provider's ID for the record"""
95-
return None
96+
pass

repo2docker/contentproviders/figshare.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,14 @@ def detect(self, doi, ref=None, extra_args=None):
6868
def fetch(self, spec, output_dir, yield_output=False):
6969
"""Fetch and unpack a Figshare article"""
7070
article_id = spec["article"]
71+
article_version = spec["version"]
7172
host = spec["host"]
7273

7374
yield "Fetching Figshare article {} in version {}.\n".format(
74-
self.article_id, self.article_version
75+
article_id, article_version
7576
)
7677
req = Request(
77-
"{}{}/versions/{}".format(
78-
host["api"], self.article_id, self.article_version
79-
),
78+
"{}{}/versions/{}".format(host["api"], article_id, article_version),
8079
headers={"accept": "application/json"},
8180
)
8281
resp = self.urlopen(req)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import json
2+
import os
3+
import re
4+
import urllib
5+
import pytest
6+
import tempfile
7+
import logging
8+
9+
from unittest.mock import patch, MagicMock, mock_open
10+
from zipfile import ZipFile
11+
12+
from repo2docker.contentproviders.doi import DoiProvider
13+
from repo2docker.contentproviders.base import ContentProviderException
14+
15+
16+
def test_content_id():
17+
doi = DoiProvider()
18+
assert doi.content_id is None
19+
20+
21+
def fake_urlopen(req):
22+
print(req)
23+
return req.headers
24+
25+
26+
@patch("urllib.request.urlopen", fake_urlopen)
27+
def test_url_headers():
28+
doi = DoiProvider()
29+
30+
headers = {"test1": "value1", "Test2": "value2"}
31+
result = doi.urlopen("https://mybinder.org", headers=headers)
32+
assert "Test1" in result
33+
assert "Test2" in result
34+
assert len(result) is 3 # User-agent is also set
35+
36+
37+
def test_unresolving_doi():
38+
doi = DoiProvider()
39+
40+
fakedoi = "10.1/1234"
41+
assert doi.doi2url(fakedoi) is fakedoi

tests/unit/contentproviders/test_figshare.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,13 @@ def test_content_id():
6363
"https://figshare.com/articles/title/9782777/",
6464
{"host": test_fig.hosts[0], "article": "9782777", "version": "1"},
6565
),
66+
(
67+
"https://figshare.com/articles/title/9782777/1234",
68+
{"host": test_fig.hosts[0], "article": "9782777", "version": "1234"},
69+
),
6670
]
6771

68-
test_spec = {"host": test_fig.hosts[0], "article": "1234", "version": "42"}
72+
test_spec = {"host": test_fig.hosts[0], "article": "123456", "version": "42"}
6973

7074

7175
@pytest.mark.parametrize("test_input,expected", test_dois_links)

0 commit comments

Comments
 (0)