From 62ef3e78ccdfc9dee4b0e1969e25715d05d0ef8d Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Tue, 12 May 2020 10:01:36 -0700 Subject: [PATCH] Tests for books_manager --- fedireads/books_manager.py | 5 +- fedireads/tests/test_books_manager.py | 67 +++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 fedireads/tests/test_books_manager.py diff --git a/fedireads/books_manager.py b/fedireads/books_manager.py index eea56303..e0053413 100644 --- a/fedireads/books_manager.py +++ b/fedireads/books_manager.py @@ -121,7 +121,6 @@ def local_search(query): return connector.search(query) - def first_search_result(query): ''' search until you find a result that fits ''' for connector in get_connectors(): @@ -139,8 +138,8 @@ def update_book(book, data=None): def get_connectors(): ''' load all connectors ''' - connectors_info = models.Connector.objects.order_by('priority').all() - return [load_connector(c) for c in connectors_info] + for info in models.Connector.objects.order_by('priority').all(): + yield load_connector(info) def load_connector(connector_info): diff --git a/fedireads/tests/test_books_manager.py b/fedireads/tests/test_books_manager.py new file mode 100644 index 00000000..8ec330b1 --- /dev/null +++ b/fedireads/tests/test_books_manager.py @@ -0,0 +1,67 @@ +from django.test import TestCase + +from fedireads import books_manager, models +from fedireads.connectors.fedireads_connector import Connector +from fedireads.settings import DOMAIN + + +class Book(TestCase): + def setUp(self): + self.work = models.Work.objects.create( + title='Example Work' + ) + + self.edition = models.Edition.objects.create( + title='Example Edition', + parent_work=self.work + ) + + def test_get_edition(self): + edition = books_manager.get_edition(self.edition.id) + self.assertEqual(edition, self.edition) + + + def test_get_edition_work(self): + edition = books_manager.get_edition(self.work.id) + self.assertEqual(edition, self.edition) + + + def test_get_or_create_connector(self): + remote_id = 'https://example.com/object/1' + connector = books_manager.get_or_create_connector(remote_id) + self.assertIsInstance(connector, Connector) + self.assertEqual(connector.identifier, 'example.com') + self.assertEqual(connector.base_url, 'https://example.com') + + same_connector = books_manager.get_or_create_connector(remote_id) + self.assertEqual(connector.identifier, same_connector.identifier) + + + def test_get_by_absolute_id_local(self): + abs_id = 'https://%s/book/%d' % (DOMAIN, self.work.id) + work = books_manager.get_by_absolute_id(abs_id, models.Work) + self.assertEqual(work, self.work) + + work = books_manager.get_by_absolute_id(abs_id, models.Edition) + self.assertIsNone(work) + + + def test_get_by_absolute_id_remote(self): + remote_work = models.Work.objects.create( + title='Example Work', + remote_id='https://example.com/book/123', + ) + + abs_id = 'https://example.com/book/123' + work = books_manager.get_by_absolute_id(abs_id, models.Work) + self.assertEqual(work, remote_work) + + + def test_get_by_absolute_id_invalid(self): + abs_id = 'https://%s/book/34534623' % DOMAIN + result = books_manager.get_by_absolute_id(abs_id, models.Work) + self.assertIsNone(result) + + abs_id = 'httook534623' + result = books_manager.get_by_absolute_id(abs_id, models.Work) + self.assertIsNone(result)