# -*-coding:utf-8 -*- #
# ---------------------------------------------------------------------------
# ProjectName:   Selenium_Project
# FileName:      api_test.py
# Author:       xxxxxxx
# Datetime:     2024/5/20 20:17
# Description:
# 命名规范：文件名全小写+下划线，类名大驼峰，方法和变量小写+下划线连接，
# 常量大写，变量和常量用名词，方法用动词
# ---------------------------------------------------------------------------
import logging

import pytest

from api_test_0520.common.read_excel import ReadExcel

excel = ReadExcel("user_02")

class TestBpm:
    @pytest.mark.parametrize("method, url, mime, case_data, expect_data, sql_data, sql_type, sql_update", excel.get_data())
    def test_bpm(self, db_fix, req_fix, method, url, mime, case_data, expect_data, sql_data, sql_type, sql_update):
        if sql_type == "select":
            result = db_fix.select_func(sql_data)
            case_data[sql_update] = result

        elif sql_type == "delete":
            db_fix.delete_func(sql_data)

        elif sql_type == "delete|select" or sql_type == "select|delect":
            db_fix.delete_func(sql_data["delete"])
            select_result = db_fix.select_func(sql_data["select"])
            print(select_result)
            case_data[sql_update] = select_result

        res = req_fix.request_all(req_method=method, req_url=url, req_mime=mime, req_data=case_data)
        try:
            for key in expect_data:
                assert expect_data[key] == res.json().get(key)
        except Exception as e:
            logging.error(f"断言失败， 用例数据为{case_data}, 期望数据为:{expect_data}, 服务器返回的数据为：{res.text}")
            print("断言失败")
            raise e
        else:
            logging.info(f"断言成功， 用例数据为{case_data}, 期望数据为:{expect_data}, 服务器返回的数据为：{res.text}")
            print("断言成功")


if __name__ == '__main__':
    test = TestBpm()
    print(test.test_bpm)