refactor: format code with black

This commit is contained in:
debugtalk
2022-04-30 15:06:31 +08:00
parent 737379b49f
commit be8c053ed8
52 changed files with 1809 additions and 2254 deletions

View File

@@ -6,17 +6,13 @@ from tests.base import ApiServerUnittest, gen_random_string
class TestContext(ApiServerUnittest):
def setUp(self):
loader.load_project_data(os.path.join(os.getcwd(), "tests"))
self.context = context.SessionContext(
variables={"SECRET_KEY": "DebugTalk"}
)
self.context = context.SessionContext(variables={"SECRET_KEY": "DebugTalk"})
def test_init_test_variables_initialize(self):
self.assertEqual(
self.context.test_variables_mapping,
{'SECRET_KEY': 'DebugTalk'}
self.context.test_variables_mapping, {"SECRET_KEY": "DebugTalk"}
)
def test_init_test_variables(self):
@@ -28,36 +24,25 @@ class TestContext(ApiServerUnittest):
# "data": '{"name": "$username", "password": "123456"}',
"TOKEN": "debugtalk",
"username": "user1",
"num": 6
}
functions = {
"gen_random_string": gen_random_string,
"gen_md5": gen_md5
"num": 6,
}
functions = {"gen_random_string": gen_random_string, "gen_md5": gen_md5}
variables = parser.prepare_lazy_data(variables, functions, variables.keys())
variables = parser.parse_variables_mapping(variables)
self.context.init_test_variables(variables)
variables_mapping = self.context.test_variables_mapping
self.assertEqual(len(variables_mapping["random"]), 6)
self.assertEqual(len(variables_mapping["authorization"]), 32)
self.assertEqual(variables_mapping["data"], 'user1')
self.assertEqual(variables_mapping["data"], "user1")
def test_update_seesion_variables(self):
self.context.update_session_variables({"TOKEN": "debugtalk"})
self.assertEqual(
self.context.session_variables_mapping["TOKEN"],
"debugtalk"
)
self.assertEqual(self.context.session_variables_mapping["TOKEN"], "debugtalk")
def test_eval_content_variables(self):
variables = {
"SECRET_KEY": "DebugTalk"
}
variables = {"SECRET_KEY": "DebugTalk"}
content = parser.prepare_lazy_data("abc$SECRET_KEY", {}, variables.keys())
self.assertEqual(
self.context.eval_content(content),
"abcDebugTalk"
)
self.assertEqual(self.context.eval_content(content), "abcDebugTalk")
# TODO: fix variable extraction
# content = "abc$SECRET_KEYdef"
@@ -71,12 +56,9 @@ class TestContext(ApiServerUnittest):
"random": "${gen_random_string(5)}",
"data": '{"name": "user", "password": "123456"}',
"authorization": "${gen_md5($TOKEN, $data, $random)}",
"TOKEN": "debugtalk"
}
functions = {
"gen_random_string": gen_random_string,
"gen_md5": gen_md5
"TOKEN": "debugtalk",
}
functions = {"gen_random_string": gen_random_string, "gen_md5": gen_md5}
variables = parser.prepare_lazy_data(variables, functions, variables.keys())
variables = parser.parse_variables_mapping(variables)
self.context.init_test_variables(variables)
@@ -88,14 +70,12 @@ class TestContext(ApiServerUnittest):
"Content-Type": "application/json",
"authorization": "$authorization",
"random": "$random",
"secret_key": "$SECRET_KEY"
"secret_key": "$SECRET_KEY",
},
"data": "$data"
"data": "$data",
}
prepared_request = parser.prepare_lazy_data(
request,
functions,
{"authorization", "random", "SECRET_KEY", "data"}
request, functions, {"authorization", "random", "SECRET_KEY", "data"}
)
parsed_request = self.context.eval_content(prepared_request)
self.assertIn("authorization", parsed_request["headers"])
@@ -104,17 +84,14 @@ class TestContext(ApiServerUnittest):
self.assertEqual(len(parsed_request["headers"]["random"]), 5)
self.assertIn("data", parsed_request)
self.assertEqual(
parsed_request["data"],
'{"name": "user", "password": "123456"}'
parsed_request["data"], '{"name": "user", "password": "123456"}'
)
self.assertEqual(parsed_request["headers"]["secret_key"], "DebugTalk")
def test_validate(self):
testcases = [
{
"config": {
'name': "test validation"
},
"config": {"name": "test validation"},
"teststeps": [
{
"name": "test validation",
@@ -124,26 +101,32 @@ class TestContext(ApiServerUnittest):
},
"variables": {
"resp_status_code": 200,
"resp_body_success": True
"resp_body_success": True,
},
"validate": [
{"eq": ["$resp_status_code", 200]},
{"check": "$resp_status_code", "comparator": "eq", "expect": 200},
{
"check": "$resp_status_code",
"comparator": "eq",
"expect": 200,
},
{"check": "$resp_body_success", "expect": True},
{"check": "${is_status_code_200($resp_status_code)}", "expect": True}
]
{
"check": "${is_status_code_200($resp_status_code)}",
"expect": True,
},
],
}
]
],
}
]
from tests.debugtalk import is_status_code_200
tests_mapping = {
"project_mapping": {
"functions": {
"is_status_code_200": is_status_code_200
}
"functions": {"is_status_code_200": is_status_code_200}
},
"testcases": testcases
"testcases": testcases,
}
testcases = parser.parse_tests(tests_mapping)
parsed_testcase = testcases[0]
@@ -154,9 +137,7 @@ class TestContext(ApiServerUnittest):
def test_validate_exception(self):
testcases = [
{
"config": {
'name': "test validation"
},
"config": {"name": "test validation"},
"teststeps": [
{
"name": "test validation",
@@ -166,20 +147,22 @@ class TestContext(ApiServerUnittest):
},
"variables": {
"resp_status_code": 200,
"resp_body_success": True
"resp_body_success": True,
},
"validate": [
{"eq": ["$resp_status_code", 201]},
{"check": "$resp_status_code", "expect": 201},
{"check": "$resp_body_success", "comparator": "eq", "expect": True}
]
{
"check": "$resp_body_success",
"comparator": "eq",
"expect": True,
},
],
}
]
],
}
]
tests_mapping = {
"testcases": testcases
}
tests_mapping = {"testcases": testcases}
testcases = parser.parse_tests(tests_mapping)
parsed_testcase = testcases[0]
test_runner = runner.Runner(parsed_testcase["config"])