Remove nesting in several with..patch calls

This commit is contained in:
Adeodato Simó 2024-03-17 20:36:48 -03:00
parent 1b9e0546e6
commit 4b9fe0af0c
9 changed files with 100 additions and 94 deletions

View file

@ -234,8 +234,10 @@ 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:
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())

View file

@ -268,8 +268,10 @@ 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:
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")

View file

@ -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,10 +83,10 @@ 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:
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
)
@ -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,9 +254,10 @@ 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
@ -272,9 +271,10 @@ 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"][0], tarfile
) # Seeing like a state
@ -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
)

View file

@ -194,10 +194,12 @@ 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:
) as search,
):
search.return_value = result
with patch(
"bookwyrm.connectors.openlibrary.Connector.get_authors_from_data"

View file

@ -91,8 +91,10 @@ 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"):
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):

View file

@ -39,8 +39,10 @@ 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"):
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",

View file

@ -38,8 +38,10 @@ 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"):
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",

View file

@ -25,8 +25,8 @@ 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",
@ -36,8 +36,10 @@ class ViewsHelpers(TestCase): # pylint: disable=too-many-public-methods
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"):
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",

View file

@ -56,14 +56,11 @@ class UpdateViews(TestCase):
request = self.factory.get("")
request.user = self.local_user
with (
patch(
with patch(
"bookwyrm.activitystreams.ActivityStream.get_unread_count"
) as mock_count,
patch(
) as mock_count, patch(
"bookwyrm.activitystreams.ActivityStream.get_unread_count_by_status_type"
) as mock_count_by_status,
):
) 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(
with patch(
"bookwyrm.activitystreams.ActivityStream.get_unread_count"
) as mock_count,
patch(
) as mock_count, patch(
"bookwyrm.activitystreams.ActivityStream.get_unread_count_by_status_type"
) as mock_count_by_status,
):
) as mock_count_by_status:
mock_count.return_value = 3
mock_count_by_status.return_value = {
"generated_note": 1,