comparison tests/test_uriutil.py @ 484:d4321317beae

internal: Correctly split sub URIs. Add unit tests.
author Ludovic Chabant <ludovic@chabant.com>
date Wed, 22 Jul 2015 00:07:25 -0700
parents 422052d2e978
children 4284c673bb91
comparison
equal deleted inserted replaced
483:64e1cd71b30b 484:d4321317beae
25 actual = parse_uri(routes, uri) 25 actual = parse_uri(routes, uri)
26 assert actual == expected 26 assert actual == expected
27 27
28 28
29 @pytest.mark.parametrize('uri, expected, pretty_urls', [ 29 @pytest.mark.parametrize('uri, expected, pretty_urls', [
30 ('foo/bar', ('foo/bar', 1), True), 30 ('/', ('/', 1), True),
31 ('foo/bar/2', ('foo/bar', 2), True), 31 ('/2', ('/', 2), True),
32 ('foo/bar.ext', ('foo/bar.ext', 1), True), 32 ('/foo/bar', ('/foo/bar', 1), True),
33 ('foo/bar.ext/2', ('foo/bar.ext', 2), True), 33 ('/foo/bar/2', ('/foo/bar', 2), True),
34 ('foo/bar.html', ('foo/bar.html', 1), False), 34 ('/foo/bar.ext', ('/foo/bar.ext', 1), True),
35 ('foo/bar/2.html', ('foo/bar.html', 2), False), 35 ('/foo/bar.ext/2', ('/foo/bar.ext', 2), True),
36 ('foo/bar.ext', ('foo/bar.ext', 1), False), 36 ('/', ('/', 1), False),
37 ('foo/bar/2.ext', ('foo/bar.ext', 2), False) 37 ('/2.html', ('/', 2), False),
38 ('/foo/bar.html', ('/foo/bar.html', 1), False),
39 ('/foo/bar/2.html', ('/foo/bar.html', 2), False),
40 ('/foo/bar.ext', ('/foo/bar.ext', 1), False),
41 ('/foo/bar/2.ext', ('/foo/bar.ext', 2), False)
38 ]) 42 ])
39 def test_split_sub_uri(uri, expected, pretty_urls): 43 def test_split_sub_uri(uri, expected, pretty_urls):
44 app = mock.MagicMock()
45 app.config = {
46 'site/root': '/',
47 'site/pretty_urls': pretty_urls,
48 '__cache/pagination_suffix_re': '/(?P<num>\\d+)$'}
49 actual = split_sub_uri(app, uri)
50 assert actual == (expected[0], expected[1])
51
52
53 @pytest.mark.parametrize('uri, expected, pretty_urls', [
54 ('/', ('/', 1), True),
55 ('/2/', ('/', 2), True),
56 ('/foo/bar/', ('/foo/bar/', 1), True),
57 ('/foo/bar/2/', ('/foo/bar/', 2), True),
58 ('/foo/bar.ext/', ('/foo/bar.ext/', 1), True),
59 ('/foo/bar.ext/2/', ('/foo/bar.ext/', 2), True),
60 ])
61 def test_split_sub_uri_trailing_slash(uri, expected, pretty_urls):
62 app = mock.MagicMock()
63 app.config = {
64 'site/root': '/',
65 'site/pretty_urls': pretty_urls,
66 'site/trailing_slash': True,
67 '__cache/pagination_suffix_re': '/(?P<num>\\d+)$'}
68 actual = split_sub_uri(app, uri)
69 assert actual == (expected[0], expected[1])
70
71
72 @pytest.mark.parametrize('uri, expected, pretty_urls', [
73 ('/', ('/', 1), True),
74 ('/2', ('/', 2), True),
75 ('/foo/bar', ('/foo/bar', 1), True),
76 ('/foo/bar/2', ('/foo/bar', 2), True),
77 ('/foo/bar.ext', ('/foo/bar.ext', 1), True),
78 ('/foo/bar.ext/2', ('/foo/bar.ext', 2), True),
79 ('/', ('/', 1), False),
80 ('/2.html', ('/', 2), False),
81 ('/foo/bar.html', ('/foo/bar.html', 1), False),
82 ('/foo/bar/2.html', ('/foo/bar.html', 2), False),
83 ('/foo/bar.ext', ('/foo/bar.ext', 1), False),
84 ('/foo/bar/2.ext', ('/foo/bar.ext', 2), False)
85 ])
86 def test_split_sub_uri_with_root(uri, expected, pretty_urls):
40 app = mock.MagicMock() 87 app = mock.MagicMock()
41 app.config = { 88 app.config = {
42 'site/root': '/whatever/', 89 'site/root': '/whatever/',
43 'site/pretty_urls': pretty_urls, 90 'site/pretty_urls': pretty_urls,
44 '__cache/pagination_suffix_re': '/(?P<num>\\d+)$'} 91 '__cache/pagination_suffix_re': '/(?P<num>\\d+)$'}
45 actual = split_sub_uri(app, '/whatever/' + uri) 92 actual = split_sub_uri(app, '/whatever' + uri)
46 assert actual == ('/whatever/' + expected[0], expected[1]) 93 assert actual == ('/whatever' + expected[0], expected[1])
47 94