From ffd0759f6fd48a13c01dfa715120612800e27bfa Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Sat, 6 Feb 2021 16:13:59 -0800 Subject: [PATCH] Fixes field and lists tests --- bookwyrm/models/base_model.py | 5 ++++- bookwyrm/models/readthrough.py | 2 +- bookwyrm/tests/models/test_fields.py | 18 ++++++++++++------ bookwyrm/tests/models/test_list.py | 17 ++++++++++------- .../tests/models/test_relationship_models.py | 2 +- 5 files changed, 28 insertions(+), 16 deletions(-) diff --git a/bookwyrm/models/base_model.py b/bookwyrm/models/base_model.py index 9c0865df..06546c9b 100644 --- a/bookwyrm/models/base_model.py +++ b/bookwyrm/models/base_model.py @@ -38,4 +38,7 @@ def execute_after_save(sender, instance, created, *args, **kwargs): return if not instance.remote_id: instance.remote_id = instance.get_remote_id() - instance.save(broadcast=False) + try: + instance.save(broadcast=False) + except TypeError: + instance.save() diff --git a/bookwyrm/models/readthrough.py b/bookwyrm/models/readthrough.py index 2a3dd9f4..2bec3a81 100644 --- a/bookwyrm/models/readthrough.py +++ b/bookwyrm/models/readthrough.py @@ -54,5 +54,5 @@ class ProgressUpdate(BookWyrmModel): def save(self, *args, **kwargs): ''' update user active time ''' self.user.last_active_date = timezone.now() - self.user.save() + self.user.save(broadcast=False) super().save(*args, **kwargs) diff --git a/bookwyrm/tests/models/test_fields.py b/bookwyrm/tests/models/test_fields.py index ff8c800a..38de9c98 100644 --- a/bookwyrm/tests/models/test_fields.py +++ b/bookwyrm/tests/models/test_fields.py @@ -178,7 +178,8 @@ class ActivitypubFields(TestCase): self.assertEqual(model_instance.privacy_field, 'unlisted') - def test_privacy_field_set_activity_from_field(self): + @patch('bookwyrm.models.activitypub_mixin.ObjectMixin.broadcast') + def test_privacy_field_set_activity_from_field(self, _): ''' translate between to/cc fields and privacy ''' user = User.objects.create_user( 'rat', 'rat@rat.rat', 'ratword', @@ -195,13 +196,15 @@ class ActivitypubFields(TestCase): self.assertEqual(activity['to'], [public]) self.assertEqual(activity['cc'], [followers]) - model_instance = Status.objects.create(user=user, privacy='unlisted') + model_instance = Status.objects.create( + user=user, content='hi', privacy='unlisted') activity = {} instance.set_activity_from_field(activity, model_instance) self.assertEqual(activity['to'], [followers]) self.assertEqual(activity['cc'], [public]) - model_instance = Status.objects.create(user=user, privacy='followers') + model_instance = Status.objects.create( + user=user, content='hi', privacy='followers') activity = {} instance.set_activity_from_field(activity, model_instance) self.assertEqual(activity['to'], [followers]) @@ -209,6 +212,7 @@ class ActivitypubFields(TestCase): model_instance = Status.objects.create( user=user, + content='hi', privacy='direct', ) model_instance.mention_users.set([user]) @@ -290,12 +294,13 @@ class ActivitypubFields(TestCase): 'mouse', 'mouse@mouse.mouse', 'mouseword', local=True, localname='mouse') user.remote_id = 'https://example.com/user/mouse' - user.save() + user.save(broadcast=False) User.objects.create_user( 'rat', 'rat@rat.rat', 'ratword', local=True, localname='rat') - value = instance.field_from_activity(userdata) + with patch('bookwyrm.models.activitypub_mixin.ObjectMixin.broadcast'): + value = instance.field_from_activity(userdata) self.assertEqual(value, user) @@ -394,7 +399,8 @@ class ActivitypubFields(TestCase): @responses.activate - def test_image_field(self): + @patch('bookwyrm.models.activitypub_mixin.ObjectMixin.broadcast') + def test_image_field(self, _): ''' storing images ''' user = User.objects.create_user( 'mouse', 'mouse@mouse.mouse', 'mouseword', diff --git a/bookwyrm/tests/models/test_list.py b/bookwyrm/tests/models/test_list.py index b53ee380..d41b81c7 100644 --- a/bookwyrm/tests/models/test_list.py +++ b/bookwyrm/tests/models/test_list.py @@ -1,9 +1,11 @@ ''' testing models ''' +from unittest.mock import patch from django.test import TestCase from bookwyrm import models, settings +@patch('bookwyrm.models.activitypub_mixin.broadcast_task.delay') class List(TestCase): ''' some activitypub oddness ahead ''' def setUp(self): @@ -11,17 +13,18 @@ class List(TestCase): self.user = models.User.objects.create_user( 'mouse', 'mouse@mouse.mouse', 'mouseword', local=True, localname='mouse') - self.list = models.List.objects.create( - name='Test List', user=self.user) + with patch('bookwyrm.models.activitypub_mixin.broadcast_task.delay'): + self.list = models.List.objects.create( + name='Test List', user=self.user) - def test_remote_id(self): + def test_remote_id(self, _): ''' shelves use custom remote ids ''' expected_id = 'https://%s/list/%d' % \ (settings.DOMAIN, self.list.id) self.assertEqual(self.list.get_remote_id(), expected_id) - def test_to_activity(self): + def test_to_activity(self, _): ''' jsonify it ''' activity_json = self.list.to_activity() self.assertIsInstance(activity_json, dict) @@ -31,7 +34,7 @@ class List(TestCase): self.assertEqual(activity_json['name'], 'Test List') self.assertEqual(activity_json['owner'], self.user.remote_id) - def test_list_item(self): + def test_list_item(self, _): ''' a list entry ''' work = models.Work.objects.create(title='hello') book = models.Edition.objects.create(title='hi', parent_work=work) @@ -43,12 +46,12 @@ class List(TestCase): self.assertTrue(item.approved) - add_activity = item.to_add_activity(self.user) + add_activity = item.to_add_activity() self.assertEqual(add_activity['actor'], self.user.remote_id) self.assertEqual(add_activity['object']['id'], book.remote_id) self.assertEqual(add_activity['target'], self.list.remote_id) - remove_activity = item.to_remove_activity(self.user) + remove_activity = item.to_remove_activity() self.assertEqual(remove_activity['actor'], self.user.remote_id) self.assertEqual(remove_activity['object']['id'], book.remote_id) self.assertEqual(remove_activity['target'], self.list.remote_id) diff --git a/bookwyrm/tests/models/test_relationship_models.py b/bookwyrm/tests/models/test_relationship_models.py index a1232c1b..56f37e1e 100644 --- a/bookwyrm/tests/models/test_relationship_models.py +++ b/bookwyrm/tests/models/test_relationship_models.py @@ -19,7 +19,7 @@ class Relationship(TestCase): 'mouse', 'mouse@mouse.com', 'mouseword', local=True, localname='mouse') self.local_user.remote_id = 'http://local.com/user/mouse' - self.local_user.save() + self.local_user.save(broadcast=False) def test_user_follows(self): rel = models.UserFollows.objects.create(