1 # -*- coding: utf-8 -*-
2 from django.core.files.base import ContentFile
3 from catalogue.test_utils import *
4 from catalogue import models
6 class BookImportLogicTests(WLTestCase):
10 self.book_info = BookInfoStub(
11 url=u"http://wolnelektury.pl/example/default_book",
12 about=u"http://wolnelektury.pl/example/URI/default_book",
13 title=u"Default Book",
14 author=PersonStub(("Jim",), "Lazy"),
20 self.expected_tags = [
21 ('author', 'jim-lazy'),
26 self.expected_tags.sort()
28 def test_empty_book(self):
29 BOOK_TEXT = "<utwor />"
30 book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info)
32 self.assertEqual(book.title, "Default Book")
33 self.assertEqual(book.slug, "default_book")
34 self.assert_(book.parent is None)
35 self.assertFalse(book.has_html_file())
37 # no fragments generated
38 self.assertEqual(book.fragments.count(), 0)
40 # TODO: this should be filled out probably...
41 self.assertEqual(book.wiki_link, '')
42 self.assertEqual(book.gazeta_link, '')
43 self.assertEqual(book._short_html, '')
44 self.assertEqual(book.description, '')
46 tags = [ (tag.category, tag.slug) for tag in book.tags ]
49 self.assertEqual(tags, self.expected_tags)
51 def test_not_quite_empty_book(self):
52 """ Not empty, but without any real text.
54 Should work like any other non-empty book.
57 BOOK_TEXT = """<utwor>
59 <nazwa_utworu>Nic</nazwa_utworu>
63 book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info)
64 self.assertTrue(book.has_html_file())
66 def test_book_with_fragment(self):
67 BOOK_TEXT = """<utwor>
69 <akap><begin id="m01" /><motyw id="m01">Love</motyw>Ala ma kota<end id="m01" /></akap>
70 </opowiadanie></utwor>
73 book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info)
74 self.assertTrue(book.has_html_file())
76 self.assertEqual(book.fragments.count(), 1)
77 self.assertEqual(book.fragments.all()[0].text, u'<p class="paragraph">Ala ma kota</p>\n')
79 self.assert_(('theme', 'love') in [ (tag.category, tag.slug) for tag in book.fragments.all()[0].tags ])
81 def test_book_with_empty_theme(self):
82 """ empty themes should be ignored """
84 BOOK_TEXT = """<utwor>
86 <akap><begin id="m01" /><motyw id="m01"> , Love , , </motyw>Ala ma kota<end id="m01" /></akap>
87 </opowiadanie></utwor>
90 book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info)
91 self.assert_([('theme', 'love')],
92 [ (tag.category, tag.slug) for tag in book.fragments.all()[0].tags.filter(category='theme') ])
94 def test_book_with_no_theme(self):
95 """ fragments with no themes shouldn't be created at all """
97 BOOK_TEXT = """<utwor>
99 <akap><begin id="m01" /><motyw id="m01"></motyw>Ala ma kota<end id="m01" /></akap>
100 </opowiadanie></utwor>
103 book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info)
104 self.assertEqual(book.fragments.count(), 0)
105 self.assertEqual(book.tags.filter(category='theme').count(), 0)
107 def test_book_replace_title(self):
108 BOOK_TEXT = """<utwor />"""
109 book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info)
110 self.book_info.title = u"Extraordinary"
111 book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info, overwrite=True)
113 tags = [ (tag.category, tag.slug) for tag in book.tags ]
116 self.assertEqual(tags, self.expected_tags)
118 def test_book_replace_author(self):
119 BOOK_TEXT = """<utwor />"""
120 book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info)
121 self.book_info.author = PersonStub(("Hans", "Christian"), "Andersen")
122 book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info, overwrite=True)
124 tags = [ (tag.category, tag.slug) for tag in book.tags ]
127 self.expected_tags.remove(('author', 'jim-lazy'))
128 self.expected_tags.append(('author', 'hans-christian-andersen'))
129 self.expected_tags.sort()
131 self.assertEqual(tags, self.expected_tags)
133 # the old tag shouldn't disappear
134 models.Tag.objects.get(slug="jim-lazy", category="author")
136 def test_multiple_tags(self):
137 BOOK_TEXT = """<utwor />"""
138 self.book_info.authors = self.book_info.author, PersonStub(("Joe",), "Dilligent"),
139 self.book_info.kinds = self.book_info.kind, 'Y-Kind',
140 self.book_info.genres = self.book_info.genre, 'Y-Genre',
141 self.book_info.epochs = self.book_info.epoch, 'Y-Epoch',
143 self.expected_tags.extend([
144 ('author', 'joe-dilligent'),
145 ('genre', 'y-genre'),
146 ('epoch', 'y-epoch'),
149 self.expected_tags.sort()
151 book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info)
152 tags = [ (tag.category, tag.slug) for tag in book.tags ]
155 self.assertEqual(tags, self.expected_tags)