Adds more tests for books_manager

from 47% to 77%
This commit is contained in:
Mouse Reeve 2020-09-21 11:14:56 -07:00
parent 585c66b64f
commit 80cb1c3de2
2 changed files with 48 additions and 7 deletions

View file

@ -1,8 +1,8 @@
from django.test import TestCase from django.test import TestCase
from bookwyrm import books_manager, models from bookwyrm import books_manager, models
from bookwyrm.connectors.bookwyrm_connector import Connector from bookwyrm.connectors.bookwyrm_connector import Connector as BookWyrmConnector
from bookwyrm.settings import DOMAIN from bookwyrm.connectors.self_connector import Connector as SelfConnector
class Book(TestCase): class Book(TestCase):
@ -16,6 +16,16 @@ class Book(TestCase):
parent_work=self.work parent_work=self.work
) )
self.connector = models.Connector.objects.create(
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/',
)
def test_get_edition(self): def test_get_edition(self):
edition = books_manager.get_edition(self.edition.id) edition = books_manager.get_edition(self.edition.id)
self.assertEqual(edition, self.edition) self.assertEqual(edition, self.edition)
@ -29,9 +39,40 @@ class Book(TestCase):
def test_get_or_create_connector(self): def test_get_or_create_connector(self):
remote_id = 'https://example.com/object/1' remote_id = 'https://example.com/object/1'
connector = books_manager.get_or_create_connector(remote_id) connector = books_manager.get_or_create_connector(remote_id)
self.assertIsInstance(connector, Connector) self.assertIsInstance(connector, BookWyrmConnector)
self.assertEqual(connector.identifier, 'example.com') self.assertEqual(connector.identifier, 'example.com')
self.assertEqual(connector.base_url, 'https://example.com') self.assertEqual(connector.base_url, 'https://example.com')
same_connector = books_manager.get_or_create_connector(remote_id) same_connector = books_manager.get_or_create_connector(remote_id)
self.assertEqual(connector.identifier, same_connector.identifier) self.assertEqual(connector.identifier, same_connector.identifier)
def test_get_connectors(self):
remote_id = 'https://example.com/object/1'
books_manager.get_or_create_connector(remote_id)
connectors = list(books_manager.get_connectors())
self.assertEqual(len(connectors), 2)
self.assertIsInstance(connectors[0], SelfConnector)
self.assertIsInstance(connectors[1], BookWyrmConnector)
def test_search(self):
results = books_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')
def test_local_search(self):
results = books_manager.local_search('Example')
self.assertEqual(len(results), 1)
self.assertEqual(results[0].title, 'Example Edition')
def test_first_search_result(self):
result = books_manager.first_search_result('Example')
self.assertEqual(result.title, 'Example Edition')
no_result = books_manager.first_search_result('dkjfhg')
self.assertIsNone(no_result)
def test_load_connector(self):
connector = books_manager.load_connector(self.connector)
self.assertIsInstance(connector, SelfConnector)
self.assertEqual(connector.identifier, 'test_connector')

View file

@ -90,15 +90,15 @@ BOOKWYRM_DATABASE_BACKEND = env('BOOKWYRM_DATABASE_BACKEND', 'postgres')
BOOKWYRM_DBS = { BOOKWYRM_DBS = {
'postgres': { 'postgres': {
'ENGINE': 'django.db.backends.postgresql_psycopg2', 'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': env('POSTGRES_DB', 'bookwyrm'), 'NAME': env('POSTGRES_DB', 'fedireads'),
'USER': env('POSTGRES_USER', 'bookwyrm'), 'USER': env('POSTGRES_USER', 'fedireads'),
'PASSWORD': env('POSTGRES_PASSWORD', 'bookwyrm'), 'PASSWORD': env('POSTGRES_PASSWORD', 'fedireads'),
'HOST': env('POSTGRES_HOST', ''), 'HOST': env('POSTGRES_HOST', ''),
'PORT': 5432 'PORT': 5432
}, },
'sqlite': { 'sqlite': {
'ENGINE': 'django.db.backends.sqlite3', 'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'bookwyrm.db') 'NAME': os.path.join(BASE_DIR, 'fedireads.db')
} }
} }