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
8 from nose.tools import raises
10 from os import unlink,path
12 class BookImportLogicTests(WLTestCase):
15 WLTestCase.setUp(self)
16 self.book_info = BookInfoStub(
17 url=u"http://wolnelektury.pl/example/default-book",
18 about=u"http://wolnelektury.pl/example/URI/default_book",
19 title=u"Default Book",
20 author=PersonStub(("Jim",), "Lazy"),
26 self.expected_tags = [
27 ('author', 'jim-lazy'),
32 self.expected_tags.sort()
34 def test_empty_book(self):
35 BOOK_TEXT = "<utwor />"
36 book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info)
38 self.assertEqual(book.title, "Default Book")
39 self.assertEqual(book.slug, "default-book")
40 self.assert_(book.parent is None)
41 self.assertFalse(book.has_html_file())
43 # no fragments generated
44 self.assertEqual(book.fragments.count(), 0)
46 # TODO: this should be filled out probably...
47 self.assertEqual(book.wiki_link, '')
48 self.assertEqual(book.gazeta_link, '')
49 self.assertEqual(book.description, '')
51 tags = [ (tag.category, tag.slug) for tag in book.tags ]
54 self.assertEqual(tags, self.expected_tags)
56 def test_not_quite_empty_book(self):
57 """ Not empty, but without any real text.
59 Should work like any other non-empty book.
62 BOOK_TEXT = """<utwor>
64 <nazwa_utworu>Nic</nazwa_utworu>
68 book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info)
69 self.assertTrue(book.has_html_file())
71 def test_book_with_fragment(self):
72 BOOK_TEXT = """<utwor>
74 <akap><begin id="m01" /><motyw id="m01">Love</motyw>Ala ma kota<end id="m01" /></akap>
75 </opowiadanie></utwor>
78 book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info)
79 self.assertTrue(book.has_html_file())
81 self.assertEqual(book.fragments.count(), 1)
82 self.assertEqual(book.fragments.all()[0].text, u'<p class="paragraph">Ala ma kota</p>\n')
84 self.assert_(('theme', 'love') in [ (tag.category, tag.slug) for tag in book.fragments.all()[0].tags ])
86 def test_book_with_empty_theme(self):
87 """ empty themes should be ignored """
89 BOOK_TEXT = """<utwor>
91 <akap><begin id="m01" /><motyw id="m01"> , Love , , </motyw>Ala ma kota<end id="m01" /></akap>
92 </opowiadanie></utwor>
95 book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info)
96 self.assert_([('theme', 'love')],
97 [ (tag.category, tag.slug) for tag in book.fragments.all()[0].tags.filter(category='theme') ])
99 def test_book_with_no_theme(self):
100 """ fragments with no themes shouldn't be created at all """
102 BOOK_TEXT = """<utwor>
104 <akap><begin id="m01" /><motyw id="m01"></motyw>Ala ma kota<end id="m01" /></akap>
105 </opowiadanie></utwor>
108 book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info)
109 self.assertEqual(book.fragments.count(), 0)
110 self.assertEqual(book.tags.filter(category='theme').count(), 0)
113 def test_book_with_invalid_slug(self):
114 """ Book with invalid characters in slug shouldn't be imported """
115 self.book_info.url = "http://wolnelektury.pl/example/default_book"
116 BOOK_TEXT = "<utwor />"
117 book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info)
119 def test_book_replace_title(self):
120 BOOK_TEXT = """<utwor />"""
121 book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info)
122 self.book_info.title = u"Extraordinary"
123 book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info, overwrite=True)
125 tags = [ (tag.category, tag.slug) for tag in book.tags ]
128 self.assertEqual(tags, self.expected_tags)
130 def test_book_replace_author(self):
131 BOOK_TEXT = """<utwor />"""
132 book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info)
133 self.book_info.author = PersonStub(("Hans", "Christian"), "Andersen")
134 book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info, overwrite=True)
136 tags = [ (tag.category, tag.slug) for tag in book.tags ]
139 self.expected_tags.remove(('author', 'jim-lazy'))
140 self.expected_tags.append(('author', 'hans-christian-andersen'))
141 self.expected_tags.sort()
143 self.assertEqual(tags, self.expected_tags)
145 # the old tag shouldn't disappear
146 models.Tag.objects.get(slug="jim-lazy", category="author")
148 def test_book_remove_fragment(self):
149 BOOK_TEXT = """<utwor>
152 <begin id="m01" /><motyw id="m01">Love</motyw>Ala ma kota<end id="m01" />
153 <begin id="m02" /><motyw id="m02">Hatred</motyw>To kot Ali<end id="m02" />
155 </opowiadanie></utwor>
157 BOOK_TEXT_AFTER = """<utwor>
160 <begin id="m01" /><motyw id="m01">Love</motyw>Ala ma kota<end id="m01" />
163 </opowiadanie></utwor>
166 book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info)
167 self.assertEqual(book.fragments.count(), 2)
168 book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT_AFTER), self.book_info, overwrite=True)
169 self.assertEqual(book.fragments.count(), 1)
171 def test_multiple_tags(self):
172 BOOK_TEXT = """<utwor />"""
173 self.book_info.authors = self.book_info.author, PersonStub(("Joe",), "Dilligent"),
174 self.book_info.kinds = self.book_info.kind, 'Y-Kind',
175 self.book_info.genres = self.book_info.genre, 'Y-Genre',
176 self.book_info.epochs = self.book_info.epoch, 'Y-Epoch',
178 self.expected_tags.extend([
179 ('author', 'joe-dilligent'),
180 ('genre', 'y-genre'),
181 ('epoch', 'y-epoch'),
184 self.expected_tags.sort()
186 book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info)
187 tags = [ (tag.category, tag.slug) for tag in book.tags ]
190 self.assertEqual(tags, self.expected_tags)
193 class ChildImportTests(WLTestCase):
196 WLTestCase.setUp(self)
197 self.child_info = BookInfoStub(
201 author=PersonStub(("Joe",), "Doe"),
205 self.parent_info = BookInfoStub(
209 author=PersonStub(("Jim",), "Lazy"),
210 parts=[self.child_info.url],
211 **info_args("Parent")
214 def test_child_replace(self):
215 PARENT_TEXT = """<utwor />"""
216 CHILD_TEXT = """<utwor>
218 <akap><begin id="m01" /><motyw id="m01">Pies</motyw>Ala ma kota<end id="m01" /></akap>
219 </opowiadanie></utwor>
221 child = models.Book.from_text_and_meta(ContentFile(CHILD_TEXT), self.child_info)
222 parent = models.Book.from_text_and_meta(ContentFile(PARENT_TEXT), self.parent_info)
223 CHILD_TEXT = """<utwor>
225 <akap><begin id="m01" /><motyw id="m01">Kot</motyw>Ala ma kota<end id="m01" /></akap>
226 </opowiadanie></utwor>
228 child = models.Book.from_text_and_meta(ContentFile(CHILD_TEXT), self.child_info, overwrite=True)
230 themes = self.client.get(parent.get_absolute_url()).context['book_themes']
232 self.assertEqual(['Kot'], [tag.name for tag in themes],
233 'wrong related theme list')
236 class BookImportGenerateTest(WLTestCase):
238 WLTestCase.setUp(self)
239 self.book_info = BookInfoStub(
240 url=u"http://wolnelektury.pl/example/default-book",
241 about=u"http://wolnelektury.pl/example/URI/default_book",
242 title=u"Default Book",
243 author=PersonStub(("Jim",), "Lazy"),
249 self.expected_tags = [
250 ('author', 'jim-lazy'),
251 ('genre', 'x-genre'),
252 ('epoch', 'x-epoch'),
255 self.expected_tags.sort()
257 def test_gen_pdf(self):
258 input = open(path.dirname(__file__) + '/but-w-butonierce-but-w-butonierce.xml')
259 book = models.Book.from_text_and_meta(File(input), self.book_info, overwrite=True)
261 self.assertTrue(path.exists(book.pdf_file.path))
263 def test_gen_pdf_child(self):
264 input = open(path.dirname(__file__) + "/fraszka-do-anusie.xml")
265 book = models.Book.from_text_and_meta(File(input), self.book_info, overwrite=True)
267 self.assertTrue(path.exists(book.pdf_file.path))