def test_no_params(self): """No actual params given.""" dparams = {'seed': 23, 'foo': 'ciaone proprio!'} actual = configurable.merge(dparams, None) self.assertEqual(dparams, actual) self.assertFalse(actual is dparams) actual = configurable.merge(dparams, {}) self.assertEqual(dparams, actual) self.assertFalse(actual is dparams)
def test_no_params(self): """No actual params given.""" dparams = { 'seed': 23, 'foo': 'ciaone proprio!' } actual = configurable.merge(dparams, None) self.assertEqual(dparams, actual) self.assertFalse(actual is dparams) actual = configurable.merge(dparams, {}) self.assertEqual(dparams, actual) self.assertFalse(actual is dparams)
def test_override_with_none(self): """`None` can override all the values.""" dparams = {'seed': 23, 'foo': {'bar': 7, 'baz': 11}, 'text': 'fates warning.'} params = {'seed': None, 'foo': None, 'text': None} merged = configurable.merge(dparams, params) for key, value in merged.items(): self.assertIn(key, params) self.assertIsNone(value)
def test_dict_param_merged(self): """If the param is a dict, merge it with the default one.""" dparams = {'seed': 23, 'foo': {'bar': 7, 'baz': 11}} params = {'foo': {'bar': 9}} merged = configurable.merge(dparams, params) dexp = dparams['foo'] exp = params['foo'] act = merged['foo'] for key, value in act.items(): if key in exp: self.assertEqual(value, exp[key]) elif key in dexp: self.assertEqual(value, dexp[key]) else: raise RuntimeError('Un-mapped key %s', key)
def test_override_with_none(self): """`None` can override all the values.""" dparams = { 'seed': 23, 'foo': { 'bar': 7, 'baz': 11 }, 'text': 'fates warning.' } params = {'seed': None, 'foo': None, 'text': None} merged = configurable.merge(dparams, params) for key, value in merged.items(): self.assertIn(key, params) self.assertIsNone(value)
def test_none_as_default(self): """If `None` is a default param value, it is overwritten.""" dparams = {'seed': 23, 'foo': None, 'bar': 'baz'} params = {'seed': 23, 'foo': 'raise a Warning and carry on!'} merged = configurable.merge(dparams, params) self.assertEqual(len(dparams), len(merged)) for key in dparams: self.assertIn(key, merged) for key in merged: act = merged[key] if key in params: exp = params[key] else: exp = dparams[key] self.assertEqual(exp, act)
def test_empty_dict_as_default(self): """If a default param is an empty string, it gets overriden.""" dparams = {'seed': 23, 'foo': {}} params = {'foo': {'seven': 7, 'eleven': 11}} merged = configurable.merge(dparams, params) self.assertEqual(merged['foo'], params['foo'])