Mercurial > piecrust2
annotate tests/test_sources_posts.py @ 380:f33712c4cfab
routing: Fix bugs with matching URLs with correct route but missing metadata.
When matching a route like `/foo/%slug%` against an URL like `/foo`, the route
will (correctly) return a match, but it will be completely missing the `slug`
metadata, resulting in problems elsewhere. This change makes it so that any
missing route metadata will be filled in with an empty string.
And because this means generated URLs may differ from the incoming URL when
using trailing slashes (`/foo/` _vs._ `/foo`), we make the assert in the
chef server handle those discrepancies.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Sun, 10 May 2015 00:34:21 -0700 |
parents | cb6eadea0845 |
children | e7b865f8f335 |
rev | line source |
---|---|
95
cb6eadea0845
Fixed a bug with the `shallow` source. Add unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
1 import os |
cb6eadea0845
Fixed a bug with the `shallow` source. Add unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
2 import pytest |
cb6eadea0845
Fixed a bug with the `shallow` source. Add unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
3 from .mockutil import mock_fs, mock_fs_scope |
cb6eadea0845
Fixed a bug with the `shallow` source. Add unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
4 |
cb6eadea0845
Fixed a bug with the `shallow` source. Add unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
5 |
cb6eadea0845
Fixed a bug with the `shallow` source. Add unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
6 @pytest.mark.parametrize('fs, src_type, expected_paths, expected_metadata', [ |
cb6eadea0845
Fixed a bug with the `shallow` source. Add unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
7 (mock_fs(), 'flat', [], []), |
cb6eadea0845
Fixed a bug with the `shallow` source. Add unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
8 (mock_fs().withPage('test/2014-01-01_foo.md'), |
cb6eadea0845
Fixed a bug with the `shallow` source. Add unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
9 'flat', |
cb6eadea0845
Fixed a bug with the `shallow` source. Add unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
10 ['2014-01-01_foo.md'], |
cb6eadea0845
Fixed a bug with the `shallow` source. Add unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
11 [(2014, 1, 1, 'foo')]), |
cb6eadea0845
Fixed a bug with the `shallow` source. Add unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
12 (mock_fs(), 'shallow', [], []), |
cb6eadea0845
Fixed a bug with the `shallow` source. Add unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
13 (mock_fs().withPage('test/2014/01-01_foo.md'), |
cb6eadea0845
Fixed a bug with the `shallow` source. Add unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
14 'shallow', |
cb6eadea0845
Fixed a bug with the `shallow` source. Add unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
15 ['2014/01-01_foo.md'], |
cb6eadea0845
Fixed a bug with the `shallow` source. Add unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
16 [(2014, 1, 1, 'foo')]), |
cb6eadea0845
Fixed a bug with the `shallow` source. Add unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
17 (mock_fs(), 'hierarchy', [], []), |
cb6eadea0845
Fixed a bug with the `shallow` source. Add unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
18 (mock_fs().withPage('test/2014/01/01_foo.md'), |
cb6eadea0845
Fixed a bug with the `shallow` source. Add unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
19 'hierarchy', |
cb6eadea0845
Fixed a bug with the `shallow` source. Add unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
20 ['2014/01/01_foo.md'], |
cb6eadea0845
Fixed a bug with the `shallow` source. Add unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
21 [(2014, 1, 1, 'foo')]), |
cb6eadea0845
Fixed a bug with the `shallow` source. Add unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
22 ]) |
cb6eadea0845
Fixed a bug with the `shallow` source. Add unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
23 def test_post_source_factories(fs, src_type, expected_paths, expected_metadata): |
cb6eadea0845
Fixed a bug with the `shallow` source. Add unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
24 fs.withConfig({ |
cb6eadea0845
Fixed a bug with the `shallow` source. Add unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
25 'site': { |
cb6eadea0845
Fixed a bug with the `shallow` source. Add unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
26 'sources': { |
cb6eadea0845
Fixed a bug with the `shallow` source. Add unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
27 'test': {'type': 'posts/%s' % src_type}}, |
cb6eadea0845
Fixed a bug with the `shallow` source. Add unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
28 'routes': [ |
cb6eadea0845
Fixed a bug with the `shallow` source. Add unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
29 {'url': '/%slug%', 'source': 'test'}] |
cb6eadea0845
Fixed a bug with the `shallow` source. Add unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
30 } |
cb6eadea0845
Fixed a bug with the `shallow` source. Add unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
31 }) |
cb6eadea0845
Fixed a bug with the `shallow` source. Add unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
32 fs.withDir('kitchen/test') |
cb6eadea0845
Fixed a bug with the `shallow` source. Add unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
33 with mock_fs_scope(fs): |
cb6eadea0845
Fixed a bug with the `shallow` source. Add unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
34 app = fs.getApp(cache=False) |
cb6eadea0845
Fixed a bug with the `shallow` source. Add unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
35 s = app.getSource('test') |
cb6eadea0845
Fixed a bug with the `shallow` source. Add unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
36 facs = list(s.buildPageFactories()) |
cb6eadea0845
Fixed a bug with the `shallow` source. Add unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
37 paths = [f.rel_path for f in facs] |
cb6eadea0845
Fixed a bug with the `shallow` source. Add unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
38 assert paths == expected_paths |
cb6eadea0845
Fixed a bug with the `shallow` source. Add unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
39 metadata = [ |
cb6eadea0845
Fixed a bug with the `shallow` source. Add unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
40 (f.metadata['year'], f.metadata['month'], |
cb6eadea0845
Fixed a bug with the `shallow` source. Add unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
41 f.metadata['day'], f.metadata['slug']) |
cb6eadea0845
Fixed a bug with the `shallow` source. Add unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
42 for f in facs] |
cb6eadea0845
Fixed a bug with the `shallow` source. Add unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
43 assert metadata == expected_metadata |
cb6eadea0845
Fixed a bug with the `shallow` source. Add unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
44 |