Mercurial > piecrust2
comparison tests/test_configuration.py @ 2:40fa08b261b9
Added unit tests (using `py.test`) for `Configuration`.
Fixed some configuration module bugs.
| author | Ludovic Chabant <ludovic@chabant.com> |
|---|---|
| date | Wed, 25 Dec 2013 22:16:46 -0800 |
| parents | |
| children | f485ba500df3 |
comparison
equal
deleted
inserted
replaced
| 1:aaa8fb7c8918 | 2:40fa08b261b9 |
|---|---|
| 1 import copy | |
| 2 import pytest | |
| 3 from piecrust.configuration import Configuration, merge_dicts | |
| 4 | |
| 5 | |
| 6 @pytest.mark.parametrize('values, expected', [ | |
| 7 (None, {}), | |
| 8 ({'foo': 'bar'}, {'foo': 'bar'}) | |
| 9 ]) | |
| 10 def test_config_init(values, expected): | |
| 11 config = Configuration(values) | |
| 12 assert config.get() == expected | |
| 13 | |
| 14 | |
| 15 def test_config_set_all(): | |
| 16 config = Configuration() | |
| 17 config.set_all({'foo': 'bar'}) | |
| 18 assert config.get() == {'foo': 'bar'} | |
| 19 | |
| 20 | |
| 21 def test_config_get_and_set(): | |
| 22 config = Configuration({'foo': 'bar', 'answer': 42}) | |
| 23 assert config.get('foo') == 'bar' | |
| 24 assert config.get('answer') == 42 | |
| 25 | |
| 26 config.set('foo', 'something') | |
| 27 assert config.get('foo') == 'something' | |
| 28 | |
| 29 | |
| 30 def test_config_get_and_set_nested(): | |
| 31 config = Configuration({ | |
| 32 'foo': [4, 2], | |
| 33 'bar': { | |
| 34 'child1': 'one', | |
| 35 'child2': 'two' | |
| 36 } | |
| 37 }) | |
| 38 assert config.get('foo') == [4, 2] | |
| 39 assert config.get('bar/child1') == 'one' | |
| 40 assert config.get('bar/child2') == 'two' | |
| 41 | |
| 42 config.set('bar/child1', 'other one') | |
| 43 config.set('bar/child3', 'new one') | |
| 44 assert config.get('bar/child1') == 'other one' | |
| 45 assert config.get('bar/child3') == 'new one' | |
| 46 | |
| 47 | |
| 48 def test_config_get_missing(): | |
| 49 config = Configuration({'foo': 'bar'}) | |
| 50 assert config.get('baz') is None | |
| 51 | |
| 52 | |
| 53 def test_config_has(): | |
| 54 config = Configuration({'foo': 'bar'}) | |
| 55 assert config.has('foo') is True | |
| 56 assert config.has('baz') is False | |
| 57 | |
| 58 | |
| 59 @pytest.mark.parametrize('local, incoming, expected', [ | |
| 60 ({}, {}, {}), | |
| 61 ({'foo': 'bar'}, {}, {'foo': 'bar'}), | |
| 62 ({}, {'foo': 'bar'}, {'foo': 'bar'}), | |
| 63 ({'foo': 'bar'}, {'foo': 'other'}, {'foo': 'other'}), | |
| 64 ({'foo': [1, 2]}, {'foo': [3]}, {'foo': [3, 1, 2]}), | |
| 65 ({'foo': [1, 2]}, {'foo': 'bar'}, {'foo': 'bar'}), | |
| 66 ({'foo': {'bar': 1, 'baz': 2}}, {'foo': 'bar'}, {'foo': 'bar'}), | |
| 67 ({'foo': {'bar': 1, 'baz': 2}}, {'foo': {'other': 3}}, {'foo': {'bar': 1, 'baz': 2, 'other': 3}}), | |
| 68 ({'foo': {'bar': 1, 'baz': 2}}, {'foo': {'baz': 10}}, {'foo': {'bar': 1, 'baz': 10}}) | |
| 69 ]) | |
| 70 def test_merge_dicts(local, incoming, expected): | |
| 71 local2 = copy.deepcopy(local) | |
| 72 merge_dicts(local2, incoming) | |
| 73 assert local2 == expected | |
| 74 | |
| 75 | |
| 76 def test_config_merge(): | |
| 77 config = Configuration({ | |
| 78 'foo': [4, 2], | |
| 79 'bar': { | |
| 80 'child1': 'one', | |
| 81 'child2': 'two' | |
| 82 } | |
| 83 }) | |
| 84 other = Configuration({ | |
| 85 'baz': True, | |
| 86 'blah': 'blah blah', | |
| 87 'bar': { | |
| 88 'child1': 'other one', | |
| 89 'child10': 'ten' | |
| 90 } | |
| 91 }) | |
| 92 config.merge(other) | |
| 93 | |
| 94 expected = { | |
| 95 'foo': [4, 2], | |
| 96 'baz': True, | |
| 97 'blah': 'blah blah', | |
| 98 'bar': { | |
| 99 'child1': 'other one', | |
| 100 'child2': 'two', | |
| 101 'child10': 'ten' | |
| 102 } | |
| 103 } | |
| 104 assert config.get() == expected | |
| 105 | |
| 106 |
