From 6d08996ee94afec10ceef854a0349103d5c0c01f Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Mon, 11 May 2020 08:34:25 -0700 Subject: [PATCH 1/2] Tests abstract connector mappings --- fedireads/tests/test_connector_abstract.py | 60 ++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 fedireads/tests/test_connector_abstract.py diff --git a/fedireads/tests/test_connector_abstract.py b/fedireads/tests/test_connector_abstract.py new file mode 100644 index 000000000..536f5ab3c --- /dev/null +++ b/fedireads/tests/test_connector_abstract.py @@ -0,0 +1,60 @@ +''' testing book data connectors ''' +from django.test import TestCase + +from fedireads import models +from fedireads.connectors.abstract_connector import Mapping,\ + update_from_mappings + + +class FedireadsConnector(TestCase): + def setUp(self): + self.book = models.Edition.objects.create(title='Example Edition') + + + def test_create_mapping(self): + mapping = Mapping('isbn') + self.assertEqual(mapping.local_field, 'isbn') + self.assertEqual(mapping.remote_field, 'isbn') + self.assertEqual(mapping.model, None) + self.assertEqual(mapping.formatter('bb'), 'bb') + + + def test_create_mapping_with_remote(self): + mapping = Mapping('isbn', remote_field='isbn13') + self.assertEqual(mapping.local_field, 'isbn') + self.assertEqual(mapping.remote_field, 'isbn13') + self.assertEqual(mapping.model, None) + self.assertEqual(mapping.formatter('bb'), 'bb') + + + def test_create_mapping_with_formatter(self): + formatter = lambda x: 'aa' + x + mapping = Mapping('isbn', formatter=formatter) + self.assertEqual(mapping.local_field, 'isbn') + self.assertEqual(mapping.remote_field, 'isbn') + self.assertEqual(mapping.formatter, formatter) + self.assertEqual(mapping.model, None) + self.assertEqual(mapping.formatter('bb'), 'aabb') + + + def test_update_from_mappings(self): + data = { + 'title': 'Unused title', + 'isbn_10': '1234567890', + 'isbn_13': 'blahhh', + 'blah': 'bip', + 'format': 'hardcover', + 'series': ['one', 'two'], + } + mappings = [ + Mapping('isbn_10'), + Mapping('blah'),# not present on self.book + Mapping('physical_format', remote_field='format'), + Mapping('series', formatter=lambda x: x[0]), + ] + book = update_from_mappings(self.book, data, mappings) + self.assertEqual(book.title, 'Example Edition') + self.assertEqual(book.isbn_10, '1234567890') + self.assertEqual(book.isbn_13, None) + self.assertEqual(book.physical_format, 'hardcover') + self.assertEqual(book.series, 'one') From 0dde027f3defaafc542f77961593406d06416f9b Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Mon, 11 May 2020 10:40:48 -0700 Subject: [PATCH 2/2] Tests match_from_mappings --- fedireads/connectors/abstract_connector.py | 2 +- fedireads/tests/test_connector_abstract.py | 62 ++++++++++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/fedireads/connectors/abstract_connector.py b/fedireads/connectors/abstract_connector.py index 029d90f82..41500741f 100644 --- a/fedireads/connectors/abstract_connector.py +++ b/fedireads/connectors/abstract_connector.py @@ -182,7 +182,7 @@ class AbstractConnector(ABC): def match_from_mappings(self, data, model): ''' try to find existing copies of this book using various keys ''' relevent_mappings = [m for m in self.key_mappings if \ - m.model and model == m.model] + not m.model or model == m.model] for mapping in relevent_mappings: # check if this field is present in the data value = data.get(mapping.remote_field) diff --git a/fedireads/tests/test_connector_abstract.py b/fedireads/tests/test_connector_abstract.py index 536f5ab3c..0f3d6b3fc 100644 --- a/fedireads/tests/test_connector_abstract.py +++ b/fedireads/tests/test_connector_abstract.py @@ -4,12 +4,39 @@ from django.test import TestCase from fedireads import models from fedireads.connectors.abstract_connector import Mapping,\ update_from_mappings +from fedireads.connectors.fedireads_connector import Connector class FedireadsConnector(TestCase): def setUp(self): self.book = models.Edition.objects.create(title='Example Edition') + models.Connector.objects.create( + identifier='example.com', + connector_file='fedireads_connector', + base_url='https://example.com', + books_url='https:/example.com', + covers_url='https://example.com', + search_url='https://example.com/search?q=', + ) + self.connector = Connector('example.com') + + self.data = { + 'title': 'Unused title', + 'ASIN': 'A00BLAH', + 'isbn_10': '1234567890', + 'isbn_13': 'blahhh', + 'blah': 'bip', + 'format': 'hardcover', + 'series': ['one', 'two'], + } + self.connector.key_mappings = [ + Mapping('isbn_10', model=models.Edition), + Mapping('isbn_13'), + Mapping('lccn', model=models.Work), + Mapping('asin', remote_field='ASIN'), + ] + def test_create_mapping(self): mapping = Mapping('isbn') @@ -58,3 +85,38 @@ class FedireadsConnector(TestCase): self.assertEqual(book.isbn_13, None) self.assertEqual(book.physical_format, 'hardcover') self.assertEqual(book.series, 'one') + + + def test_match_from_mappings(self): + edition = models.Edition.objects.create( + title='Blah', + isbn_13='blahhh', + ) + match = self.connector.match_from_mappings(self.data, models.Edition) + self.assertEqual(match, edition) + + + def test_match_from_mappings_with_model(self): + edition = models.Edition.objects.create( + title='Blah', + isbn_10='1234567890', + ) + match = self.connector.match_from_mappings(self.data, models.Edition) + self.assertEqual(match, edition) + + + def test_match_from_mappings_with_remote(self): + edition = models.Edition.objects.create( + title='Blah', + asin='A00BLAH', + ) + match = self.connector.match_from_mappings(self.data, models.Edition) + self.assertEqual(match, edition) + + + def test_match_from_mappings_no_match(self): + edition = models.Edition.objects.create( + title='Blah', + ) + match = self.connector.match_from_mappings(self.data, models.Edition) + self.assertEqual(match, None)