Ticket #5465: issue_55.patch
File issue_55.patch, 6.4 KB (added by , 8 years ago) |
---|
-
mediagoblin/tests/test_tools.py
From 6e0d2b51ff5a00f24c32783be63ae4970b93ed77 Mon Sep 17 00:00:00 2001 From: Ben Sturmfels <ben@sturm.com.au> Date: Sun, 7 Aug 2016 21:48:52 +1000 Subject: [PATCH 1/2] Add Python 3 support in pagination. This issue was visible when attempting to view the home page of a MediaGoblin site with more than a single page worth of items, under Python 3. --- mediagoblin/tests/test_tools.py | 31 +++++++++++++++++++++++++++++++ mediagoblin/tools/pagination.py | 7 +++---- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/mediagoblin/tests/test_tools.py b/mediagoblin/tests/test_tools.py index 6d3dd47..30232e2 100644
a b 16 16 17 17 from __future__ import absolute_import, unicode_literals 18 18 19 try: 20 import mock 21 except ImportError: 22 import unittest.mock as mock 23 19 24 from werkzeug.wrappers import Request 20 25 from werkzeug.test import EnvironBuilder 21 26 22 27 from mediagoblin.tools.request import decode_request 28 from mediagoblin.tools.pagination import Pagination 23 29 24 30 class TestDecodeRequest(object): 25 31 """Test the decode_request function.""" … … class TestDecodeRequest(object): 59 65 request.form = {'foo': 'bar'} 60 66 data = decode_request(request) 61 67 assert data['foo'] == 'bar' 68 69 70 class TestPagination(object): 71 def setup(self): 72 mock_cursor = mock.MagicMock() 73 mock_cursor.count.return_value = 1 74 self.paginator = Pagination(1, mock_cursor) 75 76 def test_creates_valid_page_url_from_explicit_base_url(self): 77 """Check that test_page_url_explicit runs. 78 79 This is a regression test for a Python 2/3 compatibility fix. 80 81 """ 82 url = self.paginator.get_page_url_explicit( 83 'http://example.com', [], 1) 84 assert url == 'http://example.com?page=1' 85 86 def test_iter_pages_handes_single_page(self): 87 """Check that iter_pages produces the expected result for single page. 88 89 This is a regression test for a Python 2/3 compatibility fix. 90 91 """ 92 assert list(self.paginator.iter_pages()) == [1] -
mediagoblin/tools/pagination.py
diff --git a/mediagoblin/tools/pagination.py b/mediagoblin/tools/pagination.py index a525caf..db5f69f 100644
a b 14 14 # You should have received a copy of the GNU Affero General Public License 15 15 # along with this program. If not, see <http://www.gnu.org/licenses/>. 16 16 17 import urllib18 17 import copy 19 18 from math import ceil, floor 20 19 from itertools import count 21 20 from werkzeug.datastructures import MultiDict 22 21 23 from six.moves import zip22 from six.moves import range, urllib, zip 24 23 25 24 PAGINATION_DEFAULT_PER_PAGE = 30 26 25 … … class Pagination(object): 86 85 def iter_pages(self, left_edge=2, left_current=2, 87 86 right_current=5, right_edge=2): 88 87 last = 0 89 for num in xrange(1, self.pages + 1):88 for num in range(1, self.pages + 1): 90 89 if num <= left_edge or \ 91 90 (num > self.page - left_current - 1 and \ 92 91 num < self.page + right_current) or \ … … class Pagination(object): 107 106 108 107 new_get_params['page'] = page_no 109 108 return "%s?%s" % ( 110 base_url, urllib. urlencode(new_get_params))109 base_url, urllib.parse.urlencode(new_get_params)) 111 110 112 111 def get_page_url(self, request, page_no): 113 112 """ -
mediagoblin/tests/test_tools.py
-- 1.9.1 From f9c47d68c63ef6b90ef7deca3c673a5babdb698c Mon Sep 17 00:00:00 2001 From: Ben Sturmfels <ben@sturm.com.au> Date: Thu, 15 Sep 2016 21:34:12 +1200 Subject: [PATCH 2/2] Extend Paginator tests to satisfy #55. --- mediagoblin/tests/test_tools.py | 40 +++++++++++++++++++++++++++++++++------- 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/mediagoblin/tests/test_tools.py b/mediagoblin/tests/test_tools.py index 30232e2..5f91640 100644
a b class TestDecodeRequest(object): 68 68 69 69 70 70 class TestPagination(object): 71 def setup(self): 71 def _create_paginator(self, num_items, page, per_page): 72 """Create a Paginator with a mock database cursor.""" 72 73 mock_cursor = mock.MagicMock() 73 mock_cursor.count.return_value = 174 self.paginator = Pagination(1, mock_cursor)74 mock_cursor.count.return_value = num_items 75 return Pagination(page, mock_cursor, per_page) 75 76 76 77 def test_creates_valid_page_url_from_explicit_base_url(self): 77 78 """Check that test_page_url_explicit runs. … … class TestPagination(object): 79 80 This is a regression test for a Python 2/3 compatibility fix. 80 81 81 82 """ 82 url = self.paginator.get_page_url_explicit(83 83 paginator = self._create_paginator(num_items=1, page=1, per_page=30) 84 url = paginator.get_page_url_explicit('http://example.com', [], 1) 84 85 assert url == 'http://example.com?page=1' 85 86 86 def test_iter_pages_hand es_single_page(self):87 def test_iter_pages_handles_single_page(self): 87 88 """Check that iter_pages produces the expected result for single page. 88 89 89 90 This is a regression test for a Python 2/3 compatibility fix. 90 91 91 92 """ 92 assert list(self.paginator.iter_pages()) == [1] 93 paginator = self._create_paginator(num_items=1, page=1, per_page=30) 94 assert list(paginator.iter_pages()) == [1] 95 96 def test_zero_items(self): 97 """Check that no items produces no pages.""" 98 paginator = self._create_paginator(num_items=0, page=1, per_page=30) 99 assert paginator.total_count == 0 100 assert paginator.pages == 0 101 102 def test_single_item(self): 103 """Check that one item produces one page.""" 104 paginator = self._create_paginator(num_items=1, page=1, per_page=30) 105 assert paginator.total_count == 1 106 assert paginator.pages == 1 107 108 def test_full_page(self): 109 """Check that a full page of items produces one page.""" 110 paginator = self._create_paginator(num_items=30, page=1, per_page=30) 111 assert paginator.total_count == 30 112 assert paginator.pages == 1 113 114 def test_multiple_pages(self): 115 """Check that more than a full page produces two pages.""" 116 paginator = self._create_paginator(num_items=31, page=1, per_page=30) 117 assert paginator.total_count == 31 118 assert paginator.pages == 2