1 # -*- coding: utf-8 -*-
2 from catalogue import models
3 from catalogue.test_utils import *
4 from django.core.files.base import ContentFile
6 class BooksByTagTests(WLTestCase):
7 """ tests the /katalog/category/tag page for found books """
10 WLTestCase.setUp(self)
11 author = PersonStub(("Common",), "Man")
14 self.gchild_info = BookInfoStub(genre='Genre', epoch='Epoch', kind='Kind', author=author,
15 **info_args("GChild"))
17 self.child_info = BookInfoStub(genre='Genre', epoch='Epoch', kind='Other Kind', author=author,
18 parts=[self.gchild_info.url],
21 self.parent_info = BookInfoStub(genre='Genre', epoch='Epoch', kind='Kind', author=author,
22 parts=[self.child_info.url],
23 **info_args("Parent"))
25 self.book_file = ContentFile('<utwor />')
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)
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)
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')
42 context = self.client.get('/katalog/autor/empty/').context
43 self.assertEqual(0, len(context['object_list']))
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)
51 context = self.client.get('/katalog/gatunek/genre/').context
52 self.assertEqual([book.title for book in context['object_list']],
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']],
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']],
66 from django.test import Client
67 class TagRelatedTagsTests(WLTestCase):
68 """ tests the /katalog/category/tag/ page for related tags """
71 WLTestCase.setUp(self)
72 self.client = Client()
73 author = PersonStub(("Common",), "Man")
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"))
86 for info in gchild_info, child1_info, child2_info, parent_info:
87 book_text = """<utwor><opowiadanie><akap>
89 <motyw id="m01">Theme, %sTheme</motyw>
92 </akap></opowiadanie></utwor>
93 """ % info.title.encode('utf-8')
94 book = models.Book.from_text_and_meta(ContentFile(book_text), info)
97 tag_empty = models.Tag(name='Empty tag', slug='empty', category='author')
100 def test_empty(self):
101 """ empty tag should have no related tags """
103 cats = self.client.get('/katalog/autor/empty/').context['categories']
104 self.assertEqual(cats, {}, 'tags related to empty tag')
106 def test_has_related(self):
107 """ related own and descendants' tags should be generated """
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")
130 def test_related_differ(self):
131 """ related tags shouldn't include filtering tags """
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')
141 def test_parent_tag_once(self):
142 """ if parent and descendants have a common tag, count it only once """
144 cats = self.client.get('/katalog/rodzaj/kind/').context['categories']
145 self.assertEqual([(tag.name, tag.count) for tag in cats['epoch']],
147 'wrong related tag epoch tag on tag page')
150 def test_siblings_tags_count(self):
151 """ if children have tags and parent hasn't, count the children """
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')
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')
162 class CleanTagRelationTests(WLTestCase):
163 """ tests for tag relations cleaning after deleting things """
166 WLTestCase.setUp(self)
167 author = PersonStub(("Common",), "Man")
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
174 </akap></opowiadanie></utwor>
176 book = models.Book.from_text_and_meta(ContentFile(book_text), book_info)
178 def test_delete_objects(self):
179 """ there should be no related tags left after deleting some objects """
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")
189 def test_deleted_tag(self):
190 """ there should be no tag relations left after deleting tags """
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")
199 class TestIdenticalTag(WLTestCase):
202 WLTestCase.setUp(self)
203 author = PersonStub((), "Tag")
205 self.book_info = BookInfoStub(author=author,
210 self.book_text = """<utwor>
213 <begin id="m01" /><motyw id="m01">tag</motyw>Ala ma kota<end id="m01" />
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)
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']])
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'])
240 class BookTagsTests(WLTestCase):
241 """ tests the /katalog/lektura/book/ page for related tags """
244 WLTestCase.setUp(self)
245 author1 = PersonStub(("Common",), "Man")
246 author2 = PersonStub(("Jim",), "Lazy")
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"))
254 for info in child_info, parent_info:
255 book_text = """<utwor><opowiadanie><akap>
257 <motyw id="m01">Theme, %sTheme</motyw>
260 </akap></opowiadanie></utwor>
261 """ % info.title.encode('utf-8')
262 book = models.Book.from_text_and_meta(ContentFile(book_text), info)
264 def test_book_tags(self):
265 """ book should have own tags and whole tree's themes """
267 context = self.client.get('/katalog/lektura/parent/').context
269 self.assertEqual([tag.name for tag in context['categories']['author']],
271 self.assertEqual([tag.name for tag in context['categories']['kind']],
273 self.assertEqual([(tag.name, tag.count) for tag in context['book_themes']],
274 [('ChildTheme', 1), ('ParentTheme', 1), ('Theme', 2)])
276 def test_main_page_tags(self):
277 """ test main page tags and counts """
279 context = self.client.get('/katalog/').context
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)])