change in multilingual
[wolnelektury.git] / apps / catalogue / tests / tags.py
1 # -*- coding: utf-8 -*-
2 from catalogue import models
3 from catalogue.test_utils import *
4 from django.core.files.base import ContentFile
5
6 class BooksByTagTests(WLTestCase):
7     """ tests the /katalog/category/tag page for found books """
8
9     def setUp(self):
10         WLTestCase.setUp(self)
11         author = PersonStub(("Common",), "Man")
12
13         # grandchild
14         self.gchild_info = BookInfoStub(genre='Genre', epoch='Epoch', kind='Kind', author=author,
15                                         **info_args("GChild"))
16         # child
17         self.child_info = BookInfoStub(genre='Genre', epoch='Epoch', kind='Other Kind', author=author,
18                                        parts=[self.gchild_info.url],
19                                        **info_args("Child"))
20         # parent
21         self.parent_info = BookInfoStub(genre='Genre', epoch='Epoch', kind='Kind', author=author,
22                                         parts=[self.child_info.url],
23                                         **info_args("Parent"))
24
25         self.book_file = ContentFile('<utwor />')
26
27     def test_nonexistent_tag(self):
28         """ Looking for a non-existent tag should yield 404 """
29         # NOTE: this yields a false positive, 'cause of URL change
30         self.assertEqual(404, self.client.get('/katalog/autor/czeslaw_milosz/').status_code)
31
32     def test_book_tag(self):
33         """ Looking for a book tag isn't permitted """
34         models.Book.from_text_and_meta(self.book_file, self.gchild_info)
35         self.assertEqual(404, self.client.get('/katalog/gchild/').status_code)
36
37     def test_tag_empty(self):
38         """ Tag with no books should return no books """
39         models.Book.from_text_and_meta(self.book_file, self.gchild_info)
40         models.Tag.objects.create(name='Empty tag', slug='empty', category='author')
41
42         context = self.client.get('/katalog/autor/empty/').context
43         self.assertEqual(0, len(context['object_list']))
44
45     def test_tag_eliminate(self):
46         """ Filtering by tag should only yield top-level qualifying books. """
47         for info in self.gchild_info, self.child_info, self.parent_info:
48             models.Book.from_text_and_meta(self.book_file, info)
49
50         # all three qualify
51         context = self.client.get('/katalog/gatunek/genre/').context
52         self.assertEqual([book.title for book in context['object_list']],
53                          ['Parent'])
54
55         # parent and gchild qualify, child doesn't
56         context = self.client.get('/katalog/rodzaj/kind/').context
57         self.assertEqual([book.title for book in context['object_list']],
58                          ['Parent'])
59
60         # Filtering by child's tag should yield the child
61         context = self.client.get('/katalog/rodzaj/other-kind/').context
62         self.assertEqual([book.title for book in context['object_list']],
63                          ['Child'])
64
65
66 from django.test import Client
67 class TagRelatedTagsTests(WLTestCase):
68     """ tests the /katalog/category/tag/ page for related tags """
69
70     def setUp(self):
71         WLTestCase.setUp(self)
72         self.client = Client()
73         author = PersonStub(("Common",), "Man")
74
75         gchild_info = BookInfoStub(author=author, genre="GchildGenre", epoch='Epoch', kind="Kind",
76                                    **info_args(u"GChild"))
77         child1_info = BookInfoStub(author=author, genre="ChildGenre", epoch='Epoch', kind="ChildKind",
78                                    parts=[gchild_info.url],
79                                    **info_args(u"Child1"))
80         child2_info = BookInfoStub(author=author, genre="ChildGenre", epoch='Epoch', kind="ChildKind",
81                                    **info_args(u"Child2"))
82         parent_info = BookInfoStub(author=author, genre="Genre", epoch='Epoch', kind="Kind",
83                                    parts=[child1_info.url, child2_info.url],
84                                    **info_args(u"Parent"))
85
86         for info in gchild_info, child1_info, child2_info, parent_info:
87             book_text = """<utwor><opowiadanie><akap>
88                 <begin id="m01" />
89                     <motyw id="m01">Theme, %sTheme</motyw>
90                     Ala ma kota
91                 <end id="m01" />
92                 </akap></opowiadanie></utwor>
93                 """ % info.title.encode('utf-8')
94             book = models.Book.from_text_and_meta(ContentFile(book_text), info)
95             book.save()
96
97         tag_empty = models.Tag(name='Empty tag', slug='empty', category='author')
98         tag_empty.save()
99
100     def test_empty(self):
101         """ empty tag should have no related tags """
102
103         cats = self.client.get('/katalog/autor/empty/').context['categories']
104         self.assertEqual(cats, {}, 'tags related to empty tag')
105
106     def test_has_related(self):
107         """ related own and descendants' tags should be generated """
108
109         cats = self.client.get('/katalog/rodzaj/kind/').context['categories']
110         self.assertTrue('Common Man' in [tag.name for tag in cats['author']],
111                         'missing `author` related tag')
112         self.assertTrue('Epoch' in [tag.name for tag in cats['epoch']],
113                         'missing `epoch` related tag')
114         self.assertTrue("ChildKind" in [tag.name for tag in cats['kind']],
115                         "missing `kind` related tag")
116         self.assertTrue("Genre" in [tag.name for tag in cats['genre']],
117                         'missing `genre` related tag')
118         self.assertTrue("ChildGenre" in [tag.name for tag in cats['genre']],
119                         "missing child's related tag")
120         self.assertTrue("GchildGenre" in [tag.name for tag in cats['genre']],
121                         "missing grandchild's related tag")
122         self.assertTrue('Theme' in [tag.name for tag in cats['theme']],
123                         "missing related theme")
124         self.assertTrue('Child1Theme' in [tag.name for tag in cats['theme']],
125                         "missing child's related theme")
126         self.assertTrue('GChildTheme' in [tag.name for tag in cats['theme']],
127                         "missing grandchild's related theme")
128
129
130     def test_related_differ(self):
131         """ related tags shouldn't include filtering tags """
132
133         response = self.client.get('/katalog/rodzaj/kind/')
134         cats = response.context['categories']
135         self.assertFalse('Kind' in [tag.name for tag in cats['kind']],
136                          'filtering tag wrongly included in related')
137         cats = self.client.get('/katalog/motyw/theme/').context['categories']
138         self.assertFalse('Theme' in [tag.name for tag in cats['theme']],
139                          'filtering theme wrongly included in related')
140
141     def test_parent_tag_once(self):
142         """ if parent and descendants have a common tag, count it only once """
143
144         cats = self.client.get('/katalog/rodzaj/kind/').context['categories']
145         self.assertEqual([(tag.name, tag.count) for tag in cats['epoch']],
146                          [('Epoch', 1)],
147                          'wrong related tag epoch tag on tag page')
148
149
150     def test_siblings_tags_count(self):
151         """ if children have tags and parent hasn't, count the children """
152
153         cats = self.client.get('/katalog/epoka/epoch/').context['categories']
154         self.assertTrue(('ChildKind', 2) in [(tag.name, tag.count) for tag in cats['kind']],
155                     'wrong related kind tags on tag page')
156
157         # all occurencies of theme should be counted
158         self.assertTrue(('Theme', 4) in [(tag.name, tag.count) for tag in cats['theme']],
159                     'wrong related theme count')
160
161
162 class CleanTagRelationTests(WLTestCase):
163     """ tests for tag relations cleaning after deleting things """
164
165     def setUp(self):
166         WLTestCase.setUp(self)
167         author = PersonStub(("Common",), "Man")
168
169         book_info = BookInfoStub(author=author, genre="G", epoch='E', kind="K",
170                                    **info_args(u"Book"))
171         book_text = """<utwor><opowiadanie><akap>
172             <begin id="m01" /><motyw id="m01">Theme</motyw>Ala ma kota
173             <end id="m01" />
174             </akap></opowiadanie></utwor>
175             """
176         book = models.Book.from_text_and_meta(ContentFile(book_text), book_info)
177
178     def test_delete_objects(self):
179         """ there should be no related tags left after deleting some objects """
180
181         models.Book.objects.all().delete()
182         cats = self.client.get('/katalog/rodzaj/k/').context['categories']
183         self.assertEqual(cats, {})
184         self.assertEqual(models.Fragment.objects.all().count(), 0,
185                          "orphaned fragments left")
186         self.assertEqual(models.Tag.intermediary_table_model.objects.all().count(), 0,
187                          "orphaned TagRelation objects left")
188
189     def test_deleted_tag(self):
190         """ there should be no tag relations left after deleting tags """
191
192         models.Tag.objects.all().delete()
193         cats = self.client.get('/katalog/lektura/book/').context['categories']
194         self.assertEqual(cats, {})
195         self.assertEqual(models.Tag.intermediary_table_model.objects.all().count(), 0,
196                          "orphaned TagRelation objects left")
197
198
199 class TestIdenticalTag(WLTestCase):
200
201     def setUp(self):
202         WLTestCase.setUp(self)
203         author = PersonStub((), "Tag")
204
205         self.book_info = BookInfoStub(author=author,
206                                  genre="tag",
207                                  epoch='tag',
208                                  kind="tag",
209                                    **info_args(u"tag"))
210         self.book_text = """<utwor>
211             <opowiadanie>
212             <akap>
213                 <begin id="m01" /><motyw id="m01">tag</motyw>Ala ma kota<end id="m01" />
214             </akap>
215             </opowiadanie>
216             </utwor>
217         """
218
219
220     def test_book_tags(self):
221         """ there should be all related tags in relevant categories """
222         models.Book.from_text_and_meta(ContentFile(self.book_text), self.book_info)
223
224         context = self.client.get('/katalog/lektura/tag/').context
225         for category in 'author', 'kind', 'genre', 'epoch':
226             self.assertTrue('tag' in [tag.slug for tag in context['categories'][category]],
227                             'missing related tag for %s' % category)
228         self.assertTrue('tag' in [tag.slug for tag in context['book_themes']])
229
230     def test_qualified_url(self):
231         models.Book.from_text_and_meta(ContentFile(self.book_text), self.book_info)
232         categories = {'author': 'autor', 'theme': 'motyw', 'epoch': 'epoka', 'kind':'rodzaj', 'genre':'gatunek'}
233         for cat, localcat in categories.iteritems():
234             context = self.client.get('/katalog/%s/tag/' % localcat).context
235             self.assertEqual(1, len(context['object_list']))
236             self.assertNotEqual({}, context['categories'])
237             self.assertFalse(cat in context['categories'])
238
239
240 class BookTagsTests(WLTestCase):
241     """ tests the /katalog/lektura/book/ page for related tags """
242
243     def setUp(self):
244         WLTestCase.setUp(self)
245         author1 = PersonStub(("Common",), "Man")
246         author2 = PersonStub(("Jim",), "Lazy")
247
248         child_info = BookInfoStub(authors=(author1, author2), genre="ChildGenre", epoch='Epoch', kind="ChildKind",
249                                    **info_args(u"Child"))
250         parent_info = BookInfoStub(author=author1, genre="Genre", epoch='Epoch', kind="Kind",
251                                    parts=[child_info.url],
252                                    **info_args(u"Parent"))
253
254         for info in child_info, parent_info:
255             book_text = """<utwor><opowiadanie><akap>
256                 <begin id="m01" />
257                     <motyw id="m01">Theme, %sTheme</motyw>
258                     Ala ma kota
259                 <end id="m01" />
260                 </akap></opowiadanie></utwor>
261                 """ % info.title.encode('utf-8')
262             book = models.Book.from_text_and_meta(ContentFile(book_text), info)
263
264     def test_book_tags(self):
265         """ book should have own tags and whole tree's themes """
266
267         context = self.client.get('/katalog/lektura/parent/').context
268
269         self.assertEqual([tag.name for tag in context['categories']['author']],
270                          ['Common Man'])
271         self.assertEqual([tag.name for tag in context['categories']['kind']],
272                          ['Kind'])
273         self.assertEqual([(tag.name, tag.count) for tag in context['book_themes']],
274                          [('ChildTheme', 1), ('ParentTheme', 1), ('Theme', 2)])
275
276     def test_main_page_tags(self):
277         """ test main page tags and counts """
278
279         context = self.client.get('/katalog/').context
280
281         self.assertEqual([(tag.name, tag.count) for tag in context['categories']['author']],
282                          [('Jim Lazy', 1), ('Common Man', 1)])
283         self.assertEqual([(tag.name, tag.count) for tag in context['fragment_tags']],
284                          [('ChildTheme', 1), ('ParentTheme', 1), ('Theme', 2)])
285