Merge pull request #168 from mouse-reeve/books-manager-tests

Tests for books_manager
This commit is contained in:
Mouse Reeve 2020-05-12 18:58:48 -07:00 committed by GitHub
commit 15cce156a0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 69 additions and 3 deletions

View file

@ -121,7 +121,6 @@ def local_search(query):
return connector.search(query)
def first_search_result(query):
''' search until you find a result that fits '''
for connector in get_connectors():
@ -139,8 +138,8 @@ def update_book(book, data=None):
def get_connectors():
''' load all connectors '''
connectors_info = models.Connector.objects.order_by('priority').all()
return [load_connector(c) for c in connectors_info]
for info in models.Connector.objects.order_by('priority').all():
yield load_connector(info)
def load_connector(connector_info):

View file

@ -0,0 +1,67 @@
from django.test import TestCase
from fedireads import books_manager, models
from fedireads.connectors.fedireads_connector import Connector
from fedireads.settings import DOMAIN
class Book(TestCase):
def setUp(self):
self.work = models.Work.objects.create(
title='Example Work'
)
self.edition = models.Edition.objects.create(
title='Example Edition',
parent_work=self.work
)
def test_get_edition(self):
edition = books_manager.get_edition(self.edition.id)
self.assertEqual(edition, self.edition)
def test_get_edition_work(self):
edition = books_manager.get_edition(self.work.id)
self.assertEqual(edition, self.edition)
def test_get_or_create_connector(self):
remote_id = 'https://example.com/object/1'
connector = books_manager.get_or_create_connector(remote_id)
self.assertIsInstance(connector, Connector)
self.assertEqual(connector.identifier, 'example.com')
self.assertEqual(connector.base_url, 'https://example.com')
same_connector = books_manager.get_or_create_connector(remote_id)
self.assertEqual(connector.identifier, same_connector.identifier)
def test_get_by_absolute_id_local(self):
abs_id = 'https://%s/book/%d' % (DOMAIN, self.work.id)
work = books_manager.get_by_absolute_id(abs_id, models.Work)
self.assertEqual(work, self.work)
work = books_manager.get_by_absolute_id(abs_id, models.Edition)
self.assertIsNone(work)
def test_get_by_absolute_id_remote(self):
remote_work = models.Work.objects.create(
title='Example Work',
remote_id='https://example.com/book/123',
)
abs_id = 'https://example.com/book/123'
work = books_manager.get_by_absolute_id(abs_id, models.Work)
self.assertEqual(work, remote_work)
def test_get_by_absolute_id_invalid(self):
abs_id = 'https://%s/book/34534623' % DOMAIN
result = books_manager.get_by_absolute_id(abs_id, models.Work)
self.assertIsNone(result)
abs_id = 'httook534623'
result = books_manager.get_by_absolute_id(abs_id, models.Work)
self.assertIsNone(result)