X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/57d57a9da6eaa250ed9c49c3a569b78b69fdf9bd..82c3054bcdeb000aa9782da80d644070797b5cbe:/apps/catalogue/tests/tags.py diff --git a/apps/catalogue/tests/tags.py b/apps/catalogue/tests/tags.py index 262821460..42ea6e245 100644 --- a/apps/catalogue/tests/tags.py +++ b/apps/catalogue/tests/tags.py @@ -1,99 +1,78 @@ # -*- 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 from catalogue.test_utils import * -from django.core.files.base import ContentFile -from slughifi import slughifi - -from nose.tools import raises - -def info_args(title): - """ generate some keywords for comfortable BookInfoCreation """ - slug = unicode(slughifi(title)) - return { - 'title': unicode(title), - 'slug': slug, - 'url': u"http://wolnelektury.pl/example/%s" % slug, - 'about': u"http://wolnelektury.pl/example/URI/%s" % slug, - } class BooksByTagTests(WLTestCase): - """ tests the /katalog/tag page for found books """ + """ tests the /katalog/category/tag page for found books """ def setUp(self): WLTestCase.setUp(self) author = PersonStub(("Common",), "Man") - tags = dict(genre='G', epoch='E', author=author, kind="K") # grandchild - kwargs = info_args(u"GChild") - kwargs.update(tags) - gchild_info = BookInfoStub(**kwargs) + self.gchild_info = BookInfoStub(genre='Genre', epoch='Epoch', kind='Kind', author=author, + **info_args("GChild")) # child - kwargs = info_args(u"Child") - kwargs.update(tags) - child_info = BookInfoStub(parts=[gchild_info.url], **kwargs) - # other grandchild - kwargs = info_args(u"Different GChild") - kwargs.update(tags) - diffgchild_info = BookInfoStub(**kwargs) - # other child - kwargs = info_args(u"Different Child") - kwargs.update(tags) - kwargs['kind'] = 'K2' - diffchild_info = BookInfoStub(parts=[diffgchild_info.url], **kwargs) + self.child_info = BookInfoStub(genre='Genre', epoch='Epoch', kind='Other Kind', author=author, + parts=[self.gchild_info.url], + **info_args("Child")) # parent - kwargs = info_args(u"Parent") - kwargs.update(tags) - parent_info = BookInfoStub(parts=[child_info.url, diffchild_info.url], **kwargs) + self.parent_info = BookInfoStub(genre='Genre', epoch='Epoch', kind='Kind', author=author, + parts=[self.child_info.url], + **info_args("Parent")) - # create the books - book_file = ContentFile('') - for info in gchild_info, child_info, diffgchild_info, diffchild_info, parent_info: - book = models.Book.from_text_and_meta(book_file, info) - - # useful tags - self.author = models.Tag.objects.get(name='Common Man', category='author') - models.Tag.objects.create(name='Empty tag', slug='empty', category='author') + self.book_file = ContentFile('') 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/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 """ - self.assertEqual(404, self.client.get('/katalog/parent/').status_code) + models.Book.from_text_and_meta(self.book_file, self.gchild_info) + self.assertEqual(404, self.client.get('/katalog/gchild/').status_code) def test_tag_empty(self): """ Tag with no books should return no books """ - context = self.client.get('/katalog/empty/').context + models.Book.from_text_and_meta(self.book_file, self.gchild_info) + models.Tag.objects.create(name='Empty tag', slug='empty', category='author') + + context = self.client.get('/katalog/autor/empty/').context self.assertEqual(0, len(context['object_list'])) - def test_tag_common(self): - """ Filtering by tag should only yield top-level books. """ - context = self.client.get('/katalog/%s/' % self.author.slug).context + def test_tag_eliminate(self): + """ Filtering by tag should only yield top-level qualifying books. """ + for info in self.gchild_info, self.child_info, self.parent_info: + models.Book.from_text_and_meta(self.book_file, info) + + # all three qualify + context = self.client.get('/katalog/gatunek/genre/').context self.assertEqual([book.title for book in context['object_list']], ['Parent']) - def test_tag_child(self): - """ Filtering by child's tag should yield the child """ - context = self.client.get('/katalog/k2/').context + # parent and gchild qualify, child doesn't + context = self.client.get('/katalog/rodzaj/kind/').context self.assertEqual([book.title for book in context['object_list']], - ['Different Child']) + ['Parent']) - def test_tag_child_jump(self): - """ Of parent and grandchild, only parent should be returned. """ - context = self.client.get('/katalog/k/').context + # Filtering by child's tag should yield the child + context = self.client.get('/katalog/rodzaj/other-kind/').context self.assertEqual([book.title for book in context['object_list']], - ['Parent']) + ['Child']) class TagRelatedTagsTests(WLTestCase): - """ tests the /katalog/tag/ page for related tags """ + """ tests the /katalog/category/tag/ page for related tags """ def setUp(self): WLTestCase.setUp(self) + self.client = Client() author = PersonStub(("Common",), "Man") gchild_info = BookInfoStub(author=author, genre="GchildGenre", epoch='Epoch', kind="Kind", @@ -124,67 +103,75 @@ class TagRelatedTagsTests(WLTestCase): 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') + cats = self.client.get('/katalog/autor/empty/').context['categories'] + 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 """ - cats = self.client.get('/katalog/kind/').context['categories'] + cats = self.client.get('/katalog/rodzaj/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.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 """ - cats = self.client.get('/katalog/kind/').context['categories'] - self.assertFalse('Kind' in [tag.name for tag in cats['kind']], + response = self.client.get('/katalog/rodzaj/kind/') + cats = response.context['categories'] + self.assertFalse(cats.get('kind', False), 'filtering tag wrongly included in related') - cats = self.client.get('/katalog/theme/').context['categories'] + cats = self.client.get('/katalog/motyw/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'] + cats = self.client.get('/katalog/rodzaj/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): + def test_siblings_tags_count(self): """ if children have tags and parent hasn't, count the children """ - cats = self.client.get('/katalog/epoch/').context['categories'] + 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') - - def test_themes_add(self): - """ all occurencies of theme should be counted """ + 'wrong related kind tags on tag page, got: ' + + unicode([(tag.name, tag.count) for tag in cats['kind']])) - cats = self.client.get('/katalog/epoch/').context['categories'] + # 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 """ @@ -200,21 +187,26 @@ class CleanTagRelationTests(WLTestCase): """ - book = models.Book.from_text_and_meta(ContentFile(book_text), book_info) + self.book = models.Book.from_text_and_meta(ContentFile(book_text), book_info) def test_delete_objects(self): """ there should be no related tags left after deleting some objects """ models.Book.objects.all().delete() - cats = self.client.get('/katalog/k/').context['categories'] - self.assertEqual(cats, {}) + cats = self.client.get('/katalog/rodzaj/k/').context['categories'] + 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, + "orphaned TagRelation objects left") def test_deleted_tag(self): """ there should be no tag relations left after deleting tags """ models.Tag.objects.all().delete() - cats = self.client.get('/katalog/lektura/book/').context['categories'] - self.assertEqual(cats, {}) + self.assertEqual(len(self.book.related_themes()), 0) + self.assertEqual(models.Tag.intermediary_table_model.objects.all().count(), 0, + "orphaned TagRelation objects left") class TestIdenticalTag(WLTestCase): @@ -240,12 +232,13 @@ class TestIdenticalTag(WLTestCase): def test_book_tags(self): """ there should be all related tags in relevant categories """ - models.Book.from_text_and_meta(ContentFile(self.book_text), self.book_info) + book = models.Book.from_text_and_meta(ContentFile(self.book_text), self.book_info) - cats = self.client.get('/katalog/lektura/tag/').context['categories'] - for category in 'author', 'kind', 'genre', 'epoch', 'theme': - self.assertTrue('tag' in [tag.slug for tag in cats[category]], + related_themes = book.related_themes() + for category in 'author', 'kind', 'genre', 'epoch': + 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]) def test_qualified_url(self): models.Book.from_text_and_meta(ContentFile(self.book_text), self.book_info) @@ -254,4 +247,51 @@ 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): + """ tests the /katalog/lektura/book/ page for related tags """ + + def setUp(self): + WLTestCase.setUp(self) + author1 = PersonStub(("Common",), "Man") + author2 = PersonStub(("Jim",), "Lazy") + + child_info = BookInfoStub(authors=(author1, author2), genre="ChildGenre", epoch='Epoch', kind="ChildKind", + **info_args(u"Child")) + parent_info = BookInfoStub(author=author1, genre="Genre", epoch='Epoch', kind="Kind", + parts=[child_info.url], + **info_args(u"Parent")) + + for info in child_info, parent_info: + book_text = """ + + Theme, %sTheme + Ala ma kota + + + """ % info.title.encode('utf-8') + models.Book.from_text_and_meta(ContentFile(book_text), info) + + def test_book_tags(self): + """ book should have own tags and whole tree's themes """ + + book = models.Book.objects.get(slug='parent') + related_themes = book.related_themes() + + 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.count) for tag in context['categories']['author']], + [('Jim Lazy', 1), ('Common Man', 1)]) + self.assertEqual([(tag.name, tag.count) for tag in context['categories']['theme']], + [('ChildTheme', 1), ('ParentTheme', 1), ('Theme', 2)]) +