fix in librarian
[wolnelektury.git] / apps / catalogue / tests / tags.py
index 17f9a77..42ea6e2 100644 (file)
@@ -1,4 +1,7 @@
 # -*- coding: utf-8 -*-
+# This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
+# Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
+#
 from django.core.files.base import ContentFile
 from django.test import Client
 from catalogue import models
@@ -28,8 +31,7 @@ class BooksByTagTests(WLTestCase):
 
     def test_nonexistent_tag(self):
         """ Looking for a non-existent tag should yield 404 """
-        # NOTE: this yields a false positive, 'cause of URL change
-        self.assertEqual(404, self.client.get('/katalog/autor/czeslaw_milosz/').status_code)
+        self.assertEqual(404, self.client.get('/katalog/autor/czeslaw-milosz/').status_code)
 
     def test_book_tag(self):
         """ Looking for a book tag isn't permitted """
@@ -102,7 +104,8 @@ class TagRelatedTagsTests(WLTestCase):
         """ empty tag should have no related tags """
 
         cats = self.client.get('/katalog/autor/empty/').context['categories']
-        self.assertEqual(cats, {}, 'tags related to empty tag')
+        self.assertEqual({k: v for (k, v) in cats.items() if v}, {},
+            'tags related to empty tag')
 
     def test_has_related(self):
         """ related own and descendants' tags should be generated """
@@ -112,28 +115,27 @@ class TagRelatedTagsTests(WLTestCase):
                         '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.assertFalse(cats.get("kind", False),
+                        "There should be no child-only related `kind` tags")
         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.assertFalse("ChildGenre" in [tag.name for tag in cats['genre']],
+                        "There should be no child-only related `genre` tags")
         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.assertFalse('Child1Theme' in [tag.name for tag in cats['theme']],
+                        "There should be no child-only related `theme` tags")
         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 """
 
         response = self.client.get('/katalog/rodzaj/kind/')
         cats = response.context['categories']
-        self.assertFalse('Kind' in [tag.name for tag in cats['kind']],
+        self.assertFalse(cats.get('kind', False),
                          'filtering tag wrongly included in related')
         cats = self.client.get('/katalog/motyw/theme/').context['categories']
         self.assertFalse('Theme' in [tag.name for tag in cats['theme']],
@@ -153,12 +155,23 @@ class TagRelatedTagsTests(WLTestCase):
 
         cats = self.client.get('/katalog/epoka/epoch/').context['categories']
         self.assertTrue(('ChildKind', 2) in [(tag.name, tag.count) for tag in cats['kind']],
-                    'wrong related kind tags on tag page')
+                    'wrong related kind tags on tag page, got: ' +
+                    unicode([(tag.name, tag.count) for tag in cats['kind']]))
 
         # all occurencies of theme should be counted
         self.assertTrue(('Theme', 4) in [(tag.name, tag.count) for tag in cats['theme']],
                     'wrong related theme count')
 
+    def test_query_child_tag(self):
+        """
+        If child and parent have a common tag, but parent isn't included
+        in the result, child should still count.
+        """
+        cats = self.client.get('/katalog/gatunek/childgenre/').context['categories']
+        self.assertTrue(('Epoch', 2) in [(tag.name, tag.count) for tag in cats['epoch']],
+                    'wrong related kind tags on tag page, got: ' +
+                    unicode([(tag.name, tag.count) for tag in cats['epoch']]))
+
 
 class CleanTagRelationTests(WLTestCase):
     """ tests for tag relations cleaning after deleting things """
@@ -181,7 +194,7 @@ class CleanTagRelationTests(WLTestCase):
 
         models.Book.objects.all().delete()
         cats = self.client.get('/katalog/rodzaj/k/').context['categories']
-        self.assertEqual(cats, {})
+        self.assertEqual({k: v for (k, v) in cats.items() if v}, {})
         self.assertEqual(models.Fragment.objects.all().count(), 0,
                          "orphaned fragments left")
         self.assertEqual(models.Tag.intermediary_table_model.objects.all().count(), 0,
@@ -191,7 +204,6 @@ class CleanTagRelationTests(WLTestCase):
         """ there should be no tag relations left after deleting tags """
 
         models.Tag.objects.all().delete()
-        self.assertEqual(len(self.book.related_info()['tags']), 0)
         self.assertEqual(len(self.book.related_themes()), 0)
         self.assertEqual(models.Tag.intermediary_table_model.objects.all().count(), 0,
                          "orphaned TagRelation objects left")
@@ -222,10 +234,9 @@ class TestIdenticalTag(WLTestCase):
         """ there should be all related tags in relevant categories """
         book = models.Book.from_text_and_meta(ContentFile(self.book_text), self.book_info)
 
-        related_info = book.related_info()
         related_themes = book.related_themes()
         for category in 'author', 'kind', 'genre', 'epoch':
-            self.assertTrue('tag' in [tag['slug'] for tag in related_info['tags'][category]],
+            self.assertTrue('tag' in [tag.slug for tag in book.tags.filter(category=category)],
                             'missing related tag for %s' % category)
         self.assertTrue('tag' in [tag.slug for tag in related_themes])
 
@@ -236,7 +247,7 @@ class TestIdenticalTag(WLTestCase):
             context = self.client.get('/katalog/%s/tag/' % localcat).context
             self.assertEqual(1, len(context['object_list']))
             self.assertNotEqual({}, context['categories'])
-            self.assertFalse(cat in context['categories'])
+            self.assertFalse(context['categories'].get(cat, False))
 
 
 class BookTagsTests(WLTestCase):
@@ -267,21 +278,20 @@ class BookTagsTests(WLTestCase):
         """ book should have own tags and whole tree's themes """
 
         book = models.Book.objects.get(slug='parent')
-        related_info = book.related_info()
         related_themes = book.related_themes()
 
-        self.assertEqual(related_info['tags']['author'],
-                         [{'pl': 'Common Man', 'slug': 'common-man'}])
-        self.assertEqual(related_info['tags']['kind'],
-                         [{'pl': 'Kind', 'slug': 'kind'}])
+        self.assertEqual([t.slug for t in book.tags.filter(category='author')],
+                         ['common-man'])
+        self.assertEqual([t.slug for t in book.tags.filter(category='kind')],
+                         ['kind'])
         self.assertEqual([(tag.name, tag.count) for tag in related_themes],
                          [('ChildTheme', 1), ('ParentTheme', 1), ('Theme', 2)])
 
     def test_catalogue_tags(self):
         """ test main page tags and counts """
         context = self.client.get('/katalog/').context
-        self.assertEqual([(tag.name, tag.book_count) for tag in context['categories']['author']],
+        self.assertEqual([(tag.name, tag.count) for tag in context['categories']['author']],
                          [('Jim Lazy', 1), ('Common Man', 1)])
-        self.assertEqual([(tag.name, tag.book_count) for tag in context['categories']['theme']],
+        self.assertEqual([(tag.name, tag.count) for tag in context['categories']['theme']],
                          [('ChildTheme', 1), ('ParentTheme', 1), ('Theme', 2)])