Files
张翔 1e3dc11d59 refactor(test): 重构测试套件结构并优化测试配置
feat(test-suite): 新增测试套件模块,包含API测试客户端和测试配置
fix(api): 修复数据库实体和仓库的删除操作返回值
style(api): 统一数据库表名和字段命名
perf(api): 添加缓存注解提升配置查询性能
test(api): 添加H2测试数据库配置支持
chore: 清理旧的测试文件和脚本
2026-04-01 20:57:24 +08:00

150 lines
5.7 KiB
Python

"""
字典管理测试用例
"""
import pytest
from api.dictionary_api import DictionaryAPI
@pytest.mark.dictionary
@pytest.mark.regression
class TestDictionary:
"""字典管理测试类"""
@pytest.mark.asyncio
async def test_create_dictionary_success(self, authenticated_client, test_dictionary_data, cleanup_dictionary):
"""测试创建字典成功"""
dict_api = DictionaryAPI(authenticated_client)
response = await dict_api.create_dictionary(test_dictionary_data)
assert response.status_code == 201
data = response.json()
assert "id" in data
assert data["type"] == test_dictionary_data["type"]
assert data["code"] == test_dictionary_data["code"]
assert data["name"] == test_dictionary_data["name"]
cleanup_dictionary.append(data["id"])
@pytest.mark.asyncio
async def test_create_dictionary_duplicate_type_code(self, authenticated_client, test_dictionary_data, cleanup_dictionary):
"""测试创建重复类型和编码"""
dict_api = DictionaryAPI(authenticated_client)
create_response = await dict_api.create_dictionary(test_dictionary_data)
dict_id = create_response.json()["id"]
response = await dict_api.create_dictionary(test_dictionary_data)
assert response.status_code in [400, 409]
cleanup_dictionary.append(dict_id)
@pytest.mark.asyncio
async def test_get_dictionary_by_id_success(self, authenticated_client, test_dictionary_data, cleanup_dictionary):
"""测试根据ID获取字典成功"""
dict_api = DictionaryAPI(authenticated_client)
create_response = await dict_api.create_dictionary(test_dictionary_data)
dict_id = create_response.json()["id"]
response = await dict_api.get_dictionary_by_id(dict_id)
assert response.status_code == 200
data = response.json()
assert data["id"] == dict_id
assert data["type"] == test_dictionary_data["type"]
cleanup_dictionary.append(dict_id)
@pytest.mark.asyncio
async def test_get_dictionary_by_id_not_found(self, authenticated_client):
"""测试获取不存在的字典"""
dict_api = DictionaryAPI(authenticated_client)
response = await dict_api.get_dictionary_by_id(999999)
assert response.status_code == 404
@pytest.mark.asyncio
async def test_get_dictionaries_by_type_success(self, authenticated_client, test_dictionary_data, cleanup_dictionary):
"""测试根据类型获取字典成功"""
dict_api = DictionaryAPI(authenticated_client)
create_response = await dict_api.create_dictionary(test_dictionary_data)
dict_id = create_response.json()["id"]
response = await dict_api.get_dictionaries_by_type(test_dictionary_data["type"])
assert response.status_code == 200
data = response.json()
assert isinstance(data, list)
assert any(d["id"] == dict_id for d in data)
cleanup_dictionary.append(dict_id)
@pytest.mark.asyncio
async def test_get_all_dictionaries_success(self, authenticated_client):
"""测试获取所有字典成功"""
dict_api = DictionaryAPI(authenticated_client)
response = await dict_api.get_all_dictionaries()
assert response.status_code == 200
data = response.json()
assert isinstance(data, list)
@pytest.mark.asyncio
async def test_update_dictionary_success(self, authenticated_client, test_dictionary_data, cleanup_dictionary):
"""测试更新字典成功"""
dict_api = DictionaryAPI(authenticated_client)
create_response = await dict_api.create_dictionary(test_dictionary_data)
dict_id = create_response.json()["id"]
update_data = {"name": "Updated name"}
response = await dict_api.update_dictionary(dict_id, update_data)
assert response.status_code == 200
data = response.json()
assert data["name"] == "Updated name"
cleanup_dictionary.append(dict_id)
@pytest.mark.asyncio
async def test_delete_dictionary_success(self, authenticated_client, test_dictionary_data, cleanup_dictionary):
"""测试删除字典成功"""
dict_api = DictionaryAPI(authenticated_client)
create_response = await dict_api.create_dictionary(test_dictionary_data)
dict_id = create_response.json()["id"]
response = await dict_api.delete_dictionary(dict_id)
assert response.status_code == 204
@pytest.mark.asyncio
async def test_check_type_and_code_exists_true(self, authenticated_client, test_dictionary_data, cleanup_dictionary):
"""测试检查类型和编码存在-返回true"""
dict_api = DictionaryAPI(authenticated_client)
create_response = await dict_api.create_dictionary(test_dictionary_data)
dict_id = create_response.json()["id"]
response = await dict_api.check_type_and_code_exists(
test_dictionary_data["type"],
test_dictionary_data["code"]
)
assert response.status_code == 200
assert response.json() is True
cleanup_dictionary.append(dict_id)
@pytest.mark.asyncio
async def test_check_type_and_code_exists_false(self, authenticated_client):
"""测试检查类型和编码存在-返回false"""
dict_api = DictionaryAPI(authenticated_client)
response = await dict_api.check_type_and_code_exists("NONEXISTENT_TYPE", "NONEXISTENT_CODE")
assert response.status_code == 200
assert response.json() is False