bookwyrm/bookwyrm/tests/connectors/test_bookwyrm_connector.py

55 lines
2.2 KiB
Python
Raw Normal View History

2021-03-08 16:49:10 +00:00
""" testing book data connectors """
2020-05-10 21:53:45 +00:00
import json
import pathlib
2020-12-03 21:22:50 +00:00
from django.test import TestCase
2020-05-10 21:53:45 +00:00
from bookwyrm import models
2021-09-16 19:41:30 +00:00
from bookwyrm.book_search import SearchResult
from bookwyrm.connectors.bookwyrm_connector import Connector
2020-05-10 21:53:45 +00:00
2020-09-21 15:22:58 +00:00
class BookWyrmConnector(TestCase):
2021-04-26 16:15:42 +00:00
"""this connector doesn't do much, just search"""
2021-03-08 16:49:10 +00:00
@classmethod
def setUpTestData(cls):
"""create bookwrym_connector in the database"""
2020-05-10 21:53:45 +00:00
models.Connector.objects.create(
2021-03-08 16:49:10 +00:00
identifier="example.com",
connector_file="bookwyrm_connector",
base_url="https://example.com",
books_url="https://example.com",
covers_url="https://example.com/images/covers",
search_url="https://example.com/search?q=",
2020-05-10 21:53:45 +00:00
)
def setUp(self):
"""test data"""
2021-03-08 16:49:10 +00:00
self.connector = Connector("example.com")
2020-05-10 21:53:45 +00:00
2021-03-13 17:41:40 +00:00
def test_get_or_create_book_existing(self):
2021-04-26 16:15:42 +00:00
"""load book activity"""
2021-08-02 23:05:40 +00:00
work = models.Work.objects.create(title="Test Work")
book = models.Edition.objects.create(title="Test Edition", parent_work=work)
2021-03-13 17:41:40 +00:00
result = self.connector.get_or_create_book(book.remote_id)
self.assertEqual(book, result)
2020-05-10 21:53:45 +00:00
2022-05-30 23:16:10 +00:00
def test_parse_search_data(self):
2021-04-26 16:15:42 +00:00
"""create a SearchResult object from search response json"""
2021-03-08 16:49:10 +00:00
datafile = pathlib.Path(__file__).parent.joinpath("../data/bw_search.json")
2020-05-10 21:53:45 +00:00
search_data = json.loads(datafile.read_bytes())
2022-05-31 00:00:34 +00:00
result = list(self.connector.parse_search_data(search_data, 0))[0]
2020-05-10 21:53:45 +00:00
self.assertIsInstance(result, SearchResult)
2021-03-08 16:49:10 +00:00
self.assertEqual(result.title, "Jonathan Strange and Mr Norrell")
self.assertEqual(result.key, "https://example.com/book/122")
self.assertEqual(result.author, "Susanna Clarke")
2020-05-10 21:53:45 +00:00
self.assertEqual(result.year, 2017)
self.assertEqual(result.connector, self.connector)
2021-03-13 17:41:40 +00:00
2022-05-30 23:16:10 +00:00
def test_parse_isbn_search_data(self):
2021-04-26 16:15:42 +00:00
"""just gotta attach the connector"""
2021-03-13 17:41:40 +00:00
datafile = pathlib.Path(__file__).parent.joinpath("../data/bw_search.json")
search_data = json.loads(datafile.read_bytes())
2022-05-30 23:16:10 +00:00
result = list(self.connector.parse_isbn_search_data(search_data))[0]
2021-03-13 17:41:40 +00:00
self.assertEqual(result.connector, self.connector)