some new tests, some fixes in old ones
authorRadek Czajka <radoslaw.czajka@nowoczesnapolska.org.pl>
Wed, 16 Jun 2010 13:21:13 +0000 (15:21 +0200)
committerRadek Czajka <radoslaw.czajka@nowoczesnapolska.org.pl>
Wed, 16 Jun 2010 14:09:44 +0000 (16:09 +0200)
apps/catalogue/templatetags/catalogue_tags.py
apps/catalogue/test_utils.py
apps/catalogue/tests/__init__.py
apps/catalogue/tests/book_import.py
apps/catalogue/tests/tags.py
apps/catalogue/tests/templatetags.py [new file with mode: 0644]

index fa78495..36a015a 100644 (file)
@@ -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 = ['<a href="%s">%s</a>' % (tag.get_absolute_url(), tag.name) for tag in names]
index 3a8af57..398a0fe 100644 (file)
@@ -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,
+    }
index d656d45..2366653 100644 (file)
@@ -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 *
index fed9922..50a8cb6 100644 (file)
@@ -82,7 +82,7 @@ class BookImportLogicTests(WLTestCase):
         BOOK_TEXT = """<utwor />"""
         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 = """<utwor />"""
         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")
index 49e176e..00d1167 100644 (file)
@@ -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('<utwor />')
-        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('<utwor />')
 
     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 (file)
index 0000000..5b1283c
--- /dev/null
@@ -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('<utwor/>'), child_info)
+        models.Book.from_text_and_meta(ContentFile('<utwor/>'), 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')