2021-03-08 16:49:10 +00:00
|
|
|
""" testing book data connectors """
|
2021-01-02 17:08:37 +00:00
|
|
|
from unittest.mock import patch
|
2020-05-11 15:34:25 +00:00
|
|
|
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
|
2022-02-03 23:11:01 +00:00
|
|
|
from bookwyrm.connectors import abstract_connector, ConnectorException
|
|
|
|
from bookwyrm.connectors.abstract_connector import Mapping, get_data
|
2020-12-31 20:22:00 +00:00
|
|
|
from bookwyrm.settings import DOMAIN
|
2020-05-11 15:34:25 +00:00
|
|
|
|
|
|
|
|
2020-09-21 17:25:26 +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-05-11 15:34:25 +00:00
|
|
|
def setUp(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""we need an example connector"""
|
2020-12-30 17:48:37 +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=",
|
2020-05-11 17:40:48 +00:00
|
|
|
)
|
2020-12-31 20:22:00 +00:00
|
|
|
work_data = {
|
2021-03-08 16:49:10 +00:00
|
|
|
"id": "abc1",
|
|
|
|
"title": "Test work",
|
|
|
|
"type": "work",
|
|
|
|
"openlibraryKey": "OL1234W",
|
2020-12-31 20:22:00 +00:00
|
|
|
}
|
|
|
|
self.work_data = work_data
|
|
|
|
edition_data = {
|
2021-03-08 16:49:10 +00:00
|
|
|
"id": "abc2",
|
|
|
|
"title": "Test edition",
|
|
|
|
"type": "edition",
|
|
|
|
"openlibraryKey": "OL1234M",
|
2020-12-31 20:22:00 +00:00
|
|
|
}
|
|
|
|
self.edition_data = edition_data
|
|
|
|
|
|
|
|
class TestConnector(abstract_connector.AbstractConnector):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""nothing added here"""
|
2021-03-08 16:49:10 +00:00
|
|
|
|
2021-12-07 21:48:22 +00:00
|
|
|
generated_remote_link_field = "openlibrary_link"
|
|
|
|
|
2020-12-31 19:04:19 +00:00
|
|
|
def format_search_result(self, search_result):
|
|
|
|
return search_result
|
2021-03-08 16:49:10 +00:00
|
|
|
|
2020-12-31 19:04:19 +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
|
2021-03-08 16:49:10 +00:00
|
|
|
|
2020-12-31 20:22:00 +00:00
|
|
|
def is_work_data(self, data):
|
2021-03-08 16:49:10 +00:00
|
|
|
return data["type"] == "work"
|
|
|
|
|
2020-12-31 20:22:00 +00:00
|
|
|
def get_edition_from_work_data(self, data):
|
|
|
|
return edition_data
|
2021-03-08 16:49:10 +00:00
|
|
|
|
2020-12-31 20:22:00 +00:00
|
|
|
def get_work_from_edition_data(self, data):
|
|
|
|
return work_data
|
2021-03-08 16:49:10 +00:00
|
|
|
|
2020-12-31 20:22:00 +00:00
|
|
|
def get_authors_from_data(self, data):
|
|
|
|
return []
|
2021-03-08 16:49:10 +00:00
|
|
|
|
2020-12-31 20:22:00 +00:00
|
|
|
def expand_book_data(self, book):
|
|
|
|
pass
|
2021-03-08 16:49:10 +00:00
|
|
|
|
|
|
|
self.connector = TestConnector("example.com")
|
2020-12-31 20:22:00 +00:00
|
|
|
self.connector.book_mappings = [
|
2021-03-08 16:49:10 +00:00
|
|
|
Mapping("id"),
|
|
|
|
Mapping("title"),
|
|
|
|
Mapping("openlibraryKey"),
|
2020-12-31 20:22:00 +00:00
|
|
|
]
|
2020-12-31 19:04:19 +00:00
|
|
|
|
2021-08-02 23:05:40 +00:00
|
|
|
self.book = models.Edition.objects.create(
|
|
|
|
title="Test Book",
|
|
|
|
remote_id="https://example.com/book/1234",
|
|
|
|
openlibrary_key="OL1234M",
|
|
|
|
)
|
2020-12-31 20:22:00 +00:00
|
|
|
|
|
|
|
def test_abstract_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
|
|
|
self.assertIsInstance(self.connector.book_mappings, list)
|
2020-12-30 17:48:37 +00:00
|
|
|
|
2020-12-31 20:22:00 +00:00
|
|
|
def test_get_or_create_book_existing(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""find an existing book by remote/origin id"""
|
2020-12-31 20:22:00 +00:00
|
|
|
self.assertEqual(models.Book.objects.count(), 1)
|
2021-12-07 21:48:22 +00:00
|
|
|
self.assertEqual(self.book.remote_id, f"https://{DOMAIN}/book/{self.book.id}")
|
2021-03-08 16:49:10 +00:00
|
|
|
self.assertEqual(self.book.origin_id, "https://example.com/book/1234")
|
2020-12-31 20:22:00 +00:00
|
|
|
|
|
|
|
# dedupe by origin id
|
2021-03-08 16:49:10 +00:00
|
|
|
result = self.connector.get_or_create_book("https://example.com/book/1234")
|
2020-12-31 20:22:00 +00:00
|
|
|
self.assertEqual(models.Book.objects.count(), 1)
|
|
|
|
self.assertEqual(result, self.book)
|
|
|
|
|
|
|
|
# dedupe by remote id
|
|
|
|
result = self.connector.get_or_create_book(
|
2021-12-07 21:08:25 +00:00
|
|
|
f"https://{DOMAIN}/book/{self.book.id}"
|
2021-03-08 16:49:10 +00:00
|
|
|
)
|
2020-12-31 20:22:00 +00:00
|
|
|
self.assertEqual(models.Book.objects.count(), 1)
|
|
|
|
self.assertEqual(result, self.book)
|
|
|
|
|
2020-12-30 17:48:37 +00:00
|
|
|
@responses.activate
|
2020-12-31 20:22:00 +00:00
|
|
|
def test_get_or_create_book_deduped(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""load remote data and deduplicate"""
|
2020-12-30 17:48:37 +00:00
|
|
|
responses.add(
|
2021-03-08 16:49:10 +00:00
|
|
|
responses.GET, "https://example.com/book/abcd", json=self.edition_data
|
2020-12-30 17:48:37 +00:00
|
|
|
)
|
2021-08-02 23:05:40 +00:00
|
|
|
with patch("bookwyrm.connectors.abstract_connector.load_more_data.delay"):
|
2021-08-02 23:07:39 +00:00
|
|
|
result = self.connector.get_or_create_book("https://example.com/book/abcd")
|
2020-12-31 20:22:00 +00:00
|
|
|
self.assertEqual(result, self.book)
|
|
|
|
self.assertEqual(models.Edition.objects.count(), 1)
|
|
|
|
self.assertEqual(models.Edition.objects.count(), 1)
|
2021-03-13 17:27:59 +00:00
|
|
|
|
|
|
|
@responses.activate
|
|
|
|
def test_get_or_create_author(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""load an author"""
|
2021-12-07 21:08:25 +00:00
|
|
|
# pylint: disable=attribute-defined-outside-init
|
|
|
|
self.connector.author_mappings = [
|
2021-09-16 19:52:10 +00:00
|
|
|
Mapping("id"),
|
|
|
|
Mapping("name"),
|
|
|
|
]
|
2021-03-13 17:27:59 +00:00
|
|
|
|
|
|
|
responses.add(
|
|
|
|
responses.GET,
|
|
|
|
"https://www.example.com/author",
|
|
|
|
json={"id": "https://www.example.com/author", "name": "Test Author"},
|
|
|
|
)
|
|
|
|
result = self.connector.get_or_create_author("https://www.example.com/author")
|
|
|
|
self.assertIsInstance(result, models.Author)
|
|
|
|
self.assertEqual(result.name, "Test Author")
|
|
|
|
self.assertEqual(result.origin_id, "https://www.example.com/author")
|
|
|
|
|
|
|
|
def test_get_or_create_author_existing(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""get an existing author"""
|
2021-03-13 17:27:59 +00:00
|
|
|
author = models.Author.objects.create(name="Test Author")
|
|
|
|
result = self.connector.get_or_create_author(author.remote_id)
|
|
|
|
self.assertEqual(author, result)
|
2021-12-07 21:48:22 +00:00
|
|
|
|
|
|
|
@responses.activate
|
|
|
|
def test_update_author_from_remote(self):
|
|
|
|
"""trigger the function that looks up the remote data"""
|
|
|
|
author = models.Author.objects.create(name="Test", openlibrary_key="OL123A")
|
|
|
|
# pylint: disable=attribute-defined-outside-init
|
|
|
|
self.connector.author_mappings = [
|
|
|
|
Mapping("id"),
|
|
|
|
Mapping("name"),
|
|
|
|
Mapping("isni"),
|
|
|
|
]
|
|
|
|
|
|
|
|
responses.add(
|
|
|
|
responses.GET,
|
|
|
|
"https://openlibrary.org/authors/OL123A",
|
|
|
|
json={"id": "https://www.example.com/author", "name": "Beep", "isni": "hi"},
|
|
|
|
)
|
|
|
|
|
|
|
|
self.connector.update_author_from_remote(author)
|
|
|
|
|
|
|
|
author.refresh_from_db()
|
|
|
|
self.assertEqual(author.name, "Test")
|
|
|
|
self.assertEqual(author.isni, "hi")
|
2022-02-03 23:11:01 +00:00
|
|
|
|
|
|
|
def test_get_data_invalid_url(self):
|
|
|
|
"""load json data from an arbitrary url"""
|
|
|
|
with self.assertRaises(ConnectorException):
|
|
|
|
get_data("file://hello.com/image/jpg")
|
|
|
|
|
|
|
|
with self.assertRaises(ConnectorException):
|
|
|
|
get_data("http://127.0.0.1/image/jpg")
|