import style
[wolnelektury.git] / src / 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, overwrite=True)
43         self.assertEqual(parent_cover_changed.call_count, 0)
44
45         # Now change cover in parent.
46         parent_cover_changed.reset_mock()
47         self.parent.cover_url = "http://example.com/other-cover.jpg"
48         parent = Book.from_text_and_meta(ContentFile(self.TEXT), self.parent, overwrite=True)
49         parent_cover_changed.assert_called_with(child)
50
51     @patch.object(Book, 'parent_cover_changed', autospec=True)
52     def test_change_cover(self, parent_cover_changed):
53         child = Book.from_text_and_meta(ContentFile(self.TEXT), self.child)
54         parent = Book.from_text_and_meta(ContentFile(self.TEXT), self.parent)
55         parent_cover_changed.assert_called_with(child)
56
57     @patch.object(Book, 'parent_cover_changed', autospec=True)
58     def test_new_child(self, parent_cover_changed):
59         # Add parent without child first.
60         parts, self.parent.parts = self.parent.parts, []
61         parent = Book.from_text_and_meta(ContentFile(self.TEXT), self.parent)
62
63         # Now import child and reimport parent.
64         child = Book.from_text_and_meta(ContentFile(self.TEXT), self.child)
65         self.parent.parts = parts
66         parent = Book.from_text_and_meta(ContentFile(self.TEXT), self.parent, overwrite=True)
67         parent_cover_changed.assert_called_with(child)
68
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, overwrite=True)
73         parent_cover_changed.assert_called_with(child)