From: Radek Czajka Date: Tue, 21 Aug 2012 13:17:08 +0000 (+0200) Subject: cover inheritance tests and fix X-Git-Url: https://git.mdrn.pl/wolnelektury.git/commitdiff_plain/e574992403e4b13920e4912511f5c48b006198ba?ds=sidebyside;hp=1405761d13e0ba62db4b1f9a9d5fa1472142b6d0 cover inheritance tests and fix --- diff --git a/apps/catalogue/models/book.py b/apps/catalogue/models/book.py index e6f08a91a..c2b54e4c5 100644 --- a/apps/catalogue/models/book.py +++ b/apps/catalogue/models/book.py @@ -275,10 +275,11 @@ class Book(models.Model): obsolete_children = set(b for b in book.children.all() if b not in children) for n, child_book in enumerate(children): + new_child = child_book.parent != book child_book.parent = book child_book.parent_number = n child_book.save() - if cover_changed: + if new_child or cover_changed: child_book.parent_cover_changed() # Disown unfaithful children and let them cope on their own. for child in obsolete_children: diff --git a/apps/catalogue/tests/__init__.py b/apps/catalogue/tests/__init__.py index 9c0163aad..17a05b47c 100644 --- a/apps/catalogue/tests/__init__.py +++ b/apps/catalogue/tests/__init__.py @@ -1,5 +1,6 @@ from catalogue.tests.book_import import * from catalogue.tests.bookmedia import * +from catalogue.tests.cover import * from catalogue.tests.search import * from catalogue.tests.tags import * from catalogue.tests.templatetags import * diff --git a/apps/catalogue/tests/cover.py b/apps/catalogue/tests/cover.py new file mode 100755 index 000000000..765c56b07 --- /dev/null +++ b/apps/catalogue/tests/cover.py @@ -0,0 +1,74 @@ +# -*- coding: utf-8 -*- +from django.core.files.base import ContentFile +from catalogue.test_utils import BookInfoStub, PersonStub, info_args, WLTestCase +from catalogue.models import Book +from mock import patch + + +class CoverTests(WLTestCase): + """Checks in parent_cover_changed is properly called.""" + def setUp(self): + WLTestCase.setUp(self) + self.TEXT = """""" + self.child = BookInfoStub( + genre='X-Genre', + epoch='X-Epoch', + kind='X-Kind', + author=PersonStub(("Joe",), "Doe"), + **info_args("Child") + ) + + self.parent = BookInfoStub( + genre='X-Genre', + epoch='X-Epoch', + kind='X-Kind', + author=PersonStub(("Jim",), "Lazy"), + cover_url="http://example.com/cover.jpg", + parts=[self.child.url], + **info_args("Parent") + ) + + @patch.object(Book, 'parent_cover_changed', autospec=True) + def test_simple_import(self, parent_cover_changed): + child = Book.from_text_and_meta(ContentFile(self.TEXT), self.child) + parent = Book.from_text_and_meta(ContentFile(self.TEXT), self.parent) + parent_cover_changed.assert_called_with(child) + + # Now reimport parent. + parent_cover_changed.reset_mock() + parent = Book.from_text_and_meta(ContentFile(self.TEXT), self.parent, + overwrite=True) + self.assertEqual(parent_cover_changed.call_count, 0) + + # Now change cover in parent. + parent_cover_changed.reset_mock() + self.parent.cover_url = "http://example.com/other-cover.jpg" + parent = Book.from_text_and_meta(ContentFile(self.TEXT), self.parent, + overwrite=True) + parent_cover_changed.assert_called_with(child) + + @patch.object(Book, 'parent_cover_changed', autospec=True) + def test_change_cover(self, parent_cover_changed): + child = Book.from_text_and_meta(ContentFile(self.TEXT), self.child) + parent = Book.from_text_and_meta(ContentFile(self.TEXT), self.parent) + parent_cover_changed.assert_called_with(child) + + @patch.object(Book, 'parent_cover_changed', autospec=True) + def test_new_child(self, parent_cover_changed): + # Add parent without child first. + parts, self.parent.parts = self.parent.parts, [] + parent = Book.from_text_and_meta(ContentFile(self.TEXT), self.parent) + + # Now import child and reimport parent. + child = Book.from_text_and_meta(ContentFile(self.TEXT), self.child) + self.parent.parts = parts + parent = Book.from_text_and_meta(ContentFile(self.TEXT), self.parent, + overwrite=True) + parent_cover_changed.assert_called_with(child) + + # Now remove the child. + parent_cover_changed.reset_mock() + self.parent.parts = [] + parent = Book.from_text_and_meta(ContentFile(self.TEXT), self.parent, + overwrite=True) + parent_cover_changed.assert_called_with(child) diff --git a/requirements-test.txt b/requirements-test.txt index d766c0725..1f1021311 100644 --- a/requirements-test.txt +++ b/requirements-test.txt @@ -2,3 +2,4 @@ nose>=0.11 django-nose nosexcover polib +mock