1 # -*- coding: utf-8 -*-
2 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
3 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
5 from django.core.files.base import ContentFile
6 from catalogue.test_utils import BookInfoStub, PersonStub, info_args, WLTestCase
7 from catalogue.models import Book
11 class CoverTests(WLTestCase):
12 """Checks in parent_cover_changed is properly called."""
14 WLTestCase.setUp(self)
15 self.TEXT = """<utwor />"""
16 self.child = BookInfoStub(
20 author=PersonStub(("Joe",), "Doe"),
24 self.parent = BookInfoStub(
28 author=PersonStub(("Jim",), "Lazy"),
29 cover_url="http://example.com/cover.jpg",
30 parts=[self.child.url],
34 @patch.object(Book, 'parent_cover_changed', autospec=True)
35 def test_simple_import(self, parent_cover_changed):
36 child = Book.from_text_and_meta(ContentFile(self.TEXT), self.child)
37 parent = Book.from_text_and_meta(ContentFile(self.TEXT), self.parent)
38 parent_cover_changed.assert_called_with(child)
40 # Now reimport parent.
41 parent_cover_changed.reset_mock()
42 parent = Book.from_text_and_meta(ContentFile(self.TEXT), self.parent,
44 self.assertEqual(parent_cover_changed.call_count, 0)
46 # Now change cover in parent.
47 parent_cover_changed.reset_mock()
48 self.parent.cover_url = "http://example.com/other-cover.jpg"
49 parent = Book.from_text_and_meta(ContentFile(self.TEXT), self.parent,
51 parent_cover_changed.assert_called_with(child)
53 @patch.object(Book, 'parent_cover_changed', autospec=True)
54 def test_change_cover(self, parent_cover_changed):
55 child = Book.from_text_and_meta(ContentFile(self.TEXT), self.child)
56 parent = Book.from_text_and_meta(ContentFile(self.TEXT), self.parent)
57 parent_cover_changed.assert_called_with(child)
59 @patch.object(Book, 'parent_cover_changed', autospec=True)
60 def test_new_child(self, parent_cover_changed):
61 # Add parent without child first.
62 parts, self.parent.parts = self.parent.parts, []
63 parent = Book.from_text_and_meta(ContentFile(self.TEXT), self.parent)
65 # Now import child and reimport parent.
66 child = Book.from_text_and_meta(ContentFile(self.TEXT), self.child)
67 self.parent.parts = parts
68 parent = Book.from_text_and_meta(ContentFile(self.TEXT), self.parent,
70 parent_cover_changed.assert_called_with(child)
72 # Now remove the child.
73 parent_cover_changed.reset_mock()
74 self.parent.parts = []
75 parent = Book.from_text_and_meta(ContentFile(self.TEXT), self.parent,
77 parent_cover_changed.assert_called_with(child)