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.conf import settings
7 from django.core.files.base import ContentFile
8 from catalogue.test_utils import *
9 from catalogue import models
10 from librarian import WLURI
12 from nose.tools import raises
13 from os import path, makedirs
15 class BookImportLogicTests(WLTestCase):
18 WLTestCase.setUp(self)
19 self.book_info = BookInfoStub(
20 url=WLURI.from_slug(u"default-book"),
21 about=u"http://wolnelektury.pl/example/URI/default_book",
22 title=u"Default Book",
23 author=PersonStub(("Jim",), "Lazy"),
30 self.expected_tags = [
31 ('author', 'jim-lazy'),
36 self.expected_tags.sort()
38 def test_empty_book(self):
39 BOOK_TEXT = "<utwor />"
40 book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info)
42 self.assertEqual(book.title, "Default Book")
43 self.assertEqual(book.slug, "default-book")
44 self.assert_(book.parent is None)
45 self.assertFalse(book.has_html_file())
47 # no fragments generated
48 self.assertEqual(book.fragments.count(), 0)
50 # TODO: this should be filled out probably...
51 self.assertEqual(book.wiki_link, '')
52 self.assertEqual(book.gazeta_link, '')
53 self.assertEqual(book.description, '')
55 tags = [ (tag.category, tag.slug) for tag in book.tags ]
58 self.assertEqual(tags, self.expected_tags)
60 def test_not_quite_empty_book(self):
61 """ Not empty, but without any real text.
63 Should work like any other non-empty book.
66 BOOK_TEXT = """<utwor>
68 <nazwa_utworu>Nic</nazwa_utworu>
72 book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info)
73 self.assertTrue(book.has_html_file())
75 def test_book_with_fragment(self):
76 BOOK_TEXT = """<utwor>
78 <akap><begin id="m01" /><motyw id="m01">Love</motyw>Ala ma kota<end id="m01" /></akap>
79 </opowiadanie></utwor>
82 book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info)
83 self.assertTrue(book.has_html_file())
85 self.assertEqual(book.fragments.count(), 1)
86 self.assertEqual(book.fragments.all()[0].text, u'<p class="paragraph">Ala ma kota</p>\n')
88 self.assert_(('theme', 'love') in [ (tag.category, tag.slug) for tag in book.fragments.all()[0].tags ])
90 def test_book_with_empty_theme(self):
91 """ empty themes should be ignored """
93 BOOK_TEXT = """<utwor>
95 <akap><begin id="m01" /><motyw id="m01"> , Love , , </motyw>Ala ma kota<end id="m01" /></akap>
96 </opowiadanie></utwor>
99 book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info)
100 self.assert_([('theme', 'love')],
101 [ (tag.category, tag.slug) for tag in book.fragments.all()[0].tags.filter(category='theme') ])
103 def test_book_with_no_theme(self):
104 """ fragments with no themes shouldn't be created at all """
106 BOOK_TEXT = """<utwor>
108 <akap><begin id="m01" /><motyw id="m01"></motyw>Ala ma kota<end id="m01" /></akap>
109 </opowiadanie></utwor>
112 book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info)
113 self.assertEqual(book.fragments.count(), 0)
114 self.assertEqual(book.tags.filter(category='theme').count(), 0)
117 def test_book_with_invalid_slug(self):
118 """ Book with invalid characters in slug shouldn't be imported """
119 self.book_info.url = WLURI.from_slug(u"default_book")
120 BOOK_TEXT = "<utwor />"
121 models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info)
123 def test_book_replace_title(self):
124 BOOK_TEXT = """<utwor />"""
125 book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info)
126 self.book_info.title = u"Extraordinary"
127 book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info, overwrite=True)
129 tags = [ (tag.category, tag.slug) for tag in book.tags ]
132 self.assertEqual(tags, self.expected_tags)
134 def test_book_replace_author(self):
135 BOOK_TEXT = """<utwor />"""
136 book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info)
137 self.book_info.author = PersonStub(("Hans", "Christian"), "Andersen")
138 book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info, overwrite=True)
140 tags = [ (tag.category, tag.slug) for tag in book.tags ]
143 self.expected_tags.remove(('author', 'jim-lazy'))
144 self.expected_tags.append(('author', 'hans-christian-andersen'))
145 self.expected_tags.sort()
147 self.assertEqual(tags, self.expected_tags)
149 # the old tag shouldn't disappear
150 models.Tag.objects.get(slug="jim-lazy", category="author")
152 def test_book_remove_fragment(self):
153 BOOK_TEXT = """<utwor>
156 <begin id="m01" /><motyw id="m01">Love</motyw>Ala ma kota<end id="m01" />
157 <begin id="m02" /><motyw id="m02">Hatred</motyw>To kot Ali<end id="m02" />
159 </opowiadanie></utwor>
161 BOOK_TEXT_AFTER = """<utwor>
164 <begin id="m01" /><motyw id="m01">Love</motyw>Ala ma kota<end id="m01" />
167 </opowiadanie></utwor>
170 book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info)
171 self.assertEqual(book.fragments.count(), 2)
172 book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT_AFTER), self.book_info, overwrite=True)
173 self.assertEqual(book.fragments.count(), 1)
175 def test_multiple_tags(self):
176 BOOK_TEXT = """<utwor />"""
177 self.book_info.authors = self.book_info.author, PersonStub(("Joe",), "Dilligent"),
178 self.book_info.kinds = self.book_info.kind, 'Y-Kind',
179 self.book_info.genres = self.book_info.genre, 'Y-Genre',
180 self.book_info.epochs = self.book_info.epoch, 'Y-Epoch',
182 self.expected_tags.extend([
183 ('author', 'joe-dilligent'),
184 ('genre', 'y-genre'),
185 ('epoch', 'y-epoch'),
188 self.expected_tags.sort()
190 book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info)
191 tags = [ (tag.category, tag.slug) for tag in book.tags ]
194 self.assertEqual(tags, self.expected_tags)
197 class ChildImportTests(WLTestCase):
200 WLTestCase.setUp(self)
201 self.child_info = BookInfoStub(
205 author=PersonStub(("Joe",), "Doe"),
209 self.parent_info = BookInfoStub(
213 author=PersonStub(("Jim",), "Lazy"),
214 parts=[self.child_info.url],
215 **info_args("Parent")
218 def test_child(self):
219 TEXT = """<utwor />"""
220 child = models.Book.from_text_and_meta(ContentFile(TEXT), self.child_info)
221 parent = models.Book.from_text_and_meta(ContentFile(TEXT), self.parent_info)
222 author = parent.tags.get(category='author')
223 books = self.client.get(author.get_absolute_url()).context['object_list']
224 self.assertEqual(len(books), 1,
225 "Only parent book should be visible on author's page")
227 def test_child_replace(self):
228 PARENT_TEXT = """<utwor />"""
229 CHILD_TEXT = """<utwor>
231 <akap><begin id="m01" /><motyw id="m01">Pies</motyw>Ala ma kota<end id="m01" /></akap>
232 </opowiadanie></utwor>
234 child = models.Book.from_text_and_meta(ContentFile(CHILD_TEXT), self.child_info)
235 parent = models.Book.from_text_and_meta(ContentFile(PARENT_TEXT), self.parent_info)
236 CHILD_TEXT = """<utwor>
238 <akap><begin id="m01" /><motyw id="m01">Kot</motyw>Ala ma kota<end id="m01" /></akap>
239 </opowiadanie></utwor>
241 child = models.Book.from_text_and_meta(ContentFile(CHILD_TEXT), self.child_info, overwrite=True)
242 themes = parent.related_themes()
243 self.assertEqual(['Kot'], [tag.name for tag in themes],
244 'wrong related theme list')
247 class TreeImportTest(WLTestCase):
249 WLTestCase.setUp(self)
250 self.child_info = BookInfoStub(
254 author=PersonStub(("Joe",), "Doe"),
257 self.CHILD_TEXT = """<utwor>
259 <akap><begin id="m01" /><motyw id="m01">Pies</motyw>
260 Ala ma kota<end id="m01" /></akap>
261 </opowiadanie></utwor>
263 self.child = models.Book.from_text_and_meta(
264 ContentFile(self.CHILD_TEXT), self.child_info)
266 self.book_info = BookInfoStub(
270 author=PersonStub(("Joe",), "Doe"),
271 parts=[self.child_info.url],
274 self.BOOK_TEXT = """<utwor />"""
275 self.book = models.Book.from_text_and_meta(
276 ContentFile(self.BOOK_TEXT), self.book_info)
278 self.parent_info = BookInfoStub(
282 author=PersonStub(("Jim",), "Lazy"),
283 parts=[self.book_info.url],
284 **info_args("Parent")
286 self.PARENT_TEXT = """<utwor />"""
287 self.parent = models.Book.from_text_and_meta(
288 ContentFile(self.PARENT_TEXT), self.parent_info)
292 list(self.client.get('/katalog/gatunek/x-genre/'
293 ).context['object_list']),
295 u"There should be only parent on common tag page."
297 pies = models.Tag.objects.get(slug='pies')
298 themes = self.parent.related_themes()
299 self.assertEqual(len(themes), 1,
300 u"There should be child theme in parent theme counter."
302 # TODO: book_count is deprecated, update here.
303 #~ epoch = models.Tag.objects.get(slug='x-epoch')
304 #~ self.assertEqual(epoch.book_count, 1,
305 #~ u"There should be only parent in common tag's counter."
308 def test_child_republish(self):
309 CHILD_TEXT = """<utwor>
311 <akap><begin id="m01" /><motyw id="m01">Pies, Kot</motyw>
312 Ala ma kota<end id="m01" /></akap>
313 </opowiadanie></utwor>
315 models.Book.from_text_and_meta(
316 ContentFile(CHILD_TEXT), self.child_info, overwrite=True)
318 list(self.client.get('/katalog/gatunek/x-genre/'
319 ).context['object_list']),
321 u"There should only be parent on common tag page."
323 pies = models.Tag.objects.get(slug='pies')
324 kot = models.Tag.objects.get(slug='kot')
325 self.assertEqual(len(self.parent.related_themes()), 2,
326 u"There should be child themes in parent theme counter."
328 # TODO: book_count is deprecated, update here.
329 #~ epoch = models.Tag.objects.get(slug='x-epoch')
330 #~ self.assertEqual(epoch.book_count, 1,
331 #~ u"There should only be parent in common tag's counter."
334 def test_book_change_child(self):
335 second_child_info = BookInfoStub(
339 author=PersonStub(("Joe",), "Doe"),
340 **info_args("Second Child")
342 SECOND_CHILD_TEXT = """<utwor>
344 <akap><begin id="m01" /><motyw id="m01">Kot</motyw>
345 Ala ma kota<end id="m01" /></akap>
346 </opowiadanie></utwor>
348 # Import a second child.
349 second_child = models.Book.from_text_and_meta(
350 ContentFile(SECOND_CHILD_TEXT), second_child_info)
351 # The book has only this new child now.
352 self.book_info.parts = [second_child_info.url]
353 self.book = models.Book.from_text_and_meta(
354 ContentFile(self.BOOK_TEXT), self.book_info, overwrite=True)
357 set(self.client.get('/katalog/gatunek/x-genre/'
358 ).context['object_list']),
359 set([self.parent, self.child]),
360 u"There should be parent and old child on common tag page."
362 kot = models.Tag.objects.get(slug='kot')
363 self.assertEqual(len(self.parent.related_themes()), 1,
364 u"There should only be new child themes in parent theme counter."
366 epoch = models.Tag.objects.get(slug='x-epoch')
367 # book_count deprecated, update test.
368 #~ self.assertEqual(epoch.book_count, 2,
369 #~ u"There should be parent and old child in common tag's counter."
372 list(self.client.get('/katalog/lektura/parent/motyw/kot/'
373 ).context['fragments']),
374 [second_child.fragments.all()[0]],
375 u"There should be new child's fragments on parent's theme page."
378 list(self.client.get('/katalog/lektura/parent/motyw/pies/'
379 ).context['fragments']),
381 u"There should be no old child's fragments on parent's theme page."
385 class MultilingualBookImportTest(WLTestCase):
387 WLTestCase.setUp(self)
388 common_uri = WLURI.from_slug('common-slug')
390 self.pol_info = BookInfoStub(
394 author=PersonStub(("Joe",), "Doe"),
395 variant_of=common_uri,
396 **info_args(u"Książka")
399 self.eng_info = BookInfoStub(
403 author=PersonStub(("Joe",), "Doe"),
404 variant_of=common_uri,
405 **info_args("A book", "eng")
408 def test_multilingual_import(self):
409 BOOK_TEXT = """<utwor><opowiadanie><akap>A</akap></opowiadanie></utwor>"""
411 book1 = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.pol_info)
412 book2 = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.eng_info)
415 set([b.language for b in models.Book.objects.all()]),
417 'Books imported in wrong languages.'
421 class BookImportGenerateTest(WLTestCase):
423 WLTestCase.setUp(self)
424 xml = path.join(path.dirname(__file__), 'files/fraszka-do-anusie.xml')
425 self.book = models.Book.from_xml_file(xml)
427 def test_gen_pdf(self):
428 self.book.pdf_file.build()
429 book = models.Book.objects.get(pk=self.book.pk)
430 self.assertTrue(path.exists(book.pdf_file.path))
432 def test_gen_pdf_parent(self):
433 """This book contains a child."""
434 xml = path.join(path.dirname(__file__), "files/fraszki.xml")
435 parent = models.Book.from_xml_file(xml)
436 parent.pdf_file.build()
437 parent = models.Book.objects.get(pk=parent.pk)
438 self.assertTrue(path.exists(parent.pdf_file.path))
440 def test_custom_pdf(self):
441 from catalogue.tasks import build_custom_pdf
442 out = 'test-custom.pdf'
443 absoulute_path = path.join(settings.MEDIA_ROOT, out)
445 if not path.exists(path.dirname(absoulute_path)):
446 makedirs(path.dirname(absoulute_path))
448 build_custom_pdf(self.book.id,
449 customizations=['nofootnotes', '13pt', 'a4paper'], file_name=out)
450 self.assertTrue(path.exists(absoulute_path))