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_book_remove_fragment(self):
147 BOOK_TEXT = """<utwor>
150 <begin id="m01" /><motyw id="m01">Love</motyw>Ala ma kota<end id="m01" />
151 <begin id="m02" /><motyw id="m02">Hatred</motyw>To kot Ali<end id="m02" />
153 </opowiadanie></utwor>
155 BOOK_TEXT_AFTER = """<utwor>
158 <begin id="m01" /><motyw id="m01">Love</motyw>Ala ma kota<end id="m01" />
161 </opowiadanie></utwor>
164 book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info)
165 self.assertEqual(book.fragments.count(), 2)
166 book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT_AFTER), self.book_info, overwrite=True)
167 self.assertEqual(book.fragments.count(), 1)
169 def test_multiple_tags(self):
170 BOOK_TEXT = """<utwor />"""
171 self.book_info.authors = self.book_info.author, PersonStub(("Joe",), "Dilligent"),
172 self.book_info.kinds = self.book_info.kind, 'Y-Kind',
173 self.book_info.genres = self.book_info.genre, 'Y-Genre',
174 self.book_info.epochs = self.book_info.epoch, 'Y-Epoch',
176 self.expected_tags.extend([
177 ('author', 'joe-dilligent'),
178 ('genre', 'y-genre'),
179 ('epoch', 'y-epoch'),
182 self.expected_tags.sort()
184 book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info)
185 tags = [ (tag.category, tag.slug) for tag in book.tags ]
188 self.assertEqual(tags, self.expected_tags)
191 class ChildImportTests(WLTestCase):
194 WLTestCase.setUp(self)
195 self.child_info = BookInfoStub(
199 author=PersonStub(("Joe",), "Doe"),
203 self.parent_info = BookInfoStub(
207 author=PersonStub(("Jim",), "Lazy"),
208 parts=[self.child_info.url],
209 **info_args("Parent")
212 def test_child_replace(self):
213 PARENT_TEXT = """<utwor />"""
214 CHILD_TEXT = """<utwor>
216 <akap><begin id="m01" /><motyw id="m01">Pies</motyw>Ala ma kota<end id="m01" /></akap>
217 </opowiadanie></utwor>
219 child = models.Book.from_text_and_meta(ContentFile(CHILD_TEXT), self.child_info)
220 parent = models.Book.from_text_and_meta(ContentFile(PARENT_TEXT), self.parent_info)
221 CHILD_TEXT = """<utwor>
223 <akap><begin id="m01" /><motyw id="m01">Kot</motyw>Ala ma kota<end id="m01" /></akap>
224 </opowiadanie></utwor>
226 child = models.Book.from_text_and_meta(ContentFile(CHILD_TEXT), self.child_info, overwrite=True)
228 themes = self.client.get(parent.get_absolute_url()).context['book_themes']
230 self.assertEqual(['Kot'], [tag.name for tag in themes],
231 'wrong related theme list')