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

115 lines
3.2 KiB
Python

"""
文件管理测试用例
"""
import pytest
import os
import time
from api.file_api import SysFileAPI
@pytest.mark.file
@pytest.mark.regression
class TestSysFile:
"""文件管理测试类"""
@pytest.mark.asyncio
async def test_upload_file(self, authenticated_client):
"""测试文件上传"""
api = SysFileAPI(authenticated_client)
test_file_path = "/tmp/test_file.txt"
with open(test_file_path, "w") as f:
f.write("This is a test file content")
response = await api.upload(test_file_path, "test_user")
os.remove(test_file_path)
assert response.status_code == 201
result = response.json()
assert "id" in result
@pytest.mark.asyncio
async def test_get_all_files(self, authenticated_client):
"""测试获取所有文件"""
api = SysFileAPI(authenticated_client)
response = await api.get_all()
assert response.status_code == 200
assert isinstance(response.json(), list)
@pytest.mark.asyncio
async def test_get_file_by_id(self, authenticated_client):
"""测试根据ID获取文件"""
api = SysFileAPI(authenticated_client)
test_file_path = "/tmp/test_file.txt"
with open(test_file_path, "w") as f:
f.write("Test content")
upload_response = await api.upload(test_file_path, "test_user")
file_id = upload_response.json()["id"]
os.remove(test_file_path)
response = await api.get_by_id(file_id)
assert response.status_code == 200
assert response.json()["id"] == file_id
@pytest.mark.asyncio
async def test_download_file(self, authenticated_client):
"""测试文件下载"""
api = SysFileAPI(authenticated_client)
test_file_path = "/tmp/test_file.txt"
with open(test_file_path, "w") as f:
f.write("Download test content")
upload_response = await api.upload(test_file_path, "test_user")
file_name = upload_response.json()["fileName"]
os.remove(test_file_path)
response = await api.download(file_name)
assert response.status_code == 200
@pytest.mark.asyncio
async def test_preview_file(self, authenticated_client):
"""测试文件预览"""
api = SysFileAPI(authenticated_client)
test_file_path = "/tmp/test_file.txt"
with open(test_file_path, "w") as f:
f.write("Preview test content")
upload_response = await api.upload(test_file_path, "test_user")
file_name = upload_response.json()["fileName"]
os.remove(test_file_path)
response = await api.preview(file_name)
assert response.status_code == 200
@pytest.mark.asyncio
async def test_delete_file(self, authenticated_client):
"""测试删除文件"""
api = SysFileAPI(authenticated_client)
test_file_path = "/tmp/test_file.txt"
with open(test_file_path, "w") as f:
f.write("Delete test content")
upload_response = await api.upload(test_file_path, "test_user")
file_id = upload_response.json()["id"]
os.remove(test_file_path)
response = await api.delete(file_id)
assert response.status_code == 204