1 # -*- coding: utf-8 -*-
2 from catalogue import models
3 from catalogue.test_utils import *
4 from django.core.files.base import ContentFile
6 from nose.tools import raises
9 class BooksByTagTests(WLTestCase):
10 """ tests the /katalog/tag page for found books """
13 WLTestCase.setUp(self)
14 author = PersonStub(("Common",), "Man")
17 self.gchild_info = BookInfoStub(genre='Genre', epoch='Epoch', kind='Kind', author=author,
18 **info_args("GChild"))
20 self.child_info = BookInfoStub(genre='Genre', epoch='Epoch', kind='Other Kind', author=author,
21 parts=[self.gchild_info.url],
24 self.parent_info = BookInfoStub(genre='Genre', epoch='Epoch', kind='Kind', author=author,
25 parts=[self.child_info.url],
26 **info_args("Parent"))
28 self.book_file = ContentFile('<utwor />')
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/czeslaw_milosz/').status_code)
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)
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')
45 context = self.client.get('/katalog/empty/').context
46 self.assertEqual(0, len(context['object_list']))
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)
54 context = self.client.get('/katalog/genre/').context
55 self.assertEqual([book.title for book in context['object_list']],
58 # parent and gchild qualify, child doesn't
59 context = self.client.get('/katalog/kind/').context
60 self.assertEqual([book.title for book in context['object_list']],
63 # Filtering by child's tag should yield the child
64 context = self.client.get('/katalog/other-kind/').context
65 self.assertEqual([book.title for book in context['object_list']],
70 class TagRelatedTagsTests(WLTestCase):
71 """ tests the /katalog/tag/ page for related tags """
74 WLTestCase.setUp(self)
75 author = PersonStub(("Common",), "Man")
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"))
88 for info in gchild_info, child1_info, child2_info, parent_info:
89 book_text = """<utwor><opowiadanie><akap>
91 <motyw id="m01">Theme, %sTheme</motyw>
94 </akap></opowiadanie></utwor>
95 """ % info.title.encode('utf-8')
96 book = models.Book.from_text_and_meta(ContentFile(book_text), info)
99 tag_empty = models.Tag(name='Empty tag', slug='empty', category='author')
102 def test_empty(self):
103 """ empty tag should have no related tags """
105 cats = self.client.get('/katalog/empty/').context['categories']
106 self.assertEqual(cats, {}, 'tags related to empty tag')
108 def test_has_related(self):
109 """ related own and descendants' tags should be generated """
111 cats = self.client.get('/katalog/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")
132 def test_related_differ(self):
133 """ related tags shouldn't include filtering tags """
135 cats = self.client.get('/katalog/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/theme/').context['categories']
139 self.assertFalse('Theme' in [tag.name for tag in cats['theme']],
140 'filtering theme wrongly included in related')
143 def test_parent_tag_once(self):
144 """ if parent and descendants have a common tag, count it only once """
146 cats = self.client.get('/katalog/kind/').context['categories']
147 self.assertEqual([(tag.name, tag.count) for tag in cats['epoch']],
149 'wrong related tag epoch tag on tag page')
152 def test_siblings_tags_count(self):
153 """ if children have tags and parent hasn't, count the children """
155 cats = self.client.get('/katalog/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')
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')
164 class CleanTagRelationTests(WLTestCase):
165 """ tests for tag relations cleaning after deleting things """
168 WLTestCase.setUp(self)
169 author = PersonStub(("Common",), "Man")
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
176 </akap></opowiadanie></utwor>
178 book = models.Book.from_text_and_meta(ContentFile(book_text), book_info)
180 def test_delete_objects(self):
181 """ there should be no related tags left after deleting some objects """
183 models.Book.objects.all().delete()
184 cats = self.client.get('/katalog/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")
191 def test_deleted_tag(self):
192 """ there should be no tag relations left after deleting tags """
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")
201 class TestIdenticalTag(WLTestCase):
204 WLTestCase.setUp(self)
205 author = PersonStub((), "Tag")
207 self.book_info = BookInfoStub(author=author,
212 self.book_text = """<utwor>
215 <begin id="m01" /><motyw id="m01">tag</motyw>Ala ma kota<end id="m01" />
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)
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']])
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'])
242 class BookTagsTests(WLTestCase):
243 """ tests the /katalog/lektura/book/ page for related tags """
246 WLTestCase.setUp(self)
247 author1 = PersonStub(("Common",), "Man")
248 author2 = PersonStub(("Jim",), "Lazy")
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"))
256 for info in child_info, parent_info:
257 book_text = """<utwor><opowiadanie><akap>
259 <motyw id="m01">Theme, %sTheme</motyw>
262 </akap></opowiadanie></utwor>
263 """ % info.title.encode('utf-8')
264 book = models.Book.from_text_and_meta(ContentFile(book_text), info)
266 def test_book_tags(self):
267 """ book should have own tags and whole tree's themes """
269 context = self.client.get('/katalog/lektura/parent/').context
271 self.assertEqual([tag.name for tag in context['categories']['author']],
273 self.assertEqual([tag.name for tag in context['categories']['kind']],
275 self.assertEqual([(tag.name, tag.count) for tag in context['book_themes']],
276 [('ChildTheme', 1), ('ParentTheme', 1), ('Theme', 2)])
278 def test_main_page_tags(self):
279 """ test main page tags and counts """
281 context = self.client.get('/katalog/').context
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)])