rename main code directory
This commit is contained in:
1
bookwyrm/tests/connectors/__init__.py
Normal file
1
bookwyrm/tests/connectors/__init__.py
Normal file
@ -0,0 +1 @@
|
||||
from . import *
|
122
bookwyrm/tests/connectors/test_abstract_connector.py
Normal file
122
bookwyrm/tests/connectors/test_abstract_connector.py
Normal file
@ -0,0 +1,122 @@
|
||||
''' testing book data connectors '''
|
||||
from django.test import TestCase
|
||||
|
||||
from fedireads import models
|
||||
from fedireads.connectors.abstract_connector import Mapping,\
|
||||
update_from_mappings
|
||||
from fedireads.connectors.fedireads_connector import Connector
|
||||
|
||||
|
||||
class FedireadsConnector(TestCase):
|
||||
def setUp(self):
|
||||
self.book = models.Edition.objects.create(title='Example Edition')
|
||||
|
||||
models.Connector.objects.create(
|
||||
identifier='example.com',
|
||||
connector_file='fedireads_connector',
|
||||
base_url='https://example.com',
|
||||
books_url='https:/example.com',
|
||||
covers_url='https://example.com',
|
||||
search_url='https://example.com/search?q=',
|
||||
)
|
||||
self.connector = Connector('example.com')
|
||||
|
||||
self.data = {
|
||||
'title': 'Unused title',
|
||||
'ASIN': 'A00BLAH',
|
||||
'isbn_10': '1234567890',
|
||||
'isbn_13': 'blahhh',
|
||||
'blah': 'bip',
|
||||
'format': 'hardcover',
|
||||
'series': ['one', 'two'],
|
||||
}
|
||||
self.connector.key_mappings = [
|
||||
Mapping('isbn_10', model=models.Edition),
|
||||
Mapping('isbn_13'),
|
||||
Mapping('lccn', model=models.Work),
|
||||
Mapping('asin', remote_field='ASIN'),
|
||||
]
|
||||
|
||||
|
||||
def test_create_mapping(self):
|
||||
mapping = Mapping('isbn')
|
||||
self.assertEqual(mapping.local_field, 'isbn')
|
||||
self.assertEqual(mapping.remote_field, 'isbn')
|
||||
self.assertEqual(mapping.model, None)
|
||||
self.assertEqual(mapping.formatter('bb'), 'bb')
|
||||
|
||||
|
||||
def test_create_mapping_with_remote(self):
|
||||
mapping = Mapping('isbn', remote_field='isbn13')
|
||||
self.assertEqual(mapping.local_field, 'isbn')
|
||||
self.assertEqual(mapping.remote_field, 'isbn13')
|
||||
self.assertEqual(mapping.model, None)
|
||||
self.assertEqual(mapping.formatter('bb'), 'bb')
|
||||
|
||||
|
||||
def test_create_mapping_with_formatter(self):
|
||||
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.model, None)
|
||||
self.assertEqual(mapping.formatter('bb'), 'aabb')
|
||||
|
||||
|
||||
def test_update_from_mappings(self):
|
||||
data = {
|
||||
'title': 'Unused title',
|
||||
'isbn_10': '1234567890',
|
||||
'isbn_13': 'blahhh',
|
||||
'blah': 'bip',
|
||||
'format': 'hardcover',
|
||||
'series': ['one', 'two'],
|
||||
}
|
||||
mappings = [
|
||||
Mapping('isbn_10'),
|
||||
Mapping('blah'),# not present on self.book
|
||||
Mapping('physical_format', remote_field='format'),
|
||||
Mapping('series', formatter=lambda x: x[0]),
|
||||
]
|
||||
book = update_from_mappings(self.book, data, mappings)
|
||||
self.assertEqual(book.title, 'Example Edition')
|
||||
self.assertEqual(book.isbn_10, '1234567890')
|
||||
self.assertEqual(book.isbn_13, None)
|
||||
self.assertEqual(book.physical_format, 'hardcover')
|
||||
self.assertEqual(book.series, 'one')
|
||||
|
||||
|
||||
def test_match_from_mappings(self):
|
||||
edition = models.Edition.objects.create(
|
||||
title='Blah',
|
||||
isbn_13='blahhh',
|
||||
)
|
||||
match = self.connector.match_from_mappings(self.data, models.Edition)
|
||||
self.assertEqual(match, edition)
|
||||
|
||||
|
||||
def test_match_from_mappings_with_model(self):
|
||||
edition = models.Edition.objects.create(
|
||||
title='Blah',
|
||||
isbn_10='1234567890',
|
||||
)
|
||||
match = self.connector.match_from_mappings(self.data, models.Edition)
|
||||
self.assertEqual(match, edition)
|
||||
|
||||
|
||||
def test_match_from_mappings_with_remote(self):
|
||||
edition = models.Edition.objects.create(
|
||||
title='Blah',
|
||||
asin='A00BLAH',
|
||||
)
|
||||
match = self.connector.match_from_mappings(self.data, models.Edition)
|
||||
self.assertEqual(match, edition)
|
||||
|
||||
|
||||
def test_match_from_mappings_no_match(self):
|
||||
edition = models.Edition.objects.create(
|
||||
title='Blah',
|
||||
)
|
||||
match = self.connector.match_from_mappings(self.data, models.Edition)
|
||||
self.assertEqual(match, None)
|
64
bookwyrm/tests/connectors/test_fedireads_connector.py
Normal file
64
bookwyrm/tests/connectors/test_fedireads_connector.py
Normal file
@ -0,0 +1,64 @@
|
||||
''' testing book data connectors '''
|
||||
from dateutil import parser
|
||||
from django.test import TestCase
|
||||
import json
|
||||
import pathlib
|
||||
|
||||
from fedireads import models
|
||||
from fedireads.connectors.fedireads_connector import Connector
|
||||
from fedireads.connectors.abstract_connector import SearchResult, get_date
|
||||
|
||||
|
||||
class FedireadsConnector(TestCase):
|
||||
def setUp(self):
|
||||
models.Connector.objects.create(
|
||||
identifier='example.com',
|
||||
connector_file='fedireads_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/fr_work.json')
|
||||
edition_file = pathlib.Path(__file__).parent.joinpath(
|
||||
'../data/fr_edition.json')
|
||||
self.work_data = json.loads(work_file.read_bytes())
|
||||
self.edition_data = json.loads(edition_file.read_bytes())
|
||||
|
||||
|
||||
def test_is_work_data(self):
|
||||
self.assertEqual(self.connector.is_work_data(self.work_data), True)
|
||||
self.assertEqual(self.connector.is_work_data(self.edition_data), False)
|
||||
|
||||
|
||||
def test_get_edition_from_work_data(self):
|
||||
edition = self.connector.get_edition_from_work_data(self.work_data)
|
||||
self.assertEqual(edition['url'], 'https://example.com/book/122')
|
||||
|
||||
|
||||
def test_get_work_from_edition_data(self):
|
||||
work = self.connector.get_work_from_edition_date(self.edition_data)
|
||||
self.assertEqual(work['url'], 'https://example.com/book/121')
|
||||
|
||||
|
||||
def test_format_search_result(self):
|
||||
datafile = pathlib.Path(__file__).parent.joinpath('../data/fr_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.year, 2017)
|
||||
|
||||
|
||||
def test_get_date(self):
|
||||
date = get_date(self.edition_data['published_date'])
|
||||
expected = parser.parse("2017-05-10T00:00:00+00:00")
|
||||
self.assertEqual(date, expected)
|
84
bookwyrm/tests/connectors/test_openlibrary_connector.py
Normal file
84
bookwyrm/tests/connectors/test_openlibrary_connector.py
Normal file
@ -0,0 +1,84 @@
|
||||
''' testing book data connectors '''
|
||||
from dateutil import parser
|
||||
from django.test import TestCase
|
||||
import json
|
||||
import pathlib
|
||||
import pytz
|
||||
|
||||
from fedireads import models
|
||||
from fedireads.connectors.openlibrary import Connector
|
||||
from fedireads.connectors.openlibrary import get_languages, get_description
|
||||
from fedireads.connectors.openlibrary import pick_default_edition, get_openlibrary_key
|
||||
from fedireads.connectors.abstract_connector import SearchResult, get_date
|
||||
|
||||
|
||||
class Openlibrary(TestCase):
|
||||
def setUp(self):
|
||||
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=',
|
||||
)
|
||||
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')
|
||||
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_is_work_data(self):
|
||||
self.assertEqual(self.connector.is_work_data(self.work_data), True)
|
||||
self.assertEqual(self.connector.is_work_data(self.edition_data), False)
|
||||
|
||||
|
||||
def test_pick_default_edition(self):
|
||||
edition = pick_default_edition(self.edition_list_data['entries'])
|
||||
self.assertEqual(edition['key'], '/books/OL9952943M')
|
||||
|
||||
|
||||
def test_format_search_result(self):
|
||||
''' 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.year, 2019)
|
||||
|
||||
|
||||
def test_get_description(self):
|
||||
description = get_description(self.work_data['description'])
|
||||
expected = 'First in the Old Kingdom/Abhorsen series.'
|
||||
self.assertEqual(description, expected)
|
||||
|
||||
|
||||
def test_get_date(self):
|
||||
date = get_date(self.work_data['first_publish_date'])
|
||||
expected = pytz.utc.localize(parser.parse('1995'))
|
||||
self.assertEqual(date, expected)
|
||||
|
||||
|
||||
def test_get_languages(self):
|
||||
languages = get_languages(self.edition_data['languages'])
|
||||
self.assertEqual(languages, ['English'])
|
||||
|
||||
|
||||
def test_get_ol_key(self):
|
||||
key = get_openlibrary_key('/books/OL27320736M')
|
||||
self.assertEqual(key, 'OL27320736M')
|
||||
|
75
bookwyrm/tests/connectors/test_self_connector.py
Normal file
75
bookwyrm/tests/connectors/test_self_connector.py
Normal file
@ -0,0 +1,75 @@
|
||||
''' testing book data connectors '''
|
||||
import datetime
|
||||
from django.test import TestCase
|
||||
|
||||
from fedireads import models
|
||||
from fedireads.connectors.self_connector import Connector
|
||||
from fedireads.settings import DOMAIN
|
||||
|
||||
|
||||
class SelfConnector(TestCase):
|
||||
def setUp(self):
|
||||
models.Connector.objects.create(
|
||||
identifier=DOMAIN,
|
||||
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,
|
||||
priority=1,
|
||||
)
|
||||
self.connector = Connector(DOMAIN)
|
||||
self.work = models.Work.objects.create(
|
||||
title='Example Work',
|
||||
)
|
||||
self.edition = models.Edition.objects.create(
|
||||
title='Edition of Example Work',
|
||||
author_text='Anonymous',
|
||||
published_date=datetime.datetime(1980, 5, 10),
|
||||
parent_work=self.work,
|
||||
)
|
||||
models.Edition.objects.create(
|
||||
title='Another Edition',
|
||||
parent_work=self.work,
|
||||
series='Anonymous'
|
||||
)
|
||||
models.Edition.objects.create(
|
||||
title='More Editions',
|
||||
subtitle='The Anonymous Edition',
|
||||
parent_work=self.work,
|
||||
)
|
||||
models.Edition.objects.create(
|
||||
title='An Edition',
|
||||
author_text='Fish',
|
||||
parent_work=self.work
|
||||
)
|
||||
|
||||
|
||||
def test_format_search_result(self):
|
||||
result = self.connector.format_search_result(self.edition)
|
||||
self.assertEqual(result.title, 'Edition of Example Work')
|
||||
self.assertEqual(result.key, self.edition.remote_id)
|
||||
self.assertEqual(result.author, 'Anonymous')
|
||||
self.assertEqual(result.year, 1980)
|
||||
|
||||
|
||||
def test_search_rank(self):
|
||||
results = self.connector.search('Anonymous')
|
||||
self.assertEqual(len(results), 3)
|
||||
self.assertEqual(results[0].title, 'Edition of Example Work')
|
||||
self.assertEqual(results[1].title, 'More Editions')
|
||||
self.assertEqual(results[2].title, 'Another Edition')
|
||||
|
||||
|
||||
def test_search_default_filter(self):
|
||||
self.edition.default = True
|
||||
self.edition.save()
|
||||
results = self.connector.search('Anonymous')
|
||||
self.assertEqual(len(results), 1)
|
||||
self.assertEqual(results[0].title, 'Edition of Example Work')
|
||||
|
||||
results = self.connector.search('Fish')
|
||||
self.assertEqual(len(results), 1)
|
||||
self.assertEqual(results[0].title, 'An Edition')
|
Reference in New Issue
Block a user