From 07446fa7d207857d0997753df4c5a5b746f94fac Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Wed, 27 Oct 2021 10:03:09 -0700 Subject: [PATCH 1/3] Adds more tests for the inventaire connector --- bookwyrm/connectors/inventaire.py | 5 +- .../connectors/test_inventaire_connector.py | 119 ++++++++++++++++++ 2 files changed, 121 insertions(+), 3 deletions(-) diff --git a/bookwyrm/connectors/inventaire.py b/bookwyrm/connectors/inventaire.py index faed5429a..43aee2f43 100644 --- a/bookwyrm/connectors/inventaire.py +++ b/bookwyrm/connectors/inventaire.py @@ -104,9 +104,7 @@ class Connector(AbstractConnector): def parse_isbn_search_data(self, data): """got some daaaata""" - results = data.get("entities") - if not results: - return [] + results = data.get("entities", []) return list(results.values()) def format_isbn_search_result(self, search_result): @@ -128,6 +126,7 @@ class Connector(AbstractConnector): def load_edition_data(self, work_uri): """get a list of editions for a work""" + # pylint: disable=line-too-long url = f"{self.books_url}?action=reverse-claims&property=wdt:P629&value={work_uri}&sort=true" return get_data(url) diff --git a/bookwyrm/tests/connectors/test_inventaire_connector.py b/bookwyrm/tests/connectors/test_inventaire_connector.py index 381017727..6536bae71 100644 --- a/bookwyrm/tests/connectors/test_inventaire_connector.py +++ b/bookwyrm/tests/connectors/test_inventaire_connector.py @@ -1,11 +1,14 @@ """ testing book data connectors """ import json import pathlib +from unittest.mock import patch + from django.test import TestCase import responses from bookwyrm import models from bookwyrm.connectors.inventaire import Connector, get_language_code +from bookwyrm.connectors.connector_manager import ConnectorException class Inventaire(TestCase): @@ -48,6 +51,44 @@ class Inventaire(TestCase): self.assertEqual(result["wdt:P31"], ["wd:Q3331189"]) self.assertEqual(result["uri"], "isbn:9780375757853") + @responses.activate + def test_get_book_data_invalid(self): + """error if there isn't any entity data""" + responses.add( + responses.GET, + "https://test.url/ok", + json={ + "entities": {}, + "redirects": {}, + }, + ) + + with self.assertRaises(ConnectorException): + self.connector.get_book_data("https://test.url/ok") + + @responses.activate + def test_search(self): + """min confidence filtering""" + responses.add( + responses.GET, + "https://inventaire.io/search?q=hi", + json={ + "results": [ + { + "_score": 200, + "label": "hello", + }, + { + "_score": 100, + "label": "hi", + }, + ], + }, + ) + results = self.connector.search("hi", min_confidence=0.5) + self.assertEqual(len(results), 1) + self.assertEqual(results[0].title, "hello") + def test_format_search_result(self): """json to search result objs""" search_file = pathlib.Path(__file__).parent.joinpath( @@ -157,6 +198,84 @@ class Inventaire(TestCase): "https://covers.inventaire.io/img/entities/12345", ) + def test_isbn_search_empty(self): + """another search type""" + search_results = {} + results = self.connector.parse_isbn_search_data(search_results) + self.assertEqual(results, []) + + def test_isbn_search_no_title(self): + """another search type""" + search_file = pathlib.Path(__file__).parent.joinpath( + "../data/inventaire_isbn_search.json" + ) + search_results = json.loads(search_file.read_bytes()) + search_results["entities"]["isbn:9782290349229"]["claims"]["wdt:P1476"] = None + + result = self.connector.format_isbn_search_result( + search_results.get("entities") + ) + self.assertIsNone(result) + + def test_is_work_data(self): + """is it a work""" + work_file = pathlib.Path(__file__).parent.joinpath( + "../data/inventaire_work.json" + ) + work_data = json.loads(work_file.read_bytes()) + with patch("bookwyrm.connectors.inventaire.get_data") as get_data_mock: + get_data_mock.return_value = work_data + formatted = self.connector.get_book_data("hi") + self.assertTrue(self.connector.is_work_data(formatted)) + + edition_file = pathlib.Path(__file__).parent.joinpath( + "../data/inventaire_edition.json" + ) + edition_data = json.loads(edition_file.read_bytes()) + with patch("bookwyrm.connectors.inventaire.get_data") as get_data_mock: + get_data_mock.return_value = edition_data + formatted = self.connector.get_book_data("hi") + self.assertFalse(self.connector.is_work_data(formatted)) + + @responses.activate + def test_get_edition_from_work_data(self): + """load edition""" + responses.add( + responses.GET, + "https://inventaire.io/?action=by-uris&uris=hello", + json={"entities": {}}, + ) + data = {"uri": "blah"} + with patch( + "bookwyrm.connectors.inventaire.Connector.load_edition_data" + ) as loader_mock, patch( + "bookwyrm.connectors.inventaire.Connector.get_book_data" + ) as getter_mock: + loader_mock.return_value = {"uris": ["hello"]} + self.connector.get_edition_from_work_data(data) + self.assertTrue(getter_mock.called) + + with patch( + "bookwyrm.connectors.inventaire.Connector.load_edition_data" + ) as loader_mock: + loader_mock.return_value = {"uris": []} + with self.assertRaises(ConnectorException): + self.connector.get_edition_from_work_data(data) + + @responses.activate + def test_get_work_from_edition_data(self): + """load work""" + responses.add( + responses.GET, + "https://inventaire.io/?action=by-uris&uris=hello", + ) + data = {"wdt:P629": "hello"} + self.connector.get_work_from_edition_data(data) + + data = {"wdt:P629": None} + with self.assertRaises(ConnectorException): + self.connector.get_work_from_edition_data(data) + def test_get_language_code(self): """get english or whatever is in reach""" options = { From d3e4c7e8d9c8c1cf98e07bafd9108553aa90551d Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Wed, 27 Oct 2021 10:40:37 -0700 Subject: [PATCH 2/3] Removes change to boolean logic --- bookwyrm/connectors/inventaire.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/bookwyrm/connectors/inventaire.py b/bookwyrm/connectors/inventaire.py index 43aee2f43..e180dc62f 100644 --- a/bookwyrm/connectors/inventaire.py +++ b/bookwyrm/connectors/inventaire.py @@ -104,7 +104,9 @@ class Connector(AbstractConnector): def parse_isbn_search_data(self, data): """got some daaaata""" - results = data.get("entities", []) + results = data.get("entities") + if not results: + return [] return list(results.values()) def format_isbn_search_result(self, search_result): From 6dd7eebd98573c5d7618385ec75b5143cd2ec714 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Tue, 16 Nov 2021 10:16:28 -0800 Subject: [PATCH 3/3] Fixes tests --- bookwyrm/connectors/inventaire.py | 2 +- bookwyrm/tests/connectors/test_inventaire_connector.py | 10 +++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/bookwyrm/connectors/inventaire.py b/bookwyrm/connectors/inventaire.py index e180dc62f..e9f538569 100644 --- a/bookwyrm/connectors/inventaire.py +++ b/bookwyrm/connectors/inventaire.py @@ -67,7 +67,7 @@ class Connector(AbstractConnector): extracted = list(data.get("entities").values()) try: data = extracted[0] - except KeyError: + except (KeyError, IndexError): raise ConnectorException("Invalid book data") # flatten the data so that images, uri, and claims are on the same level return { diff --git a/bookwyrm/tests/connectors/test_inventaire_connector.py b/bookwyrm/tests/connectors/test_inventaire_connector.py index 6536bae71..65325d02f 100644 --- a/bookwyrm/tests/connectors/test_inventaire_connector.py +++ b/bookwyrm/tests/connectors/test_inventaire_connector.py @@ -269,10 +269,14 @@ class Inventaire(TestCase): responses.GET, "https://inventaire.io/?action=by-uris&uris=hello", ) - data = {"wdt:P629": "hello"} - self.connector.get_work_from_edition_data(data) + data = {"wdt:P629": ["hello"]} + with patch("bookwyrm.connectors.inventaire.Connector.get_book_data") as mock: + self.connector.get_work_from_edition_data(data) + self.assertEqual(mock.call_count, 1) + args = mock.call_args[0] + self.assertEqual(args[0], "https://inventaire.io?action=by-uris&uris=hello") - data = {"wdt:P629": None} + data = {"wdt:P629": [None]} with self.assertRaises(ConnectorException): self.connector.get_work_from_edition_data(data)