moviewyrm/bookwyrm/tests/connectors/test_abstract_minimal_connector.py

118 lines
4.6 KiB
Python
Raw Normal View History

2021-03-08 16:49:10 +00:00
""" testing book data connectors """
2020-12-31 20:22:00 +00:00
from django.test import TestCase
import responses
from bookwyrm import models
from bookwyrm.connectors import abstract_connector
2021-09-16 19:41:30 +00:00
from bookwyrm.connectors.abstract_connector import Mapping
2020-12-31 20:22:00 +00:00
class AbstractConnector(TestCase):
2021-04-26 16:15:42 +00:00
"""generic code for connecting to outside data sources"""
2021-03-08 16:49:10 +00:00
2020-12-31 20:22:00 +00:00
def setUp(self):
2021-04-26 16:15:42 +00:00
"""we need an example connector"""
2020-12-31 20:22:00 +00:00
self.connector_info = models.Connector.objects.create(
2021-03-08 16:49:10 +00:00
identifier="example.com",
connector_file="openlibrary",
base_url="https://example.com",
books_url="https://example.com/books",
covers_url="https://example.com/covers",
search_url="https://example.com/search?q=",
isbn_search_url="https://example.com/isbn?q=",
2020-12-31 20:22:00 +00:00
)
class TestConnector(abstract_connector.AbstractMinimalConnector):
2021-04-26 16:15:42 +00:00
"""nothing added here"""
2021-03-08 16:49:10 +00:00
2020-12-31 20:22:00 +00:00
def format_search_result(self, search_result):
return search_result
2021-03-08 16:49:10 +00:00
2020-12-31 20:22:00 +00:00
def get_or_create_book(self, remote_id):
pass
2021-03-08 16:49:10 +00:00
2020-12-31 20:22:00 +00:00
def parse_search_data(self, data):
return data
2021-03-08 16:49:10 +00:00
2021-03-01 20:09:21 +00:00
def format_isbn_search_result(self, search_result):
return search_result
2021-03-08 16:49:10 +00:00
2021-03-01 20:09:21 +00:00
def parse_isbn_search_data(self, data):
return data
2020-12-31 20:22:00 +00:00
2021-03-08 16:49:10 +00:00
self.test_connector = TestConnector("example.com")
2020-12-31 20:22:00 +00:00
def test_abstract_minimal_connector_init(self):
2021-04-26 16:15:42 +00:00
"""barebones connector for search with defaults"""
2020-12-31 20:22:00 +00:00
connector = self.test_connector
self.assertEqual(connector.connector, self.connector_info)
2021-03-08 16:49:10 +00:00
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.assertEqual(connector.isbn_search_url, "https://example.com/isbn?q=")
2020-12-31 20:22:00 +00:00
self.assertIsNone(connector.name)
2021-03-08 16:49:10 +00:00
self.assertEqual(connector.identifier, "example.com")
2020-12-31 20:22:00 +00:00
@responses.activate
def test_search(self):
2021-04-26 16:15:42 +00:00
"""makes an http request to the outside service"""
2020-12-31 20:22:00 +00:00
responses.add(
responses.GET,
2021-03-08 16:49:10 +00:00
"https://example.com/search?q=a%20book%20title",
json=["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l"],
status=200,
)
results = self.test_connector.search("a book title")
2020-12-31 20:22:00 +00:00
self.assertEqual(len(results), 10)
2021-03-08 16:49:10 +00:00
self.assertEqual(results[0], "a")
self.assertEqual(results[1], "b")
self.assertEqual(results[2], "c")
2020-12-31 20:22:00 +00:00
@responses.activate
def test_search_min_confidence(self):
2021-04-26 16:15:42 +00:00
"""makes an http request to the outside service"""
responses.add(
responses.GET,
"https://example.com/search?q=a%20book%20title&min_confidence=1",
json=["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l"],
status=200,
)
results = self.test_connector.search("a book title", min_confidence=1)
self.assertEqual(len(results), 10)
@responses.activate
def test_isbn_search(self):
2021-04-26 16:15:42 +00:00
"""makes an http request to the outside service"""
responses.add(
responses.GET,
"https://example.com/isbn?q=123456",
json=["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l"],
status=200,
)
results = self.test_connector.isbn_search("123456")
self.assertEqual(len(results), 10)
2020-12-31 20:22:00 +00:00
def test_create_mapping(self):
2021-04-26 16:15:42 +00:00
"""maps remote fields for book data to bookwyrm activitypub fields"""
2021-03-08 16:49:10 +00:00
mapping = Mapping("isbn")
self.assertEqual(mapping.local_field, "isbn")
self.assertEqual(mapping.remote_field, "isbn")
self.assertEqual(mapping.formatter("bb"), "bb")
2020-12-31 20:22:00 +00:00
def test_create_mapping_with_remote(self):
2021-04-26 16:15:42 +00:00
"""the remote field is different than the local field"""
2021-03-08 16:49:10 +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")
2020-12-31 20:22:00 +00:00
def test_create_mapping_with_formatter(self):
2021-04-26 16:15:42 +00:00
"""a function is provided to modify the data"""
2021-03-08 16:49:10 +00:00
formatter = lambda x: "aa" + x
mapping = Mapping("isbn", formatter=formatter)
self.assertEqual(mapping.local_field, "isbn")
self.assertEqual(mapping.remote_field, "isbn")
2020-12-31 20:22:00 +00:00
self.assertEqual(mapping.formatter, formatter)
2021-03-08 16:49:10 +00:00
self.assertEqual(mapping.formatter("bb"), "aabb")