def create_new_user(regname, pwd='123456'): handle_mysql = HandleMysql() send_request = HttpRequests() do_config = HandleConfig(CONFIGS_FILE_TESTCASE1) url = do_config.get_value('requests', 'url_head') + '/member/register' print(url) sql = "select Id from member where MobilePhone = %s;" while True: mobilephone = handle_mysql.create_not_existed_mobile() print(mobilephone) print(pwd) print(regname) data = {"mobilephone": mobilephone, "pwd": pwd, "regname": regname} #new_data = json.dumps(data) res = send_request.to_request(method="post", url=url, data=data) print(res.text) result = handle_mysql.run_mysql(sql, (mobilephone, )) print('ddddddd{}'.format(result)) #result为空,说明注册没成功,则执行for循环下一次,直到成功 if result: user_id = result['Id'] break user_dict = { regname: { "Id": user_id, "regname": regname, "mobilephone": mobilephone, "pwd": pwd } } return user_dict handle_mysql.close()
def create_new_user(regname, pwd='123456'): '''创建一个账户''' handle_mysql = HandleMysql() send_request = HttpRequest() url = do_config.get_value('api', 'prefix_url') + '/member/register' sql = 'select id from member where MobilePhone=%s;' while True: mobilephone = handle_mysql.create_not_existed_mobile() data = {'mobilephone':mobilephone, 'pwd':pwd, 'regname':regname} send_request.to_request(method='post', url=url, data=data) result = handle_mysql.to_run(sql=sql, args=(mobilephone,)) if result: user_id = result['id'] break user_dict = { regname: { 'Id': user_id, 'regname': regname, 'mobilephone': mobilephone, 'pwd': pwd } } handle_mysql.close() send_request.close() return user_dict
def to_param(cls, data): # 不存在的手机号替换 if re.search(cls.not_existed_tel_pattern, data): do_mysql = HandleMysql() data = re.sub(cls.not_existed_tel_pattern, do_mysql.create_not_existed_mobile(), data) do_mysql.close() # 管理员手机号替换 if re.search(cls.admin_user_tel_pattern, data): # do_user_account = HandleYaml(USER_ACCOUNTS_FILE_PATH) invest_user_tel = cls.do_user_account.read('admin', 'mobile_phone') data = re.sub(cls.admin_user_tel_pattern, invest_user_tel, data) # 投资人密码替换 if re.search(cls.admin_user_pwd_pattern, data): invest_user_pwd = cls.do_user_account.read('invest', 'pwd') data = re.sub(cls.admin_user_pwd_pattern, invest_user_pwd, data) #替换companyId if re.search(cls.admin_companyId_pattern, data): # invest_companyId = cls.do_user_account.read('invest', 'companyId') # data = re.sub(cls.invest_companyId_pattern, invest_companyId, data) invest_companyId = getattr(cls, 'companyId') data = re.sub(cls.admin_companyId_pattern, str(invest_companyId), data) return data
def create_new_user(regname, pwd='123456'): ''' 新建用户的封装类 ''' do_config = HandleConfig(CONFIG_REQUEST) do_request = HandleRequest() do_mysql = HandleMysql() register_url = do_config.get_value('url', 'register_url') sql = "SELECT Id FROM member WHERE MobilePhone = %s" while True: mobile = do_mysql.create_not_existed_mobile() data = {"regname": regname, "mobilephone": mobile, "pwd": pwd} do_request.send_request(url=register_url, data=data, method='post') result = do_mysql.do_execute(sql, args=(mobile, )) if result: user_id = result['Id'] break user_dict = { regname: { "user_id": user_id, "mobile": mobile, "pwd": pwd, "regname": regname } } do_mysql.close() do_request.close_request() return user_dict
class RegisterAccount: sql = "SELECT Id,Pwd,MobilePhone FROM member WHERE MobilePhone = %s" data = '{"mobilephone":"${not_existed_tel}","pwd":"123456","regname":"jason_loan"}' url = 'http://tj.lemonban.com/futureloan/mvc/api/member/register' def __init__(self): self.handle_mysql = HandleMysql() self.do_config = HandleConfig(CONFIGS_FILE_TESTCASE2) def register(self): not_existed_mobile = self.handle_mysql.create_not_existed_mobile() new_data = re.sub(r'\$\{not_existed_tel\}', not_existed_mobile, self.data) do_http_requests.to_request('post', url=self.url, data=new_data) sql_result = self.handle_mysql.run_mysql(self.sql, args=(not_existed_mobile, )) return sql_result def close(self): self.handle_mysql.close() def write_data(self): content = self.register() datas = {"account": content} self.do_config.write_config(datas, CONFIGS_FILE_TESTCASE2)
def to_param(cls, data): # 未注册手机号的参数化 if re.search(cls.not_existed_tel_pattern, data): do_mysql = HandleMysql() data = re.sub(cls.not_existed_tel_pattern, do_mysql.create_not_existed_mobile(), data) do_mysql.close() # 不存在的用户id替换 if re.search(cls.not_existed_id_pattern, data): do_mysql = HandleMysql() sql = "SELECT id FROM member ORDER BY id DESC limit 0, 1;" not_existed_id = do_mysql.run(sql).get('id') + 1 # 获取最 大的id加1 data = re.sub(cls.not_existed_id_pattern, str(not_existed_id), data) do_mysql.close() # 注册用户手机号的参数化 if re.search(cls.invest_user_tel_pattern, data): invest_user_tel = cls.do_user_account.read('invest', 'mobile_phone') data = re.sub(cls.invest_user_tel_pattern, invest_user_tel, data) # 注册用户密码的参数化 if re.search(cls.invest_user_pwd_pattern, data): invest_user_pwd = cls.do_user_account.read('invest', 'pwd') data = re.sub(cls.invest_user_tel_pattern, invest_user_pwd, data) # 参数化投资人的id if re.search(cls.invest_user_id_pattern, data): invest_user_id = cls.do_user_account.read('invest', 'id') data = re.sub(cls.invest_user_id_pattern, str(invest_user_id), data) # loan_id 替换 if re.search(cls.loan_id_pattern, data): loan_id = getattr(cls, 'loan_id') data = re.sub(cls.loan_id_pattern, str(loan_id), data) return data
def not_existed_tel_repalce(cls, data): """ data 是待替换的原始字符串 用if先判断,可以节省资源 """ if re.search(cls.not_existed_tel_pattern, data): handle_mysql = HandleMysql() not_existed_tel = handle_mysql.create_not_existed_mobile() data = re.sub(cls.not_existed_tel_pattern, not_existed_tel, data) return data handle_mysql.close()
def register_not_existed_replace(cls, data): ''' 不存在手机号进行参数化 ''' if re.search(cls.register__unregistered_mobile, data): do_mysql = HandleMysql() data = re.sub(cls.register__unregistered_mobile, do_mysql.create_not_existed_mobile(), data) do_mysql.close() return data
def not_existed_tel_replace(cls, data): ''' :param data:待替换的原始字符串 :return: ''' handle_mysql = HandleMysql() if re.search(cls.not_existed_tel_parttern, data): not_exist_tel = handle_mysql.create_not_existed_mobile() data = re.sub(cls.not_existed_tel_parttern, not_exist_tel, data) handle_mysql.close() return data
def create_new_user(reg_name, pwd="aaa123", user_type=1): """ 创建一个用户 :param reg_name: 用户名 :param pwd: 密码 :param user_type: 用户类型 :return: 存储一个用户信息, 嵌套字典的字典(以用户名为key, 以用户信息所在字典为value) """ # 建立连接 do_mysql = HandleMysql() do_request = HandleRequest() # 添加公共请求头 do_request.add_headers(do_yaml.read('api', 'version')) url = do_yaml.read('api', 'prefix') + '/member/register' sql = do_yaml.read('mysql', 'select_userid_sql') while True: mobile_phone = do_mysql.create_not_existed_mobile() data = { "mobile_phone": mobile_phone, "pwd": pwd, "reg_name": reg_name, "type": user_type } # 先注册接口发起请求 do_request.send(url, data=data) # 查询数据库, 获取用户id result = do_mysql.run(sql=sql, args=(mobile_phone, )) if result: user_id = result["id"] break # 构造用户信息字典 user_dict = { reg_name: { "id": user_id, "reg_name": reg_name, "mobile_phone": mobile_phone, "pwd": pwd } } # 关闭连接 do_mysql.close() do_request.close() return user_dict
def replace_phone(self): # 创建正则表达式,一定要加r,有特殊字符需要在字符串前方加\ # match方法,传入正则表达式,待查询字符串 # match只能从头开始匹配,匹配不到返回None # 匹配上后会返回match对象,用group获取结果 try: # 替换生成的手机号 do_mysql = HandleMysql() real_existed_phone = do_mysql.create_not_existed_mobile() data_str = re.sub(r"{not_existed_phone}", real_existed_phone, self.one_str) do_mysql.close() return data_str except AttributeError as e: data_str = self.one_str return data_str
def user_init(): # 获取请求头 headers = do_yaml.read_yaml('api', 'version') # 创建请求对象 do_request = HandleRequests() # 添加公共请求头 do_request.add_headers(headers) # url url = 'http://api.lemonban.com/futureloan/member/register' # 用户数据 user_datas = [{ 'mobile_phone': '', 'pwd': '12345678', 'type': 0, 'reg_name': '管理员' }, { 'mobile_phone': '', 'pwd': '12345678', 'type': 1, 'reg_name': '借款人' }, { 'mobile_phone': '', 'pwd': '12345678', 'type': 1, 'reg_name': '投资人' }] write_yaml = HandleYaml(USER_FILE_DIR) # 连接数据库 do_mysql = HandleMysql() user_info = {} # 生成手机号并注册 for user in user_datas: user['mobile_phone'] = do_mysql.create_not_existed_mobile() # 请求注册接口,返回数据转换成字典并存入字典 res = do_request.send(url, data=user).json() # 获取用户id、密码等信息(区域名为昵称,选项名为data里的字段名以及密码) user_info['{}'.format(user['reg_name'])] = res['data'] user_info['{}'.format(user['reg_name'])]['pwd'] = user['pwd'] # 写入用户文件 write_yaml.write_yaml(USER_FILE_DIR, user_info) # 断开数据库 do_mysql.close() # 断开请求 do_request.close()
def create_new_user(reg_name, pwd="12345678", user_type=1): ''' 创建用户 reg_name:用户昵称 pwd:密码 user_type:用户类型 返回一个用户信息,嵌套字典的字典,用户昵称为key,用户信息为value ''' # 建立连接 后面要关闭连接 do_mysql = HandleMysql() do_request = HandleRequest() # 添加公共请求头 do_request.add_headers(do_yaml.read('api', 'version')) url = do_yaml.read('api', 'prefix') + '/member/register' sql = do_yaml.read( 'api', 'select_user_id') # 虽然响应报文中会返回用户ID,但是从数据库中取出,进一步数据校验了注册接口 while True: # 因为存在数据库中找不到手机号的可能,所以直接死循环创建一个未注册的手机号确保万无一失 mobile_phone = do_mysql.create_not_existed_mobile() data = { "mobile_phone": mobile_phone, "pwd": pwd, "reg_name": reg_name, "type": user_type } # 向注册接口发起请求 do_request.send(url, data=data) # 查询数据库,获取用户id result = do_mysql.run(sql=sql, args=(mobile_phone, )) if result: # 如果查询结果不为空,则获取id user_id = result['id'] break # 构造用户信息字典 user_dict = { reg_name: { "id": user_id, "mobile_phone": mobile_phone, "pwd": pwd, "reg_name": reg_name, } } # 关闭连接 do_request.close() do_mysql.close() return user_dict
def not_existed_replace(cls, data): do_mysql = HandleMysql() # 不存在手机号的参数化 if cls.not_existed_tel_pattern in data: # 使用成员运算 data = re.sub(cls.not_existed_tel_pattern, do_mysql.create_not_existed_mobile(), data) # 不存在的用户id替换 if re.search(cls.not_existed_id_pattern, data): sql = "SELECT id FROM member ORDER BY id DESC limit 0, 1;" not_existed_id = do_mysql.run(sql).get('id') + 1 # 获取最 大的id加1 data = re.sub(cls.not_existed_id_pattern, str(not_existed_id), data) # loan_id 替换 if re.search(cls.loan_id_pattern, data): loan_id = getattr(cls, 'loan_id') data = re.sub(cls.loan_id_pattern, str(loan_id), data) do_mysql.close() return data
def user_init(cls): # 获取请求头 headers = {"X-Lemonban-Media-Type": "lemonban.v2"} # 创建请求对象 do_request = HandleRequest() # 添加请求头 do_request.add_headers(headers) # url url = 'http://api.lemonban.com/futureloan/member/register' # 创建初始三个用户,如果已经创建过则不用再创 user = [{ 'mobile_phone': '', 'pwd': '12345678', 'type': 0, 'reg_name': '管理员' }, { 'mobile_phone': '', 'pwd': '12345678', 'type': 1, 'reg_name': '借款人' }, { 'mobile_phone': '', 'pwd': '12345678', 'type': 1, 'reg_name': '投资人' }] do_yaml = HandleYaml(USER_ACCOUNTS_FILE_DIR) number = HandleMysql() new_user = {} for i in user: i["mobile_phone"] = number.create_not_existed_mobile() # 发起请求 res = do_request.send(url=url, data=i).json() # 获取用户id new_user['{}'.format(i['reg_name'])] = res['data'] new_user['{}'.format(i['reg_name'])]['pwd'] = i['pwd'] # 写入配置文件 do_yaml.write_yaml(USER_ACCOUNTS_FILE_DIR, new_user) # 关闭请求 do_request.close() # 关闭数据库连接 number.close()
def create_user(reg_name, password=12345678, type=1): """ 创建用户并将用户信息,添加至全局数据池中 :param reg_name: 用户昵称 :param password: 用户密码,默认为12345678 :param type: 用户类型,默认为1,普通用户 :return: """ do_request = HandleRequest() do_mysql = HandleMysql() # 获取未注册手机号 mobile_phone = do_mysql.create_not_existed_mobile() # 构造请求参数 param = { "mobile_phone": mobile_phone, "pwd": password, "reg_name": reg_name, "type": type } # 构造请求url路径 url = "http://api.lemonban.com/futureloan/member/register" # 获取api头信息 do_request.add_headers(do_yaml.get_data("api", "api_version")) # 进行注册 res = do_request.send("POST", url, json=param) # sql = do_yaml.get_data("mysql", "select_user_sql") # do_mysql.get_values(sql, ) # 从响应报文中获取用户id user_id = res.json()["data"]["id"] # 关闭相关连接 do_request.close() do_mysql.close() # 将用户信息添加至全局数据池中 setattr(GlobalData, "${" + reg_name + "_user_tel}", mobile_phone) setattr(GlobalData, "${" + reg_name + "_user_pwd}", password) setattr(GlobalData, "${" + reg_name + "_user_id}", user_id)
def to_param(cls, data): # 不存在的手机号替换 if re.search(cls.not_existed_tel_pattern, data): do_mysql = HandleMysql() data = re.sub(cls.not_existed_tel_pattern, do_mysql.create_not_existed_mobile(), data) do_mysql.close() # 投资人手机号替换 if re.search(cls.invest_user_tel_pattern, data): # do_user_account = HandleYaml(USER_ACCOUNTS_FILE_PATH) invest_user_tel = cls.do_user_account.read('invest', 'mobile_phone') data = re.sub(cls.invest_user_tel_pattern, invest_user_tel, data) # 投资人密码替换 if re.search(cls.invest_user_pwd_pattern, data): invest_user_pwd = cls.do_user_account.read('invest', 'pwd') data = re.sub(cls.invest_user_pwd_pattern, invest_user_pwd, data) # 投资人id替换 if re.search(cls.invest_user_id_pattern, data): invest_user_id = cls.do_user_account.read('invest', 'id') # 投资人id为int类型, 要转化为str类型 data = re.sub(cls.invest_user_id_pattern, str(invest_user_id), data) # 不存在的用户id替换 if re.search(cls.not_existed_id_pattern, data): do_mysql = HandleMysql() sql = "SELECT id FROM member ORDER BY id DESC LIMIT 0,1;" not_existed_id = do_mysql.run(sql).get('id') + 1 # 获取最大的用户id + 1 data = re.sub(cls.not_existed_id_pattern, str(not_existed_id), data) do_mysql.close() # 借款人id替换 if re.search(cls.borrow_user_id_pattern, data): borrow_user_id = cls.do_user_account.read("borrow", "id") # 获取已经注册的借款人用户id data = re.sub(cls.borrow_user_id_pattern, str(borrow_user_id), data) # 借款人手机号替换 if re.search(cls.borrow_user_tel_pattern, data): borrow_user_mobile = cls.do_user_account.read( "borrow", "mobile_phone") # 获取已经注册的借款人手机号 data = re.sub(cls.borrow_user_tel_pattern, borrow_user_mobile, data) # 借款人密码替换 if re.search(cls.borrow_user_pwd_pattern, data): borrow_user_pwd = cls.do_user_account.read("borrow", "pwd") # 获取已经注册的借款人密码 data = re.sub(cls.borrow_user_pwd_pattern, borrow_user_pwd, data) # load_id替换 if re.search(cls.loan_id_pattern, data): loan_id = getattr(cls, 'loan_id') data = re.sub(cls.loan_id_pattern, str(loan_id), data) return data
def to_param(cls, data): if re.search(cls.not_existed_tel_pattern, data): do_mysql = HandleMysql() data = re.sub(cls.not_existed_tel_pattern, do_mysql.create_not_existed_mobile(), data) do_mysql.close() if re.search(cls.existed_tel_pattern, data): # doyaml = HandleYaml(CONFIGS_USER_FILE_PATH) data = re.sub(cls.existed_tel_pattern, cls.doyaml.read("investor", "mobile_phone"), data) if re.search(cls.existed_tel_pattern_pwd, data): # doyaml = HandleYaml(CONFIGS_USER_FILE_PATH) data = re.sub(cls.existed_tel_pattern_pwd, cls.doyaml.read("investor", "pwd"), data) if re.search(cls.error_user_pwd, data): do_mysql = HandleMysql() data = re.sub(cls.error_user_pwd, do_mysql.create_not_existed_mobile(), data) do_mysql.close() if re.search(cls.invest_user_id, data): # doyaml = HandleYaml(CONFIGS_USER_FILE_PATH) data = re.sub(cls.invest_user_id, str(cls.doyaml.read("investor", "id")), data) if re.search(cls.invest_user_tel, data): # doyaml = HandleYaml(CONFIGS_USER_FILE_PATH) data = re.sub(cls.invest_user_tel, cls.doyaml.read("investor", "mobile_phone"), data) if re.search(cls.invest_user_pwd, data): # doyaml = HandleYaml(CONFIGS_USER_FILE_PATH) data = re.sub(cls.invest_user_pwd, cls.doyaml.read("investor", "pwd"), data) if re.search(cls.not_existed_id, data): do_mysql = HandleMysql() sql = "select max(id) from member;" max_id = do_mysql.run(sql) data = re.sub(cls.not_existed_id, str(max_id["max(id)"] + 1), data) do_mysql.close() if re.search(cls.borrow_user_id, data): # doyaml = HandleYaml(CONFIGS_USER_FILE_PATH) data = re.sub(cls.borrow_user_id, str(cls.doyaml.read("borrower", "id")), data) if re.search(cls.borrow_user_tel, data): # doyaml = HandleYaml(CONFIGS_USER_FILE_PATH) data = re.sub(cls.borrow_user_tel, cls.doyaml.read("borrower", "mobile_phone"), data) if re.search(cls.borrow_user_pwd, data): # doyaml = HandleYaml(CONFIGS_USER_FILE_PATH) data = re.sub(cls.borrow_user_pwd, cls.doyaml.read("borrower", "pwd"), data) if re.search(cls.admin_user_tel, data): data = re.sub(cls.admin_user_tel, cls.doyaml.read("admin", "mobile_phone"), data) if re.search(cls.admin_user_pwd, data): data = re.sub(cls.admin_user_pwd, cls.doyaml.read("admin", "pwd"), data) if re.search(cls.loan_id_pattern, data): # loan_id = getattr(Parameterize ,"loan_id") loan_id = getattr(cls, "loan_id") data = re.sub(cls.loan_id_pattern, str(loan_id), data) if re.search(cls.not_existed_loan_id, data): loan_id = getattr(cls, "loan_id") data = re.sub(cls.not_existed_loan_id, str(loan_id + 1), data) return data
from scripts.handle_mysql import HandleMysql from scripts.handle_yaml import do_yaml from scripts.handle_path import CONFIGS_USER_FILE_PATH # 创建mysql对象 do_mysql = HandleMysql() # 构造请求url url = do_yaml.read("api", "profix") + "/member/register" # 构造公共请求头 header = {"X-Lemonban-Media-Type": "lemonban.v2"} # 构造请求参数 data = { "mobile_phone": do_mysql.create_not_existed_mobile(), "pwd": "12345678", "type": 0, "reg_name": "admin" } data1 = { "mobile_phone": do_mysql.create_not_existed_mobile(), "pwd": "12345678", "type": 1, "reg_name": "borrower" } data2 = { "mobile_phone": do_mysql.create_not_existed_mobile(), "pwd": "12345678",
# 1.创建待替换的字符串 one_str = {"mobile_phone": "{not_existed_tel}", "pwd": "12345678", "type": 1, "reg_name": "keyou"} # 正则表达式相当于一个模子,可以拿这个模子去把符合这个模子的内容全部找出来 # 2.创建正则表达式 # re_str = r'\${not_existed_tel}' # re_str = r'{"mobile_phone":"\{not_existed_tel}' # 正则表达式中,一定要加r,如果有些字符有特殊含义,需要在前面加\ # match方法第一个参数为正则表达式,第二个参数为待查询字符串 # match方法只能从头开始匹配 # 如果匹配不上,会返回None,如果能匹配上会返回match对象 # 可以用mtch.group()获取匹配成功后的值 # re.match(re_str, one_str) # mtch = re.match(re_str, one_str) # search 不用从头开始匹配,只要能匹配上,就直接返回,如果能匹配上会返回match对象,如果匹配不上会返回None mtch = re.search(r'{not_existed_tel}', one_str) # sub 先找到,找到之后再替换 # 第一个参数为正则表达式字符串,第二个参数为新的值,第三个参数为待替换的字符串(原始字符串) # 如果能匹配上,会返回替换之后的值,一定为字符串类型,匹配不上会返回原始字符串 # re.sub(r'{not_existed_tel}', '18822223333', one_str) # split # findall # finditer # 在项目中 search 和sub会合在一起用 if re.search('{not_existed_tel}', one_str): re.sub(r'{not_existed_tel}', '18822223333', one_str) do_mysql = HandleMysql() real_existed_tel = do_mysql.create_not_existed_mobile() do_mysql.close()