From: Ben Sturmfels Date: Thu, 15 Sep 2016 09:34:12 +0000 (+1200) Subject: Extend Paginator tests to satisfy #55. X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=e17566b3c3508cc7b33f7fa7c491a1ee54d24aeb;p=mediagoblin.git Extend Paginator tests to satisfy #55. --- diff --git a/mediagoblin/tests/test_tools.py b/mediagoblin/tests/test_tools.py index 30232e24..5f916400 100644 --- a/mediagoblin/tests/test_tools.py +++ b/mediagoblin/tests/test_tools.py @@ -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