1 # -*- coding: utf-8 -*-
2 from django.core.files.base import ContentFile
3 from catalogue.test_utils import BookInfoStub, PersonStub, info_args, WLTestCase
4 from catalogue.models import Book
8 class CoverTests(WLTestCase):
9 """Checks in parent_cover_changed is properly called."""
11 WLTestCase.setUp(self)
12 self.TEXT = """<utwor />"""
13 self.child = BookInfoStub(
17 author=PersonStub(("Joe",), "Doe"),
21 self.parent = BookInfoStub(
25 author=PersonStub(("Jim",), "Lazy"),
26 cover_url="http://example.com/cover.jpg",
27 parts=[self.child.url],
31 @patch.object(Book, 'parent_cover_changed', autospec=True)
32 def test_simple_import(self, parent_cover_changed):
33 child = Book.from_text_and_meta(ContentFile(self.TEXT), self.child)
34 parent = Book.from_text_and_meta(ContentFile(self.TEXT), self.parent)
35 parent_cover_changed.assert_called_with(child)
37 # Now reimport parent.
38 parent_cover_changed.reset_mock()
39 parent = Book.from_text_and_meta(ContentFile(self.TEXT), self.parent,
41 self.assertEqual(parent_cover_changed.call_count, 0)
43 # Now change cover in parent.
44 parent_cover_changed.reset_mock()
45 self.parent.cover_url = "http://example.com/other-cover.jpg"
46 parent = Book.from_text_and_meta(ContentFile(self.TEXT), self.parent,
48 parent_cover_changed.assert_called_with(child)
50 @patch.object(Book, 'parent_cover_changed', autospec=True)
51 def test_change_cover(self, parent_cover_changed):
52 child = Book.from_text_and_meta(ContentFile(self.TEXT), self.child)
53 parent = Book.from_text_and_meta(ContentFile(self.TEXT), self.parent)
54 parent_cover_changed.assert_called_with(child)
56 @patch.object(Book, 'parent_cover_changed', autospec=True)
57 def test_new_child(self, parent_cover_changed):
58 # Add parent without child first.
59 parts, self.parent.parts = self.parent.parts, []
60 parent = Book.from_text_and_meta(ContentFile(self.TEXT), self.parent)
62 # Now import child and reimport parent.
63 child = Book.from_text_and_meta(ContentFile(self.TEXT), self.child)
64 self.parent.parts = parts
65 parent = Book.from_text_and_meta(ContentFile(self.TEXT), self.parent,
67 parent_cover_changed.assert_called_with(child)
69 # Now remove the child.
70 parent_cover_changed.reset_mock()
71 self.parent.parts = []
72 parent = Book.from_text_and_meta(ContentFile(self.TEXT), self.parent,
74 parent_cover_changed.assert_called_with(child)