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