import json
import unittest
from ddt import ddt, data
import requests

from common.excel import read_excel_dict
from setting import config

cases = read_excel_dict(file=config.case_file, sheet_name='recharge')

@ddt
class TestRecharge(unittest.TestCase):

    def setUp(self) -> None:
        pass

    def tearDown(self) -> None:
        pass

    @data(*cases)
    def test_recharge_01(self, info):
        """测试步骤
        1. 访问登录接口,得到 token, member_id
        2, token 添加到 headers, v2 中
        3, member_id 添加当请求数据当中
        4, 访问充值接口
        # 5, 余额校验。

        info
        运行测试步骤
        断言

        """
        # 登录
        headers = {"X-Lemonban-Media-Type":"lemonban.v2"}
        data = {"mobile_phone": "", "pwd": ""}

        login_resp = requests.request(url=config.host + '/member/login',
                         method='post',
                         headers=headers,
                         json=data).json()
        print(login_resp)

        # 获取 member_id 和 token

        member_id = str(login_resp['data']['id'])
        token = login_resp['data']['token_info']['token']

        # info 代替之前有 ## 标记的测试数据
        json_data = info['json']
        headers = info['headers']
        if '#member_id#' in json_data:
            json_data = json_data.replace('#member_id#', member_id)
        # 替换请求头
        if '#token#' in headers:
            headers = headers.replace('#token#', token)

        # 转化成字典类型
        headers = json.loads(headers)
        json_data = json.loads(json_data)
        expected = json.loads(info['expected'])

        recharge_resp = requests.request(method=info['method'],
                         url=config.host + info['url'],
                         headers=headers,
                         json=json_data).json()
        # 断言
        for key, value in expected.items():
            self.assertEqual(value, recharge_resp[key])



# TODO
# 一个用例的代码太多
# 响应数据的获取 --> jsonpath
# 余额还没校验,1, 接口返回,   2, 数据库