mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2024-11-25 11:01:12 +00:00
Remove nesting in several with..patch calls
This commit is contained in:
parent
1b9e0546e6
commit
4b9fe0af0c
9 changed files with 100 additions and 94 deletions
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
)
|
||||
|
|
|
@ -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")
|
||||
|
|
|
@ -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"""
|
||||
|
|
|
@ -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()
|
||||
|
||||
|
|
|
@ -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()
|
||||
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Reference in a new issue