X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/9c5d9a4e77a10b4e60d89d3890e49002bd7f3993..fddf0ef87d70d72448586e1cdd844c795fdea5c5:/apps/catalogue/tests/tags.py diff --git a/apps/catalogue/tests/tags.py b/apps/catalogue/tests/tags.py index f9102a7c8..00d11678d 100644 --- a/apps/catalogue/tests/tags.py +++ b/apps/catalogue/tests/tags.py @@ -2,20 +2,9 @@ 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 """ @@ -23,38 +12,20 @@ class BooksByTagTests(WLTestCase): 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) - - # 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) + self.parent_info = BookInfoStub(genre='Genre', epoch='Epoch', kind='Kind', author=author, + parts=[self.child_info.url], + **info_args("Parent")) - # 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 """ @@ -63,30 +34,37 @@ class BooksByTagTests(WLTestCase): 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 """ + 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/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/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/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/other-kind/').context self.assertEqual([book.title for book in context['object_list']], - ['Parent']) + ['Child']) + class TagRelatedTagsTests(WLTestCase): @@ -171,17 +149,14 @@ class TagRelatedTagsTests(WLTestCase): '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'] 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'] + # 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') @@ -244,7 +219,7 @@ class TestIdenticalTag(WLTestCase): cats = self.client.get('/katalog/lektura/tag/').context['categories'] for category in 'author', 'kind', 'genre', 'epoch', 'theme': - self.assertTrue('tag' in [tag.name for tag in cats[category]], + self.assertTrue('tag' in [tag.slug for tag in cats[category]], 'missing related tag for %s' % category) def test_qualified_url(self):