# -*-coding:utf-8 -*- #
# ---------------------------------------------------------------------------
# ProjectName:   test_58
# FileName:      test_template.py
# Author:       xxxxxxx
# Datetime:     2024/1/4 11:14
# Description:
# 
# ---------------------------------------------------------------------------
import logging
import allure
import pytest
from InterfaceAutoTest.common import log_decorator
from InterfaceAutoTest.common.read_excel import ReadExcel
from InterfaceAutoTest.test_case.test_template import *


class TestTemple:

    @allure.epic("BPM系统-老王")
    # @allure.feature("模块名称")
    # @allure.story("接口名称")
    # @allure.title("用例标题")
    # @allure.severity("用例等级")
    @pytest.mark.parametrize("module_name, api_name, case_title, case_level, case_req, case_url, case_mime, case_data, expect_data, sql_type, sql_data, update_key", ReadExcel(excel_path=excel_path, table_name=table_name, case_data_path=case_data_path, expect_data_path=expect_data_path, sql_data_path=sql_data_path).get_data())
    @log_decorator
    def test_method_template(self, db_fix, req_fix, module_name, api_name, case_title, case_level, case_req, case_url, case_mime, case_data, expect_data, sql_type, sql_data, update_key):
        # 调用allure的功能，影响报告的内容
        allure.dynamic.feature(module_name)
        allure.dynamic.story(api_name)
        allure.dynamic.title(case_title)
        allure.dynamic.severity(case_level)

        # 判断sql语句类型是否为delete
        if sql_type == "delete":
            # 使用DB类对象，调用delete方法执行删除的sql语句
            db_fix.delete(sql_data)

        # 判断sql语句是否为select
        elif sql_type == "select":
            # 使用DB类对象，调用select方法执行查询的sql语句,并接收查询的结果
            select_result = db_fix.select(sql_data)
            # 将查询结果更新到用例数据中
            case_data[update_key] = select_result

        # 判断sql语句类型是否为select|delete, 或者为delete|select
        elif sql_type == "select|delete" or sql_type == "delete|select":
            # 使用DB类对象，调用delete方法执行删除的sql语句
            db_fix.delete(sql_data["delete"])
            # 使用DB类对象，调用select方法执行查询的sql语句,并接收查询的结果
            select_result = db_fix.select(sql_data["select"])
            # 将查询结果更新到用例数据中
            case_data[update_key] = select_result

        # 发送请求--使用RequestsMethod类对象的request_all方法发送请求
        result = req_fix.request_all(req_method=case_req, req_url=case_url, req_mime=case_mime, case_data=case_data)

        # 断言
        try:
            for key in expect_data.keys():
                assert expect_data[key] == result.json().get(key)

        except AssertionError:
            logging.error("断言失败：用例为："+str(case_data)+"期望数据为："+str(expect_data)+"服务器返回数据为："+result.text)
            raise AssertionError("断言失败")
        else:
            print("断言成功")


if __name__ == '__main__':
    pytest.main()