Uses get_data helper in isbn search (plus test)

This commit is contained in:
Mouse Reeve 2021-03-13 09:07:19 -08:00
parent e459c440de
commit d3b1941eaa
2 changed files with 17 additions and 15 deletions

View file

@ -57,24 +57,14 @@ class AbstractMinimalConnector(ABC):
def isbn_search(self, query):
""" isbn search """
params = {}
resp = requests.get(
data = get_data(
"%s%s" % (self.isbn_search_url, query),
params=params,
headers={
"Accept": "application/json; charset=utf-8",
"User-Agent": settings.USER_AGENT,
},
)
if not resp.ok:
resp.raise_for_status()
try:
data = resp.json()
except ValueError as e:
logger.exception(e)
raise ConnectorException("Unable to parse json response", e)
results = []
for doc in self.parse_isbn_search_data(data):
# this shouldn't be returning mutliple results, but just in case
for doc in self.parse_isbn_search_data(data)[:10]:
results.append(self.format_isbn_search_result(doc))
return results

View file

@ -19,7 +19,7 @@ class AbstractConnector(TestCase):
books_url="https://example.com/books",
covers_url="https://example.com/covers",
search_url="https://example.com/search?q=",
isbn_search_url="https://example.com/isbn",
isbn_search_url="https://example.com/isbn?q=",
)
class TestConnector(abstract_connector.AbstractMinimalConnector):
@ -50,7 +50,7 @@ class AbstractConnector(TestCase):
self.assertEqual(connector.books_url, "https://example.com/books")
self.assertEqual(connector.covers_url, "https://example.com/covers")
self.assertEqual(connector.search_url, "https://example.com/search?q=")
self.assertEqual(connector.isbn_search_url, "https://example.com/isbn")
self.assertEqual(connector.isbn_search_url, "https://example.com/isbn?q=")
self.assertIsNone(connector.name)
self.assertEqual(connector.identifier, "example.com")
self.assertIsNone(connector.max_query_count)
@ -83,6 +83,18 @@ class AbstractConnector(TestCase):
results = self.test_connector.search("a book title", min_confidence=1)
self.assertEqual(len(results), 10)
@responses.activate
def test_isbn_search(self):
""" makes an http request to the outside service """
responses.add(
responses.GET,
"https://example.com/isbn?q=123456",
json=["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l"],
status=200,
)
results = self.test_connector.isbn_search("123456")
self.assertEqual(len(results), 10)
def test_search_result(self):
""" a class that stores info about a search result """
result = SearchResult(