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

@@ -88,7 +88,11 @@ class Context(object):
"""
for variable_bind in variable_binds:
for variable_name, value in variable_bind.items():
variable_evale_value = self.get_eval_value(value)
variable_evale_value = testcase.parse_content_with_bindings(
value,
self.testcase_variables_mapping,
self.testcase_functions_config
)
if level == "testset":
self.testset_shared_variables_mapping[variable_name] = variable_evale_value
@@ -126,48 +130,13 @@ class Context(object):
def get_parsed_request(self):
""" get parsed request, with each variable replaced by bind value.
"""
parsed_request = testcase.parse_template(
parsed_request = testcase.parse_content_with_bindings(
self.testcase_request_config,
self.testcase_variables_mapping
self.testcase_variables_mapping,
self.testcase_functions_config
)
return parsed_request
def get_testcase_variables_mapping(self):
return self.testcase_variables_mapping
def get_eval_value(self, data):
""" evaluate data recursively, each variable in data will be evaluated.
"""
if isinstance(data, (list, tuple)):
return [self.get_eval_value(item) for item in data]
if isinstance(data, dict):
evaluated_data = {}
for key, value in data.items():
evaluated_data[key] = self.get_eval_value(value)
return evaluated_data
if isinstance(data, (int, float)):
return data
# data is in string format here
data = "" if data is None else data.strip()
if utils.is_functon(data):
# function marker: ${func(1, 2, a=3, b=4)}
fuction_meta = utils.parse_function(data)
func_name = fuction_meta['func_name']
args = fuction_meta.get('args', [])
kwargs = fuction_meta.get('kwargs', {})
args = self.get_eval_value(args)
kwargs = self.get_eval_value(kwargs)
return self.testcase_functions_config[func_name](*args, **kwargs)
elif utils.get_contain_variables(data):
parsed_data = utils.parse_variables(data, self.testcase_variables_mapping)
return parsed_data
else:
return data

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