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.tags ])
81 def test_book_replace_title(self):
82 BOOK_TEXT = """<utwor />"""
83 book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info)
84 self.book_info.title = u"Extraordinary"
85 book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info)
87 tags = [ (tag.category, tag.slug) for tag in book.tags ]
90 self.assertEqual(tags, self.expected_tags)
92 def test_book_replace_author(self):
93 BOOK_TEXT = """<utwor />"""
94 book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info)
95 self.book_info.author = PersonStub(("Hans", "Christian"), "Andersen")
96 book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info)
98 tags = [ (tag.category, tag.slug) for tag in book.tags ]
101 self.expected_tags.remove(('author', 'jim-lazy'))
102 self.expected_tags.append(('author', 'hans-christian-andersen'))
103 self.expected_tags.sort()
105 self.assertEqual(tags, self.expected_tags)
107 # the old tag should disappear
108 self.assertRaises(models.Tag.DoesNotExist, models.Tag.objects.get,
109 slug="jim-lazy", category="author")