# -*-coding:utf-8 -*- #
# ---------------------------------------------------------------------------
# ProjectName:   test67
# FileName:      test_bpm.py
# Author:       lao_zhao
# Datetime:     2025/6/13 15:34
# Description:
# 
# ---------------------------------------------------------------------------
from APIAutoTest_v3 import log_decorator
from APIAutoTest_v3.utils.read_excel import ReadExcel
import pytest
from APIAutoTest_v3.utils.read_system_ini import ReadSystemIni
import allure

excel = ReadExcel(ReadSystemIni().get_user_data_name("user2"))


class TestBPM:

    @log_decorator
    @allure.epic("BPM项目-张三")
    # @allure.feature("模块名称")
    # @allure.story("接口名称")
    # @allure.title("用例标题")
    # @allure.severity("用例等级")
    @pytest.mark.parametrize(
        "module, api, url, title, level, mime, req, case_data, expect_data, sql_type, sql_data, update_key",
        excel.get_data())
    def test_bpm(self, req_fix, db_fix, module, api, url, title, level, mime, req, case_data, expect_data, sql_type,
                 sql_data, update_key):
        # allure生成报告的结果
        allure.dynamic.feature(module)
        allure.dynamic.story(api)
        allure.dynamic.title(title)
        allure.dynamic.severity(level)

        # 执行删除的sql语句
        if sql_type == "delete":
            db_fix.delete(sql_data['delete'])
        # 执行查询的sql语句
        elif sql_type == "select":
            select_result = db_fix.select(sql_data["select"])
            if isinstance(case_data, str):
                case_data = case_data.replace(update_key, select_result)
            else:
                case_data[update_key] = select_result
        # 执行删除和查询语句
        elif sql_type == "select|delete" or sql_type == "delete|select":
            db_fix.delete(sql_data['delete'])
            select_result = db_fix.select(sql_data["select"])
            if isinstance(case_data, str):
                case_data = case_data.replace(update_key, select_result)
            else:
                case_data[update_key] = select_result

        # 发送请求
        res = req_fix.request_all(req_method=req, req_url=url, req_mime=mime, case_data=case_data)

        # 断言
        try:
            assert expect_data in res.text
        except AssertionError:
            raise AssertionError(
                f"断言失败,\n\t用例数据为：{case_data}, \n\t期望数据为：{expect_data}, \n\t请求的url为：{url}, \n\t请求方法为：{req}, \n\t服务器返回的结果为：{res.text}")
