Runs black

This commit is contained in:
Mouse Reeve
2021-03-08 08:49:10 -08:00
parent a07f955781
commit 70296e760b
198 changed files with 10239 additions and 8572 deletions

View File

@ -1,4 +1,4 @@
''' testing book data connectors '''
""" testing book data connectors """
from unittest.mock import patch
from django.test import TestCase
import responses
@ -10,109 +10,115 @@ from bookwyrm.settings import DOMAIN
class AbstractConnector(TestCase):
''' generic code for connecting to outside data sources '''
""" generic code for connecting to outside data sources """
def setUp(self):
''' we need an example connector '''
""" we need an example connector """
self.connector_info = models.Connector.objects.create(
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=',
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=",
)
work_data = {
'id': 'abc1',
'title': 'Test work',
'type': 'work',
'openlibraryKey': 'OL1234W',
"id": "abc1",
"title": "Test work",
"type": "work",
"openlibraryKey": "OL1234W",
}
self.work_data = work_data
edition_data = {
'id': 'abc2',
'title': 'Test edition',
'type': 'edition',
'openlibraryKey': 'OL1234M',
"id": "abc2",
"title": "Test edition",
"type": "edition",
"openlibraryKey": "OL1234M",
}
self.edition_data = edition_data
class TestConnector(abstract_connector.AbstractConnector):
''' nothing added here '''
""" nothing added here """
def format_search_result(self, search_result):
return search_result
def parse_search_data(self, data):
return data
def format_isbn_search_result(self, search_result):
return search_result
def parse_isbn_search_data(self, data):
return data
def is_work_data(self, data):
return data['type'] == 'work'
return data["type"] == "work"
def get_edition_from_work_data(self, data):
return edition_data
def get_work_from_edition_data(self, data):
return work_data
def get_authors_from_data(self, data):
return []
def expand_book_data(self, book):
pass
self.connector = TestConnector('example.com')
self.connector = TestConnector("example.com")
self.connector.book_mappings = [
Mapping('id'),
Mapping('title'),
Mapping('openlibraryKey'),
Mapping("id"),
Mapping("title"),
Mapping("openlibraryKey"),
]
self.book = models.Edition.objects.create(
title='Test Book', remote_id='https://example.com/book/1234',
openlibrary_key='OL1234M')
title="Test Book",
remote_id="https://example.com/book/1234",
openlibrary_key="OL1234M",
)
def test_abstract_connector_init(self):
''' barebones connector for search with defaults '''
""" barebones connector for search with defaults """
self.assertIsInstance(self.connector.book_mappings, list)
def test_is_available(self):
''' this isn't used.... '''
""" this isn't used.... """
self.assertTrue(self.connector.is_available())
self.connector.max_query_count = 1
self.connector.connector.query_count = 2
self.assertFalse(self.connector.is_available())
def test_get_or_create_book_existing(self):
''' find an existing book by remote/origin id '''
""" find an existing book by remote/origin id """
self.assertEqual(models.Book.objects.count(), 1)
self.assertEqual(
self.book.remote_id, 'https://%s/book/%d' % (DOMAIN, self.book.id))
self.assertEqual(
self.book.origin_id, 'https://example.com/book/1234')
self.book.remote_id, "https://%s/book/%d" % (DOMAIN, self.book.id)
)
self.assertEqual(self.book.origin_id, "https://example.com/book/1234")
# dedupe by origin id
result = self.connector.get_or_create_book(
'https://example.com/book/1234')
result = self.connector.get_or_create_book("https://example.com/book/1234")
self.assertEqual(models.Book.objects.count(), 1)
self.assertEqual(result, self.book)
# dedupe by remote id
result = self.connector.get_or_create_book(
'https://%s/book/%d' % (DOMAIN, self.book.id))
"https://%s/book/%d" % (DOMAIN, self.book.id)
)
self.assertEqual(models.Book.objects.count(), 1)
self.assertEqual(result, self.book)
@responses.activate
def test_get_or_create_book_deduped(self):
''' load remote data and deduplicate '''
""" load remote data and deduplicate """
responses.add(
responses.GET,
'https://example.com/book/abcd',
json=self.edition_data
responses.GET, "https://example.com/book/abcd", json=self.edition_data
)
with patch(
'bookwyrm.connectors.abstract_connector.load_more_data.delay'):
result = self.connector.get_or_create_book(
'https://example.com/book/abcd')
with patch("bookwyrm.connectors.abstract_connector.load_more_data.delay"):
result = self.connector.get_or_create_book("https://example.com/book/abcd")
self.assertEqual(result, self.book)
self.assertEqual(models.Edition.objects.count(), 1)
self.assertEqual(models.Edition.objects.count(), 1)

View File

@ -1,4 +1,4 @@
''' testing book data connectors '''
""" testing book data connectors """
from django.test import TestCase
import responses
@ -8,99 +8,101 @@ from bookwyrm.connectors.abstract_connector import Mapping, SearchResult
class AbstractConnector(TestCase):
''' generic code for connecting to outside data sources '''
""" generic code for connecting to outside data sources """
def setUp(self):
''' we need an example connector '''
""" we need an example connector """
self.connector_info = models.Connector.objects.create(
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',
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",
)
class TestConnector(abstract_connector.AbstractMinimalConnector):
''' nothing added here '''
""" nothing added here """
def format_search_result(self, search_result):
return search_result
def get_or_create_book(self, remote_id):
pass
def parse_search_data(self, data):
return data
def format_isbn_search_result(self, search_result):
return search_result
def parse_isbn_search_data(self, data):
return data
self.test_connector = TestConnector('example.com')
self.test_connector = TestConnector("example.com")
def test_abstract_minimal_connector_init(self):
''' barebones connector for search with defaults '''
""" barebones connector for search with defaults """
connector = self.test_connector
self.assertEqual(connector.connector, self.connector_info)
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')
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")
self.assertIsNone(connector.name)
self.assertEqual(connector.identifier, 'example.com')
self.assertEqual(connector.identifier, "example.com")
self.assertIsNone(connector.max_query_count)
self.assertFalse(connector.local)
@responses.activate
def test_search(self):
''' makes an http request to the outside service '''
""" makes an http request to the outside service """
responses.add(
responses.GET,
'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')
"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")
self.assertEqual(len(results), 10)
self.assertEqual(results[0], 'a')
self.assertEqual(results[1], 'b')
self.assertEqual(results[2], 'c')
self.assertEqual(results[0], "a")
self.assertEqual(results[1], "b")
self.assertEqual(results[2], "c")
def test_search_result(self):
''' a class that stores info about a search result '''
""" a class that stores info about a search result """
result = SearchResult(
title='Title',
key='https://example.com/book/1',
author='Author Name',
year='1850',
title="Title",
key="https://example.com/book/1",
author="Author Name",
year="1850",
connector=self.test_connector,
)
# there's really not much to test here, it's just a dataclass
self.assertEqual(result.confidence, 1)
self.assertEqual(result.title, 'Title')
self.assertEqual(result.title, "Title")
def test_create_mapping(self):
''' maps remote fields for book data to bookwyrm activitypub fields '''
mapping = Mapping('isbn')
self.assertEqual(mapping.local_field, 'isbn')
self.assertEqual(mapping.remote_field, 'isbn')
self.assertEqual(mapping.formatter('bb'), 'bb')
""" maps remote fields for book data to bookwyrm activitypub fields """
mapping = Mapping("isbn")
self.assertEqual(mapping.local_field, "isbn")
self.assertEqual(mapping.remote_field, "isbn")
self.assertEqual(mapping.formatter("bb"), "bb")
def test_create_mapping_with_remote(self):
''' the remote field is different than the local field '''
mapping = Mapping('isbn', remote_field='isbn13')
self.assertEqual(mapping.local_field, 'isbn')
self.assertEqual(mapping.remote_field, 'isbn13')
self.assertEqual(mapping.formatter('bb'), 'bb')
""" the remote field is different than the local field """
mapping = Mapping("isbn", remote_field="isbn13")
self.assertEqual(mapping.local_field, "isbn")
self.assertEqual(mapping.remote_field, "isbn13")
self.assertEqual(mapping.formatter("bb"), "bb")
def test_create_mapping_with_formatter(self):
''' a function is provided to modify the data '''
formatter = lambda x: 'aa' + x
mapping = Mapping('isbn', formatter=formatter)
self.assertEqual(mapping.local_field, 'isbn')
self.assertEqual(mapping.remote_field, 'isbn')
""" a function is provided to modify the data """
formatter = lambda x: "aa" + x
mapping = Mapping("isbn", formatter=formatter)
self.assertEqual(mapping.local_field, "isbn")
self.assertEqual(mapping.remote_field, "isbn")
self.assertEqual(mapping.formatter, formatter)
self.assertEqual(mapping.formatter('bb'), 'aabb')
self.assertEqual(mapping.formatter("bb"), "aabb")

View File

@ -1,4 +1,4 @@
''' testing book data connectors '''
""" testing book data connectors """
import json
import pathlib
from django.test import TestCase
@ -9,39 +9,36 @@ from bookwyrm.connectors.abstract_connector import SearchResult
class BookWyrmConnector(TestCase):
''' this connector doesn't do much, just search '''
def setUp(self):
''' create the connector '''
models.Connector.objects.create(
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=',
)
self.connector = Connector('example.com')
""" this connector doesn't do much, just search """
work_file = pathlib.Path(__file__).parent.joinpath(
'../data/bw_work.json')
edition_file = pathlib.Path(__file__).parent.joinpath(
'../data/bw_edition.json')
def setUp(self):
""" create the connector """
models.Connector.objects.create(
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=",
)
self.connector = Connector("example.com")
work_file = pathlib.Path(__file__).parent.joinpath("../data/bw_work.json")
edition_file = pathlib.Path(__file__).parent.joinpath("../data/bw_edition.json")
self.work_data = json.loads(work_file.read_bytes())
self.edition_data = json.loads(edition_file.read_bytes())
def test_format_search_result(self):
''' create a SearchResult object from search response json '''
datafile = pathlib.Path(__file__).parent.joinpath(
'../data/bw_search.json')
""" create a SearchResult object from search response json """
datafile = pathlib.Path(__file__).parent.joinpath("../data/bw_search.json")
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.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)
self.assertEqual(result.connector, self.connector)

View File

@ -1,54 +1,49 @@
''' interface between the app and various connectors '''
""" interface between the app and various connectors """
from django.test import TestCase
from bookwyrm import models
from bookwyrm.connectors import connector_manager
from bookwyrm.connectors.bookwyrm_connector \
import Connector as BookWyrmConnector
from bookwyrm.connectors.self_connector \
import Connector as SelfConnector
from bookwyrm.connectors.bookwyrm_connector import Connector as BookWyrmConnector
from bookwyrm.connectors.self_connector import Connector as SelfConnector
class ConnectorManager(TestCase):
''' interface between the app and various connectors '''
""" interface between the app and various connectors """
def setUp(self):
''' we'll need some books and a connector info entry '''
self.work = models.Work.objects.create(
title='Example Work'
)
""" we'll need some books and a connector info entry """
self.work = models.Work.objects.create(title="Example Work")
self.edition = models.Edition.objects.create(
title='Example Edition',
parent_work=self.work
title="Example Edition", parent_work=self.work
)
self.work.default_edition = self.edition
self.work.save()
self.connector = models.Connector.objects.create(
identifier='test_connector',
identifier="test_connector",
priority=1,
local=True,
connector_file='self_connector',
base_url='http://test.com/',
books_url='http://test.com/',
covers_url='http://test.com/',
connector_file="self_connector",
base_url="http://test.com/",
books_url="http://test.com/",
covers_url="http://test.com/",
)
def test_get_or_create_connector(self):
''' loads a connector if the data source is known or creates one '''
remote_id = 'https://example.com/object/1'
""" loads a connector if the data source is known or creates one """
remote_id = "https://example.com/object/1"
connector = connector_manager.get_or_create_connector(remote_id)
self.assertIsInstance(connector, BookWyrmConnector)
self.assertEqual(connector.identifier, 'example.com')
self.assertEqual(connector.base_url, 'https://example.com')
self.assertEqual(connector.identifier, "example.com")
self.assertEqual(connector.base_url, "https://example.com")
same_connector = connector_manager.get_or_create_connector(remote_id)
self.assertEqual(connector.identifier, same_connector.identifier)
def test_get_connectors(self):
''' load all connectors '''
remote_id = 'https://example.com/object/1'
""" load all connectors """
remote_id = "https://example.com/object/1"
connector_manager.get_or_create_connector(remote_id)
connectors = list(connector_manager.get_connectors())
self.assertEqual(len(connectors), 2)
@ -56,28 +51,28 @@ class ConnectorManager(TestCase):
self.assertIsInstance(connectors[1], BookWyrmConnector)
def test_search(self):
''' search all connectors '''
results = connector_manager.search('Example')
""" search all connectors """
results = connector_manager.search("Example")
self.assertEqual(len(results), 1)
self.assertIsInstance(results[0]['connector'], SelfConnector)
self.assertEqual(len(results[0]['results']), 1)
self.assertEqual(results[0]['results'][0].title, 'Example Edition')
self.assertIsInstance(results[0]["connector"], SelfConnector)
self.assertEqual(len(results[0]["results"]), 1)
self.assertEqual(results[0]["results"][0].title, "Example Edition")
def test_local_search(self):
''' search only the local database '''
results = connector_manager.local_search('Example')
""" search only the local database """
results = connector_manager.local_search("Example")
self.assertEqual(len(results), 1)
self.assertEqual(results[0].title, 'Example Edition')
self.assertEqual(results[0].title, "Example Edition")
def test_first_search_result(self):
''' only get one search result '''
result = connector_manager.first_search_result('Example')
self.assertEqual(result.title, 'Example Edition')
no_result = connector_manager.first_search_result('dkjfhg')
""" only get one search result """
result = connector_manager.first_search_result("Example")
self.assertEqual(result.title, "Example Edition")
no_result = connector_manager.first_search_result("dkjfhg")
self.assertIsNone(no_result)
def test_load_connector(self):
''' load a connector object from the database entry '''
""" load a connector object from the database entry """
connector = connector_manager.load_connector(self.connector)
self.assertIsInstance(connector, SelfConnector)
self.assertEqual(connector.identifier, 'test_connector')
self.assertEqual(connector.identifier, "test_connector")

View File

@ -1,4 +1,4 @@
''' testing book data connectors '''
""" testing book data connectors """
import json
import pathlib
from unittest.mock import patch
@ -9,253 +9,231 @@ import responses
from bookwyrm import models
from bookwyrm.connectors.openlibrary import Connector
from bookwyrm.connectors.openlibrary import get_languages, get_description
from bookwyrm.connectors.openlibrary import pick_default_edition, \
get_openlibrary_key
from bookwyrm.connectors.openlibrary import pick_default_edition, get_openlibrary_key
from bookwyrm.connectors.abstract_connector import SearchResult
from bookwyrm.connectors.connector_manager import ConnectorException
class Openlibrary(TestCase):
''' test loading data from openlibrary.org '''
def setUp(self):
''' creates the connector we'll use '''
models.Connector.objects.create(
identifier='openlibrary.org',
name='OpenLibrary',
connector_file='openlibrary',
base_url='https://openlibrary.org',
books_url='https://openlibrary.org',
covers_url='https://covers.openlibrary.org',
search_url='https://openlibrary.org/search?q=',
isbn_search_url='https://openlibrary.org/isbn',
)
self.connector = Connector('openlibrary.org')
""" test loading data from openlibrary.org """
work_file = pathlib.Path(__file__).parent.joinpath(
'../data/ol_work.json')
edition_file = pathlib.Path(__file__).parent.joinpath(
'../data/ol_edition.json')
def setUp(self):
""" creates the connector we'll use """
models.Connector.objects.create(
identifier="openlibrary.org",
name="OpenLibrary",
connector_file="openlibrary",
base_url="https://openlibrary.org",
books_url="https://openlibrary.org",
covers_url="https://covers.openlibrary.org",
search_url="https://openlibrary.org/search?q=",
isbn_search_url="https://openlibrary.org/isbn",
)
self.connector = Connector("openlibrary.org")
work_file = pathlib.Path(__file__).parent.joinpath("../data/ol_work.json")
edition_file = pathlib.Path(__file__).parent.joinpath("../data/ol_edition.json")
edition_list_file = pathlib.Path(__file__).parent.joinpath(
'../data/ol_edition_list.json')
"../data/ol_edition_list.json"
)
self.work_data = json.loads(work_file.read_bytes())
self.edition_data = json.loads(edition_file.read_bytes())
self.edition_list_data = json.loads(edition_list_file.read_bytes())
def test_get_remote_id_from_data(self):
''' format the remote id from the data '''
data = {'key': '/work/OL1234W'}
""" format the remote id from the data """
data = {"key": "/work/OL1234W"}
result = self.connector.get_remote_id_from_data(data)
self.assertEqual(result, 'https://openlibrary.org/work/OL1234W')
self.assertEqual(result, "https://openlibrary.org/work/OL1234W")
# error handlding
with self.assertRaises(ConnectorException):
self.connector.get_remote_id_from_data({})
def test_is_work_data(self):
''' detect if the loaded json is a work '''
""" detect if the loaded json is a work """
self.assertEqual(self.connector.is_work_data(self.work_data), True)
self.assertEqual(self.connector.is_work_data(self.edition_data), False)
@responses.activate
def test_get_edition_from_work_data(self):
''' loads a list of editions '''
data = {'key': '/work/OL1234W'}
""" loads a list of editions """
data = {"key": "/work/OL1234W"}
responses.add(
responses.GET,
'https://openlibrary.org/work/OL1234W/editions',
json={'entries': []},
status=200)
with patch('bookwyrm.connectors.openlibrary.pick_default_edition') \
as pick_edition:
pick_edition.return_value = 'hi'
"https://openlibrary.org/work/OL1234W/editions",
json={"entries": []},
status=200,
)
with patch(
"bookwyrm.connectors.openlibrary.pick_default_edition"
) as pick_edition:
pick_edition.return_value = "hi"
result = self.connector.get_edition_from_work_data(data)
self.assertEqual(result, 'hi')
self.assertEqual(result, "hi")
@responses.activate
def test_get_work_from_edition_data(self):
''' loads a list of editions '''
data = {'works': [{'key': '/work/OL1234W'}]}
""" loads a list of editions """
data = {"works": [{"key": "/work/OL1234W"}]}
responses.add(
responses.GET,
'https://openlibrary.org/work/OL1234W',
json={'hi': 'there'},
status=200)
"https://openlibrary.org/work/OL1234W",
json={"hi": "there"},
status=200,
)
result = self.connector.get_work_from_edition_data(data)
self.assertEqual(result, {'hi': 'there'})
self.assertEqual(result, {"hi": "there"})
@responses.activate
def test_get_authors_from_data(self):
''' find authors in data '''
""" find authors in data """
responses.add(
responses.GET,
'https://openlibrary.org/authors/OL382982A',
"https://openlibrary.org/authors/OL382982A",
json={
"name": "George Elliott",
"personal_name": "George Elliott",
"last_modified": {
"type": "/type/datetime",
"value": "2008-08-31 10:09:33.413686"
},
"value": "2008-08-31 10:09:33.413686",
},
"key": "/authors/OL453734A",
"type": {
"key": "/type/author"
},
"type": {"key": "/type/author"},
"id": 1259965,
"revision": 2
"revision": 2,
},
status=200)
status=200,
)
results = self.connector.get_authors_from_data(self.work_data)
result = list(results)[0]
self.assertIsInstance(result, models.Author)
self.assertEqual(result.name, 'George Elliott')
self.assertEqual(result.openlibrary_key, 'OL453734A')
self.assertEqual(result.name, "George Elliott")
self.assertEqual(result.openlibrary_key, "OL453734A")
def test_get_cover_url(self):
''' formats a url that should contain the cover image '''
blob = ['image']
""" formats a url that should contain the cover image """
blob = ["image"]
result = self.connector.get_cover_url(blob)
self.assertEqual(
result, 'https://covers.openlibrary.org/b/id/image-L.jpg')
self.assertEqual(result, "https://covers.openlibrary.org/b/id/image-L.jpg")
def test_parse_search_result(self):
''' extract the results from the search json response '''
datafile = pathlib.Path(__file__).parent.joinpath(
'../data/ol_search.json')
""" extract the results from the search json response """
datafile = pathlib.Path(__file__).parent.joinpath("../data/ol_search.json")
search_data = json.loads(datafile.read_bytes())
result = self.connector.parse_search_data(search_data)
self.assertIsInstance(result, list)
self.assertEqual(len(result), 2)
def test_format_search_result(self):
''' translate json from openlibrary into SearchResult '''
datafile = pathlib.Path(__file__).parent.joinpath(
'../data/ol_search.json')
""" translate json from openlibrary into SearchResult """
datafile = pathlib.Path(__file__).parent.joinpath("../data/ol_search.json")
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, 'This Is How You Lose the Time War')
self.assertEqual(
result.key, 'https://openlibrary.org/works/OL20639540W')
self.assertEqual(result.author, 'Amal El-Mohtar, Max Gladstone')
self.assertEqual(result.title, "This Is How You Lose the Time War")
self.assertEqual(result.key, "https://openlibrary.org/works/OL20639540W")
self.assertEqual(result.author, "Amal El-Mohtar, Max Gladstone")
self.assertEqual(result.year, 2019)
self.assertEqual(result.connector, self.connector)
def test_parse_isbn_search_result(self):
''' extract the results from the search json response '''
datafile = pathlib.Path(__file__).parent.joinpath(
'../data/ol_isbn_search.json')
""" extract the results from the search json response """
datafile = pathlib.Path(__file__).parent.joinpath("../data/ol_isbn_search.json")
search_data = json.loads(datafile.read_bytes())
result = self.connector.parse_isbn_search_data(search_data)
self.assertIsInstance(result, list)
self.assertEqual(len(result), 1)
def test_format_isbn_search_result(self):
''' translate json from openlibrary into SearchResult '''
datafile = pathlib.Path(__file__).parent.joinpath(
'../data/ol_isbn_search.json')
""" translate json from openlibrary into SearchResult """
datafile = pathlib.Path(__file__).parent.joinpath("../data/ol_isbn_search.json")
search_data = json.loads(datafile.read_bytes())
results = self.connector.parse_isbn_search_data(search_data)
self.assertIsInstance(results, list)
result = self.connector.format_isbn_search_result(results[0])
self.assertIsInstance(result, SearchResult)
self.assertEqual(result.title, 'Les ombres errantes')
self.assertEqual(
result.key, 'https://openlibrary.org/books/OL16262504M')
self.assertEqual(result.author, 'Pascal Quignard')
self.assertEqual(result.year, '2002')
self.assertEqual(result.title, "Les ombres errantes")
self.assertEqual(result.key, "https://openlibrary.org/books/OL16262504M")
self.assertEqual(result.author, "Pascal Quignard")
self.assertEqual(result.year, "2002")
self.assertEqual(result.connector, self.connector)
@responses.activate
def test_load_edition_data(self):
''' format url from key and make request '''
key = 'OL1234W'
""" format url from key and make request """
key = "OL1234W"
responses.add(
responses.GET,
'https://openlibrary.org/works/OL1234W/editions',
json={'hi': 'there'}
"https://openlibrary.org/works/OL1234W/editions",
json={"hi": "there"},
)
result = self.connector.load_edition_data(key)
self.assertEqual(result, {'hi': 'there'})
self.assertEqual(result, {"hi": "there"})
@responses.activate
def test_expand_book_data(self):
''' given a book, get more editions '''
work = models.Work.objects.create(
title='Test Work', openlibrary_key='OL1234W')
edition = models.Edition.objects.create(
title='Test Edition', parent_work=work)
""" given a book, get more editions """
work = models.Work.objects.create(title="Test Work", openlibrary_key="OL1234W")
edition = models.Edition.objects.create(title="Test Edition", parent_work=work)
responses.add(
responses.GET,
'https://openlibrary.org/works/OL1234W/editions',
json={'entries': []},
"https://openlibrary.org/works/OL1234W/editions",
json={"entries": []},
)
with patch(
'bookwyrm.connectors.abstract_connector.AbstractConnector.' \
'create_edition_from_data'):
"bookwyrm.connectors.abstract_connector.AbstractConnector."
"create_edition_from_data"
):
self.connector.expand_book_data(edition)
self.connector.expand_book_data(work)
def test_get_description(self):
''' should do some cleanup on the description data '''
description = get_description(self.work_data['description'])
expected = 'First in the Old Kingdom/Abhorsen series.'
""" should do some cleanup on the description data """
description = get_description(self.work_data["description"])
expected = "First in the Old Kingdom/Abhorsen series."
self.assertEqual(description, expected)
def test_get_openlibrary_key(self):
''' extracts the uuid '''
key = get_openlibrary_key('/books/OL27320736M')
self.assertEqual(key, 'OL27320736M')
""" extracts the uuid """
key = get_openlibrary_key("/books/OL27320736M")
self.assertEqual(key, "OL27320736M")
def test_get_languages(self):
''' looks up languages from a list '''
languages = get_languages(self.edition_data['languages'])
self.assertEqual(languages, ['English'])
""" looks up languages from a list """
languages = get_languages(self.edition_data["languages"])
self.assertEqual(languages, ["English"])
def test_pick_default_edition(self):
''' detect if the loaded json is an edition '''
edition = pick_default_edition(self.edition_list_data['entries'])
self.assertEqual(edition['key'], '/books/OL9788823M')
""" detect if the loaded json is an edition """
edition = pick_default_edition(self.edition_list_data["entries"])
self.assertEqual(edition["key"], "/books/OL9788823M")
@responses.activate
def test_create_edition_from_data(self):
''' okay but can it actually create an edition with proper metadata '''
work = models.Work.objects.create(title='Hello')
""" okay but can it actually create an edition with proper metadata """
work = models.Work.objects.create(title="Hello")
responses.add(
responses.GET,
'https://openlibrary.org/authors/OL382982A',
json={'hi': 'there'},
status=200)
with patch('bookwyrm.connectors.openlibrary.Connector.' \
'get_authors_from_data') as mock:
"https://openlibrary.org/authors/OL382982A",
json={"hi": "there"},
status=200,
)
with patch(
"bookwyrm.connectors.openlibrary.Connector." "get_authors_from_data"
) as mock:
mock.return_value = []
result = self.connector.create_edition_from_data(
work, self.edition_data)
result = self.connector.create_edition_from_data(work, self.edition_data)
self.assertEqual(result.parent_work, work)
self.assertEqual(result.title, 'Sabriel')
self.assertEqual(result.isbn_10, '0060273224')
self.assertEqual(result.title, "Sabriel")
self.assertEqual(result.isbn_10, "0060273224")
self.assertIsNotNone(result.description)
self.assertEqual(result.languages[0], 'English')
self.assertEqual(result.publishers[0], 'Harper Trophy')
self.assertEqual(result.languages[0], "English")
self.assertEqual(result.publishers[0], "Harper Trophy")
self.assertEqual(result.pages, 491)
self.assertEqual(result.subjects[0], 'Fantasy.')
self.assertEqual(result.physical_format, 'Hardcover')
self.assertEqual(result.subjects[0], "Fantasy.")
self.assertEqual(result.physical_format, "Hardcover")

View File

@ -1,4 +1,4 @@
''' testing book data connectors '''
""" testing book data connectors """
import datetime
from django.test import TestCase
from django.utils import timezone
@ -9,100 +9,98 @@ from bookwyrm.settings import DOMAIN
class SelfConnector(TestCase):
''' just uses local data '''
""" just uses local data """
def setUp(self):
''' creating the connector '''
""" creating the connector """
models.Connector.objects.create(
identifier=DOMAIN,
name='Local',
name="Local",
local=True,
connector_file='self_connector',
base_url='https://%s' % DOMAIN,
books_url='https://%s/book' % DOMAIN,
covers_url='https://%s/images/covers' % DOMAIN,
search_url='https://%s/search?q=' % DOMAIN,
connector_file="self_connector",
base_url="https://%s" % DOMAIN,
books_url="https://%s/book" % DOMAIN,
covers_url="https://%s/images/covers" % DOMAIN,
search_url="https://%s/search?q=" % DOMAIN,
priority=1,
)
self.connector = Connector(DOMAIN)
def test_format_search_result(self):
''' create a SearchResult '''
author = models.Author.objects.create(name='Anonymous')
""" create a SearchResult """
author = models.Author.objects.create(name="Anonymous")
edition = models.Edition.objects.create(
title='Edition of Example Work',
title="Edition of Example Work",
published_date=datetime.datetime(1980, 5, 10, tzinfo=timezone.utc),
)
edition.authors.add(author)
result = self.connector.search('Edition of Example')[0]
self.assertEqual(result.title, 'Edition of Example Work')
result = self.connector.search("Edition of Example")[0]
self.assertEqual(result.title, "Edition of Example Work")
self.assertEqual(result.key, edition.remote_id)
self.assertEqual(result.author, 'Anonymous')
self.assertEqual(result.author, "Anonymous")
self.assertEqual(result.year, 1980)
self.assertEqual(result.connector, self.connector)
def test_search_rank(self):
''' prioritize certain results '''
author = models.Author.objects.create(name='Anonymous')
""" prioritize certain results """
author = models.Author.objects.create(name="Anonymous")
edition = models.Edition.objects.create(
title='Edition of Example Work',
title="Edition of Example Work",
published_date=datetime.datetime(1980, 5, 10, tzinfo=timezone.utc),
parent_work=models.Work.objects.create(title='')
parent_work=models.Work.objects.create(title=""),
)
# author text is rank C
edition.authors.add(author)
# series is rank D
models.Edition.objects.create(
title='Another Edition',
series='Anonymous',
parent_work=models.Work.objects.create(title='')
title="Another Edition",
series="Anonymous",
parent_work=models.Work.objects.create(title=""),
)
# subtitle is rank B
models.Edition.objects.create(
title='More Editions',
subtitle='The Anonymous Edition',
parent_work=models.Work.objects.create(title='')
title="More Editions",
subtitle="The Anonymous Edition",
parent_work=models.Work.objects.create(title=""),
)
# title is rank A
models.Edition.objects.create(title='Anonymous')
models.Edition.objects.create(title="Anonymous")
# doesn't rank in this search
edition = models.Edition.objects.create(
title='An Edition',
parent_work=models.Work.objects.create(title='')
title="An Edition", parent_work=models.Work.objects.create(title="")
)
results = self.connector.search('Anonymous')
results = self.connector.search("Anonymous")
self.assertEqual(len(results), 3)
self.assertEqual(results[0].title, 'Anonymous')
self.assertEqual(results[1].title, 'More Editions')
self.assertEqual(results[2].title, 'Edition of Example Work')
self.assertEqual(results[0].title, "Anonymous")
self.assertEqual(results[1].title, "More Editions")
self.assertEqual(results[2].title, "Edition of Example Work")
def test_search_multiple_editions(self):
''' it should get rid of duplicate editions for the same work '''
work = models.Work.objects.create(title='Work Title')
""" it should get rid of duplicate editions for the same work """
work = models.Work.objects.create(title="Work Title")
edition_1 = models.Edition.objects.create(
title='Edition 1 Title', parent_work=work)
title="Edition 1 Title", parent_work=work
)
edition_2 = models.Edition.objects.create(
title='Edition 2 Title', parent_work=work)
edition_3 = models.Edition.objects.create(
title='Fish', parent_work=work)
title="Edition 2 Title", parent_work=work
)
edition_3 = models.Edition.objects.create(title="Fish", parent_work=work)
work.default_edition = edition_2
work.save()
# pick the best edition
results = self.connector.search('Edition 1 Title')
results = self.connector.search("Edition 1 Title")
self.assertEqual(len(results), 1)
self.assertEqual(results[0].key, edition_1.remote_id)
# pick the default edition when no match is best
results = self.connector.search('Edition Title')
results = self.connector.search("Edition Title")
self.assertEqual(len(results), 1)
self.assertEqual(results[0].key, edition_2.remote_id)
# only matches one edition, so no deduplication takes place
results = self.connector.search('Fish')
results = self.connector.search("Fish")
self.assertEqual(len(results), 1)
self.assertEqual(results[0].key, edition_3.remote_id)