Ticket #5465: issue_55.patch

File issue_55.patch, 6.4 KB (added by Ben Sturmfels, 8 years ago)

Patch copy of above repository

  • 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  
    1616
    1717from __future__ import absolute_import, unicode_literals
    1818
     19try:
     20    import mock
     21except ImportError:
     22    import unittest.mock as mock
     23
    1924from werkzeug.wrappers import Request
    2025from werkzeug.test import EnvironBuilder
    2126
    2227from mediagoblin.tools.request import decode_request
     28from mediagoblin.tools.pagination import Pagination
    2329
    2430class TestDecodeRequest(object):
    2531    """Test the decode_request function."""
    class TestDecodeRequest(object):  
    5965        request.form = {'foo': 'bar'}
    6066        data = decode_request(request)
    6167        assert data['foo'] == 'bar'
     68
     69
     70class 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  
    1414# You should have received a copy of the GNU Affero General Public License
    1515# along with this program.  If not, see <http://www.gnu.org/licenses/>.
    1616
    17 import urllib
    1817import copy
    1918from math import ceil, floor
    2019from itertools import count
    2120from werkzeug.datastructures import MultiDict
    2221
    23 from six.moves import zip
     22from six.moves import range, urllib, zip
    2423
    2524PAGINATION_DEFAULT_PER_PAGE = 30
    2625
    class Pagination(object):  
    8685    def iter_pages(self, left_edge=2, left_current=2,
    8786                   right_current=5, right_edge=2):
    8887        last = 0
    89         for num in xrange(1, self.pages + 1):
     88        for num in range(1, self.pages + 1):
    9089            if num <= left_edge or \
    9190               (num > self.page - left_current - 1 and \
    9291                num < self.page + right_current) or \
    class Pagination(object):  
    107106
    108107        new_get_params['page'] = page_no
    109108        return "%s?%s" % (
    110             base_url, urllib.urlencode(new_get_params))
     109            base_url, urllib.parse.urlencode(new_get_params))
    111110
    112111    def get_page_url(self, request, page_no):
    113112        """
  • 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):  
    6868
    6969
    7070class 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."""
    7273        mock_cursor = mock.MagicMock()
    73         mock_cursor.count.return_value = 1
    74         self.paginator = Pagination(1, mock_cursor)
     74        mock_cursor.count.return_value = num_items
     75        return Pagination(page, mock_cursor, per_page)
    7576
    7677    def test_creates_valid_page_url_from_explicit_base_url(self):
    7778        """Check that test_page_url_explicit runs.
    class TestPagination(object):  
    7980        This is a regression test for a Python 2/3 compatibility fix.
    8081
    8182        """
    82         url = self.paginator.get_page_url_explicit(
    83             'http://example.com', [], 1)
     83        paginator = self._create_paginator(num_items=1, page=1, per_page=30)
     84        url = paginator.get_page_url_explicit('http://example.com', [], 1)
    8485        assert url == 'http://example.com?page=1'
    8586
    86     def test_iter_pages_handes_single_page(self):
     87    def test_iter_pages_handles_single_page(self):
    8788        """Check that iter_pages produces the expected result for single page.
    8889
    8990        This is a regression test for a Python 2/3 compatibility fix.
    9091
    9192        """
    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