美文网首页python学习接口测试
python 连接数据库获取数据进行批量退款

python 连接数据库获取数据进行批量退款

作者: freedom_smile | 来源:发表于2020-11-02 14:38 被阅读0次

    背景:

    测试环境经常下单用微信和支付宝真实支付,在页面一笔笔进行退款太慢,账号多也会有所遗漏,用sql查出需要退款的数据字段值调用退款接口,每天下班之前调一下保证支付的钱最后退回还能顺带测试退款接口的平均响应时间。

    python源码如下:
    import requests
    from A_shop.utils.md5_python import md5Python as md5
    from A_shop.utils.date_random import get_random,get_random_param
    from A_shop.utils.tools import del_null
    from A_shop.log.logger import Logger
    from A_shop.utils.read_config import ReadConfig
    from A_shop.utils.get_mysql_data_pd import get_mysql_data_pd
    import numpy as np
    import json
    
    def pi_refund_mysql(super_merchant_code,merchant_code,order_no,url):
        random = get_random()
        refundNo = get_random_param('r')
    
      # 退款接口的请求参数
        params = {
            "appId" : super_merchant_code,
            "merchantCode" :  merchant_code,
            "random" : random,
            "outTradeNo" : order_no,
            "refundNo" : refundNo
        }
    
        params = del_null(params)
        params["sign"] = md5.md5_sign(params, "xxxxxxxxxxx")
        # 接口请求
        r = requests.post(url, data=params)
        use_time = r.elapsed.total_seconds()
        log.info("退款接口响应时间:" + str(use_time))
    
        # 返回接口的请求和返回信息
        result = {
            'req': params,
            'res': r.json(),
            'url': url,
            'usr_time': use_time
        }
        return result
    
    if __name__ == '__main__':
        log = Logger("info")
        base_url = "http://testxxxx.bbbbb.com"
        url = base_url + "/open/refund"
    
        # 读取配置文件的数据库参数
        rc = ReadConfig()
    
        # 查询语句
        sql = '''SELECT so.super_merchant_code,so.merchant_code,so.order_no
                        FROM `shop_order_2020_10_20` so
                        WHERE so.`super_merchant_code`='EW_N2012672104'
                        AND so.order_status = 1
                        AND so.pay_status = 1
                        AND so.pay_type IN(0,1)
                        AND so.refund_type = 0
                        AND so.refunds_status IN(0,2)
                        AND so.receipt_amount != 0'''
    
        df = get_mysql_data_pd(rc,sql)
    
        all_use_time = []
        for i in range(len(df)):
            result = pi_refund_mysql(df['super_merchant_code'][i],df['merchant_code'][i],df['order_no'][i],url)
            log.info("退款请求地址:" + str(result['url']))
            log.info("退款请求信息:" + str(json.dumps(result['req'], ensure_ascii=False)))
            log.info("退款返回信息:" + str(json.dumps(result['res'], ensure_ascii=False)))
            print(result['res']["msg"])
            all_use_time.append(result['usr_time'])
        average = np.mean(all_use_time)
        print('退款接口平均耗时是:{}'.format(average))
        all_use_time_arr = np.array(all_use_time)
        print('退款接口耗时排序:{}'.format(all_use_time_arr))
    
    返回信息:
    2020-11-02 14:35:04,852 - pi_refund_mysql - 退款接口响应时间:1.900857
    退款成功
    2020-11-02 14:35:04,853 - pi_refund_mysql - 退款请求地址:http://testxxxx.bbbbb.com/open/refund
    2020-11-02 14:35:04,853 - pi_refund_mysql - 退款请求信息:{"appId": "EW_N2012672333", "merchantCode": "EW_N4061492444", "random": "20201102143502218", "outTradeNo": "2020110214341454390123", "refundNo": "r2020110214350278686", "sign": "71c2de4bb9aabd30ecba5db3ea0edad7"}
    2020-11-02 14:35:04,853 - pi_refund_mysql - 退款返回信息:{"outTradeNo": "2020110214341454390123", "refundNo": "r2020110214350278686", "refundAmount": 0.02, "refundFee": 0.0, "time": "20201102143503", "code": "SUCCESS", "msg": "退款成功", "sign": "71c2de4bb9aabd30ecba5db3ea0edad7"}
    2020-11-02 14:35:07,265 - pi_refund_mysql - 退款接口响应时间:2.409654
    2020-11-02 14:35:07,266 - pi_refund_mysql - 退款请求地址:http://testxxxx.bbbbb.com/open/refund
    2020-11-02 14:35:07,266 - pi_refund_mysql - 退款请求信息:{"appId": "EW_N2012672333", "merchantCode": "EW_N4061492444", "random": "20201102143504432", "outTradeNo": "2020110214343002384787", "refundNo": "r2020110214350440177", "sign": "2f99841e2e087370ea8ec6406425b12d"}
    2020-11-02 14:35:07,267 - pi_refund_mysql - 退款返回信息:{"outTradeNo": "2020110214343002384787", "refundNo": "r2020110214350440177", "refundAmount": 0.03, "refundFee": 0.0, "time": "20201102143507", "code": "SUCCESS", "msg": "退款成功", "sign": "2f99841e2e087370ea8ec6406425b12d"}
    退款成功
    退款接口平均耗时是:2.1552555
    退款接口耗时排序:[1.900857 2.409654]
    

    相关文章

      网友评论

        本文标题:python 连接数据库获取数据进行批量退款

        本文链接:https://www.haomeiwen.com/subject/hqlmvktx.html