Adds test and fixes logic errors

This commit is contained in:
Mouse Reeve 2023-07-17 20:00:45 -07:00
parent eee4e30e25
commit 8b88de624d
2 changed files with 14 additions and 4 deletions

View file

@ -393,10 +393,10 @@ class Edition(Book):
existing_match = model.find_existing(data)
# assign this edition to the parent of the duplicate edition
new_work = existing_match.parent_work
# if not, create a new work for it
if not new_work:
new_work = models.Work.objects.create(title=self.title)
if existing_match and existing_match.parent_work:
new_work = existing_match.parent_work
else:
new_work = Work.objects.create(title=self.title)
self.parent_work = new_work
self.save(update_fields=["parent_work"], broadcast=False)

View file

@ -143,3 +143,13 @@ class Book(TestCase):
for article in articles
)
self.assertTrue(all(book.sort_title == "test edition" for book in books))
def test_repair_edition(self):
"""Fix editions with no works"""
edition = models.Edition.objects.create(title="test")
self.assertIsNone(edition.parent_work)
edition.repair()
edition.refresh_from_db()
self.assertEqual(edition.parent_work.title, "test")