1 # -*- coding: utf-8 -*-
2 from __future__ import with_statement
4 from django.core.files.base import ContentFile, File
5 from catalogue.test_utils import *
6 from catalogue import models
7 from librarian import WLURI
9 from nose.tools import raises
11 from os import unlink, path, makedirs
13 class BookImportLogicTests(WLTestCase):
16 WLTestCase.setUp(self)
17 self.book_info = BookInfoStub(
18 url=WLURI.from_slug(u"default-book"),
19 about=u"http://wolnelektury.pl/example/URI/default_book",
20 title=u"Default Book",
21 author=PersonStub(("Jim",), "Lazy"),
28 self.expected_tags = [
29 ('author', 'jim-lazy'),
34 self.expected_tags.sort()
36 def test_empty_book(self):
37 BOOK_TEXT = "<utwor />"
38 book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info)
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 self.assertTrue(book.has_html_file())
73 def test_book_with_fragment(self):
74 BOOK_TEXT = """<utwor>
76 <akap><begin id="m01" /><motyw id="m01">Love</motyw>Ala ma kota<end id="m01" /></akap>
77 </opowiadanie></utwor>
80 book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info)
81 self.assertTrue(book.has_html_file())
83 self.assertEqual(book.fragments.count(), 1)
84 self.assertEqual(book.fragments.all()[0].text, u'<p class="paragraph">Ala ma kota</p>\n')
86 self.assert_(('theme', 'love') in [ (tag.category, tag.slug) for tag in book.fragments.all()[0].tags ])
88 def test_book_with_empty_theme(self):
89 """ empty themes should be ignored """
91 BOOK_TEXT = """<utwor>
93 <akap><begin id="m01" /><motyw id="m01"> , Love , , </motyw>Ala ma kota<end id="m01" /></akap>
94 </opowiadanie></utwor>
97 book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info)
98 self.assert_([('theme', 'love')],
99 [ (tag.category, tag.slug) for tag in book.fragments.all()[0].tags.filter(category='theme') ])
101 def test_book_with_no_theme(self):
102 """ fragments with no themes shouldn't be created at all """
104 BOOK_TEXT = """<utwor>
106 <akap><begin id="m01" /><motyw id="m01"></motyw>Ala ma kota<end id="m01" /></akap>
107 </opowiadanie></utwor>
110 book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info)
111 self.assertEqual(book.fragments.count(), 0)
112 self.assertEqual(book.tags.filter(category='theme').count(), 0)
115 def test_book_with_invalid_slug(self):
116 """ Book with invalid characters in slug shouldn't be imported """
117 self.book_info.url = WLURI.from_slug(u"default_book")
118 BOOK_TEXT = "<utwor />"
119 book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info)
121 def test_book_replace_title(self):
122 BOOK_TEXT = """<utwor />"""
123 book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info)
124 self.book_info.title = u"Extraordinary"
125 book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info, overwrite=True)
127 tags = [ (tag.category, tag.slug) for tag in book.tags ]
130 self.assertEqual(tags, self.expected_tags)
132 def test_book_replace_author(self):
133 BOOK_TEXT = """<utwor />"""
134 book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info)
135 self.book_info.author = PersonStub(("Hans", "Christian"), "Andersen")
136 book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info, overwrite=True)
138 tags = [ (tag.category, tag.slug) for tag in book.tags ]
141 self.expected_tags.remove(('author', 'jim-lazy'))
142 self.expected_tags.append(('author', 'hans-christian-andersen'))
143 self.expected_tags.sort()
145 self.assertEqual(tags, self.expected_tags)
147 # the old tag shouldn't disappear
148 models.Tag.objects.get(slug="jim-lazy", category="author")
150 def test_book_remove_fragment(self):
151 BOOK_TEXT = """<utwor>
154 <begin id="m01" /><motyw id="m01">Love</motyw>Ala ma kota<end id="m01" />
155 <begin id="m02" /><motyw id="m02">Hatred</motyw>To kot Ali<end id="m02" />
157 </opowiadanie></utwor>
159 BOOK_TEXT_AFTER = """<utwor>
162 <begin id="m01" /><motyw id="m01">Love</motyw>Ala ma kota<end id="m01" />
165 </opowiadanie></utwor>
168 book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info)
169 self.assertEqual(book.fragments.count(), 2)
170 book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT_AFTER), self.book_info, overwrite=True)
171 self.assertEqual(book.fragments.count(), 1)
173 def test_multiple_tags(self):
174 BOOK_TEXT = """<utwor />"""
175 self.book_info.authors = self.book_info.author, PersonStub(("Joe",), "Dilligent"),
176 self.book_info.kinds = self.book_info.kind, 'Y-Kind',
177 self.book_info.genres = self.book_info.genre, 'Y-Genre',
178 self.book_info.epochs = self.book_info.epoch, 'Y-Epoch',
180 self.expected_tags.extend([
181 ('author', 'joe-dilligent'),
182 ('genre', 'y-genre'),
183 ('epoch', 'y-epoch'),
186 self.expected_tags.sort()
188 book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info)
189 tags = [ (tag.category, tag.slug) for tag in book.tags ]
192 self.assertEqual(tags, self.expected_tags)
195 class ChildImportTests(WLTestCase):
198 WLTestCase.setUp(self)
199 self.child_info = BookInfoStub(
203 author=PersonStub(("Joe",), "Doe"),
207 self.parent_info = BookInfoStub(
211 author=PersonStub(("Jim",), "Lazy"),
212 parts=[self.child_info.url],
213 **info_args("Parent")
216 def test_child(self):
217 TEXT = """<utwor />"""
218 child = models.Book.from_text_and_meta(ContentFile(TEXT), self.child_info)
219 parent = models.Book.from_text_and_meta(ContentFile(TEXT), self.parent_info)
220 author = parent.tags.get(category='author')
221 books = self.client.get(author.get_absolute_url()).context['object_list']
222 self.assertEqual(len(books), 1,
223 "Only parent book should be visible on author's page")
225 def test_child_replace(self):
226 PARENT_TEXT = """<utwor />"""
227 CHILD_TEXT = """<utwor>
229 <akap><begin id="m01" /><motyw id="m01">Pies</motyw>Ala ma kota<end id="m01" /></akap>
230 </opowiadanie></utwor>
232 child = models.Book.from_text_and_meta(ContentFile(CHILD_TEXT), self.child_info)
233 parent = models.Book.from_text_and_meta(ContentFile(PARENT_TEXT), self.parent_info)
234 CHILD_TEXT = """<utwor>
236 <akap><begin id="m01" /><motyw id="m01">Kot</motyw>Ala ma kota<end id="m01" /></akap>
237 </opowiadanie></utwor>
239 child = models.Book.from_text_and_meta(ContentFile(CHILD_TEXT), self.child_info, overwrite=True)
241 themes = self.client.get(parent.get_absolute_url()).context['book_themes']
243 self.assertEqual(['Kot'], [tag.name for tag in themes],
244 'wrong related theme list')
247 class MultilingualBookImportTest(WLTestCase):
249 WLTestCase.setUp(self)
250 common_uri = WLURI.from_slug('common-slug')
252 self.pol_info = BookInfoStub(
256 author=PersonStub(("Joe",), "Doe"),
257 variant_of=common_uri,
258 **info_args(u"Książka")
261 self.eng_info = BookInfoStub(
265 author=PersonStub(("Joe",), "Doe"),
266 variant_of=common_uri,
267 **info_args("A book", "eng")
270 def test_multilingual_import(self):
271 BOOK_TEXT = """<utwor><opowiadanie><akap>A</akap></opowiadanie></utwor>"""
273 book1 = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.pol_info)
274 book2 = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.eng_info)
277 set([b.language for b in models.Book.objects.all()]),
279 'Books imported in wrong languages.'
283 class BookImportGenerateTest(WLTestCase):
285 WLTestCase.setUp(self)
286 input = path.join(path.dirname(__file__), 'files/fraszka-do-anusie.xml')
287 self.book = models.Book.from_xml_file(input)
289 def test_gen_pdf(self):
290 self.book.build_pdf()
291 self.assertTrue(path.exists(self.book.pdf_file.path))
293 def test_gen_pdf_parent(self):
294 """This book contains a child."""
295 input = path.join(path.dirname(__file__), "files/fraszki.xml")
296 parent = models.Book.from_xml_file(input)
298 self.assertTrue(path.exists(parent.pdf_file.path))
300 def test_custom_pdf(self):
301 out = models.get_dynamic_path(None, 'test-custom', ext='pdf')
302 absoulute_path = path.join(settings.MEDIA_ROOT, out)
304 if not path.exists(path.dirname(absoulute_path)):
305 makedirs(path.dirname(absoulute_path))
307 self.book.build_pdf(customizations=['nofootnotes', '13pt', 'a4paper'], file_name=out)
308 self.assertTrue(path.exists(absoulute_path))