Fixes shelf model tests

This commit is contained in:
Mouse Reeve 2021-02-06 21:00:08 -08:00
parent b02a2c1aa4
commit 63fe9777e2
2 changed files with 79 additions and 10 deletions

View file

@ -27,12 +27,11 @@ class Shelf(OrderedCollectionMixin, BookWyrmModel):
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
''' set the identifier ''' ''' set the identifier '''
saved = super().save(*args, **kwargs) super().save(*args, **kwargs)
if not self.identifier: if not self.identifier:
slug = re.sub(r'[^\w]', '', self.name).lower() slug = re.sub(r'[^\w]', '', self.name).lower()
self.identifier = '%s-%d' % (slug, self.id) self.identifier = '%s-%d' % (slug, self.id)
return super().save(*args, **kwargs) super().save(*args, **kwargs)
return saved
@property @property
def collection_queryset(self): def collection_queryset(self):

View file

@ -8,24 +8,94 @@ class Shelf(TestCase):
''' some activitypub oddness ahead ''' ''' some activitypub oddness ahead '''
def setUp(self): def setUp(self):
''' look, a shelf ''' ''' look, a shelf '''
self.user = models.User.objects.create_user( self.local_user = models.User.objects.create_user(
'mouse', 'mouse@mouse.mouse', 'mouseword', 'mouse', 'mouse@mouse.mouse', 'mouseword',
local=True, localname='mouse') local=True, localname='mouse')
self.shelf = models.Shelf.objects.create( work = models.Work.objects.create(title='Test Work')
name='Test Shelf', identifier='test-shelf', user=self.user) self.book = models.Edition.objects.create(
title='test book',
parent_work=work)
def test_remote_id(self): def test_remote_id(self):
''' shelves use custom remote ids ''' ''' shelves use custom remote ids '''
models.Shelf.broadcast = lambda x, y, z: None
shelf = models.Shelf.objects.create(
name='Test Shelf', identifier='test-shelf',
user=self.local_user)
expected_id = 'https://%s/user/mouse/shelf/test-shelf' % settings.DOMAIN expected_id = 'https://%s/user/mouse/shelf/test-shelf' % settings.DOMAIN
self.assertEqual(self.shelf.get_remote_id(), expected_id) self.assertEqual(shelf.get_remote_id(), expected_id)
def test_to_activity(self): def test_to_activity(self):
''' jsonify it ''' ''' jsonify it '''
activity_json = self.shelf.to_activity() models.Shelf.broadcast = lambda x, y, z: None
shelf = models.Shelf.objects.create(
name='Test Shelf', identifier='test-shelf',
user=self.local_user)
activity_json = shelf.to_activity()
self.assertIsInstance(activity_json, dict) self.assertIsInstance(activity_json, dict)
self.assertEqual(activity_json['id'], self.shelf.remote_id) self.assertEqual(activity_json['id'], shelf.remote_id)
self.assertEqual(activity_json['totalItems'], 0) self.assertEqual(activity_json['totalItems'], 0)
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.user.remote_id) self.assertEqual(activity_json['owner'], self.local_user.remote_id)
def test_create_update_shelf(self):
''' create and broadcast shelf creation '''
def create_mock(_, activity, user):
''' ok '''
self.assertEqual(user.remote_id, self.local_user.remote_id)
self.assertEqual(activity['type'], 'Create')
self.assertEqual(activity['actor'], self.local_user.remote_id)
self.assertEqual(activity['object']['name'], 'Test Shelf')
models.Shelf.broadcast = create_mock
shelf = models.Shelf.objects.create(
name='Test Shelf', identifier='test-shelf', user=self.local_user)
def update_mock(_, activity, user):
''' 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.save()
self.assertEqual(shelf.name, 'arthur russel')
def test_shelve(self):
''' create and broadcast shelf creation '''
def add_mock(_, activity, user):
''' ok '''
self.assertEqual(user.remote_id, self.local_user.remote_id)
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)
def remove_mock(_, activity, user):
''' ok '''
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)
models.Shelf.broadcast = lambda x, y, z: None
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)
models.ShelfBook.broadcast = remove_mock
shelf_book.delete()
self.assertFalse(shelf.books.exists())