新增 API 测试用例,修复失效接口问题
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
import asyncio
|
||||
from datetime import datetime, timezone
|
||||
from uuid import uuid4
|
||||
|
||||
from app.infra.db.metadb.repositories.audit_repository import AuditRepository
|
||||
from tests.conftest import FakeAsyncSession, FakeExecuteResult, make_audit_log
|
||||
|
||||
|
||||
def test_create_log_adds_commits_and_refreshes(monkeypatch):
|
||||
class FakeAuditLog:
|
||||
def __init__(self, **kwargs):
|
||||
self.id = uuid4()
|
||||
for key, value in kwargs.items():
|
||||
setattr(self, key, value)
|
||||
|
||||
session = FakeAsyncSession()
|
||||
repo = AuditRepository(session)
|
||||
monkeypatch.setattr(
|
||||
"app.infra.db.metadb.repositories.audit_repository.models.AuditLog",
|
||||
FakeAuditLog,
|
||||
)
|
||||
|
||||
result = asyncio.run(
|
||||
repo.create_log(
|
||||
action="LOGIN",
|
||||
request_method="POST",
|
||||
request_path="/auth/login",
|
||||
response_status=200,
|
||||
)
|
||||
)
|
||||
|
||||
assert result.action == "LOGIN"
|
||||
assert result.request_method == "POST"
|
||||
assert session.commit_count == 1
|
||||
assert len(session.added) == 1
|
||||
assert len(session.refreshed) == 1
|
||||
|
||||
|
||||
def test_get_logs_builds_filtered_query_and_returns_models():
|
||||
log = make_audit_log(action="UPDATE_USER", resource_type="user")
|
||||
session = FakeAsyncSession(
|
||||
execute_results=[FakeExecuteResult(rows=[log])],
|
||||
)
|
||||
repo = AuditRepository(session)
|
||||
user_id = uuid4()
|
||||
project_id = uuid4()
|
||||
start_time = datetime(2025, 1, 1, tzinfo=timezone.utc)
|
||||
|
||||
results = asyncio.run(
|
||||
repo.get_logs(
|
||||
user_id=user_id,
|
||||
project_id=project_id,
|
||||
action="UPDATE_USER",
|
||||
resource_type="user",
|
||||
start_time=start_time,
|
||||
skip=5,
|
||||
limit=10,
|
||||
)
|
||||
)
|
||||
|
||||
assert len(results) == 1
|
||||
assert results[0].action == "UPDATE_USER"
|
||||
stmt = session.executed[0]
|
||||
assert len(stmt._where_criteria) == 5
|
||||
assert stmt._offset == 5
|
||||
assert stmt._limit == 10
|
||||
|
||||
|
||||
def test_get_log_count_returns_zero_when_scalar_none():
|
||||
session = FakeAsyncSession(
|
||||
execute_results=[FakeExecuteResult(scalar_value=None)],
|
||||
)
|
||||
repo = AuditRepository(session)
|
||||
|
||||
result = asyncio.run(repo.get_log_count(action="DELETE_USER"))
|
||||
|
||||
assert result == 0
|
||||
stmt = session.executed[0]
|
||||
assert len(stmt._where_criteria) == 1
|
||||
Reference in New Issue
Block a user