增加自动生成page的脚本 根据urlData.yml文件

This commit is contained in:
jing song 2021-09-15 18:03:56 +08:00
parent 6d92b3460a
commit d2569fb929
3 changed files with 101 additions and 53 deletions

View File

@ -8,6 +8,7 @@ import jsonpath
from scripts.dataBase import MYSQL
from scripts.log import Log
from scripts.randomData import replace_random
from scripts.readYamlFile import ini_yaml
Log()
@ -138,12 +139,13 @@ def assert_sql(hope_res, real_res):
if jsonpath.jsonpath(real_res, i["path"]):
r_res = jsonpath.jsonpath(real_res, i["path"])[0]
t1 = time.time()
datas = db.run_sql(h_sql)
r_sql = replace_random(h_sql,real_res)
datas = db.run_sql(r_sql)
try:
with allure.step("数据库校验"):
allure.attach(name="期望结果", body=str(datas))
allure.attach(name='实际实际结果', body=str(r_res))
allure.attach(name='sql命令', body=str(h_sql))
allure.attach(name='sql命令', body=str(r_sql))
assert r_res == datas[0][i["name"]]
logging.info(
"sql断言通过, 期望结果{0}, 实际结果{1},sql耗时{2:.5f}".format(datas, r_res, time.time() - t1))
@ -203,4 +205,4 @@ if __name__ == '__main__':
# }
hp = ini_yaml("loginData.yml")
print(hp)
# asserting(hp, j,23)
asserting(hp, j,23)

View File

@ -7,7 +7,6 @@ import pymysql
from scripts.log import Log
import logging
from config.confManage import db_manage
from scripts.randomData import replace_random
Log()
@ -39,9 +38,8 @@ class MYSQL(object):
"""
try:
logging.debug("准备执行SQL语句..")
r_sql = replace_random(sql)
logging.debug("sql语句:{}".format(r_sql))
self.cursor.execute(r_sql)
logging.debug("sql语句:{}".format(sql))
self.cursor.execute(sql)
rs = self.cursor.fetchall()
return rs
except Exception as e:

View File

@ -5,6 +5,8 @@ import random
import re
import time
import jsonpath
def choice_data(data):
"""
@ -50,6 +52,9 @@ def md5(data):
data = m1.hexdigest()
return data
def sql_json(jspath,res):
return jsonpath.jsonpath(res, jspath)[0]
def random_int(scope):
"""
@ -157,9 +162,10 @@ def get_time(time_type, layout, unit="0,0,0,0,0"):
return ti
def replace_random(value):
def replace_random(value,res=None):
"""
调用定义方法替换字符串
:param res:
:param value:
:return:
"""
@ -169,55 +175,69 @@ def replace_random(value):
float_list = re.findall(r'\$RandomFloat\(([0-9]*,[0-9]*,[0-9]*)\)\$', value)
time_list = re.findall(r"\$GetTime\(time_type=(.*?),layout=(.*?),unit=([0-9],[0-9],[0-9],[0-9],[0-9])\)\$", value)
choice_list = re.findall(r"\$Choice\((.*?)\)\$", value)
sqljson_list = re.findall(r"\$json\((.*?)\)\$", value)
if len(sqljson_list):
for i in sqljson_list:
pattern = re.compile(r'\$json\(' + i.replace('$', "\$").replace('[', '\[') + r'\)\$')
key = str(sql_json(i, res))
value = re.sub(pattern, key, value, count=1)
value = replace_random(value)
if len(int_list):
# 获取整型数据替换
for i in int_list:
pattern = re.compile(r'\$RandomInt\(' + i + r'\)\$')
key = str(random_int(i))
value = re.sub(pattern, key, value, count=1)
value = replace_random(value)
if not len(sqljson_list):
# 获取整型数据替换
for i in int_list:
pattern = re.compile(r'\$RandomInt\(' + i + r'\)\$')
key = str(random_int(i))
value = re.sub(pattern, key, value, count=1)
value = replace_random(value)
elif len(posint_list):
# 获取整型数据替换
for i in posint_list:
pattern = re.compile(r'\$RandomPosInt\(' + i + r'\)\$')
key = str(random_int(i))
value = re.sub(pattern, key, value, count=1)
value = replace_random(value)
if not len(sqljson_list):
# 获取整型数据替换
for i in posint_list:
pattern = re.compile(r'\$RandomPosInt\(' + i + r'\)\$')
key = str(random_int(i))
value = re.sub(pattern, key, value, count=1)
value = replace_random(value)
elif len(string_list):
# 获取字符串数据替换
for j in string_list:
pattern = re.compile(r'\$RandomString\(' + j + r'\)\$')
key = str(random_string(j))
value = re.sub(pattern, key, value, count=1)
value = replace_random(value)
if not len(sqljson_list):
# 获取字符串数据替换
for j in string_list:
pattern = re.compile(r'\$RandomString\(' + j + r'\)\$')
key = str(random_string(j))
value = re.sub(pattern, key, value, count=1)
value = replace_random(value)
elif len(float_list):
# 获取浮点数数据替换
for n in float_list:
pattern = re.compile(r'\$RandomFloat\(' + n + r'\)\$')
key = str(random_float(n))
value = re.sub(pattern, key, value, count=1)
value = replace_random(value)
if not len(sqljson_list):
# 获取浮点数数据替换
for n in float_list:
pattern = re.compile(r'\$RandomFloat\(' + n + r'\)\$')
key = str(random_float(n))
value = re.sub(pattern, key, value, count=1)
value = replace_random(value)
elif len(time_list):
# 获取时间替换
for m in time_list:
if len(m[0]) and len(m[1]):
pattern = re.compile(r'\$GetTime\(time_type=' + m[0] + ',layout=' + m[1] + ',unit=' + m[2] + r'\)\$')
key = str(get_time(m[0], m[1], m[2]))
value = re.sub(pattern, key, value, count=1)
else:
print("$GetTime$参数错误time_type, layout为必填")
value = replace_random(value)
if not len(sqljson_list):
# 获取时间替换
for m in time_list:
if len(m[0]) and len(m[1]):
pattern = re.compile(r'\$GetTime\(time_type=' + m[0] + ',layout=' + m[1] + ',unit=' + m[2] + r'\)\$')
key = str(get_time(m[0], m[1], m[2]))
value = re.sub(pattern, key, value, count=1)
else:
print("$GetTime$参数错误time_type, layout为必填")
value = replace_random(value)
elif len(choice_list):
# 调用choice方法
for i in choice_list:
pattern = re.compile(r'\$Choice\(' + i + r'\)\$')
key = str(choice_data(i))
value = re.sub(pattern, key, value, count=1)
value = replace_random(value)
if not len(sqljson_list):
# 调用choice方法
for i in choice_list:
pattern = re.compile(r'\$Choice\(' + i + r'\)\$')
key = str(choice_data(i))
value = re.sub(pattern, key, value, count=1)
value = replace_random(value)
else:
pass
@ -225,20 +245,48 @@ def replace_random(value):
if __name__ == '__main__':
from scripts.readYamlFile import ini_yaml
import json
int_num = "$RandomPosInt(1,333)$"
str_num = '$RandomString($RandomPosInt(2,23)$)$'
float_num = '$RandomFloat($RandomPosInt(2,13)$,$RandomPosInt(2,13)$,$RandomPosInt(2,13)$)$'
time_num = '$GetTime(time_type=else,layout=%Y-%m-%d %H:%M:%S,unit=0,0,0,0,0)$'
choice_num = '$Choice($RandomPosInt(2,13)$,$RandomPosInt(2,13)$)$'
a = json.dumps(ini_yaml("homePageData.yml")["companyAlarm"])
jsons = """
select count(*) '$json($.data.alarm[2].grade)$' from (
select
dr.qr_code,
alarm.device_id,
alarm.grade,
alarm.create_time,
alarm.handler_status
from alarm
inner join (
select device.id,
device.qr_code,
device.region_id,
region.enterprise_id,
device.status,
device.category_id
from device
inner join region
on device.region_id = region.id
and region.enterprise_id = 88
) as dr
on dr.qr_code = alarm.device_id and alarm.create_time >= '$GetTime(time_type=else,layout=%Y-%m-%d 00:00:00,unit=0,0,0,0,0)$'
) as ad where grade=1
"""
# a = json.dumps(ini_yaml("homePageData.yml")["companyAlarm"])
# print(type(ini_yaml("家庭详情.yml")[0]["data"]))
# print(replace_random(int_num))
# print(replace_random(str_num))
# print(replace_random(float_num))
print(replace_random(time_num))
t = "$GetTime(time_type=past,layout=%Y-%m-%d %H:%M:%S,unit=0,0,0,5,0)$"
# print(replace_random(time_num))
res1 = {'code': 0, 'msg': 'ok', 'data': {'alarm': [{'grade': 1, 'number': 3}, {'grade': 0, 'number': 0}, {'grade': 2, 'number': 0}], 'designate': [{'status': 2, 'number': 0}, {'status': 1, 'number': 0}]}}
# print(replace_random(jsons,res=res1))
t = "$GetTime(time_type=past,layout=%Y-%m-%d 00:00:00,unit=0,0,0,1,4)$"
# print(replace_random(choice_num))
print(replace_random(t))
# pattern = re.compile(r'\$json\(' + '$.data.alarm[1].number'.replace('$',"\$").replace('[','\[') + r'\)\$')
# # key = str(sql_json(i))
# key = "123"
# value = re.sub(pattern, "*", jsons, count=1)
# print(value)