Permalink
Browse files

Added test suite, and prevented pagination if the elusion would take …

…up only one space.

git-svn-id: https://django-pagination.googlecode.com/svn/trunk@13 7f1efe38-554e-0410-b69d-834cb44da2d5
  • Loading branch information...
1 parent 4d9362d commit a35ff9bd925bb5fd3ab2e43cae19a62afb2f5701 floguy committed May 30, 2008
Showing with 55 additions and 3 deletions.
  1. +13 −3 pagination/templatetags/pagination_tags.py
  2. +20 −0 pagination/tests.py
  3. +16 −0 tests/runtests.py
  4. +6 −0 tests/settings.py
View
16 pagination/templatetags/pagination_tags.py
@@ -92,14 +92,24 @@ def paginate(context, window=4):
first_list = sorted(list(first))
second_list = sorted(list(current))
pages.extend(first_list)
- if first_list[-1] + 1 != second_list[0]:
+ diff = second_list[0] - first_list[-1]
+ if diff == 2:
+ pages.append(second_list[0] - 1)
+ elif diff == 1:
+ pass
+ else:
pages.append(None)
pages.extend(second_list)
else:
pages.extend(sorted(list(first.union(current))))
if len(current.intersection(last)) == 0:
second_list = sorted(list(last))
- if pages[-1] + 1 != second_list[0]:
+ diff = second_list[0] - pages[-1]
+ if diff == 2:
+ pages.append(second_list[0] - 1)
+ elif diff == 1:
+ pass
+ else:
pages.append(None)
pages.extend(second_list)
else:
@@ -113,4 +123,4 @@ def paginate(context, window=4):
except KeyError:
return u''
register.inclusion_tag('pagination/pagination.html', takes_context=True)(paginate)
-register.tag('autopaginate', do_autopaginate)
+register.tag('autopaginate', do_autopaginate)
View
20 pagination/tests.py
@@ -0,0 +1,20 @@
+"""
+>>> from django.core.paginator import Paginator
+>>> from pagination.templatetags.pagination_tags import paginate
+
+>>> p = Paginator(range(15), 2)
+>>> paginate({'paginator': p, 'page_obj': p.page(1)})['pages']
+[1, 2, 3, 4, 5, 6, 7, 8]
+
+>>> p = Paginator(range(17), 2)
+>>> paginate({'paginator': p, 'page_obj': p.page(1)})['pages']
+[1, 2, 3, 4, 5, 6, 7, 8, 9]
+
+>>> p = Paginator(range(19), 2)
+>>> paginate({'paginator': p, 'page_obj': p.page(1)})['pages']
+[1, 2, 3, 4, None, 7, 8, 9, 10]
+
+>>> p = Paginator(range(21), 2)
+>>> paginate({'paginator': p, 'page_obj': p.page(1)})['pages']
+[1, 2, 3, 4, None, 8, 9, 10, 11]
+"""
View
16 tests/runtests.py
@@ -0,0 +1,16 @@
+import sys
+sys.path.append('..')
+
+import os
+# Make a backup of DJANGO_SETTINGS_MODULE environment variable to restore later.
+backup = os.environ.get('DJANGO_SETTINGS_MODULE', '')
+os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
+
+from django.test.simple import run_tests
+
+if __name__ == "__main__":
+ failures = run_tests(['pagination',], verbosity=9)
+ if failures:
+ sys.exit(failures)
+ # Reset the DJANGO_SETTINGS_MODULE to what it was before running tests.
+ os.environ['DJANGO_SETTINGS_MODULE'] = backup
View
6 tests/settings.py
@@ -0,0 +1,6 @@
+DATABASE_ENGINE = 'sqlite3'
+ROOT_URLCONF = ''
+SITE_ID = 1
+INSTALLED_APPS = (
+ 'pagination',
+)

0 comments on commit a35ff9b

Please sign in to comment.