From 3f44389c6bb17156294f958223e449d435fb27d8 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Thu, 30 Sep 2021 13:03:04 -0700 Subject: [PATCH] Adds new test file for search --- .../test_abstract_minimal_connector.py | 14 --- .../connectors/test_connector_manager.py | 2 +- bookwyrm/tests/test_book_search.py | 118 ++++++++++++++++++ 3 files changed, 119 insertions(+), 15 deletions(-) create mode 100644 bookwyrm/tests/test_book_search.py diff --git a/bookwyrm/tests/connectors/test_abstract_minimal_connector.py b/bookwyrm/tests/connectors/test_abstract_minimal_connector.py index 58907212..a90ce0c7 100644 --- a/bookwyrm/tests/connectors/test_abstract_minimal_connector.py +++ b/bookwyrm/tests/connectors/test_abstract_minimal_connector.py @@ -3,7 +3,6 @@ from django.test import TestCase import responses from bookwyrm import models -from bookwyrm.book_search import SearchResult from bookwyrm.connectors import abstract_connector from bookwyrm.connectors.abstract_connector import Mapping @@ -94,19 +93,6 @@ class AbstractConnector(TestCase): 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( - title="Title", - key="https://example.com/book/1", - author="Author Name", - year="1850", - connector=self.test_connector, - ) - # there's really not much to test here, it's just a dataclass - self.assertEqual(result.confidence, 1) - self.assertEqual(result.title, "Title") - 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_connector_manager.py b/bookwyrm/tests/connectors/test_connector_manager.py index d6d82fc8..c88a8036 100644 --- a/bookwyrm/tests/connectors/test_connector_manager.py +++ b/bookwyrm/tests/connectors/test_connector_manager.py @@ -14,7 +14,7 @@ class ConnectorManager(TestCase): """we'll need some books and a connector info entry""" self.work = models.Work.objects.create(title="Example Work") - self.edition = models.Edition.objects.create( + models.Edition.objects.create( title="Example Edition", parent_work=self.work, isbn_10="0000000000" ) self.edition = models.Edition.objects.create( diff --git a/bookwyrm/tests/test_book_search.py b/bookwyrm/tests/test_book_search.py new file mode 100644 index 00000000..4b9a0681 --- /dev/null +++ b/bookwyrm/tests/test_book_search.py @@ -0,0 +1,118 @@ +""" test searching for books """ +import datetime +from django.test import TestCase +from django.utils import timezone + +from bookwyrm import book_search, models +from bookwyrm.connectors.abstract_connector import AbstractMinimalConnector + + +class BookSearch(TestCase): + """look for some books""" + + def setUp(self): + """we need basic test data and mocks""" + self.work = models.Work.objects.create(title="Example Work") + + self.first_edition = models.Edition.objects.create( + title="Example Edition", + parent_work=self.work, + isbn_10="0000000000", + physical_format="Paperback", + published_date=datetime.datetime(2019, 4, 9, 0, 0, tzinfo=timezone.utc), + ) + self.second_edition = models.Edition.objects.create( + title="Another Edition", + parent_work=self.work, + isbn_10="1111111111", + openlibrary_key="hello", + ) + + def test_search(self): + """search for a book in the db""" + # title/author + results = book_search.search("Example") + self.assertEqual(len(results), 1) + self.assertEqual(results[0], self.first_edition) + + # isbn + results = book_search.search("0000000000") + self.assertEqual(len(results), 1) + self.assertEqual(results[0], self.first_edition) + + # identifier + results = book_search.search("hello") + self.assertEqual(len(results), 1) + self.assertEqual(results[0], self.second_edition) + + def test_isbn_search(self): + """test isbn search""" + results = book_search.isbn_search("0000000000") + self.assertEqual(len(results), 1) + self.assertEqual(results[0], self.first_edition) + + def test_search_identifiers(self): + """search by unique identifiers""" + results = book_search.search_identifiers("hello") + self.assertEqual(len(results), 1) + self.assertEqual(results[0], self.second_edition) + + def test_search_title_author(self): + """search by unique identifiers""" + results = book_search.search_title_author("Another", min_confidence=0) + self.assertEqual(len(results), 1) + self.assertEqual(results[0], self.second_edition) + + def test_format_search_result(self): + """format a search result""" + result = book_search.format_search_result(self.first_edition) + self.assertEqual(result["title"], "Example Edition") + self.assertEqual(result["key"], self.first_edition.remote_id) + self.assertEqual(result["year"], 2019) + + result = book_search.format_search_result(self.second_edition) + self.assertEqual(result["title"], "Another Edition") + self.assertEqual(result["key"], self.second_edition.remote_id) + self.assertIsNone(result["year"]) + + def test_search_result(self): + """a class that stores info about a search result""" + models.Connector.objects.create( + identifier="example.com", + connector_file="openlibrary", + base_url="https://example.com", + 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?q=", + ) + + 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 + + test_connector = TestConnector("example.com") + result = book_search.SearchResult( + title="Title", + key="https://example.com/book/1", + author="Author Name", + year="1850", + connector=test_connector, + ) + # there's really not much to test here, it's just a dataclass + self.assertEqual(result.confidence, 1) + self.assertEqual(result.title, "Title")