2020-05-11 15:34:25 +00:00
|
|
|
''' testing book data connectors '''
|
|
|
|
from django.test import TestCase
|
2020-12-30 17:48:37 +00:00
|
|
|
import responses
|
2020-05-11 15:34:25 +00:00
|
|
|
|
2020-09-21 15:10:37 +00:00
|
|
|
from bookwyrm import models
|
2020-12-30 17:48:37 +00:00
|
|
|
from bookwyrm.connectors import abstract_connector
|
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-12-30 17:48:37 +00:00
|
|
|
self.connector_info = models.Connector.objects.create(
|
2020-05-11 17:40:48 +00:00
|
|
|
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',
|
2020-12-30 17:48:37 +00:00
|
|
|
books_url='https://example.com/books',
|
|
|
|
covers_url='https://example.com/covers',
|
2020-05-11 17:40:48 +00:00
|
|
|
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
|
|
|
|
2020-12-30 17:48:37 +00:00
|
|
|
def test_abstract_minimal_connector_init(self):
|
|
|
|
''' barebones connector for search with defaults '''
|
|
|
|
class TestConnector(abstract_connector.AbstractMinimalConnector):
|
|
|
|
''' nothing added here '''
|
|
|
|
def format_search_result():
|
|
|
|
pass
|
|
|
|
def get_or_create_book():
|
|
|
|
pass
|
|
|
|
def parse_search_data():
|
|
|
|
pass
|
|
|
|
|
|
|
|
connector = TestConnector('example.com')
|
|
|
|
self.assertEqual(connector.connector, self.connector_info)
|
|
|
|
self.assertEqual(connector.base_url, 'https://example.com')
|
|
|
|
self.assertEqual(connector.books_url, 'https://example.com/books')
|
|
|
|
self.assertEqual(connector.covers_url, 'https://example.com/covers')
|
|
|
|
self.assertEqual(connector.search_url, 'https://example.com/search?q=')
|
|
|
|
self.assertIsNone(connector.name),
|
|
|
|
self.assertEqual(connector.identifier, 'example.com'),
|
|
|
|
self.assertIsNone(connector.max_query_count)
|
|
|
|
self.assertFalse(connector.local)
|
|
|
|
|
|
|
|
|
|
|
|
@responses.activate
|
|
|
|
def test_abstract_minimal_connector_search(self):
|
|
|
|
''' makes an http request to the outside service '''
|
|
|
|
class TestConnector(abstract_connector.AbstractMinimalConnector):
|
|
|
|
''' nothing added here '''
|
|
|
|
def format_search_result(self, data):
|
|
|
|
return data
|
|
|
|
def get_or_create_book(self, data):
|
|
|
|
pass
|
|
|
|
def parse_search_data(self, data):
|
|
|
|
return data
|
|
|
|
connector = TestConnector('example.com')
|
|
|
|
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 = 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')
|
|
|
|
|
|
|
|
|
|
|
|
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')
|
|
|
|
|
|
|
|
|
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')
|