cb63fc06901042371b562ad60c72a1a55ced008e
[wolnelektury.git] / apps / catalogue / tests / book_import.py
1 # -*- coding: utf-8 -*-
2 from django.core.files.base import ContentFile
3 from catalogue.test_utils import *
4 from catalogue import models
5
6 class BookImportLogicTests(WLTestCase):
7
8     def setUp(self):
9         WLTestCase.setUp(self)
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"),
15             kind="X-Kind",
16             genre="X-Genre",
17             epoch="X-Epoch",
18         )
19
20         self.expected_tags = [
21            ('author', 'jim-lazy'),
22            ('genre', 'x-genre'),
23            ('epoch', 'x-epoch'),
24            ('kind', 'x-kind'),
25         ]
26         self.expected_tags.sort()
27
28     def test_empty_book(self):
29         BOOK_TEXT = "<utwor />"
30         book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info)
31
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())
36
37         # no fragments generated
38         self.assertEqual(book.fragments.count(), 0)
39
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, '')
45
46         tags = [ (tag.category, tag.slug) for tag in book.tags ]
47         tags.sort()
48
49         self.assertEqual(tags, self.expected_tags)
50
51     def test_not_quite_empty_book(self):
52         """ Not empty, but without any real text.
53
54         Should work like any other non-empty book.
55         """
56
57         BOOK_TEXT = """<utwor>
58         <liryka_l>
59             <nazwa_utworu>Nic</nazwa_utworu>
60         </liryka_l></utwor>
61         """
62
63         book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info)
64         self.assertTrue(book.has_html_file())
65
66     def test_book_with_fragment(self):
67         BOOK_TEXT = """<utwor>
68         <opowiadanie>
69             <akap><begin id="m01" /><motyw id="m01">Love</motyw>Ala ma kota<end id="m01" /></akap>
70         </opowiadanie></utwor>
71         """
72
73         book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info)
74         self.assertTrue(book.has_html_file())
75
76         self.assertEqual(book.fragments.count(), 1)
77         self.assertEqual(book.fragments.all()[0].text, u'<p class="paragraph">Ala ma kota</p>\n')
78
79         self.assert_(('theme', 'love') in [ (tag.category, tag.slug) for tag in book.fragments.all()[0].tags ])
80
81     def test_book_with_empty_theme(self):
82         """ empty themes should be ignored """
83
84         BOOK_TEXT = """<utwor>
85         <opowiadanie>
86             <akap><begin id="m01" /><motyw id="m01"> , Love , , </motyw>Ala ma kota<end id="m01" /></akap>
87         </opowiadanie></utwor>
88         """
89
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') ])
93
94     def test_book_with_no_theme(self):
95         """ fragments with no themes shouldn't be created at all """
96
97         BOOK_TEXT = """<utwor>
98         <opowiadanie>
99             <akap><begin id="m01" /><motyw id="m01"></motyw>Ala ma kota<end id="m01" /></akap>
100         </opowiadanie></utwor>
101         """
102
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)
106
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)
112
113         tags = [ (tag.category, tag.slug) for tag in book.tags ]
114         tags.sort()
115
116         self.assertEqual(tags, self.expected_tags)
117
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)
123
124         tags = [ (tag.category, tag.slug) for tag in book.tags ]
125         tags.sort()
126
127         self.expected_tags.remove(('author', 'jim-lazy'))
128         self.expected_tags.append(('author', 'hans-christian-andersen'))
129         self.expected_tags.sort()
130
131         self.assertEqual(tags, self.expected_tags)
132
133         # the old tag shouldn't disappear
134         models.Tag.objects.get(slug="jim-lazy", category="author")
135
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',
142
143         self.expected_tags.extend([
144            ('author', 'joe-dilligent'),
145            ('genre', 'y-genre'),
146            ('epoch', 'y-epoch'),
147            ('kind', 'y-kind'),
148         ])
149         self.expected_tags.sort()
150
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 ]
153         tags.sort()
154
155         self.assertEqual(tags, self.expected_tags)