bugfix: when the bind value of variable is not in string type, it should be converted to string first, or it will raise TypeError in replace function

This commit is contained in:
debugtalk
2017-07-31 21:04:19 +08:00
parent 65ae5b83df
commit 13a53681ed
2 changed files with 34 additions and 5 deletions
+10 -4
View File
@@ -151,10 +151,16 @@ def parse_variables(content, variable_mapping):
raise ParamsError( raise ParamsError(
"%s is not defined in bind variables!" % variable_name) "%s is not defined in bind variables!" % variable_name)
content = content.replace( if "${}".format(variable_name) == content:
"${}".format(variable_name), # content is a variable
variable_value content = variable_value
) else:
# content contains one or many variables
content = content.replace(
"${}".format(variable_name),
str(variable_value)
)
return content return content
def is_functon(content): def is_functon(content):
+24 -1
View File
@@ -164,7 +164,10 @@ class TestUtils(ApiServerUnittest):
def test_parse_variables(self): def test_parse_variables(self):
variable_mapping = { variable_mapping = {
"var_1": "abc", "var_1": "abc",
"var_2": "def" "var_2": "def",
"var_3": 123,
"var_4": {"a": 1},
"var_5": True
} }
self.assertEqual( self.assertEqual(
utils.parse_variables("$var_1", variable_mapping), utils.parse_variables("$var_1", variable_mapping),
@@ -190,6 +193,26 @@ class TestUtils(ApiServerUnittest):
utils.parse_variables("${func($var_1, $var_2, xyz)}", variable_mapping), utils.parse_variables("${func($var_1, $var_2, xyz)}", variable_mapping),
"${func(abc, def, xyz)}" "${func(abc, def, xyz)}"
) )
self.assertEqual(
utils.parse_variables("$var_3", variable_mapping),
123
)
self.assertEqual(
utils.parse_variables("$var_4", variable_mapping),
{"a": 1}
)
self.assertEqual(
utils.parse_variables("$var_5", variable_mapping),
True
)
self.assertEqual(
utils.parse_variables("abc$var_5", variable_mapping),
"abcTrue"
)
self.assertEqual(
utils.parse_variables("abc$var_4", variable_mapping),
"abc{'a': 1}"
)
def test_is_functon(self): def test_is_functon(self):
content = "${func()}" content = "${func()}"