class TestAdd(unittest.TestCase):
    def setUp(self):
        self.mysql = MysqlUtil()
        self.sql = "SELECT * FROM future.loan WHERE Id != '' ORDER BY Id DESC LIMIT 1;"
        self.max_loan_id_old = self.mysql.fetch_one(self.sql)['Id']
        print("测试加标前的最大标的ID是:", self.max_loan_id_old)

    @data(*cases)
    def test_add(self, case):
        data = DoRegex.replace(case.data)  # 通过正则取excel的数据,Context覆盖excel的数据
        data = json.loads(data)  # 从excel中取到的data是一个字符串,loads()把字符串序列化为字典
        # --请求request拿到cookies
        # 先判断有没有cookies
        if hasattr(Context, 'cookies'):
            cookies = getattr(Context, 'cookies')  # 获取放到上下文里面的cookies
        else:
            cookies = None
        # 通过封装的Request类来完成接口的调用
        resp = Request(method=case.method,
                       url=case.url,
                       data=data,
                       cookies=cookies)  # request请求时,data一定要是字典类型
        # 判断有没有cookies(有说明是登录接口)
        if resp.get_cookies():  # 判断返回里面是否有cookies
            setattr(Context, 'cookies', resp.get_cookies())  # 放入到上下文中
        print("status_code:", resp.get_status_code())  # 打印响应码
        resp_dict = resp.get_json()  # 获取请求响应,字典
        self.assertEqual(case.expected,
                         int(resp_dict['code']))  # 判断返回的code是否与期望结果一致
        # 完成数据库校验
        # 再次查询数据库,获取最新的最大标的loan各项信息
        sql = "SELECT * FROM future.loan WHERE MemberID = {} ORDER BY Id DESC LIMIT 1;".format(
            Context.loan_member_id)
        loan = self.mysql.fetch_one(sql)  # loan是字典类型
        if resp_dict['msg'] == "加标成功":  # 新增标的成功的数据校验,判断数据库loan表有这条新增的数据
            if loan is not None:  # 正常加标成功,不应该返回None
                max_loan_id_new = self.max_loan_id_old + 1
                expected = max_loan_id_new  # 期望结果:测试加标成功前的最大标的ID加上1
                # 判断数据库里面的标的详情是否与测试数据一致
                # 多个字段一致才assert通过
                self.assertEqual(expected, loan['Id'])  # 实际结果:数据库查询的最大标的ID
                self.assertEqual(int(data['memberId']),
                                 loan['MemberID'])  # MemberID是借款人的ID,管理员登录进行操作
                self.assertEqual(float(data['amount']), loan['Amount'])
                self.assertEqual(data['title'], loan['Title'])
                self.assertEqual(float(data['loanRate']), loan['LoanRate'])
                self.assertEqual(data['loanTerm'], loan['LoanTerm'])
                self.assertEqual(data['loanDateType'], loan['LoanDateType'])
                self.assertEqual(data['repaymemtWay'], loan['RepaymemtWay'])
                self.assertEqual(data['biddingDays'], loan['BiddingDays'])
                self.assertEqual(1, loan['Status'])
                print("测试加标后的最大标的ID是:", max_loan_id_new)
            else:  # 返回None,表示数据库里面没有插入数据,管理员加标测试失败
                raise AssertionError
        elif resp_dict['code'] != '10001':
            expected = self.max_loan_id_old  # 期望结果:测试加标成功前的最大标的ID
            self.assertEqual(expected, loan['Id'])

    def tearDown(self):
        self.mysql.close()
class TestWithdraw(unittest.TestCase):
    def setUp(self):
        self.sql = 'SELECT * FROM future.member WHERE MobilePhone = {0}'.format(
            Context.invest_user)
        self.mysql = MysqlUtil()
        # 取现前账户余额记录
        self.leaveamount_old = self.mysql.fetch_one(
            self.sql)['LeaveAmount']  # 获取账户余额---测试取现前的账户余额
        print("取现前的账户余额是:{}元".format(self.leaveamount_old))

    @data(*cases)
    def test_withdraw(self, case):
        data = DoRegex.replace(case.data)
        data = json.loads(data)
        # 先判断有没有cookies
        if hasattr(Context, 'cookies'):
            cookies = getattr(Context, 'cookies')  # 获取放到上下文里面的cookies
        else:
            cookies = None
        # 通过封装的Request类来完成接口的调用
        resp = Request(method=case.method,
                       url=case.url,
                       data=data,
                       cookies=cookies)  # request请求时,data一定要是字典类型

        # 判断有没有cookies(有说明是登录接口)
        # --请求request拿到cookies --把cookies放到Context里
        if resp.get_cookies():  # 判断返回里面是否有cookies
            setattr(Context, 'cookies', resp.get_cookies())  # 放入到上下文中
        print("status_code:", resp.get_status_code())  # 打印响应码
        resp_dict = resp.get_json()  # 获取请求响应,字典
        self.assertEqual(case.expected,
                         int(resp_dict['code']))  # 判断返回的code是否与期望结果一致

        # 完成数据库校验,判断取现成功之后账户余额增加正确,取现失败余额不变
        # 通过数据库查可以看到取现前的余额,取现后的余额,excel中的case.data可以查到取现金额amount
        # 数据库校验:期望的取现后的余额=取现前的余额-取现金额,实际的取现后的余额=查询数据库得到取现后的余额
        leaveamount_new = self.mysql.fetch_one(
            self.sql)['LeaveAmount']  # 再次获取账户余额---测试取现后的余额
        actual = float(leaveamount_new)  # 实际的账户余额 = 查询数据库得到账户的余额
        print("测试{}后的余额是:{}元".format(case.title, actual))
        if resp_dict['code'] == '10001' and resp_dict[
                'msg'] == "取现成功":  # 取现请求后的响应码是10001并且取现成功
            amount = float(data['amount'])
            print('取现金额是:{}元'.format(amount))
            expected = float(
                self.leaveamount_old) - amount  # 取现成功,期望的账户余额=取现前的余额-取现金额
            self.assertEqual(expected, actual)  # 判断期望结果与实际结果是否一致
        elif resp_dict['code'] != '10001':
            expected = float(self.leaveamount_old)  # 取现失败,期望结果:账户余额不变,是取现前的余额
            self.assertEqual(expected, actual)  # 判断期望结果与实际结果是否一致

    def tearDown(self):
        self.mysql.close()
Example #3
0
    def setUpClass(cls):
        global mysql
        mysql = MysqlUtil()
        sql = "SELECT MobilePhone from future.member where MobilePhone!='' ORDER BY MobilePhone DESC LIMIT 1"

        global max_phone
        max_phone = int(mysql.fetch_one(sql)['MobilePhone'])
class TestGetFinanceLogList(unittest.TestCase):

    def setUp(self):
        self.mysql = MysqlUtil()

    @data(*cases)
    def test_getFinanceLogList(self, case):
        data = DoRegex.replace(case.data)  # 参数化处理
        data = json.loads(data)  # 将测试数据由字符串序列化成字典

        if hasattr(Context, 'cookies'):  # 判断是否有cookies
            cookies = getattr(Context, 'cookies')  # 获取放到上下文里面的cookies
        else:
            cookies = None
        # Request封装类请求
        resp = Request(method=case.method, url=case.url, data=data, cookies=cookies)
        resp_dict = resp.get_json()  # 获取请求响应,字典
        # 判断返回里面是否有cookies(有说明是登录接口)
        if resp.get_cookies():  # 判断返回里面是否有cookies
            setattr(Context, 'cookies', resp.get_cookies())  # 放入到上下文中

        print("status_code:", resp.get_status_code())  # 打印响应码
        resp_dict = resp.get_json()  # 获取请求响应,字典
        self.assertEqual(case.expected, int(resp_dict['code']))  # 判断返回的code是否与期望结果一致

        # 数据库校验
        if resp_dict['msg'] == "获取用户流水记录成功!":
            # 从数据库查financelog表中某个用户的全部流水记录
            sql_select_financelog = "SELECT * FROM future.financelog WHERE PayMemberID = {} ORDER BY Id DESC;".format(Context.invest_member_id)
            invest_list = self.mysql.fetch_all(sql_select_financelog)
            # 根据用户ID获取financelog表中某用户的流水记录的最大行数
            sql_select_row = "SELECT COUNT(Id) FROM future.financelog WHERE PayMemberID = {};".format(Context.invest_member_id)
            max_row = self.mysql.fetch_one(sql_select_row)['COUNT(Id)']

            if invest_list is not None:
                for i in range(0, max_row):  # 每一行数据校验
                    MyLog.info("正在校验第{}行数据".format(i))
                    # 判断请求返回的数据详情与数据库里面的详情是否一致
                    # 多个字段一致才assert通过
                    self.assertEqual(int(resp_dict['data'][i]['id']), invest_list[i]['Id'])
                    self.assertEqual(int(resp_dict['data'][i]['payMemberId']), invest_list[i]['PayMemberId'])
                    self.assertEqual(int(resp_dict['data'][i]['incomeMemberId']), invest_list[i]['IncomeMemberId'])
                    self.assertEqual(float(resp_dict['data'][i]['amount']), invest_list[i]['Amount'])
                    # 注意:钱是空值时!!! --- 还需再改!
                    # self.assertEqual(float(resp_dict['data'][i]['incomeMemberMoney']), invest_list[i]['IncomeMemberMoney'])
                    # self.assertEqual(float(resp_dict['data'][i]['payMemberMoney']), float(invest_list[i]['PayMemberMoney']))
                    self.assertEqual(int(resp_dict['data'][i]['status']), invest_list[i]['Status'])
                    self.assertEqual(resp_dict['data'][i]['createTime'][0:19], invest_list[i]['CreateTime'].strftime("%Y-%m-%d %H:%M:%S"))
                    MyLog.info("校验结果:PASS")

    def tearDown(self):
        self.mysql.close()
class TestList(unittest.TestCase):
    def setUp(self):
        self.mysql = MysqlUtil()

    @data(*cases)
    def test_list(self, case):
        data = DoRegex.replace(case.data)
        data = json.loads(data)

        # 先判断有没有cookies
        if hasattr(Context, 'cookies'):
            cookies = getattr(Context, 'cookies')
        else:
            cookies = None
        # 通过封装的Request类来完成接口的调用
        resp = Request(method=case.method,
                       url=case.url,
                       data=data,
                       cookies=cookies)
        # 判断返回里面是否有cookies(有说明是登录接口)
        if resp.get_cookies():
            setattr(Context, 'cookies', resp.get_cookies())  # cookies放入到上下文中

        print("status_code:", resp.get_status_code())  # 打印响应码
        resp_dict = resp.get_json()  # 获取请求响应,字典
        self.assertEqual(case.expected,
                         int(resp_dict['code']))  # 判断返回的code是否与期望结果一致

        # 完成数据库校验
        if resp_dict['msg'] == "获取用户列表成功":
            # 从数据库查到member表全部用户的信息
            # member_list是一个列表,列表是按照用户ID顺序排序。其中的元素是一个个字典。
            # resp_dict['data'] 也是一个列表,列表是按照用户ID顺序排序。其中的元素是一个个字典。
            sql_select_member = "SELECT * FROM future.member WHERE Id != '' ORDER BY Id ASC;"
            member_list = self.mysql.fetch_all(sql_select_member)
            # 获取member表最大行数
            sql_select_row = "SELECT COUNT(Id) FROM future.member;"
            max_row = self.mysql.fetch_one(sql_select_row)['COUNT(Id)']
            if member_list is not None:  # 如果从数据库里面查询出来不是空
                for i in range(0, max_row):  # 每一行数据校验
                    # 判断请求返回的数据详情与数据库里面的详情是否与一致
                    # 多个字段一致才assert通过
                    self.assertEqual(resp_dict['data'][i]['id'],
                                     member_list[i]['Id'])
                    self.assertEqual(resp_dict['data'][i]['mobilephone'],
                                     member_list[i]['MobilePhone'])
                    self.assertEqual(resp_dict['data'][i]['pwd'],
                                     member_list[i]['Pwd'])
                    self.assertEqual(resp_dict['data'][i]['regname'],
                                     member_list[i]['RegName'])
                    self.assertEqual(
                        float(resp_dict['data'][i]['leaveamount']),
                        float(member_list[i]['LeaveAmount']))
                    self.assertEqual(
                        resp_dict['data'][i]['regtime'][0:19], member_list[i]
                        ['RegTime'].strftime("%Y-%m-%d %H:%M:%S"))
                    self.assertEqual(int(resp_dict['data'][i]['type']),
                                     member_list[i]['Type'])

    def tearDown(self):
        self.mysql.close()
Example #6
0
class TestGetLoanList(unittest.TestCase):
    def setUp(self):
        self.mysql = MysqlUtil()

    @data(*cases)
    def test_getLoanList(self, case):
        data = DoRegex.replace(case.data)  # 参数化处理
        data = json.loads(data)  # 字符串序列化为字典
        # 先判断有没有cookies
        if hasattr(Context, 'cookies'):
            cookies = getattr(Context, 'cookies')
        else:
            cookies = None
        # 通过封装的Request类来完成接口的调用
        resp = Request(method=case.method,
                       url=case.url,
                       data=data,
                       cookies=cookies)
        # 判断返回里面是否有cookies(有说明是登录接口)
        if resp.get_cookies():
            setattr(Context, 'cookies', resp.get_cookies())  # cookies放入到上下文中
        print("status_code:", resp.get_status_code())  # 打印响应码
        resp_dict = resp.get_json()  # 获取请求响应,字典
        self.assertEqual(case.expected,
                         int(resp_dict['code']))  # 判断返回的code是否与期望结果一致

        # 完成数据库校验
        if resp_dict['msg'] == "获取标列表成功":
            # 从数据库查到loan表全部标的信息
            # loan_list是一个列表,列表是按照标的ID顺序排序。其中的元素是一个个字典。
            # resp_dict['data'] 也是一个列表,列表是按照标的ID顺序排序。其中的元素是一个个字典。
            sql_select_loan = "SELECT * FROM future.loan WHERE Id != '' ORDER BY Id ASC;"
            loan_list = self.mysql.fetch_all(sql_select_loan)
            # 获取loan表最大行数
            sql_select_row = "SELECT COUNT(Id) FROM future.loan;"
            max_row = self.mysql.fetch_one(sql_select_row)['COUNT(Id)']
            if loan_list is not None:  # 如果从数据库里面查询出来不是空
                for i in range(0, max_row):  # 每一行数据校验
                    # 判断数据库里面的标的详情是否与请求返回的数据详情一致
                    # 多个字段一致才assert通过
                    self.assertEqual(int(resp_dict['data'][i]['id']),
                                     loan_list[i]['Id'])
                    self.assertEqual(int(resp_dict['data'][i]['memberId']),
                                     loan_list[i]['MemberID'])
                    self.assertEqual(resp_dict['data'][i]['title'],
                                     loan_list[i]['Title'])
                    self.assertEqual(float(resp_dict['data'][i]['amount']),
                                     float(loan_list[i]['Amount']))
                    self.assertEqual(float(resp_dict['data'][i]['loanRate']),
                                     float(loan_list[i]['LoanRate']))
                    self.assertEqual(int(resp_dict['data'][i]['loanTerm']),
                                     loan_list[i]['LoanTerm'])
                    self.assertEqual(int(resp_dict['data'][i]['loanDateType']),
                                     loan_list[i]['LoanDateType'])
                    self.assertEqual(int(resp_dict['data'][i]['repaymemtWay']),
                                     loan_list[i]['RepaymemtWay'])
                    self.assertEqual(int(resp_dict['data'][i]['biddingDays']),
                                     loan_list[i]['BiddingDays'])
                    self.assertEqual(int(resp_dict['data'][i]['status']),
                                     loan_list[i]['Status'])
                    self.assertEqual(
                        resp_dict['data'][i]['createTime'][0:19], loan_list[i]
                        ['CreateTime'].strftime("%Y-%m-%d %H:%M:%S"))
                    # 时间是空值时,怎么校验???
                    # self.assertEqual(resp_dict['data'][i]['biddingStartTime'][0:19], loan_list[i]['BiddingStartTime'].strftime("%Y-%m-%d %H:%M:%S"))
                    # self.assertEqual(resp_dict['data'][i]['fullTime'][0:19],loan_list[i]['FullTime'].strftime("%Y-%m-%d %H:%M:%S"))
                    MyLog.info("正在校验数据库第{}行数据,结果:PASS".format(i + 1))

    def tearDown(self):
        self.mysql.close()
 def setUpClass(cls):
     global mysql
     mysql = MysqlUtil()
     sql = "select mobilephone from future.member where mobilephone != '' order by mobilephone desc limit 1;"
     global max_phone_old
     max_phone_old = mysql.fetch_one(sql)['mobilephone']
Example #8
0
class TestInvest(unittest.TestCase):
    def setUp(self):
        self.mysql = MysqlUtil()
        self.sql_select_member = 'SELECT * FROM future.member WHERE MobilePhone = {0}'.format(
            Context.invest_user)
        # 投资竞标前的账户余额
        self.leaveamount_old = self.mysql.fetch_one(
            self.sql_select_member)['LeaveAmount']  # 获取账户余额---测试投资竞标前的账户余额
        print("投资竞标前的投资人账户余额是:{}元".format(self.leaveamount_old))

    @data(*cases)
    def test_invest(self, case):
        data = DoRegex.replace(case.data)  # 参数化处理
        data = json.loads(data)  # 将测试数据由字符串序列化成字典

        if hasattr(Context, 'cookies'):  # 判断是否有cookies
            cookies = getattr(Context, 'cookies')  # 获取放到上下文里面的cookies
        else:
            cookies = None
        # Request封装类请求
        resp = Request(method=case.method,
                       url=case.url,
                       data=data,
                       cookies=cookies)
        print(resp.get_text())
        resp_dict = resp.get_json()  # 获取请求响应,字典
        # 优先判断响应返回的code是否与期望结果一致
        self.assertEqual(case.expected, int(resp_dict['code']))
        # 判断有没有cookies
        if resp.get_cookies():  # 判断返回里面是否有cookies
            setattr(Context, 'cookies', resp.get_cookies())  # 放入到上下文中

        # 当创建标的成功时,根据借款人ID查看数据库loan表是否添加数据
        if resp_dict['msg'] == '加标成功':
            sql_select_loan = 'SELECT * FROM future.loan WHERE MemberID = {} ORDER BY CreateTime DESC LIMIT 1;'.format(
                Context.loan_member_id)
            loan = self.mysql.fetch_one(sql_select_loan)
            # 管理员加标成功的数据库校验
            if loan is not None:  # 如果从数据库里面查询出来不是空,则创建标的成功
                # 判断数据库里面的标的详情是否与测试数据一致
                # 多个字段一致才assert通过
                self.assertEqual(int(data['memberId']),
                                 loan['MemberID'])  # MemberID是借款人的ID,管理员操作
                self.assertEqual(float(data['amount']), loan['Amount'])
                self.assertEqual(data['title'], loan['Title'])
                self.assertEqual(float(data['loanRate']), loan['LoanRate'])
                self.assertEqual(data['loanTerm'], loan['LoanTerm'])
                self.assertEqual(data['loanDateType'], loan['LoanDateType'])
                self.assertEqual(data['repaymemtWay'], loan['RepaymemtWay'])
                self.assertEqual(data['biddingDays'], loan['BiddingDays'])
                # 将创建成功的标的ID写入到上下文中,用于之后投资用
                setattr(Context, 'loan_id',
                        str(loan['Id']))  # 放一个str类型的进去,以备后面正则替换
            else:  # 如果数据库里面没有数据,则管理员加标测试失败
                raise AssertionError

        # 当审核成功,需校验数据库loan表中status字段更改
        if resp_dict['msg'] == "更新状态成功:竞标开始,当前标为竞标中状态":
            sql_select_loan = 'SELECT * FROM future.loan WHERE MemberID = {} ORDER BY CreateTime DESC LIMIT 1;'.format(
                Context.loan_member_id)
            loan = self.mysql.fetch_one(sql_select_loan)
            self.assertEqual(data['status'], loan['Status'])

        # 当投资成功时,根据投资人ID查看数据member表中验证余额是否减少
        if resp_dict['msg'] == "竞标成功":
            amount = float(data['amount'])  # 投资金额
            print('投资竞标金额是:{}元'.format(amount))
            leaveamount_new = self.mysql.fetch_one(
                self.sql_select_member)['LeaveAmount']  # 投资后的金额
            actual = float(leaveamount_new)
            expected = float(self.leaveamount_old) - float(
                amount)  # 期望结果 = 投资前的余额 - 投资金额
            self.assertEqual(expected, actual)
            print("测试{}后的账户余额是:{}元".format(case.title, float(actual)))
        elif resp_dict['code'] != '10001':  # 投资失败,余额不变
            leaveamount_new = self.mysql.fetch_one(
                self.sql_select_member)['LeaveAmount']  # 投资后的金额
            actual = leaveamount_new
            print("测试{}后的账户余额是:{}元".format(case.title, float(actual)))
            expected = self.leaveamount_old  # 期望结果 = 投资前的余额
            self.assertEqual(expected, actual)
        # 当投资成功时,根据标的ID查看invest表,每投资一次,验证invest表新增一条记录---再补充!
        pass

        # 用户余额有变动的话,会在资金流水表插入一条记录 ---再补充!
        pass

    def tearDown(self):
        self.mysql.close()
class TestFetInvestsByLoanId(unittest.TestCase):
    def setUp(self):
        self.mysql = MysqlUtil()

    @data(*cases)
    def test_getInvestsByLoanId(self, case):
        data = DoRegex.replace(case.data)  # 参数化处理
        data = json.loads(data)  # 将测试数据由字符串序列化成字典

        if hasattr(Context, 'cookies'):  # 判断是否有cookies
            cookies = getattr(Context, 'cookies')  # 获取放到上下文里面的cookies
        else:
            cookies = None
        # Request封装类请求
        resp = Request(method=case.method,
                       url=case.url,
                       data=data,
                       cookies=cookies)
        resp_dict = resp.get_json()  # 获取请求响应,字典
        # 判断返回里面是否有cookies(有说明是登录接口)
        if resp.get_cookies():  # 判断返回里面是否有cookies
            setattr(Context, 'cookies', resp.get_cookies())  # 放入到上下文中

        print("status_code:", resp.get_status_code())  # 打印响应码
        resp_dict = resp.get_json()  # 获取请求响应,字典
        self.assertEqual(case.expected,
                         int(resp_dict['code']))  # 判断返回的code是否与期望结果一致

        # 数据库校验
        if resp_dict['msg'] == "获取记录成功":
            # 从数据库查invest表中某个标的/项目的全部投资记录
            sql_select_invest = "SELECT * FROM future.invest WHERE LoanId = {} ORDER BY Id DESC;".format(
                Context.invest_List_by_loan_id)
            invest_list = self.mysql.fetch_all(sql_select_invest)
            # 根据标的/项目ID获取invest表中某个标的/项目的投资记录的最大行数
            sql_select_row = "SELECT COUNT(Id) FROM future.invest WHERE LoanId = {};".format(
                Context.invest_List_by_loan_id)
            max_row = self.mysql.fetch_one(sql_select_row)['COUNT(Id)']

            if invest_list is not None:
                for i in range(0, max_row):  # 每一行数据校验
                    MyLog.info("正在校验第{}行数据".format(i))
                    # 判断请求返回的数据详情与数据库里面的详情是否一致
                    # 多个字段一致才assert通过
                    self.assertEqual(int(resp_dict['data'][i]['id']),
                                     invest_list[i]['Id'])
                    self.assertEqual(int(resp_dict['data'][i]['memberId']),
                                     invest_list[i]['MemberID'])
                    self.assertEqual(int(resp_dict['data'][i]['loanId']),
                                     invest_list[i]['LoanId'])
                    self.assertEqual(float(resp_dict['data'][i]['amount']),
                                     invest_list[i]['Amount'])
                    self.assertEqual(int(resp_dict['data'][i]['isValid']),
                                     invest_list[i]['IsValid'])
                    self.assertEqual(
                        resp_dict['data'][i]['createTime'][0:19],
                        invest_list[i]['CreateTime'].strftime(
                            "%Y-%m-%d %H:%M:%S"))
                    MyLog.info("校验结果:PASS")

    def tearDown(self):
        self.mysql.close()
Example #10
0
class InvestTest(unittest.TestCase):
    def setUp(self):
        self.mysql = MysqlUtil()
        # 投资前账户余额
        self.select_member = 'select * from future.member where mobilephone = {0}'.format(
            Context.normal_user)
        self.before_amount = self.mysql.fetch_one(
            self.select_member)['LeaveAmount']
        # 自己去添加
        pass

    @data(*cases)
    def test_invest(self, case):
        # 参数化处理
        data = DoRegex.replace(case.data)
        # 将测试数据由字符串序列化成字典
        data = json.loads(data, encoding="UTF-8")
        print("Test data:{}".format(data))
        if hasattr(Context, 'cookies'):  # 判断是否有cookies
            cookies = getattr(Context, 'cookies')  # 获取放到上下文里面的cookies
        else:
            cookies = None
        # 通过封装的Request类来完成接口的调用
        resp = Request(method=case.method,
                       url=case.url,
                       data=data,
                       cookies=cookies)
        print(resp.get_text())
        resp_dict = resp.get_json()  # 获取请求响应,字典
        # 优先判断响应返回的code 是否与期望结果一致
        self.assertEqual(case.expected, int(resp_dict['code']))

        # 判断有没有cookie
        if resp.get_cookies():  # 判断返回里面是否有cookies
            setattr(Context, 'cookies', resp.get_cookies())  # 放入到上下文中

        # 当创建标的成功时,根据借款人ID查看数据库loan表是否与添加数据
        if resp_dict['msg'] == '加标成功':
            select_loan = 'select * from future.loan where memberId = {0} order by createtime desc limit 1'.format(
                Context.loan_member_id)
            loan = self.mysql.fetch_one(select_loan)
            if loan is not None:  # 如果从数据库里面查询出来不是空,则创建标的成功
                # 判断数据库里面的标的详情是否与测试数据一致,可以优化
                # self.assertEqual(float(data['amount']), float(loan['Amount']))  # 多个字段一致才assert通过
                # # self.assertEqual(data['title'], str(loan['Title']))
                # self.assertEqual(int(data['loanRate']), int(loan['LoanRate']))
                # self.assertEqual(data['loanTerm'], loan['LoanTerm'])
                # self.assertEqual(data['loanDateType'], loan['LoanDateType'])
                # self.assertEqual(data['repaymemtWay'], loan['RepaymemtWay'])
                # self.assertEqual(data['biddingDays'], loan['BiddingDays'])

                # self.assertDictEqual(data,loan)   # 断言优化
                # 将创建成功的标的ID写入到上下文中,用于之后投资用
                setattr(Context, 'loan_id',
                        str(loan['Id']))  # 放一个str类型的进去,以备后面正则替换
            else:
                raise AssertionError  # 如果数据库里面没有数据,就测试失败

        # 当审核成功,需校验数据库loan表中status字段更改,自己添加
        if resp_dict['msg'] == '成功':
            select_status = "select * from future.loan where loan_id={0}".format(
                Context.loan_id)
            loan_status = self.mysql.fetch_one(select_status)
            self.assertEqual(data['status'], loan_status['status'])

        # 当投资成功时,根据投资人ID查看数据member表中验证余额是否减少
        if resp_dict['msg'] == '竞标成功':
            amount = data['amount']  # 投资金额
            actual = self.mysql.fetch_one(
                self.select_member)['LeaveAmount']  # 投资后的金额
            expect = float(self.before_amount) - float(
                amount)  # 期望 = 投资前的余额 - 投资金额
            self.assertEqual(str(expect), str(actual))
        elif resp_dict['code'] != '10001':  # 投资失败,余额不变
            actual = self.mysql.fetch_one(
                self.select_member)['LeaveAmount']  # 投资后的金额
            expect = float(self.before_amount)  # 期望 = 投资前的余额
            self.assertEqual(str(expect), str(actual))
        # else:
        #     raise AssertionError  # 如果数据库里面没有数据,就测试失败

    def tearDown(self):
        pass
Example #11
0
# -*- coding:utf-8 -*-
# @Time    : 2019/1/5 12:08
# @Author  : totoo

import unittest
from common.do_excel import DoExcel
from common.config import constants
from common.request import Request
from common.basic_data import DoRegex,Context
import json
from ddt import ddt,data,unpack,file_data
from datas.mysql_util import MysqlUtil


do_excel = DoExcel(constants.case_file)  # 实例化一个DoExcel对象
cases = do_excel.get_cases("invest")  #返回case列表
mysql = MysqlUtil()
select_loan = 'select * from future.loan where memberId = {0} order by createtime desc limit 1'.format(Context.loan_member_id)
loan = mysql.fetch_one(select_loan)
print(loan.decode('UTF-8').encode('GBK')['Title'])
class TestAudit(unittest.TestCase):
    def setUp(self):
        self.mysql = MysqlUtil()
        self.sql = "SELECT * FROM future.loan WHERE Id != '' ORDER BY Id DESC LIMIT 1;"
        self.max_loan_id_old = self.mysql.fetch_one(self.sql)['Id']
        print("测试审核前的最大标的ID是:", self.max_loan_id_old)

    @data(*cases)  # 传进来cases列表
    def test_audit(self, case):  # 用case变量接收传进来的数据
        data = DoRegex.replace(case.data)  # 参数化处理
        data = json.loads(data)  # 将测试数据由字符串序列化成字典

        # 先判断有没有cookies
        if hasattr(Context, 'cookies'):  # 判断是否有cookies
            cookies = getattr(Context, 'cookies')  # 获取放到上下文里面的cookies
        else:
            cookies = None
        # Request封装类请求
        resp = Request(method=case.method,
                       url=case.url,
                       data=data,
                       cookies=cookies)  # request请求时,data一定要是字典类型
        print("status_code:", resp.get_status_code())  # 打印响应码
        resp_dict = resp.get_json()  # 获取请求响应,字典
        # 优先判断响应返回的code是否与期望结果一致
        self.assertEqual(case.expected, int(resp_dict['code']))
        # 判断有没有cookies(有说明是登录接口)
        if resp.get_cookies():
            setattr(Context, 'cookies', resp.get_cookies()
                    )  # --请求request拿到cookies --把cookies放到Context里

        # 当创建标的成功时,根据借款人ID查看数据库loan表是否添加数据
        if resp_dict['msg'] == "加标成功":  # 新增标的成功的数据校验,判断数据库loan表有这条新增的数据
            # 再次查询数据库,获取最新的最大标的loan各项信息
            sql = "SELECT * FROM future.loan WHERE MemberID = {} ORDER BY Id DESC LIMIT 1;".format(
                Context.loan_member_id)
            loan = self.mysql.fetch_one(sql)  # loan是字典类型
            if loan is not None:  # 正常加标成功,不应该返回None,管理员加标成功的数据库校验
                max_loan_id_new = self.max_loan_id_old + 1
                expected = max_loan_id_new  # 期望结果:测试加标成功前的最大标的ID加上1
                # 判断数据库里面的标的详情是否与测试数据一致
                # 多个字段一致才assert通过
                self.assertEqual(expected, loan['Id'])  # 实际结果:数据库查询的最大标的ID
                self.assertEqual(int(data['memberId']),
                                 loan['MemberID'])  # MemberID是借款人的ID,管理员登录进行操作
                self.assertEqual(float(data['amount']), loan['Amount'])
                self.assertEqual(data['title'], loan['Title'])
                self.assertEqual(float(data['loanRate']), loan['LoanRate'])
                self.assertEqual(data['loanTerm'], loan['LoanTerm'])
                self.assertEqual(data['loanDateType'], loan['LoanDateType'])
                self.assertEqual(data['repaymemtWay'], loan['RepaymemtWay'])
                self.assertEqual(data['biddingDays'], loan['BiddingDays'])
                self.assertEqual(1, loan['Status'])
                print("测试加标后的最大标的ID是:", max_loan_id_new)
                # 将创建成功的标的ID写入到上下文中,用于之后投资用
                setattr(Context, 'loan_id',
                        str(loan['Id']))  # 放一个str类型的进去,以备后面正则替换
            else:  # 返回None,表示数据库里面没有插入数据,管理员加标测试失败
                raise AssertionError

        # 当审核成功,需校验数据库loan表中status字段更改
        if case.url == '/loan/audit' and resp_dict['code'] == 10001:
            sql = 'SELECT * FROM future.loan WHERE MemberID = {} ORDER BY CreateTime DESC LIMIT 1;'.format(
                Context.loan_member_id)
            loan = self.mysql.fetch_one(sql)
            self.assertEqual(data['status'], loan['Status'])