moviewyrm/bookwyrm/tests/models/test_shelf_model.py

93 lines
4.1 KiB
Python
Raw Normal View History

2021-03-08 16:49:10 +00:00
""" testing models """
2021-04-09 03:58:15 +00:00
import json
from unittest.mock import patch
2020-12-14 18:25:43 +00:00
from django.test import TestCase
from bookwyrm import models, settings
2021-03-08 16:49:10 +00:00
# pylint: disable=unused-argument
2020-12-14 18:25:43 +00:00
class Shelf(TestCase):
2021-03-08 16:49:10 +00:00
""" some activitypub oddness ahead """
2020-12-14 18:25:43 +00:00
def setUp(self):
2021-03-08 16:49:10 +00:00
""" look, a shelf """
2021-02-07 05:00:08 +00:00
self.local_user = models.User.objects.create_user(
2021-03-08 16:49:10 +00:00
"mouse", "mouse@mouse.mouse", "mouseword", local=True, localname="mouse"
)
work = models.Work.objects.create(title="Test Work")
self.book = models.Edition.objects.create(title="test book", parent_work=work)
2020-12-14 18:25:43 +00:00
def test_remote_id(self):
2021-03-08 16:49:10 +00:00
""" shelves use custom remote ids """
2021-04-09 03:58:15 +00:00
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
shelf = models.Shelf.objects.create(
name="Test Shelf", identifier="test-shelf", user=self.local_user
)
2021-03-31 17:24:23 +00:00
expected_id = "https://%s/user/mouse/books/test-shelf" % settings.DOMAIN
2021-02-07 05:00:08 +00:00
self.assertEqual(shelf.get_remote_id(), expected_id)
2020-12-14 18:25:43 +00:00
def test_to_activity(self):
2021-03-08 16:49:10 +00:00
""" jsonify it """
2021-04-09 03:58:15 +00:00
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
shelf = models.Shelf.objects.create(
name="Test Shelf", identifier="test-shelf", user=self.local_user
)
2021-02-07 05:00:08 +00:00
activity_json = shelf.to_activity()
2020-12-14 18:25:43 +00:00
self.assertIsInstance(activity_json, dict)
2021-03-08 16:49:10 +00:00
self.assertEqual(activity_json["id"], shelf.remote_id)
self.assertEqual(activity_json["totalItems"], 0)
self.assertEqual(activity_json["type"], "Shelf")
self.assertEqual(activity_json["name"], "Test Shelf")
self.assertEqual(activity_json["owner"], self.local_user.remote_id)
2021-02-07 05:00:08 +00:00
def test_create_update_shelf(self):
2021-03-08 16:49:10 +00:00
""" create and broadcast shelf creation """
2021-02-07 05:00:08 +00:00
2021-04-09 03:58:15 +00:00
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay") as mock:
shelf = models.Shelf.objects.create(
name="Test Shelf", identifier="test-shelf", user=self.local_user
)
activity = json.loads(mock.call_args[0][1])
self.assertEqual(activity["type"], "Create")
self.assertEqual(activity["actor"], self.local_user.remote_id)
self.assertEqual(activity["object"]["name"], "Test Shelf")
2021-02-07 05:00:08 +00:00
2021-03-08 16:49:10 +00:00
shelf.name = "arthur russel"
2021-04-09 03:58:15 +00:00
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay") as mock:
shelf.save()
activity = json.loads(mock.call_args[0][1])
self.assertEqual(activity["type"], "Update")
self.assertEqual(activity["actor"], self.local_user.remote_id)
self.assertEqual(activity["object"]["name"], "arthur russel")
2021-03-08 16:49:10 +00:00
self.assertEqual(shelf.name, "arthur russel")
2021-02-07 05:00:08 +00:00
def test_shelve(self):
2021-03-08 16:49:10 +00:00
""" create and broadcast shelf creation """
2021-04-09 03:58:15 +00:00
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
shelf = models.Shelf.objects.create(
name="Test Shelf", identifier="test-shelf", user=self.local_user
)
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay") as mock:
shelf_book = models.ShelfBook.objects.create(
shelf=shelf, user=self.local_user, book=self.book
)
self.assertEqual(mock.call_count, 1)
activity = json.loads(mock.call_args[0][1])
self.assertEqual(activity["type"], "Add")
self.assertEqual(activity["actor"], self.local_user.remote_id)
self.assertEqual(activity["object"]["id"], shelf_book.remote_id)
self.assertEqual(activity["target"], shelf.remote_id)
2021-02-07 05:00:08 +00:00
self.assertEqual(shelf.books.first(), self.book)
2021-04-09 03:58:15 +00:00
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay") as mock:
shelf_book.delete()
self.assertEqual(mock.call_count, 1)
activity = json.loads(mock.call_args[0][1])
self.assertEqual(activity["type"], "Remove")
self.assertEqual(activity["actor"], self.local_user.remote_id)
self.assertEqual(activity["object"]["id"], shelf_book.remote_id)
self.assertEqual(activity["target"], shelf.remote_id)
2021-02-07 05:00:08 +00:00
self.assertFalse(shelf.books.exists())