From: Radek Czajka Date: Wed, 16 Jun 2010 13:21:13 +0000 (+0200) Subject: some new tests, some fixes in old ones X-Git-Url: https://git.mdrn.pl/wolnelektury.git/commitdiff_plain/fddf0ef87d70d72448586e1cdd844c795fdea5c5?ds=sidebyside;hp=-c some new tests, some fixes in old ones --- fddf0ef87d70d72448586e1cdd844c795fdea5c5 diff --git a/apps/catalogue/templatetags/catalogue_tags.py b/apps/catalogue/templatetags/catalogue_tags.py index fa7849538..36a015ae6 100644 --- a/apps/catalogue/templatetags/catalogue_tags.py +++ b/apps/catalogue/templatetags/catalogue_tags.py @@ -69,7 +69,7 @@ def book_title(book, html_links=False): while book: books.append(book) book = book.parent - names.extend(reversed(books[::-1])) + names.extend(reversed(books)) if html_links: names = ['%s' % (tag.get_absolute_url(), tag.name) for tag in names] diff --git a/apps/catalogue/test_utils.py b/apps/catalogue/test_utils.py index 3a8af57aa..398a0fee2 100644 --- a/apps/catalogue/test_utils.py +++ b/apps/catalogue/test_utils.py @@ -2,6 +2,7 @@ from django.conf import settings from django.test import TestCase import shutil import tempfile +from slughifi import slughifi class WLTestCase(TestCase): """ @@ -36,3 +37,14 @@ class BookInfoStub(object): def to_dict(self): return dict((key, unicode(value)) for key, value in self.__dict.items()) + + +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, + } diff --git a/apps/catalogue/tests/__init__.py b/apps/catalogue/tests/__init__.py index d656d455b..2366653de 100644 --- a/apps/catalogue/tests/__init__.py +++ b/apps/catalogue/tests/__init__.py @@ -1,3 +1,4 @@ from catalogue.tests.book_import import * -from catalogue.tests.tags import * from catalogue.tests.search import * +from catalogue.tests.tags import * +from catalogue.tests.templatetags import * diff --git a/apps/catalogue/tests/book_import.py b/apps/catalogue/tests/book_import.py index fed99226f..50a8cb685 100644 --- a/apps/catalogue/tests/book_import.py +++ b/apps/catalogue/tests/book_import.py @@ -82,7 +82,7 @@ class BookImportLogicTests(WLTestCase): BOOK_TEXT = """""" book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info) self.book_info.title = u"Extraordinary" - book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info) + book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info, overwrite=True) tags = [ (tag.category, tag.slug) for tag in book.tags ] tags.sort() @@ -93,7 +93,7 @@ class BookImportLogicTests(WLTestCase): BOOK_TEXT = """""" book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info) self.book_info.author = PersonStub(("Hans", "Christian"), "Andersen") - book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info) + book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info, overwrite=True) tags = [ (tag.category, tag.slug) for tag in book.tags ] tags.sort() @@ -104,6 +104,5 @@ class BookImportLogicTests(WLTestCase): self.assertEqual(tags, self.expected_tags) - # the old tag should disappear - self.assertRaises(models.Tag.DoesNotExist, models.Tag.objects.get, - slug="jim-lazy", category="author") + # the old tag shouldn't disappear + models.Tag.objects.get(slug="jim-lazy", category="author") diff --git a/apps/catalogue/tests/tags.py b/apps/catalogue/tests/tags.py index 49e176e09..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): diff --git a/apps/catalogue/tests/templatetags.py b/apps/catalogue/tests/templatetags.py new file mode 100644 index 000000000..5b1283c10 --- /dev/null +++ b/apps/catalogue/tests/templatetags.py @@ -0,0 +1,27 @@ +# -*- coding: utf-8 -*- +from catalogue import models +from catalogue.templatetags import catalogue_tags +from catalogue.test_utils import * +from django.core.files.base import ContentFile + + +class BookDescTests(WLTestCase): + """ tests book_title template tag """ + + def setUp(self): + WLTestCase.setUp(self) + author = PersonStub(("Common",), "Man") + + child_info = BookInfoStub(author=author, genre="Genre", epoch='Epoch', kind="Kind", + **info_args(u"Child")) + parent_info = BookInfoStub(author=author, genre="Genre", epoch='Epoch', kind="Kind", + parts=[child_info.url], + **info_args(u"Parent")) + + self.child = models.Book.from_text_and_meta(ContentFile(''), child_info) + models.Book.from_text_and_meta(ContentFile(''), parent_info) + self.child = models.Book.objects.get(pk=self.child.pk) + + def test_book_desc(self): + """ book description should return authors, ancestors, book """ + self.assertEqual(catalogue_tags.book_title(self.child), 'Common Man, Parent, Child')