bugfix #9: handle string content with multiple identical variables.

This commit is contained in:
debugtalk
2017-07-31 21:46:11 +08:00
parent 13a53681ed
commit 94f3bfc00b
4 changed files with 147 additions and 205 deletions

View File

@@ -1,77 +1,87 @@
import re
from ate.exception import ParamsError
from ate.utils import string_type
from ate import utils
def parse_content_with_variables(content, variables_binds):
""" replace variables with bind value
"""
# check if content includes $variable
matched = re.match(r"^(.*)\$(\w+)(.*)$", content)
if matched:
# this is a variable, and will replace with its bind value
variable_name = matched.group(2)
value = variables_binds.get(variable_name)
if value is None:
raise ParamsError(
"%s is not defined in bind variables!" % variable_name)
if matched.group(1) or matched.group(3):
# e.g. /api/users/$uid
return content.replace("$%s" % variable_name, value)
def parse_content_with_bindings(content, variables_binds, functions_binds):
""" evaluate content recursively, each variable in content will be
evaluated with bind variables and functions.
return value
return content
def parse_template(testcase_template, variables_binds):
""" parse testcase_template, replace all variables with bind value.
variables marker: $variable.
@param (dict) testcase_template
@param (dict) content in any data structure
{
"url": "http://127.0.0.1:5000/api/users/$uid",
"method": "POST",
"headers": {
"Content-Type": "application/json",
"authorization": "$authorization",
"random": "$random"
"random": "$random",
"sum": "${add_two_nums(1, 2)}"
},
"body": "$data"
}
@param (dict) variables binds mapping
@param (dict) variables_binds, variables binds mapping
{
"authorization": "a83de0ff8d2e896dbd8efb81ba14e17d",
"random": "A2dEx",
"data": '{"name": "user", "password": "123456"}'
"data": {"name": "user", "password": "123456"}
}
@return (dict) parsed testcase with bind variable values
@param (dict) functions_binds, functions binds mapping
{
"add_two_nums": lambda a, b=1: a + b
}
@return (dict) parsed content with evaluated bind values
{
"url": "http://127.0.0.1:5000/api/users/1000",
"method": "POST",
"headers": {
"Content-Type": "application/json",
"authorization": "a83de0ff8d2e896dbd8efb81ba14e17d",
"random": "A2dEx"
"random": "A2dEx",
"sum": 3
},
"body": '{"name": "user", "password": "123456"}'
"body": {"name": "user", "password": "123456"}
}
"""
def substitute(content):
""" substitute content recursively, each variable will be replaced with bind value.
"""
if isinstance(content, string_type):
return parse_content_with_variables(content, variables_binds)
if isinstance(content, (list, tuple)):
return [
parse_content_with_bindings(item, variables_binds, functions_binds)
for item in content
]
if isinstance(content, list):
return [substitute(item) for item in content]
if isinstance(content, dict):
evaluated_data = {}
for key, value in content.items():
evaluated_data[key] = parse_content_with_bindings(
value, variables_binds, functions_binds)
if isinstance(content, dict):
parsed_content = {}
for key, value in content.items():
parsed_content[key] = substitute(value)
return parsed_content
return evaluated_data
if isinstance(content, (int, float)):
return content
return substitute(testcase_template)
# content is in string format here
content = "" if content is None else content.strip()
if utils.is_functon(content):
# function marker: ${func(1, 2, a=3, b=4)}
fuction_meta = utils.parse_function(content)
func_name = fuction_meta['func_name']
func = functions_binds.get(func_name)
if func is None:
raise ParamsError(
"%s is not defined in bind functions!" % func_name)
args = fuction_meta.get('args', [])
kwargs = fuction_meta.get('kwargs', {})
args = parse_content_with_bindings(args, variables_binds, functions_binds)
kwargs = parse_content_with_bindings(kwargs, variables_binds, functions_binds)
return func(*args, **kwargs)
elif utils.get_contain_variables(content):
parsed_data = utils.parse_variables(content, variables_binds)
return parsed_data
else:
return content