Refactors test mocks
This commit is contained in:
@ -74,12 +74,11 @@ class AbstractConnector(TestCase):
|
||||
Mapping("openlibraryKey"),
|
||||
]
|
||||
|
||||
with patch("bookwyrm.preview_images.generate_edition_preview_image_task.delay"):
|
||||
self.book = models.Edition.objects.create(
|
||||
title="Test Book",
|
||||
remote_id="https://example.com/book/1234",
|
||||
openlibrary_key="OL1234M",
|
||||
)
|
||||
self.book = models.Edition.objects.create(
|
||||
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"""
|
||||
@ -111,11 +110,10 @@ class AbstractConnector(TestCase):
|
||||
responses.add(
|
||||
responses.GET, "https://example.com/book/abcd", json=self.edition_data
|
||||
)
|
||||
with patch("bookwyrm.preview_images.generate_edition_preview_image_task.delay"):
|
||||
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)
|
||||
|
@ -1,5 +1,4 @@
|
||||
""" testing book data connectors """
|
||||
from unittest.mock import patch
|
||||
import json
|
||||
import pathlib
|
||||
from django.test import TestCase
|
||||
@ -26,9 +25,8 @@ class BookWyrmConnector(TestCase):
|
||||
|
||||
def test_get_or_create_book_existing(self):
|
||||
"""load book activity"""
|
||||
with patch("bookwyrm.preview_images.generate_edition_preview_image_task.delay"):
|
||||
work = models.Work.objects.create(title="Test Work")
|
||||
book = models.Edition.objects.create(title="Test Edition", parent_work=work)
|
||||
work = models.Work.objects.create(title="Test Work")
|
||||
book = models.Edition.objects.create(title="Test Edition", parent_work=work)
|
||||
result = self.connector.get_or_create_book(book.remote_id)
|
||||
self.assertEqual(book, result)
|
||||
|
||||
|
@ -1,5 +1,4 @@
|
||||
""" interface between the app and various connectors """
|
||||
from unittest.mock import patch
|
||||
from django.test import TestCase
|
||||
import responses
|
||||
|
||||
@ -14,15 +13,14 @@ class ConnectorManager(TestCase):
|
||||
|
||||
def setUp(self):
|
||||
"""we'll need some books and a connector info entry"""
|
||||
with patch("bookwyrm.preview_images.generate_edition_preview_image_task.delay"):
|
||||
self.work = models.Work.objects.create(title="Example Work")
|
||||
self.work = models.Work.objects.create(title="Example Work")
|
||||
|
||||
self.edition = models.Edition.objects.create(
|
||||
title="Example Edition", parent_work=self.work, isbn_10="0000000000"
|
||||
)
|
||||
self.edition = models.Edition.objects.create(
|
||||
title="Another Edition", parent_work=self.work, isbn_10="1111111111"
|
||||
)
|
||||
self.edition = models.Edition.objects.create(
|
||||
title="Example Edition", parent_work=self.work, isbn_10="0000000000"
|
||||
)
|
||||
self.edition = models.Edition.objects.create(
|
||||
title="Another Edition", parent_work=self.work, isbn_10="1111111111"
|
||||
)
|
||||
|
||||
self.connector = models.Connector.objects.create(
|
||||
identifier="test_connector",
|
||||
|
@ -178,26 +178,24 @@ class Openlibrary(TestCase):
|
||||
@responses.activate
|
||||
def test_expand_book_data(self):
|
||||
"""given a book, get more editions"""
|
||||
with patch("bookwyrm.preview_images.generate_edition_preview_image_task.delay"):
|
||||
work = models.Work.objects.create(
|
||||
title="Test Work", openlibrary_key="OL1234W"
|
||||
)
|
||||
edition = models.Edition.objects.create(
|
||||
title="Test Edition", parent_work=work
|
||||
)
|
||||
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": []},
|
||||
)
|
||||
with patch("bookwyrm.preview_images.generate_user_preview_image_task.delay"):
|
||||
with patch(
|
||||
"bookwyrm.connectors.abstract_connector.AbstractConnector."
|
||||
"create_edition_from_data"
|
||||
):
|
||||
self.connector.expand_book_data(edition)
|
||||
self.connector.expand_book_data(work)
|
||||
with patch(
|
||||
"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"""
|
||||
@ -230,14 +228,13 @@ class Openlibrary(TestCase):
|
||||
json={"hi": "there"},
|
||||
status=200,
|
||||
)
|
||||
with patch("bookwyrm.preview_images.generate_edition_preview_image_task.delay"):
|
||||
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
|
||||
)
|
||||
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
|
||||
)
|
||||
self.assertEqual(result.parent_work, work)
|
||||
self.assertEqual(result.title, "Sabriel")
|
||||
self.assertEqual(result.isbn_10, "0060273224")
|
||||
|
@ -1,5 +1,4 @@
|
||||
""" testing book data connectors """
|
||||
from unittest.mock import patch
|
||||
import datetime
|
||||
from django.test import TestCase
|
||||
from django.utils import timezone
|
||||
@ -30,21 +29,19 @@ class SelfConnector(TestCase):
|
||||
def test_format_search_result(self):
|
||||
"""create a SearchResult"""
|
||||
author = models.Author.objects.create(name="Anonymous")
|
||||
with patch("bookwyrm.preview_images.generate_edition_preview_image_task.delay"):
|
||||
edition = models.Edition.objects.create(
|
||||
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]
|
||||
edition = models.Edition.objects.create(
|
||||
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")
|
||||
self.assertEqual(result.key, edition.remote_id)
|
||||
self.assertEqual(result.author, "Anonymous")
|
||||
self.assertEqual(result.year, 1980)
|
||||
self.assertEqual(result.connector, self.connector)
|
||||
|
||||
@patch("bookwyrm.preview_images.generate_edition_preview_image_task.delay")
|
||||
def test_search_rank(self, _):
|
||||
def test_search_rank(self):
|
||||
"""prioritize certain results"""
|
||||
author = models.Author.objects.create(name="Anonymous")
|
||||
edition = models.Edition.objects.create(
|
||||
@ -81,8 +78,7 @@ class SelfConnector(TestCase):
|
||||
self.assertEqual(results[2].title, "Edition of Example Work")
|
||||
self.assertEqual(results[3].title, "Another Edition")
|
||||
|
||||
@patch("bookwyrm.preview_images.generate_edition_preview_image_task.delay")
|
||||
def test_search_multiple_editions(self, _):
|
||||
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")
|
||||
edition_1 = models.Edition.objects.create(
|
||||
|
Reference in New Issue
Block a user