Add Book.ancestor m2m.
[wolnelektury.git] / apps / catalogue / tests / tags.py
1 # -*- coding: utf-8 -*-
2 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
3 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
4 #
5 from django.core.files.base import ContentFile
6 from django.test import Client
7 from catalogue import models
8 from catalogue.test_utils import *
9
10
11 class BooksByTagTests(WLTestCase):
12     """ tests the /katalog/category/tag page for found books """
13
14     def setUp(self):
15         WLTestCase.setUp(self)
16         author = PersonStub(("Common",), "Man")
17
18         # grandchild
19         self.gchild_info = BookInfoStub(genre='Genre', epoch='Epoch', kind='Kind', author=author,
20                                         **info_args("GChild"))
21         # child
22         self.child_info = BookInfoStub(genre='Genre', epoch='Epoch', kind='Other Kind', author=author,
23                                        parts=[self.gchild_info.url],
24                                        **info_args("Child"))
25         # parent
26         self.parent_info = BookInfoStub(genre='Genre', epoch='Epoch', kind='Kind', author=author,
27                                         parts=[self.child_info.url],
28                                         **info_args("Parent"))
29
30         self.book_file = ContentFile('<utwor />')
31
32     def test_nonexistent_tag(self):
33         """ Looking for a non-existent tag should yield 404 """
34         # NOTE: this yields a false positive, 'cause of URL change
35         self.assertEqual(404, self.client.get('/katalog/autor/czeslaw_milosz/').status_code)
36
37     def test_book_tag(self):
38         """ Looking for a book tag isn't permitted """
39         models.Book.from_text_and_meta(self.book_file, self.gchild_info)
40         self.assertEqual(404, self.client.get('/katalog/gchild/').status_code)
41
42     def test_tag_empty(self):
43         """ Tag with no books should return no books """
44         models.Book.from_text_and_meta(self.book_file, self.gchild_info)
45         models.Tag.objects.create(name='Empty tag', slug='empty', category='author')
46
47         context = self.client.get('/katalog/autor/empty/').context
48         self.assertEqual(0, len(context['object_list']))
49
50     def test_tag_eliminate(self):
51         """ Filtering by tag should only yield top-level qualifying books. """
52         for info in self.gchild_info, self.child_info, self.parent_info:
53             models.Book.from_text_and_meta(self.book_file, info)
54
55         # all three qualify
56         context = self.client.get('/katalog/gatunek/genre/').context
57         self.assertEqual([book.title for book in context['object_list']],
58                          ['Parent'])
59
60         # parent and gchild qualify, child doesn't
61         context = self.client.get('/katalog/rodzaj/kind/').context
62         self.assertEqual([book.title for book in context['object_list']],
63                          ['Parent'])
64
65         # Filtering by child's tag should yield the child
66         context = self.client.get('/katalog/rodzaj/other-kind/').context
67         self.assertEqual([book.title for book in context['object_list']],
68                          ['Child'])
69
70
71 class TagRelatedTagsTests(WLTestCase):
72     """ tests the /katalog/category/tag/ page for related tags """
73
74     def setUp(self):
75         WLTestCase.setUp(self)
76         self.client = Client()
77         author = PersonStub(("Common",), "Man")
78
79         gchild_info = BookInfoStub(author=author, genre="GchildGenre", epoch='Epoch', kind="Kind",
80                                    **info_args(u"GChild"))
81         child1_info = BookInfoStub(author=author, genre="ChildGenre", epoch='Epoch', kind="ChildKind",
82                                    parts=[gchild_info.url],
83                                    **info_args(u"Child1"))
84         child2_info = BookInfoStub(author=author, genre="ChildGenre", epoch='Epoch', kind="ChildKind",
85                                    **info_args(u"Child2"))
86         parent_info = BookInfoStub(author=author, genre="Genre", epoch='Epoch', kind="Kind",
87                                    parts=[child1_info.url, child2_info.url],
88                                    **info_args(u"Parent"))
89
90         for info in gchild_info, child1_info, child2_info, parent_info:
91             book_text = """<utwor><opowiadanie><akap>
92                 <begin id="m01" />
93                     <motyw id="m01">Theme, %sTheme</motyw>
94                     Ala ma kota
95                 <end id="m01" />
96                 </akap></opowiadanie></utwor>
97                 """ % info.title.encode('utf-8')
98             book = models.Book.from_text_and_meta(ContentFile(book_text), info)
99             book.save()
100
101         tag_empty = models.Tag(name='Empty tag', slug='empty', category='author')
102         tag_empty.save()
103
104     def test_empty(self):
105         """ empty tag should have no related tags """
106
107         cats = self.client.get('/katalog/autor/empty/').context['categories']
108         self.assertEqual(cats, {}, 'tags related to empty tag')
109
110     def test_has_related(self):
111         """ related own and descendants' tags should be generated """
112
113         cats = self.client.get('/katalog/rodzaj/kind/').context['categories']
114         self.assertTrue('Common Man' in [tag.name for tag in cats['author']],
115                         'missing `author` related tag')
116         self.assertTrue('Epoch' in [tag.name for tag in cats['epoch']],
117                         'missing `epoch` related tag')
118         self.assertFalse("kind" in cats,
119                         "There should be no child-only related `kind` tags")
120         self.assertTrue("Genre" in [tag.name for tag in cats['genre']],
121                         'missing `genre` related tag')
122         self.assertFalse("ChildGenre" in [tag.name for tag in cats['genre']],
123                         "There should be no child-only related `genre` tags")
124         self.assertTrue("GchildGenre" in [tag.name for tag in cats['genre']],
125                         "missing grandchild's related tag")
126         self.assertTrue('Theme' in [tag.name for tag in cats['theme']],
127                         "missing related theme")
128         self.assertFalse('Child1Theme' in [tag.name for tag in cats['theme']],
129                         "There should be no child-only related `theme` tags")
130         self.assertTrue('GChildTheme' in [tag.name for tag in cats['theme']],
131                         "missing grandchild's related theme")
132
133     def test_related_differ(self):
134         """ related tags shouldn't include filtering tags """
135
136         response = self.client.get('/katalog/rodzaj/kind/')
137         cats = response.context['categories']
138         self.assertFalse('kind' in cats,
139                          'filtering tag wrongly included in related')
140         cats = self.client.get('/katalog/motyw/theme/').context['categories']
141         self.assertFalse('Theme' in [tag.name for tag in cats['theme']],
142                          'filtering theme wrongly included in related')
143
144     def test_parent_tag_once(self):
145         """ if parent and descendants have a common tag, count it only once """
146
147         cats = self.client.get('/katalog/rodzaj/kind/').context['categories']
148         self.assertEqual([(tag.name, tag.count) for tag in cats['epoch']],
149                          [('Epoch', 1)],
150                          'wrong related tag epoch tag on tag page')
151
152
153     def test_siblings_tags_count(self):
154         """ if children have tags and parent hasn't, count the children """
155
156         cats = self.client.get('/katalog/epoka/epoch/').context['categories']
157         self.assertTrue(('ChildKind', 2) in [(tag.name, tag.count) for tag in cats['kind']],
158                     'wrong related kind tags on tag page')
159
160         # all occurencies of theme should be counted
161         self.assertTrue(('Theme', 4) in [(tag.name, tag.count) for tag in cats['theme']],
162                     'wrong related theme count')
163
164
165 class CleanTagRelationTests(WLTestCase):
166     """ tests for tag relations cleaning after deleting things """
167
168     def setUp(self):
169         WLTestCase.setUp(self)
170         author = PersonStub(("Common",), "Man")
171
172         book_info = BookInfoStub(author=author, genre="G", epoch='E', kind="K",
173                                    **info_args(u"Book"))
174         book_text = """<utwor><opowiadanie><akap>
175             <begin id="m01" /><motyw id="m01">Theme</motyw>Ala ma kota
176             <end id="m01" />
177             </akap></opowiadanie></utwor>
178             """
179         self.book = models.Book.from_text_and_meta(ContentFile(book_text), book_info)
180
181     def test_delete_objects(self):
182         """ there should be no related tags left after deleting some objects """
183
184         models.Book.objects.all().delete()
185         cats = self.client.get('/katalog/rodzaj/k/').context['categories']
186         self.assertEqual(cats, {})
187         self.assertEqual(models.Fragment.objects.all().count(), 0,
188                          "orphaned fragments left")
189         self.assertEqual(models.Tag.intermediary_table_model.objects.all().count(), 0,
190                          "orphaned TagRelation objects left")
191
192     def test_deleted_tag(self):
193         """ there should be no tag relations left after deleting tags """
194
195         models.Tag.objects.all().delete()
196         self.assertEqual(len(self.book.related_themes()), 0)
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         book = models.Book.from_text_and_meta(ContentFile(self.book_text), self.book_info)
225
226         related_themes = book.related_themes()
227         for category in 'author', 'kind', 'genre', 'epoch':
228             self.assertTrue('tag' in [tag.slug for tag in book.tags.filter(category=category)],
229                             'missing related tag for %s' % category)
230         self.assertTrue('tag' in [tag.slug for tag in related_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             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         book = models.Book.objects.get(slug='parent')
270         related_themes = book.related_themes()
271
272         self.assertEqual([t.slug for t in book.tags.filter(category='author')],
273                          ['common-man'])
274         self.assertEqual([t.slug for t in book.tags.filter(category='kind')],
275                          ['kind'])
276         self.assertEqual([(tag.name, tag.count) for tag in related_themes],
277                          [('ChildTheme', 1), ('ParentTheme', 1), ('Theme', 2)])
278
279     def test_catalogue_tags(self):
280         """ test main page tags and counts """
281         context = self.client.get('/katalog/').context
282         self.assertEqual([(tag.name, tag.count) for tag in context['categories']['author']],
283                          [('Jim Lazy', 1), ('Common Man', 1)])
284         self.assertEqual([(tag.name, tag.count) for tag in context['fragment_tags']],
285                          [('ChildTheme', 1), ('ParentTheme', 1), ('Theme', 2)])
286