try to improve test coverage

pull/788/head
Daniel Nüst 2019-09-11 13:17:48 +02:00
rodzic b2b75600c0
commit 698b8a1d7e
4 zmienionych plików z 56 dodań i 11 usunięć

Wyświetl plik

@ -1,11 +1,12 @@
import os
import abc
import json
import shutil
import logging
from os import makedirs
from os import path
from urllib.request import urlopen, Request
from urllib import request # urlopen, Request
from urllib.error import HTTPError
from zipfile import ZipFile, is_zipfile
@ -21,15 +22,15 @@ class DoiProvider(ContentProvider):
def urlopen(self, req, headers=None):
"""A urlopen() helper"""
# someone passed a string, not a request
if not isinstance(req, Request):
req = Request(req)
if not isinstance(req, request.Request):
req = request.Request(req)
req.add_header("User-Agent", "repo2docker {}".format(__version__))
if headers is not None:
for key, value in headers.items():
req.add_header(key, value)
return urlopen(req)
return request.urlopen(req)
def doi2url(self, doi):
# Transform a DOI to a URL
@ -90,6 +91,6 @@ class DoiProvider(ContentProvider):
yield "Fetched files: {}\n".format(os.listdir(output_dir))
@property
@abc.abstractmethod
def content_id(self):
"""The provider's ID for the record"""
return None
pass

Wyświetl plik

@ -68,15 +68,14 @@ class Figshare(DoiProvider):
def fetch(self, spec, output_dir, yield_output=False):
"""Fetch and unpack a Figshare article"""
article_id = spec["article"]
article_version = spec["version"]
host = spec["host"]
yield "Fetching Figshare article {} in version {}.\n".format(
self.article_id, self.article_version
article_id, article_version
)
req = Request(
"{}{}/versions/{}".format(
host["api"], self.article_id, self.article_version
),
"{}{}/versions/{}".format(host["api"], article_id, article_version),
headers={"accept": "application/json"},
)
resp = self.urlopen(req)

Wyświetl plik

@ -0,0 +1,41 @@
import json
import os
import re
import urllib
import pytest
import tempfile
import logging
from unittest.mock import patch, MagicMock, mock_open
from zipfile import ZipFile
from repo2docker.contentproviders.doi import DoiProvider
from repo2docker.contentproviders.base import ContentProviderException
def test_content_id():
doi = DoiProvider()
assert doi.content_id is None
def fake_urlopen(req):
print(req)
return req.headers
@patch("urllib.request.urlopen", fake_urlopen)
def test_url_headers():
doi = DoiProvider()
headers = {"test1": "value1", "Test2": "value2"}
result = doi.urlopen("https://mybinder.org", headers=headers)
assert "Test1" in result
assert "Test2" in result
assert len(result) is 3 # User-agent is also set
def test_unresolving_doi():
doi = DoiProvider()
fakedoi = "10.1/1234"
assert doi.doi2url(fakedoi) is fakedoi

Wyświetl plik

@ -63,9 +63,13 @@ test_dois_links = [
"https://figshare.com/articles/title/9782777/",
{"host": test_fig.hosts[0], "article": "9782777", "version": "1"},
),
(
"https://figshare.com/articles/title/9782777/1234",
{"host": test_fig.hosts[0], "article": "9782777", "version": "1234"},
),
]
test_spec = {"host": test_fig.hosts[0], "article": "1234", "version": "42"}
test_spec = {"host": test_fig.hosts[0], "article": "123456", "version": "42"}
@pytest.mark.parametrize("test_input,expected", test_dois_links)