More books tests

This commit is contained in:
Mouse Reeve 2020-05-14 11:28:45 -07:00
parent d92c9af9c3
commit 3b16bb748c
2 changed files with 25 additions and 9 deletions

View file

@ -27,5 +27,6 @@ def execute_after_save(sender, instance, created, *args, **kwargs):
''' set the remote_id after save (when the id is available) ''' ''' set the remote_id after save (when the id is available) '''
if not created or not hasattr(instance, 'get_remote_id'): if not created or not hasattr(instance, 'get_remote_id'):
return return
if not instance.remote_id:
instance.remote_id = instance.get_remote_id() instance.remote_id = instance.get_remote_id()
instance.save() instance.save()

View file

@ -7,13 +7,23 @@ from fedireads import models, settings
class Book(TestCase): class Book(TestCase):
''' not too much going on in the books model but here we are ''' ''' not too much going on in the books model but here we are '''
def setUp(self): def setUp(self):
self.work = models.Work.objects.create(title='Example Work') self.work = models.Work.objects.create(
models.Edition.objects.create(title='Example Edition', parent_work=self.work) title='Example Work',
remote_id='https://example.com/book/1'
)
self.first_edition = models.Edition.objects.create(
title='Example Edition',
parent_work=self.work,
)
self.second_edition = models.Edition.objects.create(
title='Another Example Edition',
parent_work=self.work,
)
def test_remote_id(self): def test_remote_id(self):
''' editions and works use the same remote_id syntax ''' local_id = 'https://%s/book/%d' % (settings.DOMAIN, self.work.id)
expected_id = 'https://%s/book/%d' % (settings.DOMAIN, self.work.id) self.assertEqual(self.work.get_remote_id(), local_id)
self.assertEqual(self.work.get_remote_id(), expected_id) self.assertEqual(self.work.remote_id, 'https://example.com/book/1')
def test_local_id(self): def test_local_id(self):
''' the local_id property for books ''' ''' the local_id property for books '''
@ -30,8 +40,13 @@ class Book(TestCase):
def test_default_edition(self): def test_default_edition(self):
''' a work should always be able to produce a deafult edition ''' ''' a work should always be able to produce a deafult edition '''
default_edition = models.Work.objects.first().default_edition self.assertIsInstance(self.work.default_edition, models.Edition)
self.assertIsInstance(default_edition, models.Edition) self.assertEqual(self.work.default_edition, self.first_edition)
self.second_edition.default = True
self.second_edition.save()
self.assertEqual(self.work.default_edition, self.second_edition)
class Shelf(TestCase): class Shelf(TestCase):