"""
系统配置测试用例
"""

import pytest
import time
from api.config_api import SysConfigAPI


@pytest.mark.config
@pytest.mark.regression
class TestSysConfig:
    """系统参数配置测试类"""

    @pytest.mark.asyncio
    async def test_create_config_success(self, authenticated_client):
        """测试创建系统配置成功"""
        api = SysConfigAPI(authenticated_client)
        timestamp = int(time.time() * 1000)
        data = {
            "configName": f"测试配置_{timestamp}",
            "configKey": f"test.config.key.{timestamp}",
            "configValue": "test_value",
            "configType": "N"
        }

        response = await api.create(data)

        assert response.status_code == 201
        result = response.json()
        assert result["configName"] == data["configName"]
        assert result["configKey"] == data["configKey"]

    @pytest.mark.asyncio
    async def test_get_all_configs(self, authenticated_client):
        """测试获取所有配置"""
        api = SysConfigAPI(authenticated_client)

        response = await api.get_all()

        assert response.status_code == 200
        assert isinstance(response.json(), list)

    @pytest.mark.asyncio
    async def test_get_config_by_key(self, authenticated_client):
        """测试根据key获取配置"""
        api = SysConfigAPI(authenticated_client)
        timestamp = int(time.time() * 1000)
        data = {
            "configName": f"测试配置_{timestamp}",
            "configKey": f"test.config.key.{timestamp}",
            "configValue": "test_value",
            "configType": "N"
        }
        create_response = await api.create(data)
        config_key = data["configKey"]

        response = await api.get_config_by_key(config_key)

        assert response.status_code == 200
        result = response.json()
        assert result["configKey"] == config_key

    @pytest.mark.asyncio
    async def test_update_config(self, authenticated_client):
        """测试更新配置"""
        api = SysConfigAPI(authenticated_client)
        timestamp = int(time.time() * 1000)
        data = {
            "configName": f"测试配置_{timestamp}",
            "configKey": f"test.config.key.{timestamp}",
            "configValue": "old_value",
            "configType": "N"
        }
        create_response = await api.create(data)
        config_id = create_response.json()["id"]

        update_data = {
            "configName": f"更新后_{timestamp}",
            "configKey": f"test.config.key.{timestamp}",
            "configValue": "new_value",
            "configType": "N"
        }
        response = await api.update(config_id, update_data)

        assert response.status_code == 200
        assert response.json()["configValue"] == "new_value"

    @pytest.mark.asyncio
    async def test_delete_config(self, authenticated_client):
        """测试删除配置"""
        api = SysConfigAPI(authenticated_client)
        timestamp = int(time.time() * 1000)
        data = {
            "configName": f"测试配置_{timestamp}",
            "configKey": f"test.config.key.{timestamp}",
            "configValue": "test_value",
            "configType": "N"
        }
        create_response = await api.create(data)
        config_id = create_response.json()["id"]

        response = await api.delete(config_id)

        assert response.status_code == 204
