def test_simple_mapping(self): obj = {'foo': 'bar'} overrides = override.AttributeOverrides( {'foo': [ { 'when': 'bar', 'replace': 'baz' }, ]}) self.assertEqual('baz', overrides.get_attr(obj, 'foo'))
def test_default(self): obj = {'foo': 'bar'} overrides = override.AttributeOverrides({ 'foo': [{ 'when': { 'foo': ['foo'] }, 'replace': 'qux' }, { 'default': 'quux' }] }) self.assertEqual('quux', overrides.get_attr(obj, 'foo'))
def test_full_mapping1(self): obj1 = {'foo': 'bar'} obj2 = {'foo': 'baz'} overrides = override.AttributeOverrides( {'foo': [ { 'when': { 'foo': ['bar', 'baz'] }, 'replace': 'qux' }, ]}) self.assertEqual('qux', overrides.get_attr(obj1, 'foo')) self.assertEqual('qux', overrides.get_attr(obj2, 'foo'))
def test_full_mapping2(self): obj1 = { 'foo': 'bar', 'additional': 1337, } obj2 = { 'foo': 'baz', 'additional': 1338, } overrides = override.AttributeOverrides({ 'foo': [ { 'when': { 'foo': ['bar', 'baz'], 'additional': [1337], }, 'replace': 'qux' }, { 'when': { 'foo': ['bar'], 'additional': [1338], }, 'replace': 'quux' }, { 'when': { 'foo': ['baz'], 'additional': [1338], }, 'replace': 'quuux' }, ] }) self.assertEqual('qux', overrides.get_attr(obj1, 'foo')) self.assertEqual('quuux', overrides.get_attr(obj2, 'foo'))