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()
Example #2
0
 def admin_user_tel_replace(cls, data):
     admin_user_config = HandleConfig(CONFIGS_USER_ACCOUNTS)
     if re.search(cls.admin_user_tel_pattern, data):
         admin_user_tel = admin_user_config.get_value(
             'admin_user', 'mobilephone')
         data = re.sub(cls.admin_user_tel_pattern, admin_user_tel, data)
     return data
Example #3
0
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
Example #4
0
    def __init__(self):
        '''
            读取 /home/want/PycharmProjects/TestAPI/configs/log_config.conf
            目录下的配置文件
        '''
        do_config = HandleConfig(CONFIG_LOG)

        # 创建日志收集器对象
        self.log = logging.getLogger(do_config.get_value('log', 'log_name'))

        # 设置日志收集器对象收集等级
        self.log.setLevel(do_config.get_value('log', 'log_level'))

        # 创建控制台渠道
        console_handler = logging.StreamHandler()

        # 创建文件渠道
        file_handler = logging.FileHandler(os.path.join(scripts.path_constants.LOGS_PATH,
                                                        do_config.get_value('log', 'file_name')), encoding='utf-8')

        # 设置控制台渠道的等级
        console_handler.setLevel(do_config.get_value('log', 'console_level'))
        file_handler.setLevel(do_config.get_value('log', 'file_level'))

        # 设置输出格式
        console_formatter = logging.Formatter(do_config.get_value('log', 'console_formatter'))
        file_formatter = logging.Formatter(do_config.get_value('log', 'file_formatter'))

        # 绑定控制台渠道和输出格式
        console_handler.setFormatter(console_formatter)
        file_handler.setFormatter(file_formatter)

        # 给收集器添加handler
        self.log.addHandler(console_handler)
        self.log.addHandler(file_handler)
Example #5
0
class HandleLog:
    def __init__(self, file_path):
        '''
        初始化函数,封装日志相关操作
        '''
        # 创建该类实例对象时传入日志相关的配置文件路径
        self.handle_config = HandleConfig(file_path)
        # 1.定义日志收集器
        self.cases_logger = logging.getLogger(
            self.handle_config.get_value('handle_log',
                                         'log_name'))  # 会创建一个logger对象
        # 每次调用后清空已存在的handler
        self.cases_logger.handlers.clear()
        # 2.指定日志收集器日志等级,一共有6种等级
        # NOTSET(0)、DEBUG(10)、INFO(20)、WARNING(30)、ERROR(40)、CRITICAL(50)
        # 只能收集当前等级和当前等级以上的日志
        self.cases_logger.setLevel(
            self.handle_config.get_value('handle_log', 'cases_logger_level'))
        # cases_logger.setLevel('DEBUG')

        # 3.指定日志输出渠道
        # 输出到控制台
        console_handle = logging.StreamHandler()  # handler对象
        # 输出到文件
        file_handle = logging.FileHandler(os.path.join(
            LOGS_DIR,
            self.handle_config.get_value('handle_log', 'file_path_log')),
                                          encoding='utf-8')

        # 4.指定日志输出渠道的日志等级
        console_handle.setLevel(
            self.handle_config.get_value('handle_log', 'console_handle_level'))
        file_handle.setLevel(
            self.handle_config.get_value('handle_log', 'file_handle_level'))
        # 5.定义日志显示格式
        simple_formatter = logging.Formatter(
            self.handle_config.get_value('handle_log', 'simple_format'))
        detail_formatter = logging.Formatter(
            self.handle_config.get_value('handle_log', 'detail_format'))
        # 控制台显示简洁的日志
        console_handle.setFormatter(simple_formatter)
        # 日志文件显示详细日志
        file_handle.setFormatter(detail_formatter)
        # 6.经日志收集器与输出渠道进行对接
        self.cases_logger.addHandler(console_handle)
        self.cases_logger.addHandler(file_handle)

    def get_logger(self):
        '''
        :return:返回日志收集器对象
        '''
        return self.cases_logger
Example #6
0
 def borrow_user_id_replace(cls, data):
     borrow_user_config = HandleConfig(CONFIGS_USER_ACCOUNTS)
     if re.search(cls.borrow_user_id_pattern, data):
         borrow_user_id = borrow_user_config.get_value('borrow_user', 'id')
         data = re.sub(cls.borrow_user_id_pattern, borrow_user_id, data)
     return data
Example #7
0
 def admin_user_pwd_replace(cls, data):
     admin_pwd_config = HandleConfig(CONFIGS_USER_ACCOUNTS)
     if re.search(cls.admin_use_pwd_pattern, data):
         admin_user_pwd = admin_pwd_config.get_value('admin_user', 'pwd')
         data = re.sub(cls.admin_use_pwd_pattern, admin_user_pwd, data)
     return data
Example #8
0
 def invest_user_id_repalce(cls, data):
     id_config = HandleConfig(CONFIGS_USER_ACCOUNTS)
     if re.search(cls.invest_user_id_pattern, data):
         invest_id = id_config.get_value('invest_user', 'id')
         data = re.sub(cls.invest_user_id_pattern, invest_id, data)
     return data
Example #9
0
 def invest_user_pwd_replace(cls, data):
     pwd_config = HandleConfig(CONFIGS_USER_ACCOUNTS)
     if re.search(cls.invest_user_pwd_pattern, data):
         invest_pwd = pwd_config.get_value('invest_user', 'pwd')
         data = re.sub(cls.invest_user_pwd_pattern, invest_pwd, data)
     return data
Example #10
0
 def invest_user_tel_replace(cls, data):
     do_conig = HandleConfig(CONFIGS_USER_ACCOUNTS)
     if re.search(cls.invest_user_tel_pattern, data):
         invest_tel = do_conig.get_value('invest_user', 'mobilephone')
         data = re.sub(cls.invest_user_tel_pattern, invest_tel, data)
     return data
Example #11
0
        data = cls.not_existed_tel_repalce(data)
        return data

    @classmethod
    def add_paramaterization(cls, data):
        data = cls.admin_user_tel_replace(data)
        data = cls.admin_user_pwd_replace(data)
        data = cls.borrow_user_id_replace(data)
        return data

    @classmethod
    def invest_paramaterization(cls, data):
        data = cls.admin_user_tel_replace(data)
        data = cls.admin_user_pwd_replace(data)
        data = cls.borrow_user_id_replace(data)
        data = cls.invest_loan_id_replace(data)
        data = cls.invest_user_tel_replace(data)
        data = cls.invest_user_pwd_replace(data)
        data = cls.invest_user_id_repalce(data)
        return data


if __name__ == '__main__':
    context = HandleContext()
    data1 = '{"mobilephone":"${not_existed_tel}","pwd":"123456","regname":"jason"}'
    data2 = '{"mobilephone":"18934567","pwd":"123456","regname":"jason"}'
    print(context.not_existed_tel_repalce(data1))
    print(context.not_existed_tel_repalce(data2))
    o_conig = HandleConfig(CONFIGS_USER_ACCOUNTS)
    invest_tel = o_conig.get_value('invest_user', 'mobilephone')
    print(invest_tel)
Example #12
0
            else:
                # is_json 为False ,代表传入的参数为json格式,返回 session 的post方法
                return self.session.post(url, data=data)
        else:
            # 返回空内容
            return None
            print('暂不支持其他格式')

    # close_request 方法,用来关闭 Session 会话
    def close_request(self):
        self.session.close()


if __name__ == '__main__':
    do_config = HandleConfig(scripts.path_constants.CONFIG_REQUEST)
    mobile = do_config.get_value('user', 'mobile', type='int')
    pwd = do_config.get_value('user', 'password', type='int')

    login_url = do_config.get_value('url', 'login_url')
    login_data = {'mobilephone': mobile, 'pwd': pwd}
    recharge_url = do_config.get_value('url', 'recharge_url')
    recharge_data = {'mobilephone': mobile, 'amount': 100}
    do_handle_request = HandleRequest()
    login_req = do_handle_request.send_request(login_url,
                                               data=login_data,
                                               method='post')
    recharge_req = do_handle_request.send_request(recharge_url,
                                                  data=recharge_data,
                                                  method='post')

    print(login_req.json())
Example #13
0
class HandleMysql:
    '''
        PyMysql 的封装类
    '''
    def __init__(self):
        # 创建配置文件对象
        self.config = HandleConfig(CONFIG_MYSQL)
        # 读取配置文件的内容
        # self.config.read(CONFIG_MYSQL, encoding='utf-8')
        # 获取配置文件的内容
        section = 'mysql'
        # 创建数据库连接
        self.connect = pymysql.connect(
            host=self.config.get_value(section, 'mysql_host'),
            user=self.config.get_value(section, 'mysql_user'),
            password=self.config.get_value(section, 'mysql_password'),
            db=self.config.get_value(section, 'mysql_db'),
            port=self.config.get_int(section, 'mysql_port'),
            charset='utf8',
            cursorclass=pymysql.cursors.DictCursor)
        # 创建游标对象
        self.cursor = self.connect.cursor()

    # sql语句执行方法
    def do_execute(self, sql, args=None, is_one_data=True):
        '''
            @sql : 传入要执行的sql语句
            @args: sql语句有占位符需传入其他参数
            @is_one_data: sql语句查询的是否是单条数据,默认为True
        '''
        # 执行传入的sql语句,和可变参数
        self.cursor.execute(sql, args=args)
        # 提交要执行的sql语句对数据库进行操作
        self.connect.commit()
        # 如果is_one_data为True,则代表该sql语句会返回一条数据
        if is_one_data:
            # 则 fetchone 返回执行sql语句后的数据
            return self.cursor.fetchone()
        # 如果is_one_data为True,则代表该sql语句返回的数据不止为一条
        else:
            # 则 fetchall 返回执行sql语句后的所有数据
            return self.cursor.fetchall()

    @staticmethod
    def create_mobile():
        '''
            创建手机号码的静态类
        '''
        # 定义一些3位数开头的手机号码
        head_number = [
            '130', '131', '132', '138', '139', '150', '159', '176', '185'
        ]
        # 调用 random.choice() 方法在 head_number 列表中随机选择一个
        head_mobile_number = random.choice(head_number)
        # 调用 random.sample() 方法在0-9中随机出8个数字
        other_mobile_number = random.sample('0123456789', 8)
        # 将选出的号码开头和随机出的8个数字进行组合,生成一个手机号码
        mobile_number = head_mobile_number + ''.join(other_mobile_number)
        # 并将该手机号码返回
        return mobile_number

    def is_existed_mobile(self, mobile):
        '''
            判断手机号码是否存在的方法
        '''
        # 定义一个查询号码的sql语句
        sql = "SELECT * FROM member WHERE MobilePhone = %s;"
        # 调用 do_execute() 方法来执行该sql语句,获取查询的结果
        # 如果返回的是个字典,就代表已经注册,返回True
        if self.do_execute(sql, args=(mobile, )):
            return True
        # 返回None就代表还没有注册,返回False
        else:
            return False

    def create_not_existed_mobile(self):
        '''
            创建一个不存在的手机号码
        '''
        # 进行一个 while 循环,来生成手机号
        while True:
            # mobile 接收调用 create_mobile() 方法返回的手机号码
            mobile = self.create_mobile()
            # 调用 is_existed_mobile(mobile) 来判断创建的手机号是否存在
            if not self.is_existed_mobile(mobile):
                # 如果不存在,将 break 跳出循环
                break
        # 并将该手机号码插入到数据库中
        # 准备好插入的sql语句
        '''
            sql = "INSERT INTO `future`.`member`(`Id`," \
              " `RegName`," \
              " `Pwd`," \
              " `MobilePhone`," \
              " `Type`," \
              " `LeaveAmount`) VALUES (1," \
              " 'want'," \
              " 'want'," \
              " %s," \
              " 1," \
              " 0)"
        '''
        return mobile

    # 关闭方法
    def close(self):
        # 先关闭游标对象
        self.cursor.close()
        # 接着关闭与数据库的连接
        self.connect.close()