# -*-coding:utf-8 -*- #
# ---------------------------------------------------------------------------
# ProjectName:   test67
# FileName:      test_dem.py
# Author:       lao_zhao
# Datetime:     2025/6/12 10:03
# Description:
# 
# ---------------------------------------------------------------------------
import random
import string

import allure
import pytest
from faker import Faker

# 同模块下用例之间的数据依赖
dependency_data = {}


@allure.epic("BPM-添加组织的场景测试")
@allure.feature("维度管理")
@allure.story("添加维度")
@allure.title("添加维度依赖登录成功的token")
@pytest.mark.dependency(depends=["test_case/test_bpm/test_auth.py::TestAuth::test_login"], scope="session")  # 标记当前用例所依赖的用例, 也会标记当前的用例可以被后续的用例依赖
def test_add_dem(session_fix):
    """添加维度"""
    add_dem_url = "http://36.139.193.99:8088/api/demension/v1/dem/addDem"
    data = Faker(locale="zh_cn")

    dem_code = f'test_{"".join(random.sample(string.ascii_letters, 12))}'
    dependency_data["dem_code"] = dem_code

    add_dem_data = {
            "code": dem_code,
            "description": "",
            "isDefault": 0,
            "name": f"{data.company()}"
        }
    res = session_fix.request(method="post", url=add_dem_url, json=add_dem_data)
    assert "添加维度成功" in res.text


@allure.epic("BPM-添加组织的场景测试")
@allure.feature("维度管理")
@allure.story("根据维度编码获取维度信息")
@allure.title("根据维度编码获取维度信息依赖添加维度成和登录成功")
@pytest.mark.dependency(depends=["test_case/test_bpm/test_auth.py::TestAuth::test_login", "test_case/test_bpm/test_dem.py::test_add_dem"], scope="session")
def test_get_dem_message(session_fix, dependency_data_fix):
    """根据维度编码获取维度信息"""
    url = "http://36.139.193.99:8088/api/demension/v1/dem/getDem"
    data = {"code": dependency_data["dem_code"]}
    res = session_fix.get(url=url, params=data)
    try:
        assert '"isDelete":"0"' in res.text
    except AssertionError:
        raise AssertionError("断言失败")
    else:
        id = res.json()["id"]
        dependency_data_fix["demId"] = id

