2020-05-11 15:34:25 +00:00
|
|
|
''' testing book data connectors '''
|
|
|
|
from django.test import TestCase
|
|
|
|
|
2020-09-21 15:10:37 +00:00
|
|
|
from bookwyrm import models
|
2020-12-30 17:26:02 +00:00
|
|
|
from bookwyrm.connectors.abstract_connector import Mapping, SearchResult
|
2020-12-03 21:22:50 +00:00
|
|
|
from bookwyrm.connectors.openlibrary import Connector
|
2020-05-11 15:34:25 +00:00
|
|
|
|
|
|
|
|
2020-09-21 17:25:26 +00:00
|
|
|
class AbstractConnector(TestCase):
|
2020-12-30 17:26:02 +00:00
|
|
|
''' generic code for connecting to outside data sources '''
|
2020-05-11 15:34:25 +00:00
|
|
|
def setUp(self):
|
2020-12-30 17:26:02 +00:00
|
|
|
''' we need an example connector '''
|
2020-05-11 15:34:25 +00:00
|
|
|
self.book = models.Edition.objects.create(title='Example Edition')
|
|
|
|
|
2020-05-11 17:40:48 +00:00
|
|
|
models.Connector.objects.create(
|
|
|
|
identifier='example.com',
|
2020-12-03 21:22:50 +00:00
|
|
|
connector_file='openlibrary',
|
2020-05-11 17:40:48 +00:00
|
|
|
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 = [
|
2020-12-20 00:26:47 +00:00
|
|
|
Mapping('isbn_10'),
|
2020-05-11 17:40:48 +00:00
|
|
|
Mapping('isbn_13'),
|
2020-12-20 00:26:47 +00:00
|
|
|
Mapping('lccn'),
|
|
|
|
Mapping('asin'),
|
2020-05-11 17:40:48 +00:00
|
|
|
]
|
|
|
|
|
2020-05-11 15:34:25 +00:00
|
|
|
|
|
|
|
def test_create_mapping(self):
|
2020-12-30 17:26:02 +00:00
|
|
|
''' maps remote fields for book data to bookwyrm activitypub fields '''
|
2020-05-11 15:34:25 +00:00
|
|
|
mapping = Mapping('isbn')
|
|
|
|
self.assertEqual(mapping.local_field, 'isbn')
|
|
|
|
self.assertEqual(mapping.remote_field, 'isbn')
|
|
|
|
self.assertEqual(mapping.formatter('bb'), 'bb')
|
|
|
|
|
|
|
|
|
|
|
|
def test_create_mapping_with_remote(self):
|
2020-12-30 17:26:02 +00:00
|
|
|
''' the remote field is different than the local field '''
|
2020-05-11 15:34:25 +00:00
|
|
|
mapping = Mapping('isbn', remote_field='isbn13')
|
|
|
|
self.assertEqual(mapping.local_field, 'isbn')
|
|
|
|
self.assertEqual(mapping.remote_field, 'isbn13')
|
|
|
|
self.assertEqual(mapping.formatter('bb'), 'bb')
|
|
|
|
|
|
|
|
|
|
|
|
def test_create_mapping_with_formatter(self):
|
2020-12-30 17:26:02 +00:00
|
|
|
''' a function is provided to modify the data '''
|
2020-05-11 15:34:25 +00:00
|
|
|
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.formatter('bb'), 'aabb')
|
2020-12-30 17:26:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
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.connector,
|
|
|
|
)
|
|
|
|
# there's really not much to test here, it's just a dataclass
|
|
|
|
self.assertEqual(result.confidence, 1)
|
|
|
|
self.assertEqual(result.title, 'Title')
|