view tests/test_routing.py @ 182:a54d3c0b5f4a

tests: Patch `os.path.exists` and improve patching for `open`. You can specify additional modules for which to patch `open`. Also, it was incorrectly updating the opened file, even when it was opened for read only. Now it only updates the contents if the file was opened for write, and supports appending to the end. Last, it supports opening text files in binary mode.
author Ludovic Chabant <ludovic@chabant.com>
date Sun, 04 Jan 2015 14:55:41 -0800
parents 4b0c87e7df73
children 61145dcd56e0
line wrap: on
line source

import mock
import pytest
from piecrust.routing import Route


@pytest.mark.parametrize(
        'config, metadata, expected',
        [
            ({'url': '/%foo%'},
                {'foo': 'bar'}, True),
            ({'url': '/%foo%'},
                {'zoo': 'zar', 'foo': 'bar'}, True),
            ({'url': '/%foo%'},
                {'zoo': 'zar'}, False),
            ({'url': '/%foo%/%zoo%'},
                {'zoo': 'zar'}, False)
            ])
def test_matches_metadata(config, metadata, expected):
    app = mock.Mock()
    app.config = {'site/root': '/'}
    config.setdefault('source', 'blah')
    route = Route(app, config)
    m = route.matchesMetadata(metadata)
    assert m == expected


@pytest.mark.parametrize(
        'config, uri, expected_match',
        [
            ({'url': '/%foo%'},
                'something',
                {'foo': 'something'}),
            ({'url': '/%foo%'},
                'something/other',
                None),
            ({'url': '/%path:foo%'},
                'something/other',
                {'foo': 'something/other'}),
            ({'url': '/%path:foo%'},
                '',
                {'foo': ''}),
            ({'url': '/prefix/%path:foo%'},
                'prefix/something/other',
                {'foo': 'something/other'}),
            ({'url': '/prefix/%path:foo%'},
                'prefix/',
                {'foo': ''}),
            ({'url': '/prefix/%path:foo%'},
                'prefix',
                {}),
            ])
def test_match_uri(config, uri, expected_match):
    app = mock.Mock()
    app.config = {'site/root': '/'}
    config.setdefault('source', 'blah')
    route = Route(app, config)
    assert route.uri_pattern == config['url'].lstrip('/')
    m = route.matchUri(uri)
    assert m == expected_match