2021-03-08 16:49:10 +00:00
|
|
|
""" interface between the app and various connectors """
|
2020-05-12 17:01:36 +00:00
|
|
|
from django.test import TestCase
|
2021-05-10 19:53:36 +00:00
|
|
|
import responses
|
2020-05-12 17:01:36 +00:00
|
|
|
|
2021-01-02 16:14:28 +00:00
|
|
|
from bookwyrm import models
|
|
|
|
from bookwyrm.connectors import connector_manager
|
2021-03-08 16:49:10 +00:00
|
|
|
from bookwyrm.connectors.bookwyrm_connector import Connector as BookWyrmConnector
|
|
|
|
from bookwyrm.connectors.self_connector import Connector as SelfConnector
|
2020-05-12 17:01:36 +00:00
|
|
|
|
|
|
|
|
2021-01-02 16:14:28 +00:00
|
|
|
class ConnectorManager(TestCase):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""interface between the app and various connectors"""
|
2021-03-08 16:49:10 +00:00
|
|
|
|
2020-05-12 17:01:36 +00:00
|
|
|
def setUp(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""we'll need some books and a connector info entry"""
|
2021-08-02 23:05:40 +00:00
|
|
|
self.work = models.Work.objects.create(title="Example Work")
|
2020-05-12 17:01:36 +00:00
|
|
|
|
2021-08-02 23:05:40 +00:00
|
|
|
self.edition = models.Edition.objects.create(
|
|
|
|
title="Example Edition", parent_work=self.work, isbn_10="0000000000"
|
|
|
|
)
|
|
|
|
self.edition = models.Edition.objects.create(
|
|
|
|
title="Another Edition", parent_work=self.work, isbn_10="1111111111"
|
|
|
|
)
|
2020-05-12 17:01:36 +00:00
|
|
|
|
2020-09-21 18:14:56 +00:00
|
|
|
self.connector = models.Connector.objects.create(
|
2021-03-08 16:49:10 +00:00
|
|
|
identifier="test_connector",
|
2020-09-21 18:14:56 +00:00
|
|
|
priority=1,
|
|
|
|
local=True,
|
2021-03-08 16:49:10 +00:00
|
|
|
connector_file="self_connector",
|
|
|
|
base_url="http://test.com/",
|
|
|
|
books_url="http://test.com/",
|
|
|
|
covers_url="http://test.com/",
|
2021-03-13 18:01:17 +00:00
|
|
|
isbn_search_url="http://test.com/isbn/",
|
2020-09-21 18:14:56 +00:00
|
|
|
)
|
|
|
|
|
2021-05-10 19:53:36 +00:00
|
|
|
self.remote_connector = models.Connector.objects.create(
|
|
|
|
identifier="test_connector_remote",
|
|
|
|
priority=1,
|
|
|
|
local=False,
|
|
|
|
connector_file="bookwyrm_connector",
|
|
|
|
base_url="http://fake.ciom/",
|
|
|
|
books_url="http://fake.ciom/",
|
|
|
|
search_url="http://fake.ciom/search/",
|
|
|
|
covers_url="http://covers.fake.ciom/",
|
|
|
|
isbn_search_url="http://fake.ciom/isbn/",
|
|
|
|
)
|
|
|
|
|
2020-05-12 17:01:36 +00:00
|
|
|
def test_get_or_create_connector(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""loads a connector if the data source is known or creates one"""
|
2021-03-08 16:49:10 +00:00
|
|
|
remote_id = "https://example.com/object/1"
|
2021-01-02 16:14:28 +00:00
|
|
|
connector = connector_manager.get_or_create_connector(remote_id)
|
2020-09-21 18:14:56 +00:00
|
|
|
self.assertIsInstance(connector, BookWyrmConnector)
|
2021-03-08 16:49:10 +00:00
|
|
|
self.assertEqual(connector.identifier, "example.com")
|
|
|
|
self.assertEqual(connector.base_url, "https://example.com")
|
2020-05-12 17:01:36 +00:00
|
|
|
|
2021-01-02 16:14:28 +00:00
|
|
|
same_connector = connector_manager.get_or_create_connector(remote_id)
|
2020-05-12 17:01:36 +00:00
|
|
|
self.assertEqual(connector.identifier, same_connector.identifier)
|
2020-09-21 18:14:56 +00:00
|
|
|
|
|
|
|
def test_get_connectors(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""load all connectors"""
|
2021-01-02 16:14:28 +00:00
|
|
|
connectors = list(connector_manager.get_connectors())
|
2020-09-21 18:14:56 +00:00
|
|
|
self.assertEqual(len(connectors), 2)
|
|
|
|
self.assertIsInstance(connectors[0], SelfConnector)
|
|
|
|
self.assertIsInstance(connectors[1], BookWyrmConnector)
|
|
|
|
|
2021-05-10 19:53:36 +00:00
|
|
|
@responses.activate
|
2020-09-21 18:14:56 +00:00
|
|
|
def test_search(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""search all connectors"""
|
2021-05-10 19:53:36 +00:00
|
|
|
responses.add(
|
|
|
|
responses.GET,
|
|
|
|
"http://fake.ciom/search/Example?min_confidence=0.1",
|
|
|
|
json={},
|
|
|
|
)
|
2021-03-08 16:49:10 +00:00
|
|
|
results = connector_manager.search("Example")
|
2020-09-21 18:14:56 +00:00
|
|
|
self.assertEqual(len(results), 1)
|
2021-03-08 16:49:10 +00:00
|
|
|
self.assertIsInstance(results[0]["connector"], SelfConnector)
|
2021-03-13 18:01:17 +00:00
|
|
|
self.assertEqual(len(results[0]["results"]), 1)
|
|
|
|
self.assertEqual(results[0]["results"][0].title, "Example Edition")
|
|
|
|
|
2021-05-10 19:53:36 +00:00
|
|
|
def test_search_empty_query(self):
|
|
|
|
"""don't panic on empty queries"""
|
|
|
|
results = connector_manager.search("")
|
|
|
|
self.assertEqual(results, [])
|
|
|
|
|
|
|
|
@responses.activate
|
2021-03-13 18:01:17 +00:00
|
|
|
def test_search_isbn(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""special handling if a query resembles an isbn"""
|
2021-05-10 19:53:36 +00:00
|
|
|
responses.add(
|
|
|
|
responses.GET,
|
|
|
|
"http://fake.ciom/isbn/0000000000",
|
|
|
|
json={},
|
|
|
|
)
|
2021-03-13 18:01:17 +00:00
|
|
|
results = connector_manager.search("0000000000")
|
|
|
|
self.assertEqual(len(results), 1)
|
|
|
|
self.assertIsInstance(results[0]["connector"], SelfConnector)
|
2021-03-08 16:49:10 +00:00
|
|
|
self.assertEqual(len(results[0]["results"]), 1)
|
|
|
|
self.assertEqual(results[0]["results"][0].title, "Example Edition")
|
2020-09-21 18:14:56 +00:00
|
|
|
|
|
|
|
def test_local_search(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""search only the local database"""
|
2021-03-08 16:49:10 +00:00
|
|
|
results = connector_manager.local_search("Example")
|
2020-09-21 18:14:56 +00:00
|
|
|
self.assertEqual(len(results), 1)
|
2021-03-08 16:49:10 +00:00
|
|
|
self.assertEqual(results[0].title, "Example Edition")
|
2020-09-21 18:14:56 +00:00
|
|
|
|
|
|
|
def test_first_search_result(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""only get one search result"""
|
2021-03-08 16:49:10 +00:00
|
|
|
result = connector_manager.first_search_result("Example")
|
|
|
|
self.assertEqual(result.title, "Example Edition")
|
2021-05-10 19:53:36 +00:00
|
|
|
|
|
|
|
def test_first_search_result_empty_query(self):
|
|
|
|
"""only get one search result"""
|
|
|
|
result = connector_manager.first_search_result("")
|
|
|
|
self.assertIsNone(result)
|
|
|
|
|
|
|
|
@responses.activate
|
|
|
|
def test_first_search_result_no_results(self):
|
|
|
|
"""only get one search result"""
|
|
|
|
responses.add(
|
|
|
|
responses.GET,
|
|
|
|
"http://fake.ciom/search/dkjfhg?min_confidence=0.1",
|
|
|
|
json={},
|
|
|
|
)
|
|
|
|
result = connector_manager.first_search_result("dkjfhg")
|
|
|
|
self.assertIsNone(result)
|
2020-09-21 18:14:56 +00:00
|
|
|
|
|
|
|
def test_load_connector(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""load a connector object from the database entry"""
|
2021-01-02 16:14:28 +00:00
|
|
|
connector = connector_manager.load_connector(self.connector)
|
2020-09-21 18:14:56 +00:00
|
|
|
self.assertIsInstance(connector, SelfConnector)
|
2021-03-08 16:49:10 +00:00
|
|
|
self.assertEqual(connector.identifier, "test_connector")
|