moviewyrm/bookwyrm/tests/models/test_shelf_model.py

126 lines
4.8 KiB
Python
Raw Normal View History

2021-03-08 16:49:10 +00:00
""" testing models """
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-02-08 17:38:28 +00:00
real_broadcast = models.Shelf.broadcast
2021-03-08 16:49:10 +00:00
2021-02-09 20:48:46 +00:00
def broadcast_mock(_, activity, user, **kwargs):
2021-03-08 16:49:10 +00:00
""" nah """
2021-02-09 20:48:46 +00:00
models.Shelf.broadcast = broadcast_mock
2021-02-07 05:00:08 +00:00
shelf = models.Shelf.objects.create(
2021-03-08 16:49:10 +00:00
name="Test Shelf", identifier="test-shelf", user=self.local_user
)
expected_id = "https://%s/user/mouse/shelf/test-shelf" % settings.DOMAIN
2021-02-07 05:00:08 +00:00
self.assertEqual(shelf.get_remote_id(), expected_id)
2021-02-08 17:38:28 +00:00
models.Shelf.broadcast = real_broadcast
2020-12-14 18:25:43 +00:00
def test_to_activity(self):
2021-03-08 16:49:10 +00:00
""" jsonify it """
2021-02-08 17:38:28 +00:00
real_broadcast = models.Shelf.broadcast
2021-03-08 16:49:10 +00:00
2021-02-09 20:48:46 +00:00
def empty_mock(_, activity, user, **kwargs):
2021-03-08 16:49:10 +00:00
""" nah """
2021-02-09 20:48:46 +00:00
models.Shelf.broadcast = empty_mock
2021-02-07 05:00:08 +00:00
shelf = models.Shelf.objects.create(
2021-03-08 16:49:10 +00:00
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-08 17:38:28 +00:00
models.Shelf.broadcast = real_broadcast
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-08 17:38:28 +00:00
real_broadcast = models.Shelf.broadcast
2021-03-08 16:49:10 +00:00
2021-02-09 20:48:46 +00:00
def create_mock(_, activity, user, **kwargs):
2021-03-08 16:49:10 +00:00
""" ok """
2021-02-07 05:00:08 +00:00
self.assertEqual(user.remote_id, self.local_user.remote_id)
2021-03-08 16:49:10 +00:00
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
models.Shelf.broadcast = create_mock
shelf = models.Shelf.objects.create(
2021-03-08 16:49:10 +00:00
name="Test Shelf", identifier="test-shelf", user=self.local_user
)
2021-02-07 05:00:08 +00:00
2021-02-09 20:48:46 +00:00
def update_mock(_, activity, user, **kwargs):
2021-03-08 16:49:10 +00:00
""" ok """
2021-02-07 05:00:08 +00:00
self.assertEqual(user.remote_id, self.local_user.remote_id)
2021-03-08 16:49:10 +00:00
self.assertEqual(activity["type"], "Update")
self.assertEqual(activity["actor"], self.local_user.remote_id)
self.assertEqual(activity["object"]["name"], "arthur russel")
2021-02-07 05:00:08 +00:00
models.Shelf.broadcast = update_mock
2021-03-08 16:49:10 +00:00
shelf.name = "arthur russel"
2021-02-07 05:00:08 +00:00
shelf.save()
2021-03-08 16:49:10 +00:00
self.assertEqual(shelf.name, "arthur russel")
2021-02-08 17:38:28 +00:00
models.Shelf.broadcast = real_broadcast
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-02-08 17:38:28 +00:00
real_broadcast = models.Shelf.broadcast
real_shelfbook_broadcast = models.ShelfBook.broadcast
2021-02-09 20:48:46 +00:00
def add_mock(_, activity, user, **kwargs):
2021-03-08 16:49:10 +00:00
""" ok """
2021-02-07 05:00:08 +00:00
self.assertEqual(user.remote_id, self.local_user.remote_id)
2021-03-08 16:49:10 +00:00
self.assertEqual(activity["type"], "Add")
self.assertEqual(activity["actor"], self.local_user.remote_id)
self.assertEqual(activity["object"]["id"], self.book.remote_id)
self.assertEqual(activity["target"], shelf.remote_id)
2021-02-07 05:00:08 +00:00
2021-02-09 20:48:46 +00:00
def remove_mock(_, activity, user, **kwargs):
2021-03-08 16:49:10 +00:00
""" ok """
2021-02-07 05:00:08 +00:00
self.assertEqual(user.remote_id, self.local_user.remote_id)
2021-03-08 16:49:10 +00:00
self.assertEqual(activity["type"], "Remove")
self.assertEqual(activity["actor"], self.local_user.remote_id)
self.assertEqual(activity["object"]["id"], self.book.remote_id)
self.assertEqual(activity["target"], shelf.remote_id)
2021-02-07 05:00:08 +00:00
2021-02-09 20:48:46 +00:00
def empty_mock(_, activity, user, **kwargs):
2021-03-08 16:49:10 +00:00
""" nah """
2021-02-09 20:48:46 +00:00
models.Shelf.broadcast = empty_mock
2021-02-07 05:00:08 +00:00
shelf = models.Shelf.objects.create(
2021-03-08 16:49:10 +00:00
name="Test Shelf", identifier="test-shelf", user=self.local_user
)
2021-02-07 05:00:08 +00:00
models.ShelfBook.broadcast = add_mock
shelf_book = models.ShelfBook.objects.create(
2021-03-08 16:49:10 +00:00
shelf=shelf, user=self.local_user, book=self.book
)
2021-02-07 05:00:08 +00:00
self.assertEqual(shelf.books.first(), self.book)
models.ShelfBook.broadcast = remove_mock
shelf_book.delete()
self.assertFalse(shelf.books.exists())
2021-02-08 17:38:28 +00:00
models.ShelfBook.broadcast = real_shelfbook_broadcast
models.Shelf.broadcast = real_broadcast