From 4b9fe0af0c811c90415d099fa2e6973fe25e9152 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adeodato=20Sim=C3=B3?= Date: Sun, 17 Mar 2024 20:36:48 -0300 Subject: [PATCH] Remove nesting in several with..patch calls --- .../tests/activitypub/test_base_activity.py | 10 ++-- bookwyrm/tests/importers/test_importer.py | 8 ++-- .../tests/models/test_bookwyrm_import_job.py | 46 +++++++++---------- bookwyrm/tests/models/test_import_model.py | 18 ++++---- bookwyrm/tests/test_signing.py | 8 ++-- .../tests/views/inbox/test_inbox_announce.py | 16 ++++--- bookwyrm/tests/views/inbox/test_inbox_like.py | 16 ++++--- bookwyrm/tests/views/test_helpers.py | 46 ++++++++++--------- bookwyrm/tests/views/test_updates.py | 26 ++++------- 9 files changed, 100 insertions(+), 94 deletions(-) diff --git a/bookwyrm/tests/activitypub/test_base_activity.py b/bookwyrm/tests/activitypub/test_base_activity.py index 8f2cd7fb2..735353452 100644 --- a/bookwyrm/tests/activitypub/test_base_activity.py +++ b/bookwyrm/tests/activitypub/test_base_activity.py @@ -234,10 +234,12 @@ class BaseActivity(TestCase): ) # sets the celery task call to the function call - with patch("bookwyrm.activitypub.base_activity.set_related_field.delay"): - with patch("bookwyrm.models.status.Status.ignore_activity") as discarder: - discarder.return_value = False - update_data.to_model(model=models.Status, instance=status) + with ( + patch("bookwyrm.activitypub.base_activity.set_related_field.delay"), + patch("bookwyrm.models.status.Status.ignore_activity") as discarder, + ): + discarder.return_value = False + update_data.to_model(model=models.Status, instance=status) self.assertIsNone(status.attachments.first()) @responses.activate diff --git a/bookwyrm/tests/importers/test_importer.py b/bookwyrm/tests/importers/test_importer.py index 494d28ad0..a18aedd51 100644 --- a/bookwyrm/tests/importers/test_importer.py +++ b/bookwyrm/tests/importers/test_importer.py @@ -268,9 +268,11 @@ class GenericImporter(TestCase): import_item.book = self.book import_item.save() - with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"): - with patch("bookwyrm.models.Status.broadcast") as broadcast_mock: - handle_imported_book(import_item) + with ( + patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"), + patch("bookwyrm.models.Status.broadcast") as broadcast_mock, + ): + handle_imported_book(import_item) kwargs = broadcast_mock.call_args.kwargs self.assertEqual(kwargs["software"], "bookwyrm") review = models.Review.objects.get(book=self.book, user=self.local_user) diff --git a/bookwyrm/tests/models/test_bookwyrm_import_job.py b/bookwyrm/tests/models/test_bookwyrm_import_job.py index 50c136f21..259d8f5a4 100644 --- a/bookwyrm/tests/models/test_bookwyrm_import_job.py +++ b/bookwyrm/tests/models/test_bookwyrm_import_job.py @@ -24,7 +24,6 @@ class BookwyrmImport(TestCase): # pylint: disable=too-many-public-methods patch("bookwyrm.lists_stream.populate_lists_task.delay"), patch("bookwyrm.suggested_users.rerank_user_task.delay"), ): - self.local_user = models.User.objects.create_user( "mouse", "mouse@mouse.mouse", @@ -84,13 +83,13 @@ class BookwyrmImport(TestCase): # pylint: disable=too-many-public-methods patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"), patch("bookwyrm.suggested_users.rerank_user_task.delay"), ): - - with open(self.archive_file, "rb") as fileobj: - with BookwyrmTarFile.open(mode="r:gz", fileobj=fileobj) as tarfile: - - models.bookwyrm_import_job.update_user_profile( - self.local_user, tarfile, self.json_data - ) + with ( + open(self.archive_file, "rb") as fileobj, + BookwyrmTarFile.open(mode="r:gz", fileobj=fileobj) as tarfile, + ): + models.bookwyrm_import_job.update_user_profile( + self.local_user, tarfile, self.json_data + ) self.local_user.refresh_from_db() @@ -111,7 +110,6 @@ class BookwyrmImport(TestCase): # pylint: disable=too-many-public-methods patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"), patch("bookwyrm.suggested_users.rerank_user_task.delay"), ): - models.bookwyrm_import_job.update_user_settings( self.local_user, self.json_data ) @@ -256,14 +254,15 @@ class BookwyrmImport(TestCase): # pylint: disable=too-many-public-methods self.assertEqual(models.Edition.objects.count(), 1) - with open(self.archive_file, "rb") as fileobj: - with BookwyrmTarFile.open(mode="r:gz", fileobj=fileobj) as tarfile: + with ( + open(self.archive_file, "rb") as fileobj, + BookwyrmTarFile.open(mode="r:gz", fileobj=fileobj) as tarfile, + ): + bookwyrm_import_job.get_or_create_edition( + self.json_data["books"][1], tarfile + ) # Sand Talk - bookwyrm_import_job.get_or_create_edition( - self.json_data["books"][1], tarfile - ) # Sand Talk - - self.assertEqual(models.Edition.objects.count(), 1) + self.assertEqual(models.Edition.objects.count(), 1) def test_get_or_create_edition_not_existing(self): """Test take a JSON string of books and editions, @@ -272,12 +271,13 @@ class BookwyrmImport(TestCase): # pylint: disable=too-many-public-methods self.assertEqual(models.Edition.objects.count(), 1) - with open(self.archive_file, "rb") as fileobj: - with BookwyrmTarFile.open(mode="r:gz", fileobj=fileobj) as tarfile: - - bookwyrm_import_job.get_or_create_edition( - self.json_data["books"][0], tarfile - ) # Seeing like a state + with ( + open(self.archive_file, "rb") as fileobj, + BookwyrmTarFile.open(mode="r:gz", fileobj=fileobj) as tarfile, + ): + bookwyrm_import_job.get_or_create_edition( + self.json_data["books"][0], tarfile + ) # Seeing like a state self.assertTrue(models.Edition.objects.filter(isbn_13="9780300070163").exists()) self.assertEqual(models.Edition.objects.count(), 2) @@ -326,7 +326,6 @@ class BookwyrmImport(TestCase): # pylint: disable=too-many-public-methods patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"), patch("bookwyrm.models.bookwyrm_import_job.is_alias", return_value=True), ): - bookwyrm_import_job.upsert_statuses( self.local_user, models.Review, reviews, self.book.remote_id ) @@ -364,7 +363,6 @@ class BookwyrmImport(TestCase): # pylint: disable=too-many-public-methods patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"), patch("bookwyrm.models.bookwyrm_import_job.is_alias", return_value=True), ): - bookwyrm_import_job.upsert_statuses( self.local_user, models.Comment, comments, self.book.remote_id ) diff --git a/bookwyrm/tests/models/test_import_model.py b/bookwyrm/tests/models/test_import_model.py index e5be01464..aa7b5b132 100644 --- a/bookwyrm/tests/models/test_import_model.py +++ b/bookwyrm/tests/models/test_import_model.py @@ -194,14 +194,16 @@ class ImportJob(TestCase): status=200, ) - with patch("bookwyrm.connectors.abstract_connector.load_more_data.delay"): - with patch( + with ( + patch("bookwyrm.connectors.abstract_connector.load_more_data.delay"), + patch( "bookwyrm.connectors.connector_manager.first_search_result" - ) as search: - search.return_value = result - with patch( - "bookwyrm.connectors.openlibrary.Connector.get_authors_from_data" - ): - book = item.get_book_from_identifier() + ) as search, + ): + search.return_value = result + with patch( + "bookwyrm.connectors.openlibrary.Connector.get_authors_from_data" + ): + book = item.get_book_from_identifier() self.assertEqual(book.title, "Sabriel") diff --git a/bookwyrm/tests/test_signing.py b/bookwyrm/tests/test_signing.py index 80a35b94d..70f8e47cf 100644 --- a/bookwyrm/tests/test_signing.py +++ b/bookwyrm/tests/test_signing.py @@ -91,9 +91,11 @@ class Signature(TestCase): signature = make_signature( "post", signer or sender, self.rat.inbox, now, digest=digest ) - with patch("bookwyrm.views.inbox.activity_task.apply_async"): - with patch("bookwyrm.models.user.set_remote_server.delay"): - return self.send(signature, now, send_data or data, digest) + with ( + patch("bookwyrm.views.inbox.activity_task.apply_async"), + patch("bookwyrm.models.user.set_remote_server.delay"), + ): + return self.send(signature, now, send_data or data, digest) def test_correct_signature(self): """this one should just work""" diff --git a/bookwyrm/tests/views/inbox/test_inbox_announce.py b/bookwyrm/tests/views/inbox/test_inbox_announce.py index 53555f213..d982a91c8 100644 --- a/bookwyrm/tests/views/inbox/test_inbox_announce.py +++ b/bookwyrm/tests/views/inbox/test_inbox_announce.py @@ -39,13 +39,15 @@ class InboxActivities(TestCase): outbox="https://example.com/users/rat/outbox", ) - with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"): - with patch("bookwyrm.activitystreams.add_status_task.delay"): - self.status = models.Status.objects.create( - user=self.local_user, - content="Test status", - remote_id="https://example.com/status/1", - ) + with ( + patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"), + patch("bookwyrm.activitystreams.add_status_task.delay"), + ): + self.status = models.Status.objects.create( + user=self.local_user, + content="Test status", + remote_id="https://example.com/status/1", + ) models.SiteSettings.objects.create() diff --git a/bookwyrm/tests/views/inbox/test_inbox_like.py b/bookwyrm/tests/views/inbox/test_inbox_like.py index 4a397dcf8..4efe4cb98 100644 --- a/bookwyrm/tests/views/inbox/test_inbox_like.py +++ b/bookwyrm/tests/views/inbox/test_inbox_like.py @@ -38,13 +38,15 @@ class InboxActivities(TestCase): outbox="https://example.com/users/rat/outbox", ) - with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"): - with patch("bookwyrm.activitystreams.add_status_task.delay"): - self.status = models.Status.objects.create( - user=self.local_user, - content="Test status", - remote_id="https://example.com/status/1", - ) + with ( + patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"), + patch("bookwyrm.activitystreams.add_status_task.delay"), + ): + self.status = models.Status.objects.create( + user=self.local_user, + content="Test status", + remote_id="https://example.com/status/1", + ) models.SiteSettings.objects.create() diff --git a/bookwyrm/tests/views/test_helpers.py b/bookwyrm/tests/views/test_helpers.py index 2f2481931..e00e1d234 100644 --- a/bookwyrm/tests/views/test_helpers.py +++ b/bookwyrm/tests/views/test_helpers.py @@ -25,29 +25,31 @@ class ViewsHelpers(TestCase): # pylint: disable=too-many-public-methods patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch("bookwyrm.activitystreams.populate_stream_task.delay"), patch("bookwyrm.lists_stream.populate_lists_task.delay"), + patch("bookwyrm.suggested_users.rerank_user_task.delay"), ): - with patch("bookwyrm.suggested_users.rerank_user_task.delay"): - self.local_user = models.User.objects.create_user( - "mouse@local.com", - "mouse@mouse.com", - "mouseword", - local=True, - discoverable=True, - localname="mouse", - remote_id="https://example.com/users/mouse", - ) - with patch("bookwyrm.models.user.set_remote_server.delay"): - with patch("bookwyrm.suggested_users.rerank_user_task.delay"): - self.remote_user = models.User.objects.create_user( - "rat", - "rat@rat.com", - "ratword", - local=False, - remote_id="https://example.com/users/rat", - discoverable=True, - inbox="https://example.com/users/rat/inbox", - outbox="https://example.com/users/rat/outbox", - ) + self.local_user = models.User.objects.create_user( + "mouse@local.com", + "mouse@mouse.com", + "mouseword", + local=True, + discoverable=True, + localname="mouse", + remote_id="https://example.com/users/mouse", + ) + with ( + patch("bookwyrm.models.user.set_remote_server.delay"), + patch("bookwyrm.suggested_users.rerank_user_task.delay"), + ): + self.remote_user = models.User.objects.create_user( + "rat", + "rat@rat.com", + "ratword", + local=False, + remote_id="https://example.com/users/rat", + discoverable=True, + inbox="https://example.com/users/rat/inbox", + outbox="https://example.com/users/rat/outbox", + ) self.work = models.Work.objects.create(title="Test Work") self.book = models.Edition.objects.create( title="Test Book", diff --git a/bookwyrm/tests/views/test_updates.py b/bookwyrm/tests/views/test_updates.py index 129aa8d71..6f826b46f 100644 --- a/bookwyrm/tests/views/test_updates.py +++ b/bookwyrm/tests/views/test_updates.py @@ -56,14 +56,11 @@ class UpdateViews(TestCase): request = self.factory.get("") request.user = self.local_user - with ( - patch( - "bookwyrm.activitystreams.ActivityStream.get_unread_count" - ) as mock_count, - patch( - "bookwyrm.activitystreams.ActivityStream.get_unread_count_by_status_type" - ) as mock_count_by_status, - ): + with patch( + "bookwyrm.activitystreams.ActivityStream.get_unread_count" + ) as mock_count, patch( + "bookwyrm.activitystreams.ActivityStream.get_unread_count_by_status_type" + ) as mock_count_by_status: mock_count.return_value = 3 mock_count_by_status.return_value = {"review": 5} result = views.get_unread_status_string(request, "home") @@ -78,14 +75,11 @@ class UpdateViews(TestCase): request = self.factory.get("") request.user = self.local_user - with ( - patch( - "bookwyrm.activitystreams.ActivityStream.get_unread_count" - ) as mock_count, - patch( - "bookwyrm.activitystreams.ActivityStream.get_unread_count_by_status_type" - ) as mock_count_by_status, - ): + with patch( + "bookwyrm.activitystreams.ActivityStream.get_unread_count" + ) as mock_count, patch( + "bookwyrm.activitystreams.ActivityStream.get_unread_count_by_status_type" + ) as mock_count_by_status: mock_count.return_value = 3 mock_count_by_status.return_value = { "generated_note": 1,