changeset 204:f98451237371

internal: Add ability to get a default value if a config value doesn't exist. tests: Add some configuration tests.
author Ludovic Chabant <ludovic@chabant.com>
date Sun, 18 Jan 2015 11:53:18 -0800
parents 29165f2f315d
children e725af1d48fb
files piecrust/configuration.py tests/test_configuration.py
diffstat 2 files changed, 22 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/piecrust/configuration.py	Thu Jan 15 22:54:59 2015 -0800
+++ b/piecrust/configuration.py	Sun Jan 18 11:53:18 2015 -0800
@@ -39,7 +39,7 @@
     def getAll(self):
         return self.get()
 
-    def get(self, key_path=None):
+    def get(self, key_path=None, default_value=None):
         self._ensureLoaded()
         if key_path is None:
             return self._values
@@ -48,7 +48,7 @@
         for b in bits:
             cur = cur.get(b)
             if cur is None:
-                return None
+                return default_value
         return cur
 
     def set(self, key_path, value):
--- a/tests/test_configuration.py	Thu Jan 15 22:54:59 2015 -0800
+++ b/tests/test_configuration.py	Sun Jan 18 11:53:18 2015 -0800
@@ -60,6 +60,26 @@
     assert config.has('baz') is False
 
 
+def test_config_deep_set_non_existing():
+    config = Configuration({'foo': 'bar'})
+    assert config.get('baz') is None
+    config.set('baz/or/whatever', 'something')
+    assert config.has('baz') is True
+    assert config.has('baz/or') is True
+    assert config.get('baz/or/whatever') == 'something'
+
+
+def test_config_deep_set_existing():
+    config = Configuration({'foo': 'bar', 'baz': {'wat': 'nothing'}})
+    assert config.has('baz') is True
+    assert config.get('baz/wat') == 'nothing'
+    assert config.get('baz/or') is None
+    config.set('baz/or/whatever', 'something')
+    assert config.has('baz') is True
+    assert config.has('baz/or') is True
+    assert config.get('baz/or/whatever') == 'something'
+
+
 @pytest.mark.parametrize('local, incoming, expected', [
         ({}, {}, {}),
         ({'foo': 'bar'}, {}, {'foo': 'bar'}),