7e505a04c7
CI/CD Pipeline / Check if frontend-only change (push) Has been cancelled
CI/CD Pipeline / Validate - Code Quality (push) Has been cancelled
CI/CD Pipeline / Validate - Type Check (mypy) (push) Has been cancelled
CI/CD Pipeline / Validate - Migration (alembic) (push) Has been cancelled
CI/CD Pipeline / Unit Tests (push) Has been cancelled
CI/CD Pipeline / Integration Tests (push) Has been cancelled
CI/CD Pipeline / Frontend Lint (push) Has been cancelled
CI/CD Pipeline / Frontend Unit Tests (push) Has been cancelled
CI/CD Pipeline / PR Build API Image (push) Has been cancelled
CI/CD Pipeline / PR Build Web Image (push) Has been cancelled
CI/CD Pipeline / PR Build Worker Image (push) Has been cancelled
CI/CD Pipeline / Build Staging API Image (push) Has been cancelled
CI/CD Pipeline / Build Staging Web Image (push) Has been cancelled
CI/CD Pipeline / Build Staging Worker Image (push) Has been cancelled
CI/CD Pipeline / Deploy Staging (Watchtower auto-deploy) (push) Has been cancelled
CI/CD Pipeline / Staging E2E Tests (push) Has been cancelled
CI/CD Pipeline / Staging API Integration Tests (push) Has been cancelled
CI/CD Pipeline / Build Production API Image (push) Has been cancelled
CI/CD Pipeline / Build Production Web Image (push) Has been cancelled
CI/CD Pipeline / Build Production Worker Image (push) Has been cancelled
CI/CD Pipeline / Deploy Production (push) Has been cancelled
CI/CD Pipeline / Production Browser E2E (push) Has been cancelled
CI/CD Pipeline / ACR Image Cleanup (push) Has been cancelled
97 lines
2.9 KiB
Python
Executable File
97 lines
2.9 KiB
Python
Executable File
"""AI分类任务 UseCase 单元测试."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from unittest.mock import MagicMock
|
|
|
|
import pytest
|
|
|
|
from packages.application.classification_jobs import (
|
|
SubmitClassificationJobCommand,
|
|
SubmitClassificationJobUseCase,
|
|
)
|
|
from packages.domain import ClassificationJob
|
|
|
|
|
|
@pytest.fixture
|
|
def mock_repo():
|
|
return MagicMock()
|
|
|
|
|
|
class TestSubmitClassificationJobUseCase:
|
|
"""SubmitClassificationJobUseCase 测试"""
|
|
|
|
def test_submit_job_success(self, mock_repo):
|
|
"""正常提交分类任务"""
|
|
mock_repo.create.side_effect = lambda j: j
|
|
use_case = SubmitClassificationJobUseCase(mock_repo)
|
|
|
|
command = SubmitClassificationJobCommand(
|
|
project_id="proj_001",
|
|
asset_id="asset_001",
|
|
)
|
|
result = use_case.execute(command)
|
|
|
|
assert isinstance(result, ClassificationJob)
|
|
assert result.project_id == "proj_001"
|
|
assert result.asset_id == "asset_001"
|
|
assert result.status == "pending"
|
|
assert result.confidence == 0.0
|
|
assert result.error_message == ""
|
|
mock_repo.create.assert_called_once()
|
|
|
|
def test_submit_job_generates_id(self, mock_repo):
|
|
"""提交任务时生成 id"""
|
|
mock_repo.create.side_effect = lambda j: j
|
|
use_case = SubmitClassificationJobUseCase(mock_repo)
|
|
|
|
command = SubmitClassificationJobCommand(
|
|
project_id="proj_001",
|
|
asset_id="asset_001",
|
|
)
|
|
result = use_case.execute(command)
|
|
|
|
assert result.id is not None
|
|
assert len(result.id) > 0
|
|
|
|
def test_submit_job_two_different_ids(self, mock_repo):
|
|
"""两次提交生成不同的 id"""
|
|
mock_repo.create.side_effect = lambda j: j
|
|
use_case = SubmitClassificationJobUseCase(mock_repo)
|
|
|
|
command = SubmitClassificationJobCommand(
|
|
project_id="proj_001",
|
|
asset_id="asset_001",
|
|
)
|
|
r1 = use_case.execute(command)
|
|
r2 = use_case.execute(command)
|
|
|
|
assert r1.id != r2.id
|
|
|
|
def test_submit_job_initial_classification_empty(self, mock_repo):
|
|
"""初始 classification 为空"""
|
|
mock_repo.create.side_effect = lambda j: j
|
|
use_case = SubmitClassificationJobUseCase(mock_repo)
|
|
|
|
command = SubmitClassificationJobCommand(
|
|
project_id="proj_001",
|
|
asset_id="asset_001",
|
|
)
|
|
result = use_case.execute(command)
|
|
|
|
assert result.classification == ""
|
|
|
|
def test_submit_job_returns_repo_result(self, mock_repo):
|
|
"""返回 repository.create 的结果"""
|
|
expected = MagicMock(spec=ClassificationJob)
|
|
mock_repo.create.return_value = expected
|
|
|
|
use_case = SubmitClassificationJobUseCase(mock_repo)
|
|
command = SubmitClassificationJobCommand(
|
|
project_id="proj_001",
|
|
asset_id="asset_001",
|
|
)
|
|
result = use_case.execute(command)
|
|
|
|
assert result is expected
|