Cleans up abstract connector tests

a buncha linting errors and junk we don't need in setup
This commit is contained in:
Mouse Reeve 2020-12-31 11:04:19 -08:00
parent 03ed1cfa94
commit 141e7b90e8

View file

@ -5,15 +5,12 @@ import responses
from bookwyrm import models from bookwyrm import models
from bookwyrm.connectors import abstract_connector from bookwyrm.connectors import abstract_connector
from bookwyrm.connectors.abstract_connector import Mapping, SearchResult from bookwyrm.connectors.abstract_connector import Mapping, SearchResult
from bookwyrm.connectors.openlibrary import Connector
class AbstractConnector(TestCase): class AbstractConnector(TestCase):
''' generic code for connecting to outside data sources ''' ''' generic code for connecting to outside data sources '''
def setUp(self): def setUp(self):
''' we need an example connector ''' ''' we need an example connector '''
self.book = models.Edition.objects.create(title='Example Edition')
self.connector_info = models.Connector.objects.create( self.connector_info = models.Connector.objects.create(
identifier='example.com', identifier='example.com',
connector_file='openlibrary', connector_file='openlibrary',
@ -22,72 +19,51 @@ class AbstractConnector(TestCase):
covers_url='https://example.com/covers', covers_url='https://example.com/covers',
search_url='https://example.com/search?q=', search_url='https://example.com/search?q=',
) )
self.connector = Connector('example.com')
self.data = { class TestConnector(abstract_connector.AbstractMinimalConnector):
'title': 'Unused title', ''' nothing added here '''
'ASIN': 'A00BLAH', def format_search_result(self, search_result):
'isbn_10': '1234567890', return search_result
'isbn_13': 'blahhh', def get_or_create_book(self, remote_id):
'blah': 'bip', pass
'format': 'hardcover', def parse_search_data(self, data):
'series': ['one', 'two'], return data
} self.test_connector = TestConnector('example.com')
self.connector.key_mappings = [
Mapping('isbn_10'),
Mapping('isbn_13'),
Mapping('lccn'),
Mapping('asin'),
]
def test_abstract_minimal_connector_init(self): def test_abstract_minimal_connector_init(self):
''' barebones connector for search with defaults ''' ''' barebones connector for search with defaults '''
class TestConnector(abstract_connector.AbstractMinimalConnector): connector = self.test_connector
''' 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.connector, self.connector_info)
self.assertEqual(connector.base_url, 'https://example.com') self.assertEqual(connector.base_url, 'https://example.com')
self.assertEqual(connector.books_url, 'https://example.com/books') self.assertEqual(connector.books_url, 'https://example.com/books')
self.assertEqual(connector.covers_url, 'https://example.com/covers') self.assertEqual(connector.covers_url, 'https://example.com/covers')
self.assertEqual(connector.search_url, 'https://example.com/search?q=') self.assertEqual(connector.search_url, 'https://example.com/search?q=')
self.assertIsNone(connector.name), self.assertIsNone(connector.name)
self.assertEqual(connector.identifier, 'example.com'), self.assertEqual(connector.identifier, 'example.com')
self.assertIsNone(connector.max_query_count) self.assertIsNone(connector.max_query_count)
self.assertFalse(connector.local) self.assertFalse(connector.local)
@responses.activate @responses.activate
def test_abstract_minimal_connector_search(self): def test_search(self):
''' makes an http request to the outside service ''' ''' 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.add(
responses.GET, responses.GET,
'https://example.com/search?q=a%20book%20title', 'https://example.com/search?q=a%20book%20title',
json=['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l'], json=['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l'],
status=200) status=200)
results = connector.search('a book title') results = self.test_connector.search('a book title')
self.assertEqual(len(results), 10) self.assertEqual(len(results), 10)
self.assertEqual(results[0], 'a') self.assertEqual(results[0], 'a')
self.assertEqual(results[1], 'b') self.assertEqual(results[1], 'b')
self.assertEqual(results[2], 'c') self.assertEqual(results[2], 'c')
def test_is_available(self):
''' is the connector usable '''
def test_search_result(self): def test_search_result(self):
''' a class that stores info about a search result ''' ''' a class that stores info about a search result '''
result = SearchResult( result = SearchResult(
@ -95,7 +71,7 @@ class AbstractConnector(TestCase):
key='https://example.com/book/1', key='https://example.com/book/1',
author='Author Name', author='Author Name',
year='1850', year='1850',
connector=self.connector, connector=self.test_connector,
) )
# there's really not much to test here, it's just a dataclass # there's really not much to test here, it's just a dataclass
self.assertEqual(result.confidence, 1) self.assertEqual(result.confidence, 1)