add arXiv tests
This commit is contained in:
parent
c62d1c3c50
commit
30a7094eaf
@ -40,7 +40,5 @@ install:
|
||||
|
||||
# command to run tests
|
||||
script:
|
||||
- export PUBS_TESTS_MODE=ONLINE
|
||||
- python setup.py test
|
||||
- export PUBS_TESTS_MODE=MOCK
|
||||
- python setup.py test
|
||||
- PUBS_TESTS_MODE=MOCK python setup.py test
|
||||
- PUBS_TESTS_MODE=COLLECT python setup.py test
|
||||
|
@ -192,7 +192,7 @@ def arxiv2bibtex(arxiv_id, try_doi=True, ui=None):
|
||||
if not _is_arxiv_oldstyle(entry_id):
|
||||
db.entries[0]['eprintclass'] = entry['arxiv_primary_category']['term']
|
||||
if 'arxiv_doi' in entry:
|
||||
db.entries[0]['arxiv_doi'] = arxiv_doi
|
||||
db.entries[0]['arxiv_doi'] = entry['arxiv_doi']
|
||||
|
||||
bibtex = bibtexparser.dumps(db)
|
||||
return bibtex
|
||||
|
@ -23,6 +23,7 @@ running tests on Travis for instance.
|
||||
|
||||
import os
|
||||
import json
|
||||
import mock
|
||||
|
||||
import requests
|
||||
|
||||
@ -31,6 +32,7 @@ _orgininal_requests_get = requests.get
|
||||
_collected_responses = []
|
||||
_data_filepath = os.path.join(os.path.dirname(__file__), 'test_apis_data.json')
|
||||
|
||||
|
||||
class MockingResponse:
|
||||
def __init__(self, text, status_code=200, error_msg=None):
|
||||
self.text = text
|
||||
@ -43,6 +45,19 @@ class MockingResponse:
|
||||
raise requests.exceptions.RequestException(self.error_msg)
|
||||
|
||||
|
||||
def intercept_text(text):
|
||||
try:
|
||||
if '10.1103/PhysRevD.89.084044' in text:
|
||||
# replace with wrong DOI
|
||||
text = text.replace('PhysRevD', 'INVALIDDOI')
|
||||
except TypeError:
|
||||
if b'10.1103/PhysRevD.89.084044' in text:
|
||||
# replace with wrong DOI
|
||||
text = text.replace(b'PhysRevD', b'INVALIDDOI')
|
||||
|
||||
return text
|
||||
|
||||
|
||||
mode = os.environ.get('PUBS_TESTS_MODE', 'MOCK')
|
||||
|
||||
if mode == 'MOCK':
|
||||
@ -56,7 +71,7 @@ if mode == 'MOCK':
|
||||
return MockingResponse(text, status_code, error_msg)
|
||||
raise KeyError(('No stub data found for requests.get({}, {}).\n You may'
|
||||
' need to update the mock data. Look at the '
|
||||
'tests/mock_requests.py file for explanation').format(args, kwargs))
|
||||
'tests/mock_requests.py file for an explanation').format(args, kwargs))
|
||||
|
||||
elif mode == 'COLLECT':
|
||||
|
||||
@ -74,12 +89,18 @@ elif mode == 'COLLECT':
|
||||
# clear how to run once after all the tests
|
||||
# have run. If you figure it out...
|
||||
|
||||
text = intercept_text(text)
|
||||
return MockingResponse(text, status_code, error_msg)
|
||||
|
||||
def _save_collected_responses():
|
||||
with open(os.path.join(_data_filepath), 'w') as fd:
|
||||
json.dump(_collected_responses, fd)
|
||||
json.dump(_collected_responses, fd, indent=2)
|
||||
|
||||
elif mode == 'ONLINE':
|
||||
def mock_requests_get(*args, **kwargs):
|
||||
return _orgininal_requests_get(*args, **kwargs)
|
||||
# with mock.patch('requests.Response.text', new_callable=mock.PropertyMock) as mock_text:
|
||||
r = _orgininal_requests_get(*args, **kwargs)
|
||||
r._content = intercept_text(r.content)
|
||||
# print(r.content.__class__)
|
||||
# mock_text.return_value = intercept_text(r.text)
|
||||
return r
|
||||
|
@ -104,6 +104,36 @@ class TestArxiv2Bibtex(APITests):
|
||||
entry['title'],
|
||||
'The entropy formula for the Ricci flow and its geometric applications')
|
||||
|
||||
@mock.patch('pubs.apis.requests.get', side_effect=mock_requests.mock_requests_get)
|
||||
def test_arxiv_wrong_id(self, reqget):
|
||||
with self.assertRaises(ReferenceNotFoundError):
|
||||
bib = arxiv2bibtex('INVALIDID')
|
||||
|
||||
@mock.patch('pubs.apis.requests.get', side_effect=mock_requests.mock_requests_get)
|
||||
def test_arxiv_wrong_doi(self, reqget):
|
||||
bib = arxiv2bibtex('1312.2021')
|
||||
b = self.endecoder.decode_bibdata(bib)
|
||||
entry = b[list(b)[0]]
|
||||
self.assertEqual(entry['arxiv_doi'], '10.1103/INVALIDDOI.89.084044')
|
||||
|
||||
@mock.patch('pubs.apis.requests.get', side_effect=mock_requests.mock_requests_get)
|
||||
def test_arxiv_good_doi(self, reqget):
|
||||
"""Get the DOI bibtex instead of the arXiv one if possible"""
|
||||
bib = arxiv2bibtex('1710.08557')
|
||||
b = self.endecoder.decode_bibdata(bib)
|
||||
entry = b[list(b)[0]]
|
||||
self.assertTrue(not 'arxiv_doi' in entry)
|
||||
self.assertEqual(entry['doi'], '10.1186/s12984-017-0305-3')
|
||||
self.assertEqual(entry['title'].lower(), 'on neuromechanical approaches for the study of biological and robotic grasp and manipulation')
|
||||
|
||||
@mock.patch('pubs.apis.requests.get', side_effect=mock_requests.mock_requests_get)
|
||||
def test_arxiv_good_doi_force_arxiv(self, reqget):
|
||||
bib = arxiv2bibtex('1710.08557', try_doi=False)
|
||||
b = self.endecoder.decode_bibdata(bib)
|
||||
entry = b[list(b)[0]]
|
||||
self.assertEqual(entry['arxiv_doi'], '10.1186/s12984-017-0305-3')
|
||||
self.assertEqual(entry['title'].lower(), 'on neuromechanical approaches for the study of biological grasp and\nmanipulation')
|
||||
|
||||
|
||||
class TestArxiv2BibtexLocal(unittest.TestCase):
|
||||
"""Test arXiv 2 Bibtex connection; those tests don't require a connection"""
|
||||
|
File diff suppressed because one or more lines are too long
Loading…
x
Reference in New Issue
Block a user