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.
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 *
11 class BooksByTagTests(WLTestCase):
12 """ tests the /katalog/category/tag page for found books """
15 WLTestCase.setUp(self)
16 author = PersonStub(("Common",), "Man")
19 self.gchild_info = BookInfoStub(genre='Genre', epoch='Epoch', kind='Kind', author=author,
20 **info_args("GChild"))
22 self.child_info = BookInfoStub(genre='Genre', epoch='Epoch', kind='Other Kind', author=author,
23 parts=[self.gchild_info.url],
26 self.parent_info = BookInfoStub(genre='Genre', epoch='Epoch', kind='Kind', author=author,
27 parts=[self.child_info.url],
28 **info_args("Parent"))
30 self.book_file = ContentFile('<utwor />')
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)
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)
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')
47 context = self.client.get('/katalog/autor/empty/').context
48 self.assertEqual(0, len(context['object_list']))
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)
56 context = self.client.get('/katalog/gatunek/genre/').context
57 self.assertEqual([book.title for book in context['object_list']],
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']],
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']],
71 class TagRelatedTagsTests(WLTestCase):
72 """ tests the /katalog/category/tag/ page for related tags """
75 WLTestCase.setUp(self)
76 self.client = Client()
77 author = PersonStub(("Common",), "Man")
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"))
90 for info in gchild_info, child1_info, child2_info, parent_info:
91 book_text = """<utwor><opowiadanie><akap>
93 <motyw id="m01">Theme, %sTheme</motyw>
96 </akap></opowiadanie></utwor>
97 """ % info.title.encode('utf-8')
98 book = models.Book.from_text_and_meta(ContentFile(book_text), info)
101 tag_empty = models.Tag(name='Empty tag', slug='empty', category='author')
104 def test_empty(self):
105 """ empty tag should have no related tags """
107 cats = self.client.get('/katalog/autor/empty/').context['categories']
108 self.assertEqual(cats, {}, 'tags related to empty tag')
110 def test_has_related(self):
111 """ related own and descendants' tags should be generated """
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.assertTrue("ChildKind" in [tag.name for tag in cats['kind']],
119 "missing `kind` related tag")
120 self.assertTrue("Genre" in [tag.name for tag in cats['genre']],
121 'missing `genre` related tag')
122 self.assertTrue("ChildGenre" in [tag.name for tag in cats['genre']],
123 "missing child's related tag")
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.assertTrue('Child1Theme' in [tag.name for tag in cats['theme']],
129 "missing child's related theme")
130 self.assertTrue('GChildTheme' in [tag.name for tag in cats['theme']],
131 "missing grandchild's related theme")
134 def test_related_differ(self):
135 """ related tags shouldn't include filtering tags """
137 response = self.client.get('/katalog/rodzaj/kind/')
138 cats = response.context['categories']
139 self.assertFalse('Kind' in [tag.name for tag in cats['kind']],
140 'filtering tag wrongly included in related')
141 cats = self.client.get('/katalog/motyw/theme/').context['categories']
142 self.assertFalse('Theme' in [tag.name for tag in cats['theme']],
143 'filtering theme wrongly included in related')
145 def test_parent_tag_once(self):
146 """ if parent and descendants have a common tag, count it only once """
148 cats = self.client.get('/katalog/rodzaj/kind/').context['categories']
149 self.assertEqual([(tag.name, tag.count) for tag in cats['epoch']],
151 'wrong related tag epoch tag on tag page')
154 def test_siblings_tags_count(self):
155 """ if children have tags and parent hasn't, count the children """
157 cats = self.client.get('/katalog/epoka/epoch/').context['categories']
158 self.assertTrue(('ChildKind', 2) in [(tag.name, tag.count) for tag in cats['kind']],
159 'wrong related kind tags on tag page')
161 # all occurencies of theme should be counted
162 self.assertTrue(('Theme', 4) in [(tag.name, tag.count) for tag in cats['theme']],
163 'wrong related theme count')
166 class CleanTagRelationTests(WLTestCase):
167 """ tests for tag relations cleaning after deleting things """
170 WLTestCase.setUp(self)
171 author = PersonStub(("Common",), "Man")
173 book_info = BookInfoStub(author=author, genre="G", epoch='E', kind="K",
174 **info_args(u"Book"))
175 book_text = """<utwor><opowiadanie><akap>
176 <begin id="m01" /><motyw id="m01">Theme</motyw>Ala ma kota
178 </akap></opowiadanie></utwor>
180 self.book = models.Book.from_text_and_meta(ContentFile(book_text), book_info)
182 def test_delete_objects(self):
183 """ there should be no related tags left after deleting some objects """
185 models.Book.objects.all().delete()
186 cats = self.client.get('/katalog/rodzaj/k/').context['categories']
187 self.assertEqual(cats, {})
188 self.assertEqual(models.Fragment.objects.all().count(), 0,
189 "orphaned fragments left")
190 self.assertEqual(models.Tag.intermediary_table_model.objects.all().count(), 0,
191 "orphaned TagRelation objects left")
193 def test_deleted_tag(self):
194 """ there should be no tag relations left after deleting tags """
196 models.Tag.objects.all().delete()
197 self.assertEqual(len(self.book.related_info()['tags']), 0)
198 self.assertEqual(len(self.book.related_themes()), 0)
199 self.assertEqual(models.Tag.intermediary_table_model.objects.all().count(), 0,
200 "orphaned TagRelation objects left")
203 class TestIdenticalTag(WLTestCase):
206 WLTestCase.setUp(self)
207 author = PersonStub((), "Tag")
209 self.book_info = BookInfoStub(author=author,
214 self.book_text = """<utwor>
217 <begin id="m01" /><motyw id="m01">tag</motyw>Ala ma kota<end id="m01" />
224 def test_book_tags(self):
225 """ there should be all related tags in relevant categories """
226 book = models.Book.from_text_and_meta(ContentFile(self.book_text), self.book_info)
228 related_info = book.related_info()
229 related_themes = book.related_themes()
230 for category in 'author', 'kind', 'genre', 'epoch':
231 self.assertTrue('tag' in [tag['slug'] for tag in related_info['tags'][category]],
232 'missing related tag for %s' % category)
233 self.assertTrue('tag' in [tag.slug for tag in related_themes])
235 def test_qualified_url(self):
236 models.Book.from_text_and_meta(ContentFile(self.book_text), self.book_info)
237 categories = {'author': 'autor', 'theme': 'motyw', 'epoch': 'epoka', 'kind':'rodzaj', 'genre':'gatunek'}
238 for cat, localcat in categories.iteritems():
239 context = self.client.get('/katalog/%s/tag/' % localcat).context
240 self.assertEqual(1, len(context['object_list']))
241 self.assertNotEqual({}, context['categories'])
242 self.assertFalse(cat in context['categories'])
245 class BookTagsTests(WLTestCase):
246 """ tests the /katalog/lektura/book/ page for related tags """
249 WLTestCase.setUp(self)
250 author1 = PersonStub(("Common",), "Man")
251 author2 = PersonStub(("Jim",), "Lazy")
253 child_info = BookInfoStub(authors=(author1, author2), genre="ChildGenre", epoch='Epoch', kind="ChildKind",
254 **info_args(u"Child"))
255 parent_info = BookInfoStub(author=author1, genre="Genre", epoch='Epoch', kind="Kind",
256 parts=[child_info.url],
257 **info_args(u"Parent"))
259 for info in child_info, parent_info:
260 book_text = """<utwor><opowiadanie><akap>
262 <motyw id="m01">Theme, %sTheme</motyw>
265 </akap></opowiadanie></utwor>
266 """ % info.title.encode('utf-8')
267 models.Book.from_text_and_meta(ContentFile(book_text), info)
269 def test_book_tags(self):
270 """ book should have own tags and whole tree's themes """
272 book = models.Book.objects.get(slug='parent')
273 related_info = book.related_info()
274 related_themes = book.related_themes()
276 self.assertEqual([t['slug'] for t in related_info['tags']['author']],
278 self.assertEqual([t['slug'] for t in related_info['tags']['kind']],
280 self.assertEqual([(tag.name, tag.count) for tag in related_themes],
281 [('ChildTheme', 1), ('ParentTheme', 1), ('Theme', 2)])
283 def test_catalogue_tags(self):
284 """ test main page tags and counts """
285 context = self.client.get('/katalog/').context
286 self.assertEqual([(tag.name, tag.book_count) for tag in context['categories']['author']],
287 [('Jim Lazy', 1), ('Common Man', 1)])
288 self.assertEqual([(tag.name, tag.book_count) for tag in context['categories']['theme']],
289 [('ChildTheme', 1), ('ParentTheme', 1), ('Theme', 2)])