Add to TranslationRule REPLACE rule value_path del

If TranslationRule defined with REPLACE rule and value
defined with value_path, delete value_path property data
after replacing.

bp deprecating-improvements

Change-Id: I314ff90a10c86a11964200ca46187966507f32ef
This commit is contained in:
Peter Razumovsky 2015-07-27 12:48:21 +03:00
parent 72f8bc4abc
commit 9479c5bd94
2 changed files with 15 additions and 9 deletions

View File

@ -618,12 +618,12 @@ class TranslationRule(object):
list-type values can be added to such properties. Using for other
cases is prohibited and will be returned with error.
- REPLACE. This rule allows to replace some property value to another. Used
for all types of properties. Note, that if property has list type, then
value will be replaced for all elements of list, where it needed. If
element in such property must be replaced by value of another element of
this property, value_name must be defined.
for all types of properties. Note, that if property has list type,
then value will be replaced for all elements of list, where it
needed. If element in such property must be replaced by value of
another element of this property, value_name must be defined.
- DELETE. This rule allows to delete some property. If property has list
type, then deleting affects value in all list elements.
type, then deleting affects value in all list elements.
"""
RULE_KEYS = (ADD, REPLACE, DELETE) = ('Add', 'Replace', 'Delete')
@ -669,7 +669,7 @@ class TranslationRule(object):
raise ValueError(_('Use value_name only for replacing list '
'elements.'))
elif self.rule == self.ADD and not isinstance(self.value, list):
raise ValueError(_('value must be list type when rule is ADD.'))
raise ValueError(_('value must be list type when rule is Add.'))
def execute_rule(self):
(source_key, source_data) = self.get_data_from_source_path(
@ -695,7 +695,7 @@ class TranslationRule(object):
if isinstance(source_data, list):
source_data.extend(value)
else:
raise ValueError(_('ADD rule must be used only for '
raise ValueError(_('Add rule must be used only for '
'lists.'))
elif self.rule == TranslationRule.REPLACE:
if isinstance(source_data, list):
@ -718,6 +718,10 @@ class TranslationRule(object):
% dict(key=source_key,
name=value_key))
source_data[source_key] = value
# If value defined with value_path, need to delete value_path
# property data after it's replacing.
if value_data and value_data.get(value_key):
del value_data[value_key]
elif self.rule == TranslationRule.DELETE:
if isinstance(source_data, list):
for item in source_data:

View File

@ -1877,7 +1877,7 @@ class TestTranslationRule(common.HeatTestCase):
properties.TranslationRule.ADD,
['any'],
'value')
self.assertEqual('value must be list type when rule is ADD.',
self.assertEqual('value must be list type when rule is Add.',
six.text_type(exc))
def test_add_rule_exist(self):
@ -1969,7 +1969,7 @@ class TestTranslationRule(common.HeatTestCase):
[props.get('bar')])
exc = self.assertRaises(ValueError, rule.execute_rule)
self.assertEqual('ADD rule must be used only for lists.',
self.assertEqual('Add rule must be used only for lists.',
six.text_type(exc))
def test_replace_rule_map_exist(self):
@ -2110,6 +2110,7 @@ class TestTranslationRule(common.HeatTestCase):
rule.execute_rule()
self.assertEqual('one', props.get('bar'))
self.assertEqual('one', props.get('far'))
def test_replace_rule_str_value_path_error(self):
schema = {
@ -2146,6 +2147,7 @@ class TestTranslationRule(common.HeatTestCase):
rule.execute_rule()
self.assertEqual('one', props.get('bar'))
self.assertIsNone(props.get('far'))
def test_replace_rule_str_invalid(self):
schema = {