2020-05-10 21:53:45 +00:00
|
|
|
''' testing book data connectors '''
|
|
|
|
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
|
|
|
|
2020-09-21 15:10:37 +00:00
|
|
|
from bookwyrm import models
|
|
|
|
from bookwyrm.connectors.bookwyrm_connector import Connector
|
2020-12-03 21:22:50 +00:00
|
|
|
from bookwyrm.connectors.abstract_connector import SearchResult
|
2020-05-10 21:53:45 +00:00
|
|
|
|
|
|
|
|
2020-09-21 15:22:58 +00:00
|
|
|
class BookWyrmConnector(TestCase):
|
2020-12-03 21:22:50 +00:00
|
|
|
''' this connector doesn't do much, just search '''
|
2020-05-10 21:53:45 +00:00
|
|
|
def setUp(self):
|
2020-12-03 21:22:50 +00:00
|
|
|
''' create the connector '''
|
2020-05-10 21:53:45 +00:00
|
|
|
models.Connector.objects.create(
|
|
|
|
identifier='example.com',
|
2020-09-21 15:10:37 +00:00
|
|
|
connector_file='bookwyrm_connector',
|
2020-05-10 21:53:45 +00:00
|
|
|
base_url='https://example.com',
|
|
|
|
books_url='https://example.com',
|
|
|
|
covers_url='https://example.com/images/covers',
|
|
|
|
search_url='https://example.com/search?q=',
|
|
|
|
)
|
|
|
|
self.connector = Connector('example.com')
|
|
|
|
|
|
|
|
work_file = pathlib.Path(__file__).parent.joinpath(
|
2020-11-28 00:12:47 +00:00
|
|
|
'../data/bw_work.json')
|
2020-05-10 21:53:45 +00:00
|
|
|
edition_file = pathlib.Path(__file__).parent.joinpath(
|
2020-11-28 00:12:47 +00:00
|
|
|
'../data/bw_edition.json')
|
2020-05-10 21:53:45 +00:00
|
|
|
self.work_data = json.loads(work_file.read_bytes())
|
|
|
|
self.edition_data = json.loads(edition_file.read_bytes())
|
|
|
|
|
|
|
|
|
|
|
|
def test_format_search_result(self):
|
2020-12-30 17:51:37 +00:00
|
|
|
''' create a SearchResult object from search response json '''
|
2020-12-03 21:22:50 +00:00
|
|
|
datafile = pathlib.Path(__file__).parent.joinpath(
|
2021-01-14 03:45:36 +00:00
|
|
|
'../data/bw_search.json')
|
2020-05-10 21:53:45 +00:00
|
|
|
search_data = json.loads(datafile.read_bytes())
|
|
|
|
results = self.connector.parse_search_data(search_data)
|
|
|
|
self.assertIsInstance(results, list)
|
|
|
|
|
|
|
|
result = self.connector.format_search_result(results[0])
|
|
|
|
self.assertIsInstance(result, SearchResult)
|
|
|
|
self.assertEqual(result.title, 'Jonathan Strange and Mr Norrell')
|
|
|
|
self.assertEqual(result.key, 'https://example.com/book/122')
|
|
|
|
self.assertEqual(result.author, 'Susanna Clarke')
|
|
|
|
self.assertEqual(result.year, 2017)
|
2020-12-30 17:51:37 +00:00
|
|
|
self.assertEqual(result.connector, self.connector)
|