1 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
4 from django.core.files.base import ContentFile
5 from catalogue.test_utils import *
6 from catalogue import models
7 from librarian import WLURI
9 from os import path, makedirs
12 class BookImportLogicTests(WLTestCase):
15 WLTestCase.setUp(self)
16 self.book_info = BookInfoStub(
17 url=WLURI.from_slug("default-book"),
18 about="http://wolnelektury.pl/example/URI/default_book",
20 author=PersonStub(("Jim",), "Lazy"),
27 self.expected_tags = [
28 ('author', 'jim-lazy'),
33 self.expected_tags.sort()
35 def test_empty_book(self):
36 book_text = "<utwor />"
37 book = models.Book.from_text_and_meta(ContentFile(book_text), self.book_info)
38 book.refresh_from_db()
40 self.assertEqual(book.title, "Default Book")
41 self.assertEqual(book.slug, "default-book")
42 self.assert_(book.parent is None)
43 self.assertFalse(book.has_html_file())
45 # no fragments generated
46 self.assertEqual(book.fragments.count(), 0)
48 # TODO: this should be filled out probably...
49 self.assertEqual(book.wiki_link, '')
50 self.assertEqual(book.gazeta_link, '')
51 self.assertEqual(book.description, '')
53 tags = [(tag.category, tag.slug) for tag in book.tags]
56 self.assertEqual(tags, self.expected_tags)
58 def test_not_quite_empty_book(self):
59 """ Not empty, but without any real text.
61 Should work like any other non-empty book.
64 book_text = """<utwor>
66 <nazwa_utworu>Nic</nazwa_utworu>
70 book = models.Book.from_text_and_meta(ContentFile(book_text), self.book_info)
71 book.refresh_from_db()
72 self.assertTrue(book.has_html_file())
74 def test_book_with_fragment(self):
75 book_text = """<utwor>
77 <akap><begin id="m01" /><motyw id="m01">Love</motyw>Ala ma kota<end id="m01" /></akap>
78 </opowiadanie></utwor>
81 book = models.Book.from_text_and_meta(ContentFile(book_text), self.book_info)
82 book.refresh_from_db()
83 self.assertTrue(book.has_html_file())
85 self.assertEqual(book.fragments.count(), 1)
86 self.assertEqual(book.fragments.all()[0].text, '<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 book.fragments.all()[0].tags.filter(category='theme').values_list('category', 'slug'))
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)
116 def test_book_with_invalid_slug(self):
117 """ Book with invalid characters in slug shouldn't be imported """
118 self.book_info.url = WLURI.from_slug("default_book")
119 book_text = "<utwor />"
120 with self.assertRaises(ValueError):
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 models.Book.from_text_and_meta(ContentFile(book_text), self.book_info)
126 self.book_info.title = "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 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, "Only parent book should be visible on author's page")
226 def test_child_replace(self):
227 parent_text = """<utwor />"""
228 child_text = """<utwor>
230 <akap><begin id="m01" /><motyw id="m01">Pies</motyw>Ala ma kota<end id="m01" /></akap>
231 </opowiadanie></utwor>
233 child = models.Book.from_text_and_meta(ContentFile(child_text), self.child_info)
234 parent = models.Book.from_text_and_meta(ContentFile(parent_text), self.parent_info)
235 child_text = """<utwor>
237 <akap><begin id="m01" /><motyw id="m01">Kot</motyw>Ala ma kota<end id="m01" /></akap>
238 </opowiadanie></utwor>
240 child = models.Book.from_text_and_meta(ContentFile(child_text), self.child_info, overwrite=True)
241 themes = parent.related_themes()
242 self.assertEqual(['Kot'], [tag.name for tag in themes], 'wrong related theme list')
245 class TreeImportTest(WLTestCase):
247 WLTestCase.setUp(self)
248 self.child_info = BookInfoStub(
252 author=PersonStub(("Joe",), "Doe"),
255 self.CHILD_TEXT = """<utwor>
257 <akap><begin id="m01" /><motyw id="m01">Pies</motyw>
258 Ala ma kota<end id="m01" /></akap>
259 </opowiadanie></utwor>
261 self.child = models.Book.from_text_and_meta(
262 ContentFile(self.CHILD_TEXT), self.child_info)
264 self.book_info = BookInfoStub(
268 author=PersonStub(("Joe",), "Doe"),
269 parts=[self.child_info.url],
272 self.BOOK_TEXT = """<utwor />"""
273 self.book = models.Book.from_text_and_meta(
274 ContentFile(self.BOOK_TEXT), self.book_info)
276 self.parent_info = BookInfoStub(
280 author=PersonStub(("Jim",), "Lazy"),
281 parts=[self.book_info.url],
282 **info_args("Parent")
284 self.PARENT_TEXT = """<utwor />"""
285 self.parent = models.Book.from_text_and_meta(
286 ContentFile(self.PARENT_TEXT), self.parent_info)
290 list(self.client.get('/katalog/gatunek/x-genre/').context['object_list']),
292 "There should be only parent on common tag page."
294 # pies = models.Tag.objects.get(slug='pies')
295 themes = self.parent.related_themes()
296 self.assertEqual(len(themes), 1, "There should be child theme in parent theme counter.")
297 # TODO: book_count is deprecated, update here.
298 # epoch = models.Tag.objects.get(slug='x-epoch')
299 # self.assertEqual(epoch.book_count, 1, "There should be only parent in common tag's counter.")
301 def test_child_republish(self):
302 child_text = """<utwor>
304 <akap><begin id="m01" /><motyw id="m01">Pies, Kot</motyw>
305 Ala ma kota<end id="m01" /></akap>
306 </opowiadanie></utwor>
308 models.Book.from_text_and_meta(
309 ContentFile(child_text), self.child_info, overwrite=True)
311 list(self.client.get('/katalog/gatunek/x-genre/').context['object_list']),
313 "There should only be parent on common tag page."
315 # pies = models.Tag.objects.get(slug='pies')
316 # kot = models.Tag.objects.get(slug='kot')
317 self.assertEqual(len(self.parent.related_themes()), 2,
318 "There should be child themes in parent theme counter.")
319 # TODO: book_count is deprecated, update here.
320 # epoch = models.Tag.objects.get(slug='x-epoch')
321 # self.assertEqual(epoch.book_count, 1, "There should only be parent in common tag's counter.")
323 def test_book_change_child(self):
324 second_child_info = BookInfoStub(
328 author=PersonStub(("Joe",), "Doe"),
329 **info_args("Second Child")
331 second_child_text = """<utwor>
333 <akap><begin id="m01" /><motyw id="m01">Kot</motyw>
334 Ala ma kota<end id="m01" /></akap>
335 </opowiadanie></utwor>
337 # Import a second child.
338 second_child = models.Book.from_text_and_meta(
339 ContentFile(second_child_text), second_child_info)
340 # The book has only this new child now.
341 self.book_info.parts = [second_child_info.url]
342 self.book = models.Book.from_text_and_meta(
343 ContentFile(self.BOOK_TEXT), self.book_info, overwrite=True)
346 set(self.client.get('/katalog/gatunek/x-genre/').context['object_list']),
347 {self.parent, self.child},
348 "There should be parent and old child on common tag page."
350 # kot = models.Tag.objects.get(slug='kot')
351 self.assertEqual(len(self.parent.related_themes()), 1,
352 "There should only be new child themes in parent theme counter.")
353 # # book_count deprecated, update test.
354 # epoch = models.Tag.objects.get(slug='x-epoch')
355 # self.assertEqual(epoch.book_count, 2,
356 # "There should be parent and old child in common tag's counter.")
358 list(self.client.get('/katalog/lektura/parent/motyw/kot/').context['fragments']),
359 [second_child.fragments.all()[0]],
360 "There should be new child's fragments on parent's theme page."
363 list(self.client.get('/katalog/lektura/parent/motyw/pies/').context['fragments']),
365 "There should be no old child's fragments on parent's theme page."
369 class MultilingualBookImportTest(WLTestCase):
371 WLTestCase.setUp(self)
372 common_uri = WLURI.from_slug('common-slug')
374 self.pol_info = BookInfoStub(
378 author=PersonStub(("Joe",), "Doe"),
379 variant_of=common_uri,
380 **info_args("Książka")
383 self.eng_info = BookInfoStub(
387 author=PersonStub(("Joe",), "Doe"),
388 variant_of=common_uri,
389 **info_args("A book", "eng")
392 def test_multilingual_import(self):
393 book_text = """<utwor><opowiadanie><akap>A</akap></opowiadanie></utwor>"""
395 models.Book.from_text_and_meta(ContentFile(book_text), self.pol_info)
396 models.Book.from_text_and_meta(ContentFile(book_text), self.eng_info)
399 set([b.language for b in models.Book.objects.all()]),
401 'Books imported in wrong languages.'
405 class BookImportGenerateTest(WLTestCase):
407 WLTestCase.setUp(self)
408 xml = path.join(path.dirname(__file__), 'files/fraszka-do-anusie.xml')
409 self.book = models.Book.from_xml_file(xml)
411 def test_gen_pdf(self):
412 self.book.pdf_file.build()
413 book = models.Book.objects.get(pk=self.book.pk)
414 self.assertTrue(path.exists(book.pdf_file.path))
416 def test_gen_pdf_parent(self):
417 """This book contains a child."""
418 xml = path.join(path.dirname(__file__), "files/fraszki.xml")
419 parent = models.Book.from_xml_file(xml)
420 parent.pdf_file.build()
421 parent = models.Book.objects.get(pk=parent.pk)
422 self.assertTrue(path.exists(parent.pdf_file.path))
424 def test_custom_pdf(self):
425 from catalogue.tasks import build_custom_pdf
426 out = 'test-custom.pdf'
427 absoulute_path = path.join(settings.MEDIA_ROOT, out)
429 if not path.exists(path.dirname(absoulute_path)):
430 makedirs(path.dirname(absoulute_path))
432 build_custom_pdf(self.book.id, customizations=['nofootnotes', '13pt', 'a4paper'], file_name=out)
433 self.assertTrue(path.exists(absoulute_path))