# -*-coding:utf-8 -*- #
# ---------------------------------------------------------------------------
# ProjectName:   PythonProject_1
# FileName:      test_bpm.py
# Author:       H_M
# Datetime:     2024/9/3 19:24
# Description:
# --------------------------------------------------------------------------
import allure
import pytest

from MyApiAutoTest_v3.common.read_excel import ReadExcel
from MyApiAutoTest_v3 import log
from MyApiAutoTest_v3.test_case.test_zs import username

readexcel = ReadExcel(username)


class TestBpm:

    @allure.epic("BPM项目-Demo")
    @pytest.mark.parametrize(
        'module, api, title, level, url, method, mediatype, case, expect, sql, sql_type, update_key',
        readexcel.get_data())
    def test_bpm(self, fix_req, fix_db, module, api, title, level, url, method, mediatype, case, expect, sql, sql_type,
                 update_key):
        """测试用例"""
        allure.dynamic.feature(module)
        allure.dynamic.story(api)
        allure.dynamic.title(title)
        allure.dynamic.severity(level)
        print(url, method, mediatype, case, expect, sql, sql_type, update_key)

        if sql_type == 'delete':
            fix_db.delete(sql['delete'])

        elif sql_type == 'select':
            sec_res = fix_db.select(sql['select'])
            case[update_key] = sec_res

        elif sql_type == 'select|delete' or sql_type == 'delete|select':
            fix_db.delete(sql['delete'])
            sec_res = fix_db.select(sql['select'])
            case[update_key] = sec_res

        res = fix_req.request_all(url, method, mediatype, case)

        try:
            for key in expect.keys():
                assert expect[key] == res.json().get(key)
        except AssertionError:
            log.error(f"断言失败，接口url为:{url}, 用例数据:{case}, 期望数据:{expect}, 服务器返回数据:{res.text}")
            raise AssertionError("断言失败")
        else:
            log.info(f"断言成功，接口url为:{url}, 用例数据:{case}, 期望数据:{expect}, 服务器返回数据:{res.text}")


