diff --git a/bookwyrm/connectors/bookwyrm_connector.py b/bookwyrm/connectors/bookwyrm_connector.py index ec45c29b..05612d9e 100644 --- a/bookwyrm/connectors/bookwyrm_connector.py +++ b/bookwyrm/connectors/bookwyrm_connector.py @@ -17,4 +17,5 @@ class Connector(AbstractMinimalConnector): def parse_isbn_search_data(self, data): for search_result in data: - yield self.format_search_result(search_result) + search_result["connector"] = self + yield SearchResult(**search_result) diff --git a/bookwyrm/connectors/inventaire.py b/bookwyrm/connectors/inventaire.py index 7f619127..0495d8c2 100644 --- a/bookwyrm/connectors/inventaire.py +++ b/bookwyrm/connectors/inventaire.py @@ -78,7 +78,7 @@ class Connector(AbstractConnector): } def parse_search_data(self, data): - for search_result in data.get("results"): + for search_result in data.get("results", []): images = search_result.get("image") cover = f"{self.covers_url}/img/entities/{images[0]}" if images else None # a deeply messy translation of inventaire's scores @@ -96,7 +96,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 for search_result in list(results.values()): title = search_result.get("claims", {}).get("wdt:P1476", []) if not title: diff --git a/bookwyrm/tests/connectors/test_abstract_connector.py b/bookwyrm/tests/connectors/test_abstract_connector.py index 26742c05..eb3a7b01 100644 --- a/bookwyrm/tests/connectors/test_abstract_connector.py +++ b/bookwyrm/tests/connectors/test_abstract_connector.py @@ -42,15 +42,9 @@ class AbstractConnector(TestCase): generated_remote_link_field = "openlibrary_link" - def format_search_result(self, search_result): - return search_result - def parse_search_data(self, data): return data - def format_isbn_search_result(self, search_result): - return search_result - def parse_isbn_search_data(self, data): return data diff --git a/bookwyrm/tests/connectors/test_abstract_minimal_connector.py b/bookwyrm/tests/connectors/test_abstract_minimal_connector.py index a90ce0c7..06da8741 100644 --- a/bookwyrm/tests/connectors/test_abstract_minimal_connector.py +++ b/bookwyrm/tests/connectors/test_abstract_minimal_connector.py @@ -1,6 +1,5 @@ """ testing book data connectors """ from django.test import TestCase -import responses from bookwyrm import models from bookwyrm.connectors import abstract_connector @@ -25,18 +24,12 @@ class AbstractConnector(TestCase): class TestConnector(abstract_connector.AbstractMinimalConnector): """nothing added here""" - def format_search_result(self, search_result): - return search_result - def get_or_create_book(self, remote_id): pass def parse_search_data(self, data): return data - def format_isbn_search_result(self, search_result): - return search_result - def parse_isbn_search_data(self, data): return data @@ -54,45 +47,6 @@ class AbstractConnector(TestCase): self.assertIsNone(connector.name) self.assertEqual(connector.identifier, "example.com") - @responses.activate - def test_search(self): - """makes an http request to the outside service""" - responses.add( - responses.GET, - "https://example.com/search?q=a%20book%20title", - json=["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l"], - status=200, - ) - results = self.test_connector.search("a book title") - self.assertEqual(len(results), 10) - self.assertEqual(results[0], "a") - self.assertEqual(results[1], "b") - self.assertEqual(results[2], "c") - - @responses.activate - def test_search_min_confidence(self): - """makes an http request to the outside service""" - responses.add( - responses.GET, - "https://example.com/search?q=a%20book%20title&min_confidence=1", - json=["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l"], - status=200, - ) - 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_create_mapping(self): """maps remote fields for book data to bookwyrm activitypub fields""" mapping = Mapping("isbn") diff --git a/bookwyrm/tests/connectors/test_bookwyrm_connector.py b/bookwyrm/tests/connectors/test_bookwyrm_connector.py index 585080e6..b9642a50 100644 --- a/bookwyrm/tests/connectors/test_bookwyrm_connector.py +++ b/bookwyrm/tests/connectors/test_bookwyrm_connector.py @@ -30,14 +30,11 @@ class BookWyrmConnector(TestCase): result = self.connector.get_or_create_book(book.remote_id) self.assertEqual(book, result) - def test_format_search_result(self): + def test_parse_search_data(self): """create a SearchResult object from search response json""" datafile = pathlib.Path(__file__).parent.joinpath("../data/bw_search.json") search_data = json.loads(datafile.read_bytes()) - results = self.connector.parse_search_data(search_data) - self.assertIsInstance(results, list) - - result = self.connector.format_search_result(results[0]) + result = list(self.connector.parse_search_data(search_data))[0] self.assertIsInstance(result, SearchResult) self.assertEqual(result.title, "Jonathan Strange and Mr Norrell") self.assertEqual(result.key, "https://example.com/book/122") @@ -45,10 +42,9 @@ class BookWyrmConnector(TestCase): self.assertEqual(result.year, 2017) self.assertEqual(result.connector, self.connector) - def test_format_isbn_search_result(self): + def test_parse_isbn_search_data(self): """just gotta attach the connector""" datafile = pathlib.Path(__file__).parent.joinpath("../data/bw_search.json") search_data = json.loads(datafile.read_bytes()) - results = self.connector.parse_isbn_search_data(search_data) - result = self.connector.format_isbn_search_result(results[0]) + result = list(self.connector.parse_isbn_search_data(search_data))[0] self.assertEqual(result.connector, self.connector) diff --git a/bookwyrm/tests/connectors/test_connector_manager.py b/bookwyrm/tests/connectors/test_connector_manager.py index c88a8036..c0c09147 100644 --- a/bookwyrm/tests/connectors/test_connector_manager.py +++ b/bookwyrm/tests/connectors/test_connector_manager.py @@ -49,39 +49,11 @@ class ConnectorManager(TestCase): self.assertEqual(len(connectors), 1) self.assertIsInstance(connectors[0], BookWyrmConnector) - @responses.activate - def test_search_plaintext(self): - """search all connectors""" - responses.add( - responses.GET, - "http://fake.ciom/search/Example?min_confidence=0.1", - json=[{"title": "Hello", "key": "https://www.example.com/search/1"}], - ) - results = connector_manager.search("Example") - self.assertEqual(len(results), 1) - self.assertEqual(len(results[0]["results"]), 1) - self.assertEqual(results[0]["connector"].identifier, "test_connector_remote") - self.assertEqual(results[0]["results"][0].title, "Hello") - def test_search_empty_query(self): """don't panic on empty queries""" results = connector_manager.search("") self.assertEqual(results, []) - @responses.activate - def test_search_isbn(self): - """special handling if a query resembles an isbn""" - responses.add( - responses.GET, - "http://fake.ciom/isbn/0000000000", - json=[{"title": "Hello", "key": "https://www.example.com/search/1"}], - ) - results = connector_manager.search("0000000000") - self.assertEqual(len(results), 1) - self.assertEqual(len(results[0]["results"]), 1) - self.assertEqual(results[0]["connector"].identifier, "test_connector_remote") - self.assertEqual(results[0]["results"][0].title, "Hello") - def test_first_search_result(self): """only get one search result""" result = connector_manager.first_search_result("Example") diff --git a/bookwyrm/tests/connectors/test_inventaire_connector.py b/bookwyrm/tests/connectors/test_inventaire_connector.py index 55727a60..7b526f22 100644 --- a/bookwyrm/tests/connectors/test_inventaire_connector.py +++ b/bookwyrm/tests/connectors/test_inventaire_connector.py @@ -66,38 +66,14 @@ class Inventaire(TestCase): 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): + def test_parse_search_data(self): """json to search result objs""" search_file = pathlib.Path(__file__).parent.joinpath( "../data/inventaire_search.json" ) search_results = json.loads(search_file.read_bytes()) - results = self.connector.parse_search_data(search_results) - formatted = self.connector.format_search_result(results[0]) + formatted = list(self.connector.parse_search_data(search_results))[0] self.assertEqual(formatted.title, "The Stories of Vladimir Nabokov") self.assertEqual( @@ -178,15 +154,14 @@ class Inventaire(TestCase): result = self.connector.resolve_keys(keys) self.assertEqual(result, ["epistolary novel", "crime novel"]) - def test_isbn_search(self): + def test_pase_isbn_search_data(self): """another search type""" search_file = pathlib.Path(__file__).parent.joinpath( "../data/inventaire_isbn_search.json" ) search_results = json.loads(search_file.read_bytes()) - results = self.connector.parse_isbn_search_data(search_results) - formatted = self.connector.format_isbn_search_result(results[0]) + formatted = list(self.connector.parse_isbn_search_data(search_results))[0] self.assertEqual(formatted.title, "L'homme aux cercles bleus") self.assertEqual( @@ -198,25 +173,12 @@ class Inventaire(TestCase): "https://covers.inventaire.io/img/entities/12345", ) - def test_isbn_search_empty(self): + def test_parse_isbn_search_data_empty(self): """another search type""" search_results = {} - results = self.connector.parse_isbn_search_data(search_results) + results = list(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( diff --git a/bookwyrm/tests/test_book_search.py b/bookwyrm/tests/test_book_search.py index 16435fff..141e3906 100644 --- a/bookwyrm/tests/test_book_search.py +++ b/bookwyrm/tests/test_book_search.py @@ -102,18 +102,12 @@ class BookSearch(TestCase): class TestConnector(AbstractMinimalConnector): """nothing added here""" - def format_search_result(self, search_result): - return search_result - def get_or_create_book(self, remote_id): pass def parse_search_data(self, data): return data - def format_isbn_search_result(self, search_result): - return search_result - def parse_isbn_search_data(self, data): return data