1 # -*- coding: utf-8 -*-
2 from django.core.files.base import ContentFile
3 from catalogue.test_utils import *
4 from catalogue import models
6 from nose.tools import raises
9 class BookImportLogicTests(WLTestCase):
12 WLTestCase.setUp(self)
13 self.book_info = BookInfoStub(
14 url=u"http://wolnelektury.pl/example/default-book",
15 about=u"http://wolnelektury.pl/example/URI/default_book",
16 title=u"Default Book",
17 author=PersonStub(("Jim",), "Lazy"),
23 self.expected_tags = [
24 ('author', 'jim-lazy'),
29 self.expected_tags.sort()
31 def test_empty_book(self):
32 BOOK_TEXT = "<utwor />"
33 book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info)
35 self.assertEqual(book.title, "Default Book")
36 self.assertEqual(book.slug, "default-book")
37 self.assert_(book.parent is None)
38 self.assertFalse(book.has_html_file())
40 # no fragments generated
41 self.assertEqual(book.fragments.count(), 0)
43 # TODO: this should be filled out probably...
44 self.assertEqual(book.wiki_link, '')
45 self.assertEqual(book.gazeta_link, '')
46 self.assertEqual(book._short_html, '')
47 self.assertEqual(book.description, '')
49 tags = [ (tag.category, tag.slug) for tag in book.tags ]
52 self.assertEqual(tags, self.expected_tags)
54 def test_not_quite_empty_book(self):
55 """ Not empty, but without any real text.
57 Should work like any other non-empty book.
60 BOOK_TEXT = """<utwor>
62 <nazwa_utworu>Nic</nazwa_utworu>
66 book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info)
67 self.assertTrue(book.has_html_file())
69 def test_book_with_fragment(self):
70 BOOK_TEXT = """<utwor>
72 <akap><begin id="m01" /><motyw id="m01">Love</motyw>Ala ma kota<end id="m01" /></akap>
73 </opowiadanie></utwor>
76 book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info)
77 self.assertTrue(book.has_html_file())
79 self.assertEqual(book.fragments.count(), 1)
80 self.assertEqual(book.fragments.all()[0].text, u'<p class="paragraph">Ala ma kota</p>\n')
82 self.assert_(('theme', 'love') in [ (tag.category, tag.slug) for tag in book.fragments.all()[0].tags ])
84 def test_book_with_empty_theme(self):
85 """ empty themes should be ignored """
87 BOOK_TEXT = """<utwor>
89 <akap><begin id="m01" /><motyw id="m01"> , Love , , </motyw>Ala ma kota<end id="m01" /></akap>
90 </opowiadanie></utwor>
93 book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info)
94 self.assert_([('theme', 'love')],
95 [ (tag.category, tag.slug) for tag in book.fragments.all()[0].tags.filter(category='theme') ])
97 def test_book_with_no_theme(self):
98 """ fragments with no themes shouldn't be created at all """
100 BOOK_TEXT = """<utwor>
102 <akap><begin id="m01" /><motyw id="m01"></motyw>Ala ma kota<end id="m01" /></akap>
103 </opowiadanie></utwor>
106 book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info)
107 self.assertEqual(book.fragments.count(), 0)
108 self.assertEqual(book.tags.filter(category='theme').count(), 0)
111 def test_book_with_invalid_slug(self):
112 """ Book with invalid characters in slug shouldn't be imported """
113 self.book_info.url = "http://wolnelektury.pl/example/default_book"
114 BOOK_TEXT = "<utwor />"
115 book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info)
117 def test_book_replace_title(self):
118 BOOK_TEXT = """<utwor />"""
119 book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info)
120 self.book_info.title = u"Extraordinary"
121 book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info, overwrite=True)
123 tags = [ (tag.category, tag.slug) for tag in book.tags ]
126 self.assertEqual(tags, self.expected_tags)
128 def test_book_replace_author(self):
129 BOOK_TEXT = """<utwor />"""
130 book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info)
131 self.book_info.author = PersonStub(("Hans", "Christian"), "Andersen")
132 book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info, overwrite=True)
134 tags = [ (tag.category, tag.slug) for tag in book.tags ]
137 self.expected_tags.remove(('author', 'jim-lazy'))
138 self.expected_tags.append(('author', 'hans-christian-andersen'))
139 self.expected_tags.sort()
141 self.assertEqual(tags, self.expected_tags)
143 # the old tag shouldn't disappear
144 models.Tag.objects.get(slug="jim-lazy", category="author")
146 def test_multiple_tags(self):
147 BOOK_TEXT = """<utwor />"""
148 self.book_info.authors = self.book_info.author, PersonStub(("Joe",), "Dilligent"),
149 self.book_info.kinds = self.book_info.kind, 'Y-Kind',
150 self.book_info.genres = self.book_info.genre, 'Y-Genre',
151 self.book_info.epochs = self.book_info.epoch, 'Y-Epoch',
153 self.expected_tags.extend([
154 ('author', 'joe-dilligent'),
155 ('genre', 'y-genre'),
156 ('epoch', 'y-epoch'),
159 self.expected_tags.sort()
161 book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info)
162 tags = [ (tag.category, tag.slug) for tag in book.tags ]
165 self.assertEqual(tags, self.expected_tags)
168 class ChildImportTests(WLTestCase):
171 WLTestCase.setUp(self)
172 self.child_info = BookInfoStub(
176 author=PersonStub(("Joe",), "Doe"),
180 self.parent_info = BookInfoStub(
184 author=PersonStub(("Jim",), "Lazy"),
185 parts=[self.child_info.url],
186 **info_args("Parent")
189 def test_child_replace(self):
190 PARENT_TEXT = """<utwor />"""
191 CHILD_TEXT = """<utwor>
193 <akap><begin id="m01" /><motyw id="m01">Pies</motyw>Ala ma kota<end id="m01" /></akap>
194 </opowiadanie></utwor>
196 child = models.Book.from_text_and_meta(ContentFile(CHILD_TEXT), self.child_info)
197 parent = models.Book.from_text_and_meta(ContentFile(PARENT_TEXT), self.parent_info)
198 CHILD_TEXT = """<utwor>
200 <akap><begin id="m01" /><motyw id="m01">Kot</motyw>Ala ma kota<end id="m01" /></akap>
201 </opowiadanie></utwor>
203 child = models.Book.from_text_and_meta(ContentFile(CHILD_TEXT), self.child_info, overwrite=True)
205 themes = self.client.get(parent.get_absolute_url()).context['book_themes']
207 self.assertEqual(['Kot'], [tag.name for tag in themes],
208 'wrong related theme list')