Permalink
Browse files

Tests for pagination and template tag.

  • Loading branch information...
1 parent 81d7ad2 commit 3377d678b1abe1055b5b92eccdf562810e438e5f @nigma committed Jun 8, 2012
Showing with 240 additions and 0 deletions.
  1. +44 −0 run_tests.py
  2. +1 −0 tests/__init__.py
  3. +14 −0 tests/models.py
  4. +17 −0 tests/templates/article_list.html
  5. +20 −0 tests/templates/auto_list.html
  6. +121 −0 tests/tests.py
  7. +9 −0 tests/urls.py
  8. +14 −0 tests/views.py
View
@@ -0,0 +1,44 @@
+#-*- coding: utf-8 -*-
+
+import sys
+from optparse import OptionParser
+
+from django.conf import settings, global_settings
+
+if not settings.configured:
+ settings.configure(
+ DATABASES = {
+ "default": {
+ "ENGINE": "django.db.backends.sqlite3",
+ }
+ },
+ INSTALLED_APPS = [
+ "infinite_pagination",
+ "tests",
+ ],
+ TEMPLATE_CONTEXT_PROCESSORS = global_settings.TEMPLATE_CONTEXT_PROCESSORS +
+ ("django.core.context_processors.request",),
+ ROOT_URLCONF ="tests.urls",
+ DEBUG = False,
+ )
+
+from django.test.simple import DjangoTestSuiteRunner
+
+
+def run_tests(*test_args, **kwargs):
+ if not test_args:
+ test_args = ["tests"]
+ test_runner = DjangoTestSuiteRunner(
+ verbosity=kwargs.get("verbosity", 1),
+ interactive=kwargs.get("interactive", False),
+ failfast=kwargs.get("failfast")
+ )
+ failures = test_runner.run_tests(test_args)
+ sys.exit(failures)
+
+if __name__ == "__main__":
+ parser = OptionParser()
+ parser.add_option("--failfast", action="store_true", default=False, dest="failfast")
+ parser.add_option("--verbosity", action="store", default=1, type=int, dest="verbosity")
+ (options, args) = parser.parse_args()
+ run_tests(failfast=options.failfast, verbosity=options.verbosity, *args)
View
@@ -0,0 +1 @@
+#-*- coding: utf-8 -*-
View
@@ -0,0 +1,14 @@
+#-*- coding: utf-8 -*-
+
+from django.db import models
+
+
+class Article(models.Model):
+
+ title = models.CharField("title", max_length=50)
+
+ class Meta:
+ ordering = ["id"]
+
+ def __unicode__(self):
+ return self.title
@@ -0,0 +1,17 @@
+{% load infinite_pagination %}
+<!DOCTYPE html>
+<html>
+<head>
+ <title></title>
+</head>
+<body>
+ <div>
+ <ul>
+ {% for object in object_list %}
+ <li>{{ object }}</li>
+ {% endfor %}
+ </ul>
+ {% paginate %}
+ </div>
+</body>
+</html>
@@ -0,0 +1,20 @@
+{% load infinite_pagination_tags %}
+<!DOCTYPE html>
+<html>
+<head>
+ <title></title>
+</head>
+<body>
+ <div>
+ <ul>
+ {% autopaginate object_list 10 %}
+ {% for object in object_list %}
+ <li>{{ object }}</li>
+ {% endfor %}
+ </ul>
+ <div>
+ {% infinite_paginate %}
+ </div>
+ </div>
+</body>
+</html>
View
@@ -0,0 +1,121 @@
+#-*- coding: utf-8 -*-
+
+from django.core.paginator import EmptyPage
+from django.test.testcases import TestCase
+
+from infinite_pagination.paginator import InfinitePaginator
+
+from .models import Article
+
+
+class InfinitePaginatorTestCase(TestCase):
+
+ def setUp(self):
+ for x in range(25):
+ Article.objects.create(title=str(x))
+ self.paginator = InfinitePaginator(Article.objects.all(), 10)
+
+ def tearDown(self):
+ Article.objects.all().delete()
+
+ def test_pagination(self):
+ page1 = self.paginator.page(1)
+ page2 = self.paginator.page(2)
+ page3 = self.paginator.page(3)
+
+ self.assertEqual(page1.object_list, list(Article.objects.all()[0:10]))
+ self.assertEqual(page2.object_list, list(Article.objects.all()[10:20]))
+ self.assertEqual(page3.object_list, list(Article.objects.all()[20:25]))
+
+ def test_first_page(self):
+ page = self.paginator.page(1)
+ self.assertEqual(page.number, 1)
+ self.assertTrue(page.has_next())
+ self.assertFalse(page.has_previous())
+ self.assertTrue(page.has_other_pages())
+ self.assertEqual(page.next_page_number(), 2)
+ self.assertEqual(page.end_index(), 10)
+
+ def test_second_page(self):
+ page = self.paginator.page(2)
+ self.assertEqual(page.number, 2)
+ self.assertTrue(page.has_next())
+ self.assertTrue(page.has_previous())
+ self.assertTrue(page.has_other_pages())
+ self.assertEqual(page.previous_page_number(), 1)
+ self.assertEqual(page.next_page_number(), 3)
+ self.assertEqual(page.end_index(), 20)
+
+ def test_last_page(self):
+ page = self.paginator.page(3)
+ self.assertEqual(page.number, 3)
+ self.assertFalse(page.has_next())
+ self.assertTrue(page.has_previous())
+ self.assertTrue(page.has_other_pages())
+ self.assertEqual(page.previous_page_number(), 2)
+ self.assertEqual(page.end_index(), 25)
+
+ def test_invalid_page(self):
+ self.assertRaises(EmptyPage, lambda: self.paginator.page(4))
+
+ def test_single_page(self):
+ paginator = InfinitePaginator(Article.objects.all(), 100)
+ page = paginator.page(1)
+ self.assertEqual(page.number, 1)
+ self.assertFalse(page.has_next())
+ self.assertFalse(page.has_previous())
+ self.assertFalse(page.has_other_pages())
+ self.assertEqual(page.end_index(), 25)
+
+ def test_empty_page(self):
+ paginator = InfinitePaginator(Article.objects.none(), 100)
+ page = paginator.page(1)
+ self.assertEqual(page.object_list, [])
+ self.assertEqual(page.number, 1)
+ self.assertFalse(page.has_next())
+ self.assertFalse(page.has_previous())
+ self.assertFalse(page.has_other_pages())
+ self.assertEqual(page.end_index(), 0)
+
+
+class TemplateTagTestCase(TestCase):
+
+ def setUp(self):
+ for x in range(25):
+ Article.objects.create(title=str(x))
+
+ def tearDown(self):
+ Article.objects.all().delete()
+
+ def test_init_page(self):
+ resp = self.client.get("/articles/?param=x")
+ self.assertIn("""<a>&larr; Previous</a>""", resp.content)
+ self.assertIn("""<a href="?page=2&amp;param=x">Next &rarr;</a>""", resp.content)
+ self.assertIn("""<li>0</li>""", resp.content)
+ self.assertNotIn("""<li>10</li>""", resp.content)
+ self.assertNotIn("""<li>20</li>""", resp.content)
+
+ def test_first_page(self):
+ resp = self.client.get("/articles/?page=1&param=x")
+ self.assertIn("""<a>&larr; Previous</a>""", resp.content)
+ self.assertIn("""<a href="?page=2&amp;param=x">Next &rarr;</a>""", resp.content)
+ self.assertIn("""<li>0</li>""", resp.content)
+ self.assertNotIn("""<li>10</li>""", resp.content)
+ self.assertNotIn("""<li>20</li>""", resp.content)
+
+ def test_second_page(self):
+ resp = self.client.get("/articles/?page=2&param=x")
+ self.assertIn("""<a href="?page=1&amp;param=x">First</a>""", resp.content)
+ self.assertIn("""<a href="?page=1&amp;param=x">&larr; Previous</a>""", resp.content)
+ self.assertIn("""<a href="?page=3&amp;param=x">Next &rarr;</a>""", resp.content)
+ self.assertIn("""<li>10</li>""", resp.content)
+ self.assertNotIn("""<li>0</li>""", resp.content)
+ self.assertNotIn("""<li>20</li>""", resp.content)
+
+ def test_last_page(self):
+ resp = self.client.get("/articles/?page=3&param=x")
+ self.assertIn("""<a href="?page=1&amp;param=x">First</a>""", resp.content)
+ self.assertIn("""<a href="?page=2&amp;param=x">&larr; Previous</a>""", resp.content)
+ self.assertIn("""<a>Next &rarr;</a>""", resp.content)
+ self.assertIn("""<li>20</li>""", resp.content)
+ self.assertNotIn("""<li>10</li>""", resp.content)
View
@@ -0,0 +1,9 @@
+#-*- coding: utf-8 -*-
+
+from django.conf.urls import patterns, url
+
+from .views import ArticleListView
+
+urlpatterns = patterns("",
+ url(r"^articles/", ArticleListView.as_view()),
+)
View
@@ -0,0 +1,14 @@
+#-*- coding: utf-8 -*-
+
+from django.views.generic.list import ListView
+
+from infinite_pagination.paginator import InfinitePaginator
+
+from .models import Article
+
+
+class ArticleListView(ListView):
+ model = Article
+ paginate_by = 10
+ paginator_class = InfinitePaginator
+ template_name = "article_list.html"

0 comments on commit 3377d67

Please sign in to comment.