Merge branch 'master' into sunburnt
[wolnelektury.git] / apps / catalogue / tests / cover.py
1 # -*- coding: utf-8 -*-
2 from django.core.files.base import ContentFile
3 from catalogue.test_utils import BookInfoStub, PersonStub, info_args, WLTestCase
4 from catalogue.models import Book
5 from mock import patch
6
7
8 class CoverTests(WLTestCase):
9     """Checks in parent_cover_changed is properly called."""
10     def setUp(self):
11         WLTestCase.setUp(self)
12         self.TEXT = """<utwor />"""
13         self.child = BookInfoStub(
14             genre='X-Genre',
15             epoch='X-Epoch',
16             kind='X-Kind',
17             author=PersonStub(("Joe",), "Doe"),
18             **info_args("Child")
19         )
20
21         self.parent = BookInfoStub(
22             genre='X-Genre',
23             epoch='X-Epoch',
24             kind='X-Kind',
25             author=PersonStub(("Jim",), "Lazy"),
26             cover_url="http://example.com/cover.jpg",
27             parts=[self.child.url],
28             **info_args("Parent")
29         )
30
31     @patch.object(Book, 'parent_cover_changed', autospec=True)
32     def test_simple_import(self, parent_cover_changed):
33         child = Book.from_text_and_meta(ContentFile(self.TEXT), self.child)
34         parent = Book.from_text_and_meta(ContentFile(self.TEXT), self.parent)
35         parent_cover_changed.assert_called_with(child)
36
37         # Now reimport parent.
38         parent_cover_changed.reset_mock()
39         parent = Book.from_text_and_meta(ContentFile(self.TEXT), self.parent,
40                     overwrite=True)
41         self.assertEqual(parent_cover_changed.call_count, 0)
42         
43         # Now change cover in parent.
44         parent_cover_changed.reset_mock()
45         self.parent.cover_url = "http://example.com/other-cover.jpg"
46         parent = Book.from_text_and_meta(ContentFile(self.TEXT), self.parent,
47                     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,
66                     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,
73                     overwrite=True)
74         parent_cover_changed.assert_called_with(child)