view tests/test_uriutil.py @ 1168:10520472cc73

routing: Fix breakages with routing on some versions of Python. Finally figured what happened with change 6baa94da8b16 (this is a Mercurial hash by the way if you're looking at the Git mirror). Between Python 3.6 and 3.7 there was a change where the percent sign ('%') went from being escaped by `re.escape` to _not_ being escaped. So now we need to use different regex patterns dependin on the Python version, yay.
author Ludovic Chabant <ludovic@chabant.com>
date Fri, 04 Oct 2019 11:13:33 -0700
parents e85f29b28b84
children
line wrap: on
line source

import mock
import pytest
from piecrust.uriutil import split_sub_uri


@pytest.mark.parametrize('uri, expected, pretty_urls', [
    ('/', ('/', 1), True),
    ('/2', ('/', 2), True),
    ('/foo/bar', ('/foo/bar', 1), True),
    ('/foo/bar/', ('/foo/bar', 1), True),
    ('/foo/bar/2/', ('/foo/bar', 2), True),
    ('/foo/bar.ext', ('/foo/bar.ext', 1), True),
    ('/foo/bar.ext/2', ('/foo/bar.ext', 2), True),
    ('/', ('/', 1), False),
    ('/2.html', ('/', 2), False),
    ('/foo/bar.html', ('/foo/bar.html', 1), False),
    ('/foo/bar/2.html', ('/foo/bar.html', 2), False),
    ('/foo/bar.ext', ('/foo/bar.ext', 1), False),
    ('/foo/bar/2.ext', ('/foo/bar.ext', 2), False)
    ])
def test_split_sub_uri(uri, expected, pretty_urls):
    app = mock.MagicMock()
    app.config = {
            'site/root': '/',
            'site/pretty_urls': pretty_urls,
            '__cache/pagination_suffix_re': '/(?P<num>\\d+)$'}
    actual = split_sub_uri(app, uri)
    assert actual == (expected[0], expected[1])


@pytest.mark.parametrize('uri, expected, pretty_urls', [
    ('/', ('/', 1), True),
    ('/2/', ('/', 2), True),
    ('/foo/bar', ('/foo/bar/', 1), True),
    ('/foo/bar/', ('/foo/bar/', 1), True),
    ('/foo/bar/2', ('/foo/bar/', 2), True),
    ('/foo/bar/2/', ('/foo/bar/', 2), True),
    ('/foo/bar.ext/', ('/foo/bar.ext/', 1), True),
    ('/foo/bar.ext/2/', ('/foo/bar.ext/', 2), True),
    ])
def test_split_sub_uri_trailing_slash(uri, expected, pretty_urls):
    app = mock.MagicMock()
    app.config = {
            'site/root': '/',
            'site/pretty_urls': pretty_urls,
            'site/trailing_slash': True,
            '__cache/pagination_suffix_re': '/(?P<num>\\d+)$'}
    actual = split_sub_uri(app, uri)
    assert actual == (expected[0], expected[1])


@pytest.mark.parametrize('uri, expected, pretty_urls', [
    ('/', ('/', 1), True),
    ('/2', ('/', 2), True),
    ('/foo/bar', ('/foo/bar', 1), True),
    ('/foo/bar/', ('/foo/bar', 1), True),
    ('/foo/bar/2', ('/foo/bar', 2), True),
    ('/foo/bar/2/', ('/foo/bar', 2), True),
    ('/foo/bar.ext', ('/foo/bar.ext', 1), True),
    ('/foo/bar.ext/2', ('/foo/bar.ext', 2), True),
    ('/', ('/', 1), False),
    ('/2.html', ('/', 2), False),
    ('/foo/bar.html', ('/foo/bar.html', 1), False),
    ('/foo/bar/2.html', ('/foo/bar.html', 2), False),
    ('/foo/bar.ext', ('/foo/bar.ext', 1), False),
    ('/foo/bar/2.ext', ('/foo/bar.ext', 2), False)
    ])
def test_split_sub_uri_with_root(uri, expected, pretty_urls):
    app = mock.MagicMock()
    app.config = {
            'site/root': '/whatever/',
            'site/pretty_urls': pretty_urls,
            '__cache/pagination_suffix_re': '/(?P<num>\\d+)$'}
    actual = split_sub_uri(app, '/whatever' + uri)
    assert actual == ('/whatever' + expected[0], expected[1])