cover inheritance tests and fix
authorRadek Czajka <radoslaw.czajka@nowoczesnapolska.org.pl>
Tue, 21 Aug 2012 13:17:08 +0000 (15:17 +0200)
committerRadek Czajka <radoslaw.czajka@nowoczesnapolska.org.pl>
Tue, 21 Aug 2012 13:17:08 +0000 (15:17 +0200)
apps/catalogue/models/book.py
apps/catalogue/tests/__init__.py
apps/catalogue/tests/cover.py [new file with mode: 0755]
requirements-test.txt

index e6f08a9..c2b54e4 100644 (file)
@@ -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:
index 9c0163a..17a05b4 100644 (file)
@@ -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 (executable)
index 0000000..765c56b
--- /dev/null
@@ -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 = """<utwor />"""
+        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)
index d766c07..1f10213 100644 (file)
@@ -2,3 +2,4 @@ nose>=0.11
 django-nose
 nosexcover
 polib
+mock