X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/7f246737dbb0697581d59e4ed673f516b4f90240..dfd584e3b136d770bf56569030d10712a8722569:/apps/catalogue/tests/tags.py diff --git a/apps/catalogue/tests/tags.py b/apps/catalogue/tests/tags.py index 7e6e66716..f10780c0a 100644 --- a/apps/catalogue/tests/tags.py +++ b/apps/catalogue/tests/tags.py @@ -1,9 +1,11 @@ # -*- 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 nose.tools import raises class BooksByTagTests(WLTestCase): @@ -66,12 +68,12 @@ class BooksByTagTests(WLTestCase): ['Child']) - class TagRelatedTagsTests(WLTestCase): """ 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", @@ -113,33 +115,32 @@ 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("kind" in cats, + "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/rodzaj/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('kind' in cats, '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']], 'filtering theme wrongly included in related') - def test_parent_tag_once(self): """ if parent and descendants have a common tag, count it only once """ @@ -175,7 +176,7 @@ 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 """ @@ -192,8 +193,7 @@ class CleanTagRelationTests(WLTestCase): """ 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") @@ -221,13 +221,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) - context = self.client.get('/katalog/lektura/tag/').context + related_themes = book.related_themes() for category in 'author', 'kind', 'genre', 'epoch': - self.assertTrue('tag' in [tag.slug for tag in context['categories'][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 context['book_themes']]) + 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) @@ -261,25 +261,24 @@ class BookTagsTests(WLTestCase): """ % info.title.encode('utf-8') - book = models.Book.from_text_and_meta(ContentFile(book_text), info) + 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 """ - context = self.client.get('/katalog/lektura/parent/').context + book = models.Book.objects.get(slug='parent') + related_themes = book.related_themes() - self.assertEqual([tag.name for tag in context['categories']['author']], - ['Common Man']) - self.assertEqual([tag.name for tag in context['categories']['kind']], - ['Kind']) - self.assertEqual([(tag.name, tag.count) for tag in context['book_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_main_page_tags(self): + 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['fragment_tags']],