moviewyrm/bookwyrm/tests/templatetags/test_bookwyrm_tags.py

102 lines
4.2 KiB
Python
Raw Normal View History

2021-03-08 16:49:10 +00:00
""" style fixes and lookups for templates """
from unittest.mock import patch
from django.test import TestCase
from bookwyrm import models
2021-12-29 20:42:02 +00:00
from bookwyrm.templatetags import bookwyrm_tags
2021-09-06 20:53:49 +00:00
@patch("bookwyrm.activitystreams.add_status_task.delay")
2021-09-06 23:59:58 +00:00
@patch("bookwyrm.activitystreams.remove_status_task.delay")
2021-12-29 20:42:02 +00:00
class BookWyrmTags(TestCase):
2021-04-26 16:15:42 +00:00
"""lotta different things here"""
2021-03-08 16:49:10 +00:00
def setUp(self):
2021-04-26 16:15:42 +00:00
"""create some filler objects"""
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
"bookwyrm.activitystreams.populate_stream_task.delay"
):
self.user = models.User.objects.create_user(
"mouse@example.com",
"mouse@mouse.mouse",
"mouseword",
local=True,
localname="mouse",
)
2021-08-02 23:05:40 +00:00
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,
2021-05-26 21:57:29 +00:00
)
2021-08-02 23:05:40 +00:00
self.book = models.Edition.objects.create(title="Test Book")
2021-06-14 23:39:54 +00:00
def test_get_user_rating(self, *_):
2021-04-26 16:15:42 +00:00
"""get a user's most recent rating of a book"""
2021-11-12 17:17:00 +00:00
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"):
2021-08-02 23:05:40 +00:00
models.Review.objects.create(user=self.user, book=self.book, rating=3)
2021-03-08 16:49:10 +00:00
self.assertEqual(bookwyrm_tags.get_user_rating(self.book, self.user), 3)
2021-06-14 23:39:54 +00:00
def test_get_user_rating_doesnt_exist(self, *_):
2021-04-26 16:15:42 +00:00
"""there is no rating available"""
2021-03-08 16:49:10 +00:00
self.assertEqual(bookwyrm_tags.get_user_rating(self.book, self.user), 0)
2021-06-14 23:39:54 +00:00
def test_get_book_description(self, *_):
2021-04-26 16:15:42 +00:00
"""grab it from the edition or the parent"""
2021-08-02 23:05:40 +00:00
work = models.Work.objects.create(title="Test Work")
self.book.parent_work = work
self.book.save()
2021-08-02 23:05:40 +00:00
self.assertIsNone(bookwyrm_tags.get_book_description(self.book))
2021-08-02 23:05:40 +00:00
work.description = "hi"
work.save()
self.assertEqual(bookwyrm_tags.get_book_description(self.book), "hi")
2021-08-02 23:05:40 +00:00
self.book.description = "hello"
self.book.save()
self.assertEqual(bookwyrm_tags.get_book_description(self.book), "hello")
2021-12-29 21:03:23 +00:00
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)
2021-06-14 23:39:54 +00:00
def test_related_status(self, *_):
2021-04-26 16:15:42 +00:00
"""gets the subclass model for a notification status"""
2021-11-12 17:17:00 +00:00
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"):
2021-03-08 16:49:10 +00:00
status = models.Status.objects.create(content="hi", user=self.user)
2021-01-14 03:28:44 +00:00
notification = models.Notification.objects.create(
2021-03-08 16:49:10 +00:00
user=self.user, notification_type="MENTION", related_status=status
)
2021-01-14 03:28:44 +00:00
result = bookwyrm_tags.related_status(notification)
self.assertIsInstance(result, models.Status)