annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
177
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1 import mock
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2 import pytest
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
3 from piecrust.routing import Route
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
4
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
5
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
6 @pytest.mark.parametrize(
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
7 'config, metadata, expected',
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
8 [
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
9 ({'url': '/%foo%'},
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
10 {'foo': 'bar'}, True),
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
11 ({'url': '/%foo%'},
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
12 {'zoo': 'zar', 'foo': 'bar'}, True),
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
13 ({'url': '/%foo%'},
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
14 {'zoo': 'zar'}, False),
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
15 ({'url': '/%foo%/%zoo%'},
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
16 {'zoo': 'zar'}, False)
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
17 ])
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
18 def test_matches_metadata(config, metadata, expected):
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
19 app = mock.Mock()
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
20 app.config = {'site/root': '/'}
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
21 config.setdefault('source', 'blah')
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
22 route = Route(app, config)
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
23 m = route.matchesMetadata(metadata)
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
24 assert m == expected
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
25
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
26
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
27 @pytest.mark.parametrize(
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
28 'config, uri, expected_match',
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
29 [
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
30 ({'url': '/%foo%'},
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
31 'something',
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
32 {'foo': 'something'}),
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
33 ({'url': '/%foo%'},
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
34 'something/other',
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
35 None),
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
36 ({'url': '/%path:foo%'},
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
37 'something/other',
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
38 {'foo': 'something/other'}),
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
39 ({'url': '/%path:foo%'},
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
40 '',
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
41 {'foo': ''}),
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
42 ({'url': '/prefix/%path:foo%'},
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
43 'prefix/something/other',
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
44 {'foo': 'something/other'}),
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
45 ({'url': '/prefix/%path:foo%'},
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
46 'prefix/',
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
47 {'foo': ''}),
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
48 ({'url': '/prefix/%path:foo%'},
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
49 'prefix',
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
50 {}),
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
51 ])
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
52 def test_match_uri(config, uri, expected_match):
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
53 app = mock.Mock()
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
54 app.config = {'site/root': '/'}
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
55 config.setdefault('source', 'blah')
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
56 route = Route(app, config)
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
57 assert route.uri_pattern == config['url'].lstrip('/')
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
58 m = route.matchUri(uri)
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
59 assert m == expected_match
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
60