def __init__(self, case_id):
     self.case_id = case_id
     self.use_excel = UseExcel()
     self.run_method = SendGetPost()
     self.data = GetData()
     self.tool = CommonTool()
     self.use_mysql = UseMySql()
 def __init__(self):
     self.run_method = SendGetPost()
     self.data = GetData()
     self.tool = CommonTool()
     self.send_mail = SendEmail()
     self.use_mysql = UseMySql()
     self.excel = UseExcel()
 def __init__(self):
     self.use_excel = UseExcel()
class GetData:
    def __init__(self):
        self.use_excel = UseExcel()

    def get_case_rows(self):
        """获取excel的行数,得到的就是case的个数"""
        return self.use_excel.get_nrows()

    def get_is_run(self, row):
        """获取是否执行"""
        flag = None
        column = int(data_config.get_run())
        run_model = self.use_excel.get_cell_value(row, column)
        if run_model == "yes":
            flag = True
        else:
            flag = False
        return flag

    def get_request_method(self, row):
        """获取请求方式"""
        column = int(data_config.get_request_way())
        request_method = self.use_excel.get_cell_value(row, column)
        return request_method

    def get_request_url(self, row):
        """获取url"""
        column = int(data_config.get_url())
        url = self.use_excel.get_cell_value(row, column)
        return url

    def get_request_header(self, row):
        """获取请求头 header"""
        column = int(data_config.get_request_header())
        data = self.use_excel.get_cell_value(row, column)
        if data == " ":
            return None
        else:
            return data

    def get_header_value(self, row):
        """获取头关键字 拿到data数据"""
        use_json = UseJson('../data_config/request_header.json')
        request_header = use_json.get_data(self.get_request_header(row))
        return request_header

    def get_request_data(self, row):
        """获取请求数据"""
        column = int(data_config.get_request_data())
        data = self.use_excel.get_cell_value(row, column)
        if data == "":
            return None
        return data

    def get_request_excel_data(self, row):
        """获取 excel 中请求的数据"""
        colunm = int(data_config.get_request_data())
        excel_data = self.use_excel.get_cell_value(row, colunm)
        return excel_data

    def get_request_excel_data_wrapper(self, row, fun):
        """获取 excel 中请求的数据"""
        request_excel_data = self.get_request_excel_data(row)
        return fun(request_excel_data)

    def get_data_values(self, row):
        """通过获取请求关键字拿到data数据"""
        use_json = UseJson('../data_config/request_data.json')
        request_data = use_json.get_data(self.get_request_data(row))
        return request_data

    def get_data_values_wrapper(self, row, func):
        data_values = self.get_data_values(row)
        return func(data_values)

    def get_expect_data(self, row):
        """获取预期结果"""
        column = int(data_config.get_expect())
        expect = self.use_excel.get_cell_value(row, column)
        return expect

    def write_result(self, row, value):
        """写入数据"""
        column = int(data_config.get_result())
        self.use_excel.write_value(row, column, value)

    def get_depend_key(self, row):
        """获取依赖数据的 key"""
        column = int(data_config.get_data_depend())
        depend_key = self.use_excel.get_cell_value(row, column)
        if depend_key == "":
            return None
        else:
            return depend_key

    def is_depend(self, row):
        """判断是否有 case依赖"""
        column = int(data_config.get_case_depend())
        depend_case_id = self.use_excel.get_cell_value(row, column)
        if depend_case_id == "":
            return None
        else:
            return depend_case_id

    def get_depend_field(self, row):
        """获取请求依赖 字段"""
        column = int(data_config.get_field_depend())
        data = self.use_excel.get_cell_value(row, column)
        if data == "":
            return None
        else:
            return data
class DependentData:
    def __init__(self, case_id):
        self.case_id = case_id
        self.use_excel = UseExcel()
        self.run_method = SendGetPost()
        self.data = GetData()
        self.tool = CommonTool()

    def get_case_row_data(self):
        """通过 case_id 去获取依赖 case_id 的整行数据"""
        row_data = self.use_excel.get_row_data(self.case_id)
        return row_data

    def run_dependent(self):
        """执行依赖测试,获取结果"""
        row_num = self.use_excel.get_row_num(self.case_id)
        request_data = self.data.get_request_excel_data(
            row_num)  # 直接读取 excel 中的 data
        if request_data.find("${mobile}") != -1:
            request_data = request_data.replace("${mobile}",
                                                self.tool.get_mobile())

        header = self.data.get_request_header(row_num)
        header = eval(header)
        method = self.data.get_request_method(row_num)
        url = self.data.get_request_url(row_num)
        res = self.run_method.run_main(method,
                                       url,
                                       data=request_data,
                                       header=header,
                                       params=request_data)
        return res

    def get_value_for_key(self, row):
        """获取依赖字段的响应数据,通过执行依赖测试case来获取响应数据,响应中某个字段数据作为依赖key的value"""
        depend_data = self.data.get_depend_key(row)  # 获取依赖的返回数据 key
        response_data = self.run_dependent()  # 执行依赖 case 返回结果
        get_depend_data = [
            match.value for match in parse(depend_data).find(response_data)
        ][0]
        if depend_data == "accessToken":
            # get_depend_data = hashlib.md5(get_depend_data.encode(encoding='UTF-8')).hexdigest()  # md5加密
            get_depend_data = base64.b64encode(
                get_depend_data.encode('utf-8')).decode("utf-8")  # base64加密
            self.use_file(get_depend_data)
        return get_depend_data

    def md5(self):
        str = '387ff00e-1a54-49b3-87ca-a95ad3c9e08a'
        m = hashlib.md5()
        b = str.encode(encoding='utf-8')
        m.update(b)
        str_md5 = m.hexdigest()
        return str_md5

    def base64(self):
        a = "39397778-f129-41f4-ba28-68af53f5728c"
        jm = base64.b64encode(a.encode('utf-8'))
        b = str(jm)
        base = b[2:-1]
        return base

    def use_file(self, header_cookie):
        config_header = "../data_config/config_header.json"
        with open(config_header, "w") as ch:
            ch.write('{{"Cookie": "SESSION={}"}}'.format(header_cookie))
class DependentData:

    def __init__(self, case_id):
        self.case_id = case_id
        self.use_excel = UseExcel()
        self.run_method = SendGetPost()
        self.data = GetData()
        self.tool = CommonTool()
        self.use_mysql = UseMySql()


    def get_case_row_data(self):
        """通过 case_id 去获取依赖 case_id 的整行数据"""
        row_data = self.use_excel.get_row_data(self.case_id)
        return row_data

    def run_dependent(self):
        """执行依赖测试,获取结果"""
        row_num = self.use_excel.get_row_num(self.case_id)
        # request_data = self.data.get_data_values(row_num)
        request_data = self.data.get_request_excel_data(row_num)  # 直接读取 excel 中的 data
        if request_data.find("${mobile}") != -1:
            # request_data = request_data.replace("${mobile}", '18888881937')
            request_data = request_data.replace("${mobile}", self.tool.get_mobile())
        elif request_data.find("${borrowerCode}") != -1:
            use_json = UseJson("../data_config/borrowerCode.json")
            borrowerCode = use_json.get_data("borrowerCode")
            request_data = request_data.replace("${borrowerCode}", borrowerCode)
        elif request_data.find("${write_borrowerCode}") != -1:
            self.use_mysql.select_borrowercode()
            use_json = UseJson("../data_config/borrowerCode.json")
            borrowerCode = use_json.get_data("borrowerCode")
            request_data = request_data.replace("${write_borrowerCode}", borrowerCode)

        # request_data = eval(request_data)
        header = self.data.get_request_header(row_num)
        if header == 'get_cookie,31G':
            use_json = UseJson("../data_config/config_header.json")
            cookie_value = use_json.get_data("Cookie")
            header = {"Content-Type": "application/json", "Product": "31G", "Cookie": cookie_value}
        else:
            header = eval(header)
        method = self.data.get_request_method(row_num)
        url = self.data.get_request_url(row_num)
        res = self.run_method.run_main(method, url, data=request_data, header=header, params=request_data)
        return res

    def get_value_for_key(self, row):
        """获取依赖字段的响应数据,通过执行依赖测试case来获取响应数据,响应中某个字段数据作为依赖key的value"""
        depend_data = self.data.get_depend_key(row)  # 获取依赖的返回数据 key
        response_data = self.run_dependent()  # 执行依赖 case 返回结果
        get_depend_data = [match.value for match in parse(depend_data).find(response_data)][0]
        if depend_data == "accessToken":
            # get_depend_data = hashlib.md5(get_depend_data.encode(encoding='UTF-8')).hexdigest()  # md5加密
            get_depend_data = base64.b64encode(get_depend_data.encode('utf-8')).decode("utf-8")  # base64加密
            self.use_config_header(get_depend_data)
        elif depend_data == "code":
            self.use_credit_apply_code(get_depend_data)
        elif depend_data == "sessionId":
            get_depend_data = base64.b64encode(get_depend_data.encode('utf-8')).decode("utf-8")  # base64加密
            self.use_config_header(get_depend_data)

        return get_depend_data

    def md5(self):
        str = '387ff00e-1a54-49b3-87ca-a95ad3c9e08a'
        m = hashlib.md5()
        b = str.encode(encoding='utf-8')
        m.update(b)
        str_md5 = m.hexdigest()
        return str_md5

    def base64(self):
        a = "39397778-f129-41f4-ba28-68af53f5728c"
        jm = base64.b64encode(a.encode('utf-8'))
        b = str(jm)
        base = b[2:-1]
        return base

    def use_config_header(self, header_cookie):
        """将header_cookie写入config_header.json中"""
        config_header = "../data_config/config_header.json"
        with open(config_header, "w") as ch:
            ch.write('{{"Cookie": "SESSION={}"}}'.format(header_cookie))
            # ch.write('{"Cookie":' '"%s"}' % "aa")

    def use_credit_apply_code(self, code):
        credit_apply_code = "../data_config/credit_apply_code.json"
        with open(credit_apply_code, "w") as json:
            json.write('{{"code": "{}"}}'.format(code))