0e08556b442394128f172177f0998e04fe95ffb6
[wolnelektury.git] / apps / catalogue / tests / cover.py
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.
4 #
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
8 from mock import patch
9
10
11 class CoverTests(WLTestCase):
12     """Checks in parent_cover_changed is properly called."""
13     def setUp(self):
14         WLTestCase.setUp(self)
15         self.TEXT = """<utwor />"""
16         self.child = BookInfoStub(
17             genre='X-Genre',
18             epoch='X-Epoch',
19             kind='X-Kind',
20             author=PersonStub(("Joe",), "Doe"),
21             **info_args("Child")
22         )
23
24         self.parent = BookInfoStub(
25             genre='X-Genre',
26             epoch='X-Epoch',
27             kind='X-Kind',
28             author=PersonStub(("Jim",), "Lazy"),
29             cover_url="http://example.com/cover.jpg",
30             parts=[self.child.url],
31             **info_args("Parent")
32         )
33
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)
39
40         # Now reimport parent.
41         parent_cover_changed.reset_mock()
42         parent = Book.from_text_and_meta(ContentFile(self.TEXT), self.parent,
43                     overwrite=True)
44         self.assertEqual(parent_cover_changed.call_count, 0)
45
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,
50                     overwrite=True)
51         parent_cover_changed.assert_called_with(child)
52
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)
58
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)
64
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,
69                     overwrite=True)
70         parent_cover_changed.assert_called_with(child)
71
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,
76                     overwrite=True)
77         parent_cover_changed.assert_called_with(child)