Updates shelf models tests

This commit is contained in:
Mouse Reeve 2021-04-08 20:58:15 -07:00
parent c149003d89
commit 14b0b978a3

View file

@ -1,4 +1,6 @@
""" testing models """ """ testing models """
import json
from unittest.mock import patch
from django.test import TestCase from django.test import TestCase
from bookwyrm import models, settings from bookwyrm import models, settings
@ -18,30 +20,19 @@ class Shelf(TestCase):
def test_remote_id(self): def test_remote_id(self):
""" shelves use custom remote ids """ """ shelves use custom remote ids """
real_broadcast = models.Shelf.broadcast with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
shelf = models.Shelf.objects.create(
def broadcast_mock(_, activity, user, **kwargs): name="Test Shelf", identifier="test-shelf", user=self.local_user
""" nah """ )
models.Shelf.broadcast = broadcast_mock
shelf = models.Shelf.objects.create(
name="Test Shelf", identifier="test-shelf", user=self.local_user
)
expected_id = "https://%s/user/mouse/books/test-shelf" % settings.DOMAIN expected_id = "https://%s/user/mouse/books/test-shelf" % settings.DOMAIN
self.assertEqual(shelf.get_remote_id(), expected_id) self.assertEqual(shelf.get_remote_id(), expected_id)
models.Shelf.broadcast = real_broadcast
def test_to_activity(self): def test_to_activity(self):
""" jsonify it """ """ jsonify it """
real_broadcast = models.Shelf.broadcast with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
shelf = models.Shelf.objects.create(
def empty_mock(_, activity, user, **kwargs): name="Test Shelf", identifier="test-shelf", user=self.local_user
""" nah """ )
models.Shelf.broadcast = empty_mock
shelf = models.Shelf.objects.create(
name="Test Shelf", identifier="test-shelf", user=self.local_user
)
activity_json = shelf.to_activity() activity_json = shelf.to_activity()
self.assertIsInstance(activity_json, dict) self.assertIsInstance(activity_json, dict)
self.assertEqual(activity_json["id"], shelf.remote_id) self.assertEqual(activity_json["id"], shelf.remote_id)
@ -49,77 +40,53 @@ class Shelf(TestCase):
self.assertEqual(activity_json["type"], "Shelf") self.assertEqual(activity_json["type"], "Shelf")
self.assertEqual(activity_json["name"], "Test Shelf") self.assertEqual(activity_json["name"], "Test Shelf")
self.assertEqual(activity_json["owner"], self.local_user.remote_id) self.assertEqual(activity_json["owner"], self.local_user.remote_id)
models.Shelf.broadcast = real_broadcast
def test_create_update_shelf(self): def test_create_update_shelf(self):
""" create and broadcast shelf creation """ """ create and broadcast shelf creation """
real_broadcast = models.Shelf.broadcast
def create_mock(_, activity, user, **kwargs): with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay") as mock:
""" ok """ shelf = models.Shelf.objects.create(
self.assertEqual(user.remote_id, self.local_user.remote_id) name="Test Shelf", identifier="test-shelf", user=self.local_user
self.assertEqual(activity["type"], "Create") )
self.assertEqual(activity["actor"], self.local_user.remote_id) activity = json.loads(mock.call_args[0][1])
self.assertEqual(activity["object"]["name"], "Test Shelf") self.assertEqual(activity["type"], "Create")
self.assertEqual(activity["actor"], self.local_user.remote_id)
models.Shelf.broadcast = create_mock self.assertEqual(activity["object"]["name"], "Test Shelf")
shelf = models.Shelf.objects.create(
name="Test Shelf", identifier="test-shelf", user=self.local_user
)
def update_mock(_, activity, user, **kwargs):
""" ok """
self.assertEqual(user.remote_id, self.local_user.remote_id)
self.assertEqual(activity["type"], "Update")
self.assertEqual(activity["actor"], self.local_user.remote_id)
self.assertEqual(activity["object"]["name"], "arthur russel")
models.Shelf.broadcast = update_mock
shelf.name = "arthur russel" shelf.name = "arthur russel"
shelf.save() 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")
self.assertEqual(shelf.name, "arthur russel") self.assertEqual(shelf.name, "arthur russel")
models.Shelf.broadcast = real_broadcast
def test_shelve(self): def test_shelve(self):
""" create and broadcast shelf creation """ """ create and broadcast shelf creation """
real_broadcast = models.Shelf.broadcast with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
real_shelfbook_broadcast = models.ShelfBook.broadcast shelf = models.Shelf.objects.create(
name="Test Shelf", identifier="test-shelf", user=self.local_user
)
def add_mock(_, activity, user, **kwargs): with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay") as mock:
""" ok """ shelf_book = models.ShelfBook.objects.create(
self.assertEqual(user.remote_id, self.local_user.remote_id) shelf=shelf, user=self.local_user, book=self.book
self.assertEqual(activity["type"], "Add") )
self.assertEqual(activity["actor"], self.local_user.remote_id) self.assertEqual(mock.call_count, 1)
self.assertEqual(activity["object"]["id"], self.book.remote_id) activity = json.loads(mock.call_args[0][1])
self.assertEqual(activity["target"], shelf.remote_id) self.assertEqual(activity["type"], "Add")
self.assertEqual(activity["actor"], self.local_user.remote_id)
def remove_mock(_, activity, user, **kwargs): self.assertEqual(activity["object"]["id"], shelf_book.remote_id)
""" ok """ self.assertEqual(activity["target"], shelf.remote_id)
self.assertEqual(user.remote_id, self.local_user.remote_id)
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)
def empty_mock(_, activity, user, **kwargs):
""" nah """
models.Shelf.broadcast = empty_mock
shelf = models.Shelf.objects.create(
name="Test Shelf", identifier="test-shelf", user=self.local_user
)
models.ShelfBook.broadcast = add_mock
shelf_book = models.ShelfBook.objects.create(
shelf=shelf, user=self.local_user, book=self.book
)
self.assertEqual(shelf.books.first(), self.book) self.assertEqual(shelf.books.first(), self.book)
models.ShelfBook.broadcast = remove_mock with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay") as mock:
shelf_book.delete() 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)
self.assertFalse(shelf.books.exists()) self.assertFalse(shelf.books.exists())
models.ShelfBook.broadcast = real_shelfbook_broadcast
models.Shelf.broadcast = real_broadcast