Track changed fields in activity to model code

This commit is contained in:
Mouse Reeve
2021-08-03 08:48:15 -07:00
parent ee7bdc956a
commit a84a744e8d
8 changed files with 91 additions and 66 deletions

View File

@ -1,6 +1,4 @@
import datetime
from unittest.mock import patch
"""test author serializer"""
from django.test import TestCase
from bookwyrm import models

View File

@ -20,16 +20,18 @@ from bookwyrm import models
@patch("bookwyrm.activitystreams.ActivityStream.add_status")
@patch("bookwyrm.suggested_users.rerank_user_task.delay")
class BaseActivity(TestCase):
"""the super class for model-linked activitypub dataclasses"""
def setUp(self):
"""we're probably going to re-use this so why copy/paste"""
self.user = models.User.objects.create_user(
"mouse", "mouse@mouse.mouse", "mouseword", local=True, localname="mouse"
)
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"):
self.user = models.User.objects.create_user(
"mouse", "mouse@mouse.mouse", "mouseword", local=True, localname="mouse"
)
self.user.remote_id = "http://example.com/a/b"
self.user.save(broadcast=False)
self.user.save(broadcast=False, update_fields=["remote_id"])
datafile = pathlib.Path(__file__).parent.joinpath("../data/ap_user.json")
self.userdata = json.loads(datafile.read_bytes())
@ -44,24 +46,24 @@ class BaseActivity(TestCase):
image.save(output, format=image.format)
self.image_data = output.getvalue()
def test_init(self, _):
def test_init(self, *_):
"""simple successfuly init"""
instance = ActivityObject(id="a", type="b")
self.assertTrue(hasattr(instance, "id"))
self.assertTrue(hasattr(instance, "type"))
def test_init_missing(self, _):
def test_init_missing(self, *_):
"""init with missing required params"""
with self.assertRaises(ActivitySerializerError):
ActivityObject()
def test_init_extra_fields(self, _):
def test_init_extra_fields(self, *_):
"""init ignoring additional fields"""
instance = ActivityObject(id="a", type="b", fish="c")
self.assertTrue(hasattr(instance, "id"))
self.assertTrue(hasattr(instance, "type"))
def test_init_default_field(self, _):
def test_init_default_field(self, *_):
"""replace an existing required field with a default field"""
@dataclass(init=False)
@ -74,7 +76,7 @@ class BaseActivity(TestCase):
self.assertEqual(instance.id, "a")
self.assertEqual(instance.type, "TestObject")
def test_serialize(self, _):
def test_serialize(self, *_):
"""simple function for converting dataclass to dict"""
instance = ActivityObject(id="a", type="b")
serialized = instance.serialize()
@ -83,7 +85,7 @@ class BaseActivity(TestCase):
self.assertEqual(serialized["type"], "b")
@responses.activate
def test_resolve_remote_id(self, _):
def test_resolve_remote_id(self, *_):
"""look up or load remote data"""
# existing item
result = resolve_remote_id("http://example.com/a/b", model=models.User)
@ -105,14 +107,14 @@ class BaseActivity(TestCase):
self.assertEqual(result.remote_id, "https://example.com/user/mouse")
self.assertEqual(result.name, "MOUSE?? MOUSE!!")
def test_to_model_invalid_model(self, _):
def test_to_model_invalid_model(self, *_):
"""catch mismatch between activity type and model type"""
instance = ActivityObject(id="a", type="b")
with self.assertRaises(ActivitySerializerError):
instance.to_model(model=models.User)
@responses.activate
def test_to_model_image(self, _):
def test_to_model_image(self, *_):
"""update an image field"""
activity = activitypub.Person(
id=self.user.remote_id,
@ -145,7 +147,7 @@ class BaseActivity(TestCase):
self.assertEqual(self.user.name, "New Name")
self.assertEqual(self.user.key_pair.public_key, "hi")
def test_to_model_many_to_many(self, _):
def test_to_model_many_to_many(self, *_):
"""annoying that these all need special handling"""
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
status = models.Status.objects.create(
@ -176,7 +178,7 @@ class BaseActivity(TestCase):
self.assertEqual(status.mention_books.first(), book)
@responses.activate
def test_to_model_one_to_many(self, _):
def test_to_model_one_to_many(self, *_):
"""these are reversed relationships, where the secondary object
keys the primary object but not vice versa"""
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
@ -215,7 +217,7 @@ class BaseActivity(TestCase):
self.assertIsNone(status.attachments.first())
@responses.activate
def test_set_related_field(self, _):
def test_set_related_field(self, *_):
"""celery task to add back-references to created objects"""
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
status = models.Status.objects.create(