Added test suite, and prevented pagination if the elusion would take up only one...
authorfloguy <floguy@7f1efe38-554e-0410-b69d-834cb44da2d5>
Fri, 30 May 2008 04:35:24 +0000 (04:35 +0000)
committerfloguy <floguy@7f1efe38-554e-0410-b69d-834cb44da2d5>
Fri, 30 May 2008 04:35:24 +0000 (04:35 +0000)
git-svn-id: https://django-pagination.googlecode.com/svn/trunk@13 7f1efe38-554e-0410-b69d-834cb44da2d5

pagination/templatetags/pagination_tags.py
pagination/tests.py [new file with mode: 0644]
tests/runtests.py [new file with mode: 0644]
tests/settings.py [new file with mode: 0644]

index bba21b8..e688b05 100644 (file)
@@ -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)
\ No newline at end of file
diff --git a/pagination/tests.py b/pagination/tests.py
new file mode 100644 (file)
index 0000000..21aa350
--- /dev/null
@@ -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]
+"""
\ No newline at end of file
diff --git a/tests/runtests.py b/tests/runtests.py
new file mode 100644 (file)
index 0000000..6fb4b93
--- /dev/null
@@ -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
diff --git a/tests/settings.py b/tests/settings.py
new file mode 100644 (file)
index 0000000..ede4087
--- /dev/null
@@ -0,0 +1,6 @@
+DATABASE_ENGINE = 'sqlite3'
+ROOT_URLCONF = ''
+SITE_ID = 1
+INSTALLED_APPS = (
+    'pagination',
+)