Skip to content

Commit

Permalink
celery inmem test suite: tests/views/*.py
Browse files Browse the repository at this point in the history
  • Loading branch information
dato committed Jul 28, 2024
1 parent 3d28019 commit c313fbc
Show file tree
Hide file tree
Showing 7 changed files with 206 additions and 298 deletions.
46 changes: 18 additions & 28 deletions bookwyrm/tests/views/test_annual_summary.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""testing the annual summary page"""
import datetime
from unittest.mock import patch

from django.contrib.auth.models import AnonymousUser
from django.http import Http404
Expand All @@ -23,20 +22,15 @@ class AnnualSummary(TestCase):
@classmethod
def setUpTestData(cls):
"""we need basic test data and mocks"""
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(
"[email protected]",
"[email protected]",
"mouseword",
local=True,
localname="mouse",
remote_id="https://example.com/users/mouse",
summary_keys={"2020": "0123456789"},
)
cls.local_user = models.User.objects.create_user(
"[email protected]",
"[email protected]",
"mouseword",
local=True,
localname="mouse",
remote_id="https://example.com/users/mouse",
summary_keys={"2020": "0123456789"},
)
cls.work = models.Work.objects.create(title="Test Work")
cls.book = models.Edition.objects.create(
title="Example Edition",
Expand All @@ -53,7 +47,7 @@ def setUp(self):
self.anonymous_user = AnonymousUser
self.anonymous_user.is_authenticated = False

def test_annual_summary_not_authenticated(self, *_):
def test_annual_summary_not_authenticated(self):
"""there are so many views, this just makes sure it DOESN’T LOAD"""
view = views.AnnualSummary.as_view()
request = self.factory.get("")
Expand All @@ -62,7 +56,7 @@ def test_annual_summary_not_authenticated(self, *_):
with self.assertRaises(Http404):
view(request, self.local_user.localname, self.year)

def test_annual_summary_not_authenticated_with_key(self, *_):
def test_annual_summary_not_authenticated_with_key(self):
"""there are so many views, this just makes sure it DOES LOAD"""
key = self.local_user.summary_keys[self.year]
view = views.AnnualSummary.as_view()
Expand All @@ -78,7 +72,7 @@ def test_annual_summary_not_authenticated_with_key(self, *_):
validate_html(result.render())
self.assertEqual(result.status_code, 200)

def test_annual_summary_wrong_year(self, *_):
def test_annual_summary_wrong_year(self):
"""there are so many views, this just makes sure it DOESN’T LOAD"""
view = views.AnnualSummary.as_view()
request = self.factory.get("")
Expand All @@ -87,7 +81,7 @@ def test_annual_summary_wrong_year(self, *_):
with self.assertRaises(Http404):
view(request, self.local_user.localname, self.year)

def test_annual_summary_empty_page(self, *_):
def test_annual_summary_empty_page(self):
"""there are so many views, this just makes sure it LOADS"""
view = views.AnnualSummary.as_view()
request = self.factory.get("")
Expand All @@ -99,9 +93,7 @@ def test_annual_summary_empty_page(self, *_):
validate_html(result.render())
self.assertEqual(result.status_code, 200)

@patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async")
@patch("bookwyrm.activitystreams.add_book_statuses_task.delay")
def test_annual_summary_page(self, *_):
def test_annual_summary_page(self):
"""there are so many views, this just makes sure it LOADS"""
models.ReadThrough.objects.create(
user=self.local_user, book=self.book, finish_date=make_date(2020, 1, 1)
Expand All @@ -117,9 +109,7 @@ def test_annual_summary_page(self, *_):
validate_html(result.render())
self.assertEqual(result.status_code, 200)

@patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async")
@patch("bookwyrm.activitystreams.add_book_statuses_task.delay")
def test_annual_summary_page_with_review(self, *_):
def test_annual_summary_page_with_review(self):
"""there are so many views, this just makes sure it LOADS"""

models.Review.objects.create(
Expand All @@ -144,7 +134,7 @@ def test_annual_summary_page_with_review(self, *_):
validate_html(result.render())
self.assertEqual(result.status_code, 200)

def test_personal_annual_summary(self, *_):
def test_personal_annual_summary(self):
"""redirect to unique user url"""
view = views.personal_annual_summary
request = self.factory.get("")
Expand All @@ -155,7 +145,7 @@ def test_personal_annual_summary(self, *_):
self.assertEqual(result.status_code, 302)
self.assertEqual(result.url, "/user/mouse/2020-in-the-books")

def test_summary_add_key(self, *_):
def test_summary_add_key(self):
"""add shareable key"""
self.assertFalse("2022" in self.local_user.summary_keys.keys())

Expand All @@ -167,7 +157,7 @@ def test_summary_add_key(self, *_):
self.assertEqual(result.status_code, 302)
self.assertIsNotNone(self.local_user.summary_keys["2022"])

def test_summary_revoke_key(self, *_):
def test_summary_revoke_key(self):
"""add shareable key"""
self.assertTrue("2020" in self.local_user.summary_keys.keys())

Expand Down
24 changes: 9 additions & 15 deletions bookwyrm/tests/views/test_author.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,14 @@ class AuthorViews(TestCase):
@classmethod
def setUpTestData(cls):
"""we need basic test data and mocks"""
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(
"[email protected]",
"[email protected]",
"mouseword",
local=True,
localname="mouse",
remote_id="https://example.com/users/mouse",
)
cls.local_user = models.User.objects.create_user(
"[email protected]",
"[email protected]",
"mouseword",
local=True,
localname="mouse",
remote_id="https://example.com/users/mouse",
)
cls.group = Group.objects.create(name="editor")
cls.group.permissions.add(
Permission.objects.create(
Expand Down Expand Up @@ -153,8 +148,7 @@ def test_edit_author(self):
request = self.factory.post("", form.data)
request.user = self.local_user

with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"):
view(request, author.id)
view(request, author.id)
author.refresh_from_db()
self.assertEqual(author.name, "New Name")
self.assertEqual(author.last_edited_by, self.local_user)
Expand Down
28 changes: 9 additions & 19 deletions bookwyrm/tests/views/test_directory.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
""" test for app action functionality """
from unittest.mock import patch

from django.contrib.auth.models import AnonymousUser
from django.template.response import TemplateResponse
Expand All @@ -16,19 +15,14 @@ class DirectoryViews(TestCase):
@classmethod
def setUpTestData(cls):
"""we need basic test data and mocks"""
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(
"[email protected]",
"[email protected]",
"mouseword",
local=True,
localname="mouse",
remote_id="https://example.com/users/mouse",
)
cls.local_user = models.User.objects.create_user(
"[email protected]",
"[email protected]",
"mouseword",
local=True,
localname="mouse",
remote_id="https://example.com/users/mouse",
)
models.SiteSettings.objects.create()

def setUp(self):
Expand All @@ -37,11 +31,7 @@ def setUp(self):
self.anonymous_user = AnonymousUser
self.anonymous_user.is_authenticated = False

@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")
def test_directory_page(self, *_):
def test_directory_page(self):
"""there are so many views, this just makes sure it LOADS"""
models.User.objects.create_user(
"[email protected]",
Expand Down
23 changes: 8 additions & 15 deletions bookwyrm/tests/views/test_discover.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,13 @@ class DiscoverViews(TestCase):
@classmethod
def setUpTestData(cls):
"""we need basic test data and mocks"""
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(
"[email protected]",
"[email protected]",
"password",
local=True,
localname="mouse",
)
cls.local_user = models.User.objects.create_user(
"[email protected]",
"[email protected]",
"password",
local=True,
localname="mouse",
)
models.SiteSettings.objects.create()

def setUp(self):
Expand All @@ -47,9 +42,7 @@ def test_discover_page_empty(self):
self.assertEqual(result.status_code, 200)
validate_html(result.render())

@patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async")
@patch("bookwyrm.activitystreams.add_status_task.delay")
def test_discover_page_with_posts(self, *_):
def test_discover_page_with_posts(self):
"""there are so many views, this just makes sure it LOADS"""
view = views.Discover.as_view()
request = self.factory.get("")
Expand Down
Loading

0 comments on commit c313fbc

Please sign in to comment.