From f8df5012391a8ae1dec8c6de2f8c9e3d7f2b7937 Mon Sep 17 00:00:00 2001 From: Joachim Date: Thu, 27 May 2021 23:19:17 +0200 Subject: [PATCH] Fix tests (for realz) --- .../tests/activitypub/test_base_activity.py | 9 ++-- .../connectors/test_abstract_connector.py | 5 +- .../connectors/test_openlibrary_connector.py | 11 +++-- .../tests/connectors/test_self_connector.py | 30 ++++++------ bookwyrm/tests/models/test_fields.py | 29 +++++------ bookwyrm/tests/models/test_import_model.py | 3 +- bookwyrm/tests/models/test_status_model.py | 2 +- .../tests/views/inbox/test_inbox_create.py | 7 +-- .../tests/views/inbox/test_inbox_update.py | 48 ++++++++++--------- bookwyrm/tests/views/test_book.py | 21 ++++---- bookwyrm/tests/views/test_get_started.py | 11 +++-- bookwyrm/tests/views/test_helpers.py | 9 ++-- bookwyrm/tests/views/test_user.py | 22 +++++---- 13 files changed, 112 insertions(+), 95 deletions(-) diff --git a/bookwyrm/tests/activitypub/test_base_activity.py b/bookwyrm/tests/activitypub/test_base_activity.py index 54fbb49e..9a88b1c6 100644 --- a/bookwyrm/tests/activitypub/test_base_activity.py +++ b/bookwyrm/tests/activitypub/test_base_activity.py @@ -98,10 +98,11 @@ class BaseActivity(TestCase): status=200, ) - with patch("bookwyrm.models.user.set_remote_server.delay"): - result = resolve_remote_id( - "https://example.com/user/mouse", model=models.User - ) + with patch("bookwyrm.preview_images.generate_user_preview_image_task.delay"): + with patch("bookwyrm.models.user.set_remote_server.delay"): + result = resolve_remote_id( + "https://example.com/user/mouse", model=models.User + ) self.assertIsInstance(result, models.User) self.assertEqual(result.remote_id, "https://example.com/user/mouse") self.assertEqual(result.name, "MOUSE?? MOUSE!!") diff --git a/bookwyrm/tests/connectors/test_abstract_connector.py b/bookwyrm/tests/connectors/test_abstract_connector.py index 9ca3c8f2..68a4c7be 100644 --- a/bookwyrm/tests/connectors/test_abstract_connector.py +++ b/bookwyrm/tests/connectors/test_abstract_connector.py @@ -111,8 +111,9 @@ class AbstractConnector(TestCase): responses.add( 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.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") self.assertEqual(result, self.book) self.assertEqual(models.Edition.objects.count(), 1) self.assertEqual(models.Edition.objects.count(), 1) diff --git a/bookwyrm/tests/connectors/test_openlibrary_connector.py b/bookwyrm/tests/connectors/test_openlibrary_connector.py index 8c00061a..b34a68d2 100644 --- a/bookwyrm/tests/connectors/test_openlibrary_connector.py +++ b/bookwyrm/tests/connectors/test_openlibrary_connector.py @@ -230,11 +230,12 @@ class Openlibrary(TestCase): 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) + 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) self.assertEqual(result.parent_work, work) self.assertEqual(result.title, "Sabriel") self.assertEqual(result.isbn_10, "0060273224") diff --git a/bookwyrm/tests/connectors/test_self_connector.py b/bookwyrm/tests/connectors/test_self_connector.py index e6e64a7d..4f217308 100644 --- a/bookwyrm/tests/connectors/test_self_connector.py +++ b/bookwyrm/tests/connectors/test_self_connector.py @@ -36,7 +36,7 @@ class SelfConnector(TestCase): published_date=datetime.datetime(1980, 5, 10, tzinfo=timezone.utc), ) edition.authors.add(author) - result = self.connector.search("Edition of Example")[0] + 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") @@ -46,7 +46,7 @@ class SelfConnector(TestCase): def test_search_rank(self): """prioritize certain results""" author = models.Author.objects.create(name="Anonymous") - with patch("bookwyrm.preview_images.generate_user_preview_image_task.delay"): + 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), @@ -74,7 +74,7 @@ class SelfConnector(TestCase): 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") @@ -94,17 +94,17 @@ class SelfConnector(TestCase): ) edition_3 = models.Edition.objects.create(title="Fish", parent_work=work) - # pick the best edition - results = self.connector.search("Edition 1 Title") - self.assertEqual(len(results), 1) - self.assertEqual(results[0].key, edition_1.remote_id) + # pick the best edition + 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") - self.assertEqual(len(results), 1) - self.assertEqual(results[0].key, edition_2.remote_id) + # pick the default edition when no match is best + 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") - self.assertEqual(len(results), 1) - self.assertEqual(results[0].key, edition_3.remote_id) + # only matches one edition, so no deduplication takes place + results = self.connector.search("Fish") + self.assertEqual(len(results), 1) + self.assertEqual(results[0].key, edition_3.remote_id) diff --git a/bookwyrm/tests/models/test_fields.py b/bookwyrm/tests/models/test_fields.py index e8c48b0b..2fb33751 100644 --- a/bookwyrm/tests/models/test_fields.py +++ b/bookwyrm/tests/models/test_fields.py @@ -254,12 +254,12 @@ class ActivitypubFields(TestCase): "rat", "rat@rat.rat", "ratword", local=True, localname="rat" ) - # test receiving an unknown remote id and loading data - responses.add( - responses.GET, "https://example.com/user/mouse", json=userdata, status=200 - ) - with patch("bookwyrm.models.user.set_remote_server.delay"): - value = instance.field_from_activity("https://example.com/user/mouse") + # test receiving an unknown remote id and loading data + responses.add( + responses.GET, "https://example.com/user/mouse", json=userdata, status=200 + ) + with patch("bookwyrm.models.user.set_remote_server.delay"): + value = instance.field_from_activity("https://example.com/user/mouse") self.assertIsInstance(value, User) self.assertNotEqual(value, unrelated_user) self.assertEqual(value.remote_id, "https://example.com/user/mouse") @@ -278,8 +278,8 @@ class ActivitypubFields(TestCase): unrelated_user = User.objects.create_user( "rat", "rat@rat.rat", "ratword", local=True, localname="rat" ) - with patch("bookwyrm.models.user.set_remote_server.delay"): - value = instance.field_from_activity(activitypub.Person(**userdata)) + with patch("bookwyrm.models.user.set_remote_server.delay"): + value = instance.field_from_activity(activitypub.Person(**userdata)) self.assertIsInstance(value, User) self.assertNotEqual(value, unrelated_user) self.assertEqual(value.remote_id, "https://example.com/user/mouse") @@ -302,8 +302,8 @@ class ActivitypubFields(TestCase): "rat", "rat@rat.rat", "ratword", local=True, localname="rat" ) - with patch("bookwyrm.models.activitypub_mixin.ObjectMixin.broadcast"): - value = instance.field_from_activity(activitypub.Person(**userdata)) + with patch("bookwyrm.models.activitypub_mixin.ObjectMixin.broadcast"): + value = instance.field_from_activity(activitypub.Person(**userdata)) self.assertEqual(value, user) def test_foreign_key_from_activity_str_existing(self): @@ -357,10 +357,11 @@ class ActivitypubFields(TestCase): responses.add( responses.GET, "https://example.com/user/mouse", json=userdata, status=200 ) - with patch("bookwyrm.models.user.set_remote_server.delay"): - value = instance.field_from_activity( - ["https://example.com/user/mouse", "bleh"] - ) + with patch("bookwyrm.preview_images.generate_user_preview_image_task.delay"): + with patch("bookwyrm.models.user.set_remote_server.delay"): + value = instance.field_from_activity( + ["https://example.com/user/mouse", "bleh"] + ) self.assertIsInstance(value, list) self.assertEqual(len(value), 1) self.assertIsInstance(value[0], User) diff --git a/bookwyrm/tests/models/test_import_model.py b/bookwyrm/tests/models/test_import_model.py index 02263e0e..15d8766c 100644 --- a/bookwyrm/tests/models/test_import_model.py +++ b/bookwyrm/tests/models/test_import_model.py @@ -175,6 +175,7 @@ class ImportJob(TestCase): with patch( "bookwyrm.connectors.openlibrary.Connector." "get_authors_from_data" ): - book = self.item_1.get_book_from_isbn() + with patch("bookwyrm.preview_images.generate_edition_preview_image_task.delay"): + book = self.item_1.get_book_from_isbn() self.assertEqual(book.title, "Sabriel") diff --git a/bookwyrm/tests/models/test_status_model.py b/bookwyrm/tests/models/test_status_model.py index db4475c7..a14c515e 100644 --- a/bookwyrm/tests/models/test_status_model.py +++ b/bookwyrm/tests/models/test_status_model.py @@ -60,7 +60,7 @@ class Status(TestCase): child = models.Status.objects.create( content="hello", reply_parent=parent, user=self.local_user ) - with patch("bookwyrm.preview_images.generate_edition_image_task.delay"): + with patch("bookwyrm.preview_images.generate_edition_preview_image_task.delay"): models.Review.objects.create( content="hey", reply_parent=parent, user=self.local_user, book=self.book ) diff --git a/bookwyrm/tests/views/inbox/test_inbox_create.py b/bookwyrm/tests/views/inbox/test_inbox_create.py index a885239b..046291fb 100644 --- a/bookwyrm/tests/views/inbox/test_inbox_create.py +++ b/bookwyrm/tests/views/inbox/test_inbox_create.py @@ -162,9 +162,10 @@ class InboxCreate(TestCase): "rating": 3, "@context": "https://www.w3.org/ns/activitystreams", } - with patch("bookwyrm.activitystreams.ActivityStream.add_status") as redis_mock: - views.inbox.activity_task(activity) - self.assertTrue(redis_mock.called) + with patch("bookwyrm.preview_images.generate_edition_preview_image_task.delay"): + with patch("bookwyrm.activitystreams.ActivityStream.add_status") as redis_mock: + views.inbox.activity_task(activity) + self.assertTrue(redis_mock.called) rating = models.ReviewRating.objects.first() self.assertEqual(rating.book, book) self.assertEqual(rating.rating, 3.0) diff --git a/bookwyrm/tests/views/inbox/test_inbox_update.py b/bookwyrm/tests/views/inbox/test_inbox_update.py index 39ac4a89..32cac471 100644 --- a/bookwyrm/tests/views/inbox/test_inbox_update.py +++ b/bookwyrm/tests/views/inbox/test_inbox_update.py @@ -98,16 +98,18 @@ class InboxUpdate(TestCase): del userdata["icon"] self.assertIsNone(self.remote_user.name) self.assertFalse(self.remote_user.discoverable) - views.inbox.activity_task( - { - "type": "Update", - "to": [], - "cc": [], - "actor": "hi", - "id": "sdkjf", - "object": userdata, - } - ) + + with patch("bookwyrm.preview_images.generate_user_preview_image_task.delay"): + views.inbox.activity_task( + { + "type": "Update", + "to": [], + "cc": [], + "actor": "hi", + "id": "sdkjf", + "object": userdata, + } + ) user = models.User.objects.get(id=self.remote_user.id) self.assertEqual(user.name, "RAT???") self.assertEqual(user.username, "rat@example.com") @@ -130,20 +132,20 @@ class InboxUpdate(TestCase): title="Test Book", remote_id="https://bookwyrm.social/book/5989" ) - del bookdata["authors"] - self.assertEqual(book.title, "Test Book") + del bookdata["authors"] + self.assertEqual(book.title, "Test Book") - with patch("bookwyrm.activitypub.base_activity.set_related_field.delay"): - views.inbox.activity_task( - { - "type": "Update", - "to": [], - "cc": [], - "actor": "hi", - "id": "sdkjf", - "object": bookdata, - } - ) + with patch("bookwyrm.activitypub.base_activity.set_related_field.delay"): + views.inbox.activity_task( + { + "type": "Update", + "to": [], + "cc": [], + "actor": "hi", + "id": "sdkjf", + "object": bookdata, + } + ) book = models.Edition.objects.get(id=book.id) self.assertEqual(book.title, "Piranesi") self.assertEqual(book.last_edited_by, self.remote_user) diff --git a/bookwyrm/tests/views/test_book.py b/bookwyrm/tests/views/test_book.py index 37bf3e9e..375cdb03 100644 --- a/bookwyrm/tests/views/test_book.py +++ b/bookwyrm/tests/views/test_book.py @@ -74,7 +74,8 @@ class BookViews(TestCase): request = self.factory.get("") request.user = self.local_user request.user.is_superuser = True - result = view(request, self.book.id) + with patch("bookwyrm.preview_images.generate_edition_preview_image_task.delay"): + result = view(request, self.book.id) self.assertIsInstance(result, TemplateResponse) result.render() self.assertEqual(result.status_code, 200) @@ -89,7 +90,7 @@ class BookViews(TestCase): request = self.factory.post("", form.data) request.user = self.local_user - with patch("bookwyrm.preview_images.generate_user_preview_image_task.delay"): + with patch("bookwyrm.preview_images.generate_edition_preview_image_task.delay"): with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"): view(request, self.book.id) @@ -127,7 +128,7 @@ class BookViews(TestCase): request = self.factory.post("", form.data) request.user = self.local_user - with patch("bookwyrm.preview_images.generate_user_preview_image_task.delay"): + with patch("bookwyrm.preview_images.generate_edition_preview_image_task.delay"): with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"): view(request, self.book.id) @@ -149,8 +150,9 @@ class BookViews(TestCase): request = self.factory.post("", form.data) request.user = self.local_user - with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"): - view(request, self.book.id) + with patch("bookwyrm.preview_images.generate_edition_preview_image_task.delay"): + with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"): + view(request, self.book.id) self.book.refresh_from_db() self.assertEqual(self.book.title, "New Title") self.assertFalse(self.book.authors.exists()) @@ -165,7 +167,8 @@ class BookViews(TestCase): request = self.factory.post("", form.data) request.user = self.local_user - view(request) + with patch("bookwyrm.preview_images.generate_edition_preview_image_task.delay"): + view(request) book = models.Edition.objects.get(title="New Title") self.assertEqual(book.parent_work.title, "New Title") @@ -180,7 +183,8 @@ class BookViews(TestCase): request = self.factory.post("", form.data) request.user = self.local_user - view(request) + with patch("bookwyrm.preview_images.generate_edition_preview_image_task.delay"): + view(request) book = models.Edition.objects.get(title="New Title") self.assertEqual(book.parent_work, self.work) @@ -196,7 +200,8 @@ class BookViews(TestCase): request = self.factory.post("", form.data) request.user = self.local_user - view(request) + with patch("bookwyrm.preview_images.generate_edition_preview_image_task.delay"): + view(request) book = models.Edition.objects.get(title="New Title") self.assertEqual(book.parent_work.title, "New Title") self.assertEqual(book.authors.first().name, "Sappho") diff --git a/bookwyrm/tests/views/test_get_started.py b/bookwyrm/tests/views/test_get_started.py index 2eb89f10..7a3b0c44 100644 --- a/bookwyrm/tests/views/test_get_started.py +++ b/bookwyrm/tests/views/test_get_started.py @@ -55,11 +55,12 @@ class GetStartedViews(TestCase): request.user = self.local_user self.assertIsNone(self.local_user.name) - with patch( - "bookwyrm.models.activitypub_mixin.broadcast_task.delay" - ) as delay_mock: - view(request) - self.assertEqual(delay_mock.call_count, 1) + with patch("bookwyrm.preview_images.generate_user_preview_image_task.delay"): + with patch( + "bookwyrm.models.activitypub_mixin.broadcast_task.delay" + ) as delay_mock: + view(request) + self.assertEqual(delay_mock.call_count, 1) self.assertEqual(self.local_user.name, "New Name") self.assertTrue(self.local_user.discoverable) diff --git a/bookwyrm/tests/views/test_helpers.py b/bookwyrm/tests/views/test_helpers.py index f0807c55..e8d1895c 100644 --- a/bookwyrm/tests/views/test_helpers.py +++ b/bookwyrm/tests/views/test_helpers.py @@ -144,10 +144,11 @@ class ViewsHelpers(TestCase): json=self.userdata, status=200, ) - with patch("bookwyrm.models.user.set_remote_server.delay"): - result = views.helpers.handle_remote_webfinger("@mouse@example.com") - self.assertIsInstance(result, models.User) - self.assertEqual(result.username, "mouse@example.com") + with patch("bookwyrm.preview_images.generate_user_preview_image_task.delay"): + with patch("bookwyrm.models.user.set_remote_server.delay"): + result = views.helpers.handle_remote_webfinger("@mouse@example.com") + self.assertIsInstance(result, models.User) + self.assertEqual(result.username, "mouse@example.com") def test_user_on_blocked_server(self, _): """find a remote user using webfinger""" diff --git a/bookwyrm/tests/views/test_user.py b/bookwyrm/tests/views/test_user.py index 1badf142..27607f79 100644 --- a/bookwyrm/tests/views/test_user.py +++ b/bookwyrm/tests/views/test_user.py @@ -162,11 +162,12 @@ class UserViews(TestCase): request.user = self.local_user self.assertIsNone(self.local_user.name) - with patch( - "bookwyrm.models.activitypub_mixin.broadcast_task.delay" - ) as delay_mock: - view(request) - self.assertEqual(delay_mock.call_count, 1) + with patch("bookwyrm.preview_images.generate_user_preview_image_task.delay"): + with patch( + "bookwyrm.models.activitypub_mixin.broadcast_task.delay" + ) as delay_mock: + view(request) + self.assertEqual(delay_mock.call_count, 1) self.assertEqual(self.local_user.name, "New Name") self.assertEqual(self.local_user.email, "wow@email.com") @@ -186,11 +187,12 @@ class UserViews(TestCase): request = self.factory.post("", form.data) request.user = self.local_user - with patch( - "bookwyrm.models.activitypub_mixin.broadcast_task.delay" - ) as delay_mock: - view(request) - self.assertEqual(delay_mock.call_count, 1) + with patch("bookwyrm.preview_images.generate_user_preview_image_task.delay"): + with patch( + "bookwyrm.models.activitypub_mixin.broadcast_task.delay" + ) as delay_mock: + view(request) + self.assertEqual(delay_mock.call_count, 1) self.assertEqual(self.local_user.name, "New Name") self.assertEqual(self.local_user.email, "wow@email.com") self.assertIsNotNone(self.local_user.avatar)