+class TagRelatedTagsTests(TestCase):
+ """ tests the /katalog/tag/ page for related tags """
+
+ def setUp(self):
+ author = PersonStub(("Common",), "Man")
+
+ gchild_info = BookInfoStub(author=author, genre="GchildGenre", epoch='Epoch', kind="Kind",
+ **info_args(u"GChild"))
+ child1_info = BookInfoStub(author=author, genre="ChildGenre", epoch='Epoch', kind="ChildKind",
+ parts=[gchild_info.url],
+ **info_args(u"Child1"))
+ child2_info = BookInfoStub(author=author, genre="ChildGenre", epoch='Epoch', kind="ChildKind",
+ **info_args(u"Child2"))
+ parent_info = BookInfoStub(author=author, genre="Genre", epoch='Epoch', kind="Kind",
+ parts=[child1_info.url, child2_info.url],
+ **info_args(u"Parent"))
+
+ for info in gchild_info, child1_info, child2_info, parent_info:
+ book_text = """<utwor><opowiadanie><akap>
+ <begin id="m01" />
+ <motyw id="m01">Theme, %sTheme</motyw>
+ Ala ma kąta
+ <end id="m01" />
+ </akap></opowiadanie></utwor>
+ """ % info.title.encode('utf-8')
+ book = models.Book.from_text_and_meta(ContentFile(book_text), info)
+ book.save()
+
+ tag_empty = models.Tag(name='Empty tag', slug='empty', category='author')
+ tag_empty.save()
+
+ self.client = Client()
+
+
+ def tearDown(self):
+ for book in models.Book.objects.all():
+ if book.xml_file:
+ book.xml_file.delete()
+ if book.html_file:
+ book.html_file.delete()
+
+
+ def test_empty(self):
+ """ empty tag should have no related tags """
+
+ cats = self.client.get('/katalog/empty/').context['categories']
+ self.assertEqual(cats, {}, 'tags related to empty tag')
+
+
+ def test_has_related(self):
+ """ related own and descendants' tags should be generated """
+
+ cats = self.client.get('/katalog/kind/').context['categories']
+ self.assertTrue('Common Man' in [tag.name for tag in cats['author']],
+ 'missing `author` related tag')
+ self.assertTrue('Epoch' in [tag.name for tag in cats['epoch']],
+ 'missing `epoch` related tag')
+ self.assertTrue("ChildKind" in [tag.name for tag in cats['kind']],
+ "missing `kind` related tag")
+ self.assertTrue("Genre" in [tag.name for tag in cats['genre']],
+ 'missing `genre` related tag')
+ self.assertTrue("ChildGenre" in [tag.name for tag in cats['genre']],
+ "missing child's related tag")
+ self.assertTrue("GchildGenre" in [tag.name for tag in cats['genre']],
+ "missing grandchild's related tag")
+ self.assertTrue('Theme' in [tag.name for tag in cats['theme']],
+ "missing related theme")
+ self.assertTrue('Child1Theme' in [tag.name for tag in cats['theme']],
+ "missing child's related theme")
+ self.assertTrue('GChildTheme' in [tag.name for tag in cats['theme']],
+ "missing grandchild's related theme")
+
+
+ def test_related_differ(self):
+ """ related tags shouldn't include filtering tags """
+
+ cats = self.client.get('/katalog/kind/').context['categories']
+ self.assertFalse('Kind' in [tag.name for tag in cats['kind']],
+ 'filtering tag wrongly included in related')
+ cats = self.client.get('/katalog/theme/').context['categories']
+ self.assertFalse('Theme' in [tag.name for tag in cats['theme']],
+ 'filtering theme wrongly included in related')
+
+
+ def test_parent_tag_once(self):
+ """ if parent and descendants have a common tag, count it only once """
+
+ cats = self.client.get('/katalog/kind/').context['categories']
+ self.assertEqual([(tag.name, tag.count) for tag in cats['epoch']],
+ [('Epoch', 1)],
+ 'wrong related tag epoch tag on tag page')
+
+
+ def test_siblings_tags_add(self):
+ """ if children have tags and parent hasn't, count the children """
+
+ cats = self.client.get('/katalog/epoch/').context['categories']
+ self.assertTrue(('ChildKind', 2) in [(tag.name, tag.count) for tag in cats['kind']],
+ 'wrong related kind tags on tag page')
+
+ def test_themes_add(self):
+ """ all occurencies of theme should be counted """
+
+ cats = self.client.get('/katalog/epoch/').context['categories']
+ self.assertTrue(('Theme', 4) in [(tag.name, tag.count) for tag in cats['theme']],
+ 'wrong related theme count')
+
+
+