Extend Paginator tests to satisfy #55.
authorBen Sturmfels <ben@sturm.com.au>
Thu, 15 Sep 2016 09:34:12 +0000 (21:34 +1200)
committerBoris Bobrov <breton@cynicmansion.ru>
Fri, 16 Sep 2016 06:24:56 +0000 (09:24 +0300)
mediagoblin/tests/test_tools.py

index 30232e2418f5322c57e8973b0beae5ff91edf38e..5f9164001c34f8e07d725ac430e4d3c4041bffa3 100644 (file)
@@ -68,10 +68,11 @@ class TestDecodeRequest(object):
 
 
 class TestPagination(object):
-    def setup(self):
+    def _create_paginator(self, num_items, page, per_page):
+        """Create a Paginator with a mock database cursor."""
         mock_cursor = mock.MagicMock()
-        mock_cursor.count.return_value = 1
-        self.paginator = Pagination(1, mock_cursor)
+        mock_cursor.count.return_value = num_items
+        return Pagination(page, mock_cursor, per_page)
 
     def test_creates_valid_page_url_from_explicit_base_url(self):
         """Check that test_page_url_explicit runs.
@@ -79,14 +80,39 @@ class TestPagination(object):
         This is a regression test for a Python 2/3 compatibility fix.
 
         """
-        url = self.paginator.get_page_url_explicit(
-            'http://example.com', [], 1)
+        paginator = self._create_paginator(num_items=1, page=1, per_page=30)
+        url = paginator.get_page_url_explicit('http://example.com', [], 1)
         assert url == 'http://example.com?page=1'
 
-    def test_iter_pages_handes_single_page(self):
+    def test_iter_pages_handles_single_page(self):
         """Check that iter_pages produces the expected result for single page.
 
         This is a regression test for a Python 2/3 compatibility fix.
 
         """
-        assert list(self.paginator.iter_pages()) == [1]
+        paginator = self._create_paginator(num_items=1, page=1, per_page=30)
+        assert list(paginator.iter_pages()) == [1]
+
+    def test_zero_items(self):
+        """Check that no items produces no pages."""
+        paginator = self._create_paginator(num_items=0, page=1, per_page=30)
+        assert paginator.total_count == 0
+        assert paginator.pages == 0
+
+    def test_single_item(self):
+        """Check that one item produces one page."""
+        paginator = self._create_paginator(num_items=1, page=1, per_page=30)
+        assert paginator.total_count == 1
+        assert paginator.pages == 1
+
+    def test_full_page(self):
+        """Check that a full page of items produces one page."""
+        paginator = self._create_paginator(num_items=30, page=1, per_page=30)
+        assert paginator.total_count == 30
+        assert paginator.pages == 1
+
+    def test_multiple_pages(self):
+        """Check that more than a full page produces two pages."""
+        paginator = self._create_paginator(num_items=31, page=1, per_page=30)
+        assert paginator.total_count == 31
+        assert paginator.pages == 2