From 938a53b8f59d8fd0222f327183d683bdce2fb85b Mon Sep 17 00:00:00 2001 From: "Fabien C. Y. Benureau" Date: Mon, 6 Aug 2018 11:20:39 +0900 Subject: [PATCH] python setup.py test + no connection case - Make `python setup.py test work`. remove test/requirements.txt. fix #154 - Detect if no connection is present, and skip tests if not. stop-gap measure for issue #147 --- .travis.yml | 4 +--- pubs/apis.py | 9 ++++++--- requirements.txt | 8 ++++++++ setup.py | 12 ++++++++++-- tests/requirements.txt | 5 ----- tests/test_apis.py | 33 ++++++++++++++++++++++++--------- 6 files changed, 49 insertions(+), 22 deletions(-) delete mode 100644 tests/requirements.txt diff --git a/.travis.yml b/.travis.yml index 8173790..132be5d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -36,8 +36,6 @@ matrix: # command to install dependencies install: - python --version - - pip install -r tests/requirements.txt - - python setup.py install # command to run tests -script: python -m unittest discover +script: python setup.py test diff --git a/pubs/apis.py b/pubs/apis.py index 3d57ec9..b816abd 100644 --- a/pubs/apis.py +++ b/pubs/apis.py @@ -13,7 +13,7 @@ class ReferenceNotFoundError(Exception): pass -def get_bibentry_from_api(id_str, id_type, rp): +def get_bibentry_from_api(id_str, id_type, rp, ui=None): """Return a bibtex string from various ID methods. This is a wrapper around functions that will return a bibtex string given @@ -27,12 +27,14 @@ def get_bibentry_from_api(id_str, id_type, rp): id_str: A string with the ID. id_type: Name of the ID type. Must be one of `doi`, `isbn`, or `arxiv`. rp: A `Repository` object. + ui: A UI object. Returns: A bibtex string. Raises: ValueError: if `id_type` is not one of `doi`, `isbn`, or `arxiv`. + apis.ReferenceNotFoundException: if no valid reference could be found. """ id_fns = { @@ -69,7 +71,7 @@ def _get_request(url, headers=None): ## DOI support -def doi2bibtex(doi): +def doi2bibtex(doi, **kwargs): """Return a bibtex string of metadata from a DOI""" url = 'http://dx.doi.org/{}'.format(doi) @@ -83,7 +85,8 @@ def doi2bibtex(doi): ## ISBN support -def isbn2bibtex(isbn): + +def isbn2bibtex(isbn, **kwargs): """Return a bibtex string of metadata from an ISBN""" url = 'http://www.ottobib.com/isbn/{}/bibtex'.format(isbn) diff --git a/requirements.txt b/requirements.txt index da1abec..d757303 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ +-e . pyyaml bibtexparser>=1.0 python-dateutil @@ -5,3 +6,10 @@ requests configobj beautifulsoup4 feedparser +six + +# those are the additional packages required to run the tests +pyfakefs +ddt +mock +pytest # optional (python setup.py test works without it), but possible nonetheless diff --git a/setup.py b/setup.py index e1fd8a9..50058b5 100644 --- a/setup.py +++ b/setup.py @@ -1,10 +1,16 @@ #!/usr/bin/env python +import unittest from setuptools import setup with open('pubs/version.py') as f: exec(f.read()) # defines __version__ +def pubs_test_suite(): + test_loader = unittest.TestLoader() + test_suite = test_loader.discover('tests', pattern='test_*.py') + return test_suite + setup( name='pubs', version=__version__, @@ -26,9 +32,8 @@ setup( ], }, - install_requires=['pyyaml', 'bibtexparser>=1.0', 'python-dateutil', + install_requires=['pyyaml', 'bibtexparser>=1.0', 'python-dateutil', 'six', 'requests', 'configobj', 'beautifulsoup4', 'feedparser'], - tests_require=['pyfakefs>=2.7', 'mock'], extras_require={'autocompletion': ['argcomplete'], }, @@ -41,6 +46,9 @@ setup( 'Intended Audience :: Science/Research', ], + test_suite= 'tests', + tests_require=['pyfakefs>=3.4', 'mock', 'ddt'], + # in order to avoid 'zipimport.ZipImportError: bad local file header' zip_safe=False, diff --git a/tests/requirements.txt b/tests/requirements.txt deleted file mode 100644 index f3726e3..0000000 --- a/tests/requirements.txt +++ /dev/null @@ -1,5 +0,0 @@ -# those are the additional packages required to run the tests -six -pyfakefs -ddt -mock diff --git a/tests/test_apis.py b/tests/test_apis.py index 2922016..6886aa1 100644 --- a/tests/test_apis.py +++ b/tests/test_apis.py @@ -12,11 +12,29 @@ from pubs.apis import ReferenceNotFoundError, arxiv2bibtex, doi2bibtex, isbn2bib from pubs import apis -class TestDOI2Bibtex(unittest.TestCase): +def _is_connected(): + """Return False if no internet connection is detected. + + May not work if the local network redirects the connection. + """ + try: + host = socket.gethostbyname('www.google.com') + s = socket.create_connection((host, 80), 2) + return True + except: + return False + +class APITests(unittest.TestCase): def setUp(self): + if not _is_connected(): + self.skipTest('no connection detected, skiping test') self.endecoder = EnDecoder() + + +class TestDOI2Bibtex(APITests): + def test_unicode(self): bib = doi2bibtex('10.1007/BF01700692') self.assertIsInstance(bib, ustr) @@ -37,10 +55,7 @@ class TestDOI2Bibtex(unittest.TestCase): doi2bibtex('999999') -class TestISBN2Bibtex(unittest.TestCase): - - def setUp(self): - self.endecoder = EnDecoder() +class TestISBN2Bibtex(APITests): def test_unicode(self): bib = isbn2bibtex('9782081336742') @@ -61,10 +76,7 @@ class TestISBN2Bibtex(unittest.TestCase): self.endecoder.decode_bibdata(bib) -class TestArxiv2Bibtex(unittest.TestCase): - - def setUp(self): - self.endecoder = EnDecoder() +class TestArxiv2Bibtex(APITests): def test_parses_to_bibtex_with_doi(self): bib = arxiv2bibtex('astro-ph/9812133') @@ -85,6 +97,9 @@ class TestArxiv2Bibtex(unittest.TestCase): entry['title'], 'The entropy formula for the Ricci flow and its geometric applications') +class TestArxiv2BibtexLocal(unittest.TestCase): + """Test arXiv 2 Bibtex connection; those tests don't require a connection""" + def test_oldstyle_pattern(self): """Test that we can accurately differentiate between old and new style arXiv ids.""" # old-style arXiv ids