Commit e94e99ae authored by LE GAC Renaud's avatar LE GAC Renaud
Browse files

Polish InvenioStore and its tests

parent 8eb3373d
......@@ -5,6 +5,7 @@ import requests
from .exception import CdsException
from requests.adapters import HTTPAdapter
from builtins import getattr
CDS_SEARCH_KEYS = ("req", "cc", "c", "ec", "p", "f", "rg", "sf", "so", "sp",
"rm", "of", "ot", "as", "p1", "f1", "m1", "op1", "p2", "f2",
......@@ -94,7 +95,8 @@ class InvenioStore(object):
def __del__(self):
# close the session
self._session.close()
if getattr(self, "_session", None) is not None:
self._session.close()
def interrogate(self, url, timeout=10, **kwargs):
"""Interrogate the store using the *URL*.
......@@ -498,6 +500,7 @@ class InvenioStore(object):
* the server return an HTTP error.
* no JSON object could be decoded.
* more than one record
"""
url = "%s/%s" % (self._api_record, rec_id)
......@@ -509,12 +512,18 @@ class InvenioStore(object):
rep = self.interrogate(url, timeout=30, **kwargs)
try:
li = rep.json()
obj = rep.json()
except ValueError:
raise CdsException(MSG_HTTP_DECODE)
return li[0]
if isinstance(obj, dict):
return obj
if isinstance(obj, list) and len(obj) == 1:
return obj[0]
raise CdsException(MSG_HTTP_DECODE)
def last_search_url(self):
"""
......
......@@ -3,10 +3,18 @@
Test methods of the class InveniStore
"""
import pytest
from invenio_tools.exception import CdsException
from invenio_tools.inveniostore import InvenioStore
def test_get_ids_cds_01001():
def test_constructor_exception_01001():
with pytest.raises(CdsException):
InvenioStore("cds.cern.ch", shelf="literature")
def test_get_ids_cds_01002():
"""Check the list of record ids for LHCb articles published in 2015.
"""
......@@ -36,7 +44,7 @@ def test_get_ids_cds_01001():
assert set(rec_ids).difference(ref_2020_ids) == set()
def test_get_ids_ins_01002():
def test_get_ids_ins_01003():
"""Check the list of record ids for LHCb articles published in 2010.
"""
......@@ -54,3 +62,55 @@ def test_get_ids_ins_01002():
ref_2019_ids = {865584, 867355, 867647}
assert set(rec_ids).difference(ref_2019_ids) == set()
def test_get_ids_exception_01004():
store = InvenioStore("inspirehep.net", shelf="conferences")
with pytest.raises(CdsException) as e_info:
store.get_ids(p="find cn lhcb")
def test_get_record_cds_01005():
store = InvenioStore("cds.cern.ch")
recjson = store.get_record(1951625)
assert isinstance(recjson, dict)
def test_get_record_ins_none_01006():
# old inspirehep interface
store = InvenioStore("inspirehep.net", shelf=None)
recjson = store.get_record(1319638)
assert isinstance(recjson, dict)
def test_get_record_ins_literature_01007():
# old inspirehep interface (new one not yet available)
store = InvenioStore("inspirehep.net", shelf="literature")
recjson = store.get_record(1319638)
assert isinstance(recjson, dict)
def test_get_record_ins_conferences_01008():
# new inspirehep interface for conferences
store = InvenioStore("inspirehep.net", shelf="conferences")
recjson = store.get_record(980401)
assert recjson["metadata"]["cnum"] == "C10-12-06"
def test_get_record_ins_institutions_01009():
# old inspirehep interface (new one not yet available)
store = InvenioStore("inspirehep.net", shelf="institutions")
recjson = store.get_record(902989)
assert isinstance(recjson, dict)
def test_interrogate_citations_01010():
# old inspirehep interface (new one not yet available)
url = "https://old.inspirehep.net/record/1319638"
kwargs = dict(of="recjson", ot="number_of_citations")
store = InvenioStore("inspirehep.net")
rep = store.interrogate(url, timeout=60, **kwargs)
lst = rep.json()
assert str(lst[0]["number_of_citations"]).isdigit()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment