Merge pull request #1845 from bookwyrm-social/organizing-templatetags
Organizing templatetags
This commit is contained in:
62
bookwyrm/tests/templatetags/test_book_display_tags.py
Normal file
62
bookwyrm/tests/templatetags/test_book_display_tags.py
Normal file
@ -0,0 +1,62 @@
|
||||
""" style fixes and lookups for templates """
|
||||
from unittest.mock import patch
|
||||
|
||||
from django.test import TestCase
|
||||
|
||||
from bookwyrm import models
|
||||
from bookwyrm.templatetags import book_display_tags
|
||||
|
||||
|
||||
@patch("bookwyrm.activitystreams.add_status_task.delay")
|
||||
@patch("bookwyrm.activitystreams.remove_status_task.delay")
|
||||
@patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async")
|
||||
class BookDisplayTags(TestCase):
|
||||
"""lotta different things here"""
|
||||
|
||||
def setUp(self):
|
||||
"""create some filler objects"""
|
||||
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(
|
||||
"mouse@example.com",
|
||||
"mouse@mouse.mouse",
|
||||
"mouseword",
|
||||
local=True,
|
||||
localname="mouse",
|
||||
)
|
||||
self.book = models.Edition.objects.create(title="Test Book")
|
||||
|
||||
def test_get_book_description(self, *_):
|
||||
"""grab it from the edition or the parent"""
|
||||
work = models.Work.objects.create(title="Test Work")
|
||||
self.book.parent_work = work
|
||||
self.book.save()
|
||||
|
||||
self.assertIsNone(book_display_tags.get_book_description(self.book))
|
||||
|
||||
work.description = "hi"
|
||||
work.save()
|
||||
self.assertEqual(book_display_tags.get_book_description(self.book), "hi")
|
||||
|
||||
self.book.description = "hello"
|
||||
self.book.save()
|
||||
self.assertEqual(book_display_tags.get_book_description(self.book), "hello")
|
||||
|
||||
def test_get_book_file_links(self, *_):
|
||||
"""load approved links"""
|
||||
link = models.FileLink.objects.create(
|
||||
book=self.book,
|
||||
url="https://web.site/hello",
|
||||
)
|
||||
links = book_display_tags.get_book_file_links(self.book)
|
||||
# the link is pending
|
||||
self.assertFalse(links.exists())
|
||||
|
||||
domain = link.domain
|
||||
domain.status = "approved"
|
||||
domain.save()
|
||||
|
||||
links = book_display_tags.get_book_file_links(self.book)
|
||||
self.assertTrue(links.exists())
|
||||
self.assertEqual(links[0], link)
|
@ -1,101 +0,0 @@
|
||||
""" style fixes and lookups for templates """
|
||||
from unittest.mock import patch
|
||||
|
||||
from django.test import TestCase
|
||||
|
||||
from bookwyrm import models
|
||||
from bookwyrm.templatetags import bookwyrm_tags
|
||||
|
||||
|
||||
@patch("bookwyrm.activitystreams.add_status_task.delay")
|
||||
@patch("bookwyrm.activitystreams.remove_status_task.delay")
|
||||
class BookWyrmTags(TestCase):
|
||||
"""lotta different things here"""
|
||||
|
||||
def setUp(self):
|
||||
"""create some filler objects"""
|
||||
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(
|
||||
"mouse@example.com",
|
||||
"mouse@mouse.mouse",
|
||||
"mouseword",
|
||||
local=True,
|
||||
localname="mouse",
|
||||
)
|
||||
with patch("bookwyrm.models.user.set_remote_server.delay"):
|
||||
self.remote_user = models.User.objects.create_user(
|
||||
"rat",
|
||||
"rat@rat.rat",
|
||||
"ratword",
|
||||
remote_id="http://example.com/rat",
|
||||
local=False,
|
||||
)
|
||||
self.book = models.Edition.objects.create(title="Test Book")
|
||||
|
||||
def test_get_user_rating(self, *_):
|
||||
"""get a user's most recent rating of a book"""
|
||||
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"):
|
||||
models.Review.objects.create(user=self.user, book=self.book, rating=3)
|
||||
self.assertEqual(bookwyrm_tags.get_user_rating(self.book, self.user), 3)
|
||||
|
||||
def test_get_user_rating_doesnt_exist(self, *_):
|
||||
"""there is no rating available"""
|
||||
self.assertEqual(bookwyrm_tags.get_user_rating(self.book, self.user), 0)
|
||||
|
||||
def test_get_book_description(self, *_):
|
||||
"""grab it from the edition or the parent"""
|
||||
work = models.Work.objects.create(title="Test Work")
|
||||
self.book.parent_work = work
|
||||
self.book.save()
|
||||
|
||||
self.assertIsNone(bookwyrm_tags.get_book_description(self.book))
|
||||
|
||||
work.description = "hi"
|
||||
work.save()
|
||||
self.assertEqual(bookwyrm_tags.get_book_description(self.book), "hi")
|
||||
|
||||
self.book.description = "hello"
|
||||
self.book.save()
|
||||
self.assertEqual(bookwyrm_tags.get_book_description(self.book), "hello")
|
||||
|
||||
def test_get_next_shelf(self, *_):
|
||||
"""self progress helper"""
|
||||
self.assertEqual(bookwyrm_tags.get_next_shelf("to-read"), "reading")
|
||||
self.assertEqual(bookwyrm_tags.get_next_shelf("reading"), "read")
|
||||
self.assertEqual(bookwyrm_tags.get_next_shelf("read"), "complete")
|
||||
self.assertEqual(bookwyrm_tags.get_next_shelf("blooooga"), "to-read")
|
||||
|
||||
@patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async")
|
||||
def test_load_subclass(self, *_):
|
||||
"""get a status' real type"""
|
||||
review = models.Review.objects.create(user=self.user, book=self.book, rating=3)
|
||||
status = models.Status.objects.get(id=review.id)
|
||||
self.assertIsInstance(status, models.Status)
|
||||
self.assertIsInstance(bookwyrm_tags.load_subclass(status), models.Review)
|
||||
|
||||
quote = models.Quotation.objects.create(
|
||||
user=self.user, book=self.book, content="hi"
|
||||
)
|
||||
status = models.Status.objects.get(id=quote.id)
|
||||
self.assertIsInstance(status, models.Status)
|
||||
self.assertIsInstance(bookwyrm_tags.load_subclass(status), models.Quotation)
|
||||
|
||||
comment = models.Comment.objects.create(
|
||||
user=self.user, book=self.book, content="hi"
|
||||
)
|
||||
status = models.Status.objects.get(id=comment.id)
|
||||
self.assertIsInstance(status, models.Status)
|
||||
self.assertIsInstance(bookwyrm_tags.load_subclass(status), models.Comment)
|
||||
|
||||
def test_related_status(self, *_):
|
||||
"""gets the subclass model for a notification status"""
|
||||
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"):
|
||||
status = models.Status.objects.create(content="hi", user=self.user)
|
||||
notification = models.Notification.objects.create(
|
||||
user=self.user, notification_type="MENTION", related_status=status
|
||||
)
|
||||
|
||||
result = bookwyrm_tags.related_status(notification)
|
||||
self.assertIsInstance(result, models.Status)
|
49
bookwyrm/tests/templatetags/test_feed_page_tags.py
Normal file
49
bookwyrm/tests/templatetags/test_feed_page_tags.py
Normal file
@ -0,0 +1,49 @@
|
||||
""" style fixes and lookups for templates """
|
||||
from unittest.mock import patch
|
||||
|
||||
from django.test import TestCase
|
||||
|
||||
from bookwyrm import models
|
||||
from bookwyrm.templatetags import feed_page_tags
|
||||
|
||||
|
||||
@patch("bookwyrm.activitystreams.add_status_task.delay")
|
||||
@patch("bookwyrm.activitystreams.remove_status_task.delay")
|
||||
class FeedPageTags(TestCase):
|
||||
"""lotta different things here"""
|
||||
|
||||
def setUp(self):
|
||||
"""create some filler objects"""
|
||||
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(
|
||||
"mouse@example.com",
|
||||
"mouse@mouse.mouse",
|
||||
"mouseword",
|
||||
local=True,
|
||||
localname="mouse",
|
||||
)
|
||||
self.book = models.Edition.objects.create(title="Test Book")
|
||||
|
||||
@patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async")
|
||||
def test_load_subclass(self, *_):
|
||||
"""get a status' real type"""
|
||||
review = models.Review.objects.create(user=self.user, book=self.book, rating=3)
|
||||
status = models.Status.objects.get(id=review.id)
|
||||
self.assertIsInstance(status, models.Status)
|
||||
self.assertIsInstance(feed_page_tags.load_subclass(status), models.Review)
|
||||
|
||||
quote = models.Quotation.objects.create(
|
||||
user=self.user, book=self.book, content="hi"
|
||||
)
|
||||
status = models.Status.objects.get(id=quote.id)
|
||||
self.assertIsInstance(status, models.Status)
|
||||
self.assertIsInstance(feed_page_tags.load_subclass(status), models.Quotation)
|
||||
|
||||
comment = models.Comment.objects.create(
|
||||
user=self.user, book=self.book, content="hi"
|
||||
)
|
||||
status = models.Status.objects.get(id=comment.id)
|
||||
self.assertIsInstance(status, models.Status)
|
||||
self.assertIsInstance(feed_page_tags.load_subclass(status), models.Comment)
|
37
bookwyrm/tests/templatetags/test_notification_page_tags.py
Normal file
37
bookwyrm/tests/templatetags/test_notification_page_tags.py
Normal file
@ -0,0 +1,37 @@
|
||||
""" style fixes and lookups for templates """
|
||||
from unittest.mock import patch
|
||||
|
||||
from django.test import TestCase
|
||||
|
||||
from bookwyrm import models
|
||||
from bookwyrm.templatetags import notification_page_tags
|
||||
|
||||
|
||||
@patch("bookwyrm.activitystreams.add_status_task.delay")
|
||||
@patch("bookwyrm.activitystreams.remove_status_task.delay")
|
||||
class NotificationPageTags(TestCase):
|
||||
"""lotta different things here"""
|
||||
|
||||
def setUp(self):
|
||||
"""create some filler objects"""
|
||||
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(
|
||||
"mouse@example.com",
|
||||
"mouse@mouse.mouse",
|
||||
"mouseword",
|
||||
local=True,
|
||||
localname="mouse",
|
||||
)
|
||||
|
||||
def test_related_status(self, *_):
|
||||
"""gets the subclass model for a notification status"""
|
||||
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"):
|
||||
status = models.Status.objects.create(content="hi", user=self.user)
|
||||
notification = models.Notification.objects.create(
|
||||
user=self.user, notification_type="MENTION", related_status=status
|
||||
)
|
||||
|
||||
result = notification_page_tags.related_status(notification)
|
||||
self.assertIsInstance(result, models.Status)
|
80
bookwyrm/tests/templatetags/test_rating_tags.py
Normal file
80
bookwyrm/tests/templatetags/test_rating_tags.py
Normal file
@ -0,0 +1,80 @@
|
||||
""" Gettings book ratings """
|
||||
from unittest.mock import patch
|
||||
|
||||
from django.test import TestCase
|
||||
|
||||
from bookwyrm import models
|
||||
from bookwyrm.templatetags import rating_tags
|
||||
|
||||
|
||||
@patch("bookwyrm.activitystreams.add_status_task.delay")
|
||||
@patch("bookwyrm.activitystreams.remove_status_task.delay")
|
||||
class RatingTags(TestCase):
|
||||
"""lotta different things here"""
|
||||
|
||||
def setUp(self):
|
||||
"""create some filler objects"""
|
||||
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(
|
||||
"mouse@example.com",
|
||||
"mouse@mouse.mouse",
|
||||
"mouseword",
|
||||
local=True,
|
||||
localname="mouse",
|
||||
)
|
||||
with patch("bookwyrm.models.user.set_remote_server.delay"):
|
||||
self.remote_user = models.User.objects.create_user(
|
||||
"rat",
|
||||
"rat@rat.rat",
|
||||
"ratword",
|
||||
remote_id="http://example.com/rat",
|
||||
local=False,
|
||||
)
|
||||
work = models.Work.objects.create(title="Work title")
|
||||
self.book = models.Edition.objects.create(
|
||||
title="Test Book",
|
||||
parent_work=work,
|
||||
)
|
||||
|
||||
@patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async")
|
||||
def test_get_rating(self, *_):
|
||||
"""privacy filtered rating"""
|
||||
# follows-only: not included
|
||||
models.ReviewRating.objects.create(
|
||||
user=self.remote_user,
|
||||
rating=5,
|
||||
book=self.book,
|
||||
privacy="followers",
|
||||
)
|
||||
self.assertEqual(rating_tags.get_rating(self.book, self.local_user), 0)
|
||||
|
||||
# public: included
|
||||
models.ReviewRating.objects.create(
|
||||
user=self.remote_user,
|
||||
rating=5,
|
||||
book=self.book,
|
||||
privacy="public",
|
||||
)
|
||||
self.assertEqual(rating_tags.get_rating(self.book, self.local_user), 5)
|
||||
|
||||
# rating unset: not included
|
||||
models.Review.objects.create(
|
||||
name="blah",
|
||||
user=self.local_user,
|
||||
rating=0,
|
||||
book=self.book,
|
||||
privacy="public",
|
||||
)
|
||||
self.assertEqual(rating_tags.get_rating(self.book, self.local_user), 5)
|
||||
|
||||
def test_get_user_rating(self, *_):
|
||||
"""get a user's most recent rating of a book"""
|
||||
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"):
|
||||
models.Review.objects.create(user=self.local_user, book=self.book, rating=3)
|
||||
self.assertEqual(rating_tags.get_user_rating(self.book, self.local_user), 3)
|
||||
|
||||
def test_get_user_rating_doesnt_exist(self, *_):
|
||||
"""there is no rating available"""
|
||||
self.assertEqual(rating_tags.get_user_rating(self.book, self.local_user), 0)
|
70
bookwyrm/tests/templatetags/test_shelf_tags.py
Normal file
70
bookwyrm/tests/templatetags/test_shelf_tags.py
Normal file
@ -0,0 +1,70 @@
|
||||
""" style fixes and lookups for templates """
|
||||
from unittest.mock import patch
|
||||
|
||||
from django.test import TestCase
|
||||
from django.test.client import RequestFactory
|
||||
|
||||
from bookwyrm import models
|
||||
from bookwyrm.templatetags import shelf_tags
|
||||
|
||||
|
||||
@patch("bookwyrm.activitystreams.add_status_task.delay")
|
||||
@patch("bookwyrm.activitystreams.remove_status_task.delay")
|
||||
@patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async")
|
||||
@patch("bookwyrm.activitystreams.add_book_statuses_task.delay")
|
||||
class ShelfTags(TestCase):
|
||||
"""lotta different things here"""
|
||||
|
||||
def setUp(self):
|
||||
"""create some filler objects"""
|
||||
self.factory = RequestFactory()
|
||||
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(
|
||||
"mouse@example.com",
|
||||
"mouse@mouse.mouse",
|
||||
"mouseword",
|
||||
local=True,
|
||||
localname="mouse",
|
||||
)
|
||||
with patch("bookwyrm.models.user.set_remote_server.delay"):
|
||||
self.remote_user = models.User.objects.create_user(
|
||||
"rat",
|
||||
"rat@rat.rat",
|
||||
"ratword",
|
||||
remote_id="http://example.com/rat",
|
||||
local=False,
|
||||
)
|
||||
self.book = models.Edition.objects.create(
|
||||
title="Test Book",
|
||||
parent_work=models.Work.objects.create(title="Test work"),
|
||||
)
|
||||
|
||||
def test_get_is_book_on_shelf(self, *_):
|
||||
"""check if a book is on a shelf"""
|
||||
shelf = self.local_user.shelf_set.first()
|
||||
self.assertFalse(shelf_tags.get_is_book_on_shelf(self.book, shelf))
|
||||
models.ShelfBook.objects.create(
|
||||
shelf=shelf, book=self.book, user=self.local_user
|
||||
)
|
||||
self.assertTrue(shelf_tags.get_is_book_on_shelf(self.book, shelf))
|
||||
|
||||
def test_get_next_shelf(self, *_):
|
||||
"""self progress helper"""
|
||||
self.assertEqual(shelf_tags.get_next_shelf("to-read"), "reading")
|
||||
self.assertEqual(shelf_tags.get_next_shelf("reading"), "read")
|
||||
self.assertEqual(shelf_tags.get_next_shelf("read"), "complete")
|
||||
self.assertEqual(shelf_tags.get_next_shelf("blooooga"), "to-read")
|
||||
|
||||
def test_active_shelf(self, *_):
|
||||
"""get the shelf a book is on"""
|
||||
shelf = self.local_user.shelf_set.first()
|
||||
request = self.factory.get("")
|
||||
request.user = self.local_user
|
||||
context = {"request": request}
|
||||
self.assertIsInstance(shelf_tags.active_shelf(context, self.book), dict)
|
||||
models.ShelfBook.objects.create(
|
||||
shelf=shelf, book=self.book, user=self.local_user
|
||||
)
|
||||
self.assertEqual(shelf_tags.active_shelf(context, self.book).shelf, shelf)
|
@ -1,4 +1,5 @@
|
||||
""" style fixes and lookups for templates """
|
||||
from datetime import datetime
|
||||
from unittest.mock import patch
|
||||
|
||||
from django.test import TestCase
|
||||
@ -35,6 +36,12 @@ class StatusDisplayTags(TestCase):
|
||||
)
|
||||
self.book = models.Edition.objects.create(title="Test Book")
|
||||
|
||||
def test_get_mentions(self, *_):
|
||||
"""list of people mentioned"""
|
||||
status = models.Status.objects.create(content="hi", user=self.remote_user)
|
||||
result = status_display.get_mentions(status, self.user)
|
||||
self.assertEqual(result, "@rat@example.com ")
|
||||
|
||||
@patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async")
|
||||
def test_get_replies(self, *_):
|
||||
"""direct replies to a status"""
|
||||
@ -83,8 +90,16 @@ class StatusDisplayTags(TestCase):
|
||||
self.assertIsInstance(boosted, models.Review)
|
||||
self.assertEqual(boosted, status)
|
||||
|
||||
def test_get_mentions(self, *_):
|
||||
"""list of people mentioned"""
|
||||
status = models.Status.objects.create(content="hi", user=self.remote_user)
|
||||
result = status_display.get_mentions(status, self.user)
|
||||
self.assertEqual(result, "@rat@example.com ")
|
||||
def test_get_published_date(self, *_):
|
||||
"""date formatting"""
|
||||
date = datetime(2020, 1, 1, 0, 0, tzinfo=timezone.utc)
|
||||
with patch("django.utils.timezone.now") as timezone_mock:
|
||||
timezone_mock.return_value = datetime(2022, 1, 1, 0, 0, tzinfo=timezone.utc)
|
||||
result = status_display.get_published_date(date)
|
||||
self.assertEqual(result, "Jan. 1, 2020")
|
||||
|
||||
date = datetime(2022, 1, 1, 0, 0, tzinfo=timezone.utc)
|
||||
with patch("django.utils.timezone.now") as timezone_mock:
|
||||
timezone_mock.return_value = datetime(2022, 1, 8, 0, 0, tzinfo=timezone.utc)
|
||||
result = status_display.get_published_date(date)
|
||||
self.assertEqual(result, "Jan 1")
|
||||
|
@ -35,6 +35,15 @@ class UtilitiesTags(TestCase):
|
||||
)
|
||||
self.book = models.Edition.objects.create(title="Test Book")
|
||||
|
||||
def test_get_uuid(self, *_):
|
||||
"""uuid functionality"""
|
||||
uuid = utilities.get_uuid("hi")
|
||||
self.assertTrue(re.match(r"hi[A-Za-z0-9\-]", uuid))
|
||||
|
||||
def test_join(self, *_):
|
||||
"""concats things with underscores"""
|
||||
self.assertEqual(utilities.join("hi", 5, "blah", 0.75), "hi_5_blah_0.75")
|
||||
|
||||
def test_get_user_identifer_local(self, *_):
|
||||
"""fall back to the simplest uid available"""
|
||||
self.assertNotEqual(self.user.username, self.user.localname)
|
||||
@ -46,11 +55,6 @@ class UtilitiesTags(TestCase):
|
||||
utilities.get_user_identifier(self.remote_user), "rat@example.com"
|
||||
)
|
||||
|
||||
def test_get_uuid(self, *_):
|
||||
"""uuid functionality"""
|
||||
uuid = utilities.get_uuid("hi")
|
||||
self.assertTrue(re.match(r"hi[A-Za-z0-9\-]", uuid))
|
||||
|
||||
def test_get_title(self, *_):
|
||||
"""the title of a book"""
|
||||
self.assertEqual(utilities.get_title(None), "")
|
||||
|
Reference in New Issue
Block a user