Skip to content

Commit

Permalink
Merge pull request #3320 from dato/better-fmt-patch-calls
Browse files Browse the repository at this point in the history
bulk-fmt: bracket-wrap calls to patch() for better readability
  • Loading branch information
BartSchuurmans authored Mar 29, 2024
2 parents 699d637 + 3133a47 commit 1464d09
Show file tree
Hide file tree
Showing 133 changed files with 1,507 additions and 1,191 deletions.
6 changes: 3 additions & 3 deletions bookwyrm/tests/activitypub/test_author.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ class Author(TestCase):
"""serialize author tests"""

@classmethod
def setUpTestData(self): # pylint: disable=bad-classmethod-argument
def setUpTestData(cls):
"""initial data"""
self.book = models.Edition.objects.create(
cls.book = models.Edition.objects.create(
title="Example Edition",
remote_id="https://example.com/book/1",
)
self.author = models.Author.objects.create(
cls.author = models.Author.objects.create(
name="Author fullname",
aliases=["One", "Two"],
bio="bio bio bio",
Expand Down
26 changes: 15 additions & 11 deletions bookwyrm/tests/activitypub/test_base_activity.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,18 @@ class BaseActivity(TestCase):
"""the super class for model-linked activitypub dataclasses"""

@classmethod
def setUpTestData(self): # pylint: disable=bad-classmethod-argument
def setUpTestData(cls):
"""we're probably going to re-use this so why copy/paste"""
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
"bookwyrm.activitystreams.populate_stream_task.delay"
), patch("bookwyrm.lists_stream.populate_lists_task.delay"):
self.user = models.User.objects.create_user(
with (
patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"),
patch("bookwyrm.activitystreams.populate_stream_task.delay"),
patch("bookwyrm.lists_stream.populate_lists_task.delay"),
):
cls.user = models.User.objects.create_user(
"mouse", "[email protected]", "mouseword", local=True, localname="mouse"
)
self.user.remote_id = "http://example.com/a/b"
self.user.save(broadcast=False, update_fields=["remote_id"])
cls.user.remote_id = "http://example.com/a/b"
cls.user.save(broadcast=False, update_fields=["remote_id"])

def setUp(self):
datafile = pathlib.Path(__file__).parent.joinpath("../data/ap_user.json")
Expand Down Expand Up @@ -232,10 +234,12 @@ def test_to_model_one_to_many(self, *_):
)

# 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
Expand Down
18 changes: 10 additions & 8 deletions bookwyrm/tests/activitypub/test_note.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,20 @@ class Note(TestCase):
"""the model-linked ActivityPub dataclass for Note-based types"""

@classmethod
def setUpTestData(self): # pylint: disable=bad-classmethod-argument
def setUpTestData(cls):
"""create a shared user"""
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
"bookwyrm.activitystreams.populate_stream_task.delay"
), patch("bookwyrm.lists_stream.populate_lists_task.delay"):
self.user = models.User.objects.create_user(
with (
patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"),
patch("bookwyrm.activitystreams.populate_stream_task.delay"),
patch("bookwyrm.lists_stream.populate_lists_task.delay"),
):
cls.user = models.User.objects.create_user(
"mouse", "[email protected]", "mouseword", local=True, localname="mouse"
)
self.user.remote_id = "https://test-instance.org/user/critic"
self.user.save(broadcast=False, update_fields=["remote_id"])
cls.user.remote_id = "https://test-instance.org/user/critic"
cls.user.save(broadcast=False, update_fields=["remote_id"])

self.book = models.Edition.objects.create(
cls.book = models.Edition.objects.create(
title="Test Edition", remote_id="http://book.com/book"
)

Expand Down
6 changes: 3 additions & 3 deletions bookwyrm/tests/activitypub/test_quotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ class Quotation(TestCase):
"""we have hecka ways to create statuses"""

@classmethod
def setUpTestData(self): # pylint: disable=bad-classmethod-argument
def setUpTestData(cls):
"""model objects we'll need"""
with patch("bookwyrm.models.user.set_remote_server.delay"):
self.user = models.User.objects.create_user(
cls.user = models.User.objects.create_user(
"mouse",
"[email protected]",
"mouseword",
Expand All @@ -23,7 +23,7 @@ def setUpTestData(self): # pylint: disable=bad-classmethod-argument
outbox="https://example.com/user/mouse/outbox",
remote_id="https://example.com/user/mouse",
)
self.book = models.Edition.objects.create(
cls.book = models.Edition.objects.create(
title="Example Edition",
remote_id="https://example.com/book/1",
)
Expand Down
26 changes: 15 additions & 11 deletions bookwyrm/tests/activitystreams/test_abstractstream.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,25 @@ class Activitystreams(TestCase):
"""using redis to build activity streams"""

@classmethod
def setUpTestData(self): # pylint: disable=bad-classmethod-argument
def setUpTestData(cls):
"""use a test csv"""
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
"bookwyrm.activitystreams.populate_stream_task.delay"
), patch("bookwyrm.lists_stream.populate_lists_task.delay"):
self.local_user = models.User.objects.create_user(
with (
patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"),
patch("bookwyrm.activitystreams.populate_stream_task.delay"),
patch("bookwyrm.lists_stream.populate_lists_task.delay"),
):
cls.local_user = models.User.objects.create_user(
"mouse", "[email protected]", "password", local=True, localname="mouse"
)
self.another_user = models.User.objects.create_user(
cls.another_user = models.User.objects.create_user(
"nutria",
"[email protected]",
"password",
local=True,
localname="nutria",
)
with patch("bookwyrm.models.user.set_remote_server.delay"):
self.remote_user = models.User.objects.create_user(
cls.remote_user = models.User.objects.create_user(
"rat",
"[email protected]",
"ratword",
Expand All @@ -42,7 +44,7 @@ def setUpTestData(self): # pylint: disable=bad-classmethod-argument
outbox="https://example.com/users/rat/outbox",
)
work = models.Work.objects.create(title="test work")
self.book = models.Edition.objects.create(title="test book", parent_work=work)
cls.book = models.Edition.objects.create(title="test book", parent_work=work)

def setUp(self):
"""per-test setUp"""
Expand Down Expand Up @@ -105,9 +107,11 @@ def test_get_activity_stream(self, *_):
privacy="direct",
book=self.book,
)
with patch("bookwyrm.activitystreams.r.set"), patch(
"bookwyrm.activitystreams.r.delete"
), patch("bookwyrm.activitystreams.ActivityStream.get_store") as redis_mock:
with (
patch("bookwyrm.activitystreams.r.set"),
patch("bookwyrm.activitystreams.r.delete"),
patch("bookwyrm.activitystreams.ActivityStream.get_store") as redis_mock,
):
redis_mock.return_value = [status.id, status2.id]
result = self.test_stream.get_activity_stream(self.local_user)
self.assertEqual(result.count(), 2)
Expand Down
16 changes: 9 additions & 7 deletions bookwyrm/tests/activitystreams/test_booksstream.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,18 @@ class Activitystreams(TestCase):
"""using redis to build activity streams"""

@classmethod
def setUpTestData(self): # pylint: disable=bad-classmethod-argument
def setUpTestData(cls):
"""use a test csv"""
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
"bookwyrm.activitystreams.populate_stream_task.delay"
), patch("bookwyrm.lists_stream.populate_lists_task.delay"):
self.local_user = models.User.objects.create_user(
with (
patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"),
patch("bookwyrm.activitystreams.populate_stream_task.delay"),
patch("bookwyrm.lists_stream.populate_lists_task.delay"),
):
cls.local_user = models.User.objects.create_user(
"mouse", "[email protected]", "password", local=True, localname="mouse"
)
with patch("bookwyrm.models.user.set_remote_server.delay"):
self.remote_user = models.User.objects.create_user(
cls.remote_user = models.User.objects.create_user(
"rat",
"[email protected]",
"ratword",
Expand All @@ -34,7 +36,7 @@ def setUpTestData(self): # pylint: disable=bad-classmethod-argument
outbox="https://example.com/users/rat/outbox",
)
work = models.Work.objects.create(title="test work")
self.book = models.Edition.objects.create(title="test book", parent_work=work)
cls.book = models.Edition.objects.create(title="test book", parent_work=work)

def test_get_statuses_for_user_books(self, *_):
"""create a stream for a user"""
Expand Down
16 changes: 9 additions & 7 deletions bookwyrm/tests/activitystreams/test_homestream.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,25 @@ class Activitystreams(TestCase):
"""using redis to build activity streams"""

@classmethod
def setUpTestData(self): # pylint: disable=bad-classmethod-argument
def setUpTestData(cls):
"""use a test csv"""
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
"bookwyrm.activitystreams.populate_stream_task.delay"
), patch("bookwyrm.lists_stream.populate_lists_task.delay"):
self.local_user = models.User.objects.create_user(
with (
patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"),
patch("bookwyrm.activitystreams.populate_stream_task.delay"),
patch("bookwyrm.lists_stream.populate_lists_task.delay"),
):
cls.local_user = models.User.objects.create_user(
"mouse", "[email protected]", "password", local=True, localname="mouse"
)
self.another_user = models.User.objects.create_user(
cls.another_user = models.User.objects.create_user(
"nutria",
"[email protected]",
"password",
local=True,
localname="nutria",
)
with patch("bookwyrm.models.user.set_remote_server.delay"):
self.remote_user = models.User.objects.create_user(
cls.remote_user = models.User.objects.create_user(
"rat",
"[email protected]",
"ratword",
Expand Down
18 changes: 10 additions & 8 deletions bookwyrm/tests/activitystreams/test_localstream.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,25 @@ class Activitystreams(TestCase):
"""using redis to build activity streams"""

@classmethod
def setUpTestData(self): # pylint: disable=bad-classmethod-argument
def setUpTestData(cls):
"""use a test csv"""
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
"bookwyrm.activitystreams.populate_stream_task.delay"
), patch("bookwyrm.lists_stream.populate_lists_task.delay"):
self.local_user = models.User.objects.create_user(
with (
patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"),
patch("bookwyrm.activitystreams.populate_stream_task.delay"),
patch("bookwyrm.lists_stream.populate_lists_task.delay"),
):
cls.local_user = models.User.objects.create_user(
"mouse", "[email protected]", "password", local=True, localname="mouse"
)
self.another_user = models.User.objects.create_user(
cls.another_user = models.User.objects.create_user(
"nutria",
"[email protected]",
"password",
local=True,
localname="nutria",
)
with patch("bookwyrm.models.user.set_remote_server.delay"):
self.remote_user = models.User.objects.create_user(
cls.remote_user = models.User.objects.create_user(
"rat",
"[email protected]",
"ratword",
Expand All @@ -39,7 +41,7 @@ def setUpTestData(self): # pylint: disable=bad-classmethod-argument
outbox="https://example.com/users/rat/outbox",
)
work = models.Work.objects.create(title="test work")
self.book = models.Edition.objects.create(title="test book", parent_work=work)
cls.book = models.Edition.objects.create(title="test book", parent_work=work)

def test_localstream_get_audience_remote_status(self, *_):
"""get a list of users that should see a status"""
Expand Down
14 changes: 8 additions & 6 deletions bookwyrm/tests/activitystreams/test_signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,18 @@ class ActivitystreamsSignals(TestCase):
"""using redis to build activity streams"""

@classmethod
def setUpTestData(self): # pylint: disable=bad-classmethod-argument
def setUpTestData(cls):
"""use a test csv"""
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
"bookwyrm.activitystreams.populate_stream_task.delay"
), patch("bookwyrm.lists_stream.populate_lists_task.delay"):
self.local_user = models.User.objects.create_user(
with (
patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"),
patch("bookwyrm.activitystreams.populate_stream_task.delay"),
patch("bookwyrm.lists_stream.populate_lists_task.delay"),
):
cls.local_user = models.User.objects.create_user(
"mouse", "[email protected]", "password", local=True, localname="mouse"
)
with patch("bookwyrm.models.user.set_remote_server.delay"):
self.remote_user = models.User.objects.create_user(
cls.remote_user = models.User.objects.create_user(
"rat",
"[email protected]",
"ratword",
Expand Down
22 changes: 11 additions & 11 deletions bookwyrm/tests/activitystreams/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,25 @@ class Activitystreams(TestCase):
"""using redis to build activity streams"""

@classmethod
def setUpTestData(self): # pylint: disable=bad-classmethod-argument
def setUpTestData(cls):
"""use a test csv"""
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
"bookwyrm.activitystreams.populate_stream_task.delay"
), patch("bookwyrm.lists_stream.populate_lists_task.delay"):
self.local_user = models.User.objects.create_user(
with (
patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"),
patch("bookwyrm.activitystreams.populate_stream_task.delay"),
patch("bookwyrm.lists_stream.populate_lists_task.delay"),
):
cls.local_user = models.User.objects.create_user(
"mouse", "[email protected]", "password", local=True, localname="mouse"
)
self.another_user = models.User.objects.create_user(
cls.another_user = models.User.objects.create_user(
"nutria",
"[email protected]",
"password",
local=True,
localname="nutria",
)
with patch("bookwyrm.models.user.set_remote_server.delay"):
self.remote_user = models.User.objects.create_user(
cls.remote_user = models.User.objects.create_user(
"rat",
"[email protected]",
"ratword",
Expand All @@ -34,11 +36,9 @@ def setUpTestData(self): # pylint: disable=bad-classmethod-argument
outbox="https://example.com/users/rat/outbox",
)
work = models.Work.objects.create(title="test work")
self.book = models.Edition.objects.create(title="test book", parent_work=work)
cls.book = models.Edition.objects.create(title="test book", parent_work=work)
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"):
self.status = models.Status.objects.create(
content="hi", user=self.local_user
)
cls.status = models.Status.objects.create(content="hi", user=cls.local_user)

def test_add_book_statuses_task(self):
"""statuses related to a book"""
Expand Down
4 changes: 2 additions & 2 deletions bookwyrm/tests/connectors/test_abstract_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class AbstractConnector(TestCase):
"""generic code for connecting to outside data sources"""

@classmethod
def setUpTestData(self): # pylint: disable=bad-classmethod-argument
def setUpTestData(cls):
"""we need an example connector in the database"""
models.Connector.objects.create(
identifier="example.com",
Expand All @@ -23,7 +23,7 @@ def setUpTestData(self): # pylint: disable=bad-classmethod-argument
covers_url="https://example.com/covers",
search_url="https://example.com/search?q=",
)
self.book = models.Edition.objects.create(
cls.book = models.Edition.objects.create(
title="Test Book",
remote_id="https://example.com/book/1234",
openlibrary_key="OL1234M",
Expand Down
4 changes: 2 additions & 2 deletions bookwyrm/tests/connectors/test_abstract_minimal_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ class AbstractConnector(TestCase):
"""generic code for connecting to outside data sources"""

@classmethod
def setUpTestData(self): # pylint: disable=bad-classmethod-argument
def setUpTestData(cls):
"""we need an example connector in the database"""
self.connector_info = models.Connector.objects.create(
cls.connector_info = models.Connector.objects.create(
identifier="example.com",
connector_file="openlibrary",
base_url="https://example.com",
Expand Down
2 changes: 1 addition & 1 deletion bookwyrm/tests/connectors/test_bookwyrm_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class BookWyrmConnector(TestCase):
"""this connector doesn't do much, just search"""

@classmethod
def setUpTestData(self): # pylint: disable=bad-classmethod-argument
def setUpTestData(cls):
"""create bookwrym_connector in the database"""
models.Connector.objects.create(
identifier="example.com",
Expand Down
Loading

0 comments on commit 1464d09

Please sign in to comment.