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.
 
   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
 
  11 class CoverTests(WLTestCase):
 
  12     """Checks in parent_cover_changed is properly called."""
 
  14         WLTestCase.setUp(self)
 
  15         self.TEXT = """<utwor />"""
 
  16         self.child = BookInfoStub(
 
  20             author=PersonStub(("Joe",), "Doe"),
 
  24         self.parent = BookInfoStub(
 
  28             author=PersonStub(("Jim",), "Lazy"),
 
  29             cover_url="http://example.com/cover.jpg",
 
  30             parts=[self.child.url],
 
  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)
 
  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)
 
  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)
 
  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)
 
  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)
 
  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)
 
  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)