From 8b88de624d04167f3e910e925a2a68622bae9449 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Mon, 17 Jul 2023 20:00:45 -0700 Subject: [PATCH] Adds test and fixes logic errors --- bookwyrm/models/book.py | 8 ++++---- bookwyrm/tests/models/test_book_model.py | 10 ++++++++++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/bookwyrm/models/book.py b/bookwyrm/models/book.py index 88d568288..6c40061b9 100644 --- a/bookwyrm/models/book.py +++ b/bookwyrm/models/book.py @@ -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) diff --git a/bookwyrm/tests/models/test_book_model.py b/bookwyrm/tests/models/test_book_model.py index 825f42b87..a615a76af 100644 --- a/bookwyrm/tests/models/test_book_model.py +++ b/bookwyrm/tests/models/test_book_model.py @@ -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")