Files
xiaoxia-saas/tests/unit/test_classification_domain.py
T
CI Bot 5b967cf74d
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
CI/CD Pipeline / Canary Release to Production (push) Has been cancelled
style: auto-format with black + isort + prettier
2026-07-25 13:59:08 +00:00

210 lines
7.5 KiB
Python
Executable File

"""分类领域模型单元测试 - 纯逻辑部分。"""
from __future__ import annotations
import pytest
from domain.classification import (
AssetClassification,
AssetLibraryKind,
ClassificationJob,
ClassificationJobStatus,
ClassificationStatus,
IngestJobStatus,
)
class TestAssetLibraryKind:
"""素材库类型枚举。"""
def test_video_value(self):
assert AssetLibraryKind.VIDEO == "video"
def test_voice_value(self):
assert AssetLibraryKind.VOICE == "voice"
def test_image_value(self):
assert AssetLibraryKind.IMAGE == "image"
def test_is_str_enum(self):
assert isinstance(AssetLibraryKind.VIDEO, str)
assert AssetLibraryKind.VIDEO + "_test" == "video_test"
def test_members_count(self):
assert len(AssetLibraryKind) == 3
class TestIngestJobStatus:
"""导入任务状态枚举。"""
def test_pending_value(self):
assert IngestJobStatus.PENDING == "pending"
def test_processing_value(self):
assert IngestJobStatus.PROCESSING == "processing"
def test_completed_value(self):
assert IngestJobStatus.COMPLETED == "completed"
def test_failed_value(self):
assert IngestJobStatus.FAILED == "failed"
def test_members_count(self):
assert len(IngestJobStatus) == 4
class TestClassificationJobStatusMissing:
"""ClassificationJobStatus._missing_ 兼容性测试。"""
def test_standard_values(self):
"""标准值正常解析。"""
assert ClassificationJobStatus("pending") == ClassificationJobStatus.PENDING
assert ClassificationJobStatus("processing") == ClassificationJobStatus.PROCESSING
assert ClassificationJobStatus("completed") == ClassificationJobStatus.COMPLETED
assert ClassificationJobStatus("failed") == ClassificationJobStatus.FAILED
def test_done_maps_to_completed(self):
"""历史值 done 映射到 COMPLETED。"""
assert ClassificationJobStatus("done") == ClassificationJobStatus.COMPLETED
def test_success_maps_to_completed(self):
assert ClassificationJobStatus("success") == ClassificationJobStatus.COMPLETED
def test_finished_maps_to_completed(self):
assert ClassificationJobStatus("finished") == ClassificationJobStatus.COMPLETED
def test_complete_maps_to_completed(self):
assert ClassificationJobStatus("complete") == ClassificationJobStatus.COMPLETED
def test_fail_maps_to_failed(self):
assert ClassificationJobStatus("fail") == ClassificationJobStatus.FAILED
def test_error_maps_to_failed(self):
assert ClassificationJobStatus("error") == ClassificationJobStatus.FAILED
def test_err_maps_to_failed(self):
assert ClassificationJobStatus("err") == ClassificationJobStatus.FAILED
def test_process_maps_to_processing(self):
assert ClassificationJobStatus("process") == ClassificationJobStatus.PROCESSING
def test_running_maps_to_processing(self):
assert ClassificationJobStatus("running") == ClassificationJobStatus.PROCESSING
def test_run_maps_to_processing(self):
assert ClassificationJobStatus("run") == ClassificationJobStatus.PROCESSING
def test_unknown_value_defaults_to_pending(self):
"""未知值兜底为 PENDING。"""
assert ClassificationJobStatus("unknown") == ClassificationJobStatus.PENDING
assert ClassificationJobStatus("whatever") == ClassificationJobStatus.PENDING
assert ClassificationJobStatus("") == ClassificationJobStatus.PENDING
def test_case_insensitive(self):
"""大小写不敏感。"""
assert ClassificationJobStatus("DONE") == ClassificationJobStatus.COMPLETED
assert ClassificationJobStatus("Done") == ClassificationJobStatus.COMPLETED
assert ClassificationJobStatus("FAIL") == ClassificationJobStatus.FAILED
assert ClassificationJobStatus("Error") == ClassificationJobStatus.FAILED
def test_stripped(self):
"""前后空白字符被忽略。"""
assert ClassificationJobStatus(" done ") == ClassificationJobStatus.COMPLETED
assert ClassificationJobStatus("\tfail\n") == ClassificationJobStatus.FAILED
def test_none_returns_pending(self):
"""None 值也返回 PENDING(不报错)。"""
assert ClassificationJobStatus(None) == ClassificationJobStatus.PENDING
def test_integer_returns_pending(self):
"""非字符串值返回 PENDING。"""
assert ClassificationJobStatus(123) == ClassificationJobStatus.PENDING
class TestClassificationStatusAlias:
"""向后兼容别名。"""
def test_alias_same_class(self):
assert ClassificationStatus is ClassificationJobStatus
def test_alias_values_same(self):
assert ClassificationStatus.PENDING == ClassificationJobStatus.PENDING
assert ClassificationStatus.COMPLETED == ClassificationJobStatus.COMPLETED
class TestAssetClassification:
"""素材分类枚举。"""
def test_scenic(self):
assert AssetClassification.SCENIC == "scenic"
def test_product(self):
assert AssetClassification.PRODUCT == "product"
def test_person(self):
assert AssetClassification.PERSON == "person"
def test_animal(self):
assert AssetClassification.ANIMAL == "animal"
def test_food(self):
assert AssetClassification.FOOD == "food"
def test_tech(self):
assert AssetClassification.TECH == "tech"
def test_sport(self):
assert AssetClassification.SPORT == "sport"
def test_music(self):
assert AssetClassification.MUSIC == "music"
def test_other(self):
assert AssetClassification.OTHER == "other"
def test_members_count(self):
assert len(AssetClassification) == 9
class TestClassificationJobCreate:
"""ClassificationJob.create 工厂方法。"""
def test_create_basic(self):
job = ClassificationJob.create(project_id="proj-1", asset_id="asset-1")
assert job.project_id == "proj-1"
assert job.asset_id == "asset-1"
assert job.status == ClassificationJobStatus.PENDING
assert job.classification == ""
assert job.confidence == 0.0
assert job.error_message == ""
assert job.id # 自动生成的 ID 非空
def test_create_strips_whitespace(self):
job = ClassificationJob.create(project_id=" proj-1 ", asset_id="\tasset-1\n")
assert job.project_id == "proj-1"
assert job.asset_id == "asset-1"
def test_create_empty_project_id_raises(self):
with pytest.raises(ValueError, match="project_id 不能为空"):
ClassificationJob.create(project_id="", asset_id="asset-1")
def test_create_whitespace_project_id_raises(self):
with pytest.raises(ValueError, match="project_id 不能为空"):
ClassificationJob.create(project_id=" ", asset_id="asset-1")
def test_create_empty_asset_id_raises(self):
with pytest.raises(ValueError, match="asset_id 不能为空"):
ClassificationJob.create(project_id="proj-1", asset_id="")
def test_create_whitespace_asset_id_raises(self):
with pytest.raises(ValueError, match="asset_id 不能为空"):
ClassificationJob.create(project_id="proj-1", asset_id=" \t ")
def test_create_generates_unique_ids(self):
job1 = ClassificationJob.create(project_id="p", asset_id="a")
job2 = ClassificationJob.create(project_id="p", asset_id="a")
assert job1.id != job2.id
def test_create_id_is_hex(self):
job = ClassificationJob.create(project_id="p", asset_id="a")
int(job.id, 16) # 不抛异常就是合法 hex