Fix error with saving readnly inlines in admin.
[wolnelektury.git] / src / catalogue / tests / test_cover.py
1 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
3 #
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
8
9
10 class CoverTests(WLTestCase):
11     """Checks in parent_cover_changed is properly called."""
12     def setUp(self):
13         WLTestCase.setUp(self)
14         self.TEXT = """<utwor />"""
15         self.child = BookInfoStub(
16             genre='X-Genre',
17             epoch='X-Epoch',
18             kind='X-Kind',
19             author=PersonStub(("Joe",), "Doe"),
20             **info_args("Child")
21         )
22
23         self.parent = BookInfoStub(
24             genre='X-Genre',
25             epoch='X-Epoch',
26             kind='X-Kind',
27             author=PersonStub(("Jim",), "Lazy"),
28             cover_url="http://example.com/cover.jpg",
29             parts=[self.child.url],
30             **info_args("Parent")
31         )
32
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)
38
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)
43
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)
49
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)
55
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)
61
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)
67
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)