allow multiple tags (#303)
[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.tags ])
80
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, overwrite=True)
86
87         tags = [ (tag.category, tag.slug) for tag in book.tags ]
88         tags.sort()
89
90         self.assertEqual(tags, self.expected_tags)
91
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, overwrite=True)
97
98         tags = [ (tag.category, tag.slug) for tag in book.tags ]
99         tags.sort()
100
101         self.expected_tags.remove(('author', 'jim-lazy'))
102         self.expected_tags.append(('author', 'hans-christian-andersen'))
103         self.expected_tags.sort()
104
105         self.assertEqual(tags, self.expected_tags)
106
107         # the old tag shouldn't disappear
108         models.Tag.objects.get(slug="jim-lazy", category="author")
109
110     def test_multiple_tags(self):
111         BOOK_TEXT = """<utwor />"""
112         self.book_info.authors = self.book_info.author, PersonStub(("Joe",), "Dilligent"),
113         self.book_info.kinds = self.book_info.kind, 'Y-Kind',
114         self.book_info.genres = self.book_info.genre, 'Y-Genre',
115         self.book_info.epochs = self.book_info.epoch, 'Y-Epoch',
116
117         self.expected_tags.extend([
118            ('author', 'joe-dilligent'),
119            ('genre', 'y-genre'),
120            ('epoch', 'y-epoch'),
121            ('kind', 'y-kind'),
122         ])
123         self.expected_tags.sort()
124
125         book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info)
126         tags = [ (tag.category, tag.slug) for tag in book.tags ]
127         tags.sort()
128
129         self.assertEqual(tags, self.expected_tags)