1 # This file is part of Wolne Lektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Wolne Lektury. See NOTICE for more information.
4 from django.core.files.base import ContentFile
5 from catalogue.test_utils import BookInfoStub, PersonStub, info_args, WLTestCase
6 from catalogue.models import Book
7 from unittest.mock import patch
10 class CoverTests(WLTestCase):
11 """Checks in parent_cover_changed is properly called."""
13 WLTestCase.setUp(self)
14 self.TEXT = """<utwor />"""
15 self.child = BookInfoStub(
19 author=PersonStub(("Joe",), "Doe"),
23 self.parent = BookInfoStub(
27 author=PersonStub(("Jim",), "Lazy"),
28 cover_url="http://example.com/cover.jpg",
29 parts=[self.child.url],
33 @patch.object(Book, 'parent_cover_changed', autospec=True)
34 def test_simple_import(self, parent_cover_changed):
35 child = Book.from_text_and_meta(ContentFile(self.TEXT), self.child)
36 parent = Book.from_text_and_meta(ContentFile(self.TEXT), self.parent)
37 parent_cover_changed.assert_called_with(child)
39 # Now reimport parent.
40 parent_cover_changed.reset_mock()
41 parent = Book.from_text_and_meta(ContentFile(self.TEXT), self.parent, overwrite=True)
42 self.assertEqual(parent_cover_changed.call_count, 0)
44 # Now change cover in parent.
45 parent_cover_changed.reset_mock()
46 self.parent.cover_url = "http://example.com/other-cover.jpg"
47 parent = Book.from_text_and_meta(ContentFile(self.TEXT), self.parent, overwrite=True)
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, overwrite=True)
66 parent_cover_changed.assert_called_with(child)
68 # Now remove the child.
69 parent_cover_changed.reset_mock()
70 self.parent.parts = []
71 parent = Book.from_text_and_meta(ContentFile(self.TEXT), self.parent, overwrite=True)
72 parent_cover_changed.assert_called_with(child)