ai-legal-assistant/backend/tests/unit/test_contract_service.py
root ae72b180e5 fix: improve error handling and validation
- Add centralized exception classes
- Validate signature request status before signing
- Check contract status before approval/rejection
- Add exception handlers to FastAPI app
- Update tests for new validation logic

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-01 03:38:20 +08:00

104 lines
3.1 KiB
Python

"""Unit tests for Contract service."""
import pytest
from datetime import date
from sqlalchemy.ext.asyncio import AsyncSession
from app.models.contract import ContractStatus
from app.services.contract_service import ContractService, ContractTemplateService
class TestContractService:
"""Test cases for Contract service."""
@pytest.mark.asyncio
async def test_create_contract(self, db_session: AsyncSession):
"""Test creating a contract."""
service = ContractService(db_session)
contract = await service.create_contract(
title="测试合同",
party_a="甲方",
party_b="乙方",
content="合同内容",
created_by=1,
)
assert contract.id is not None
assert contract.title == "测试合同"
assert contract.status == ContractStatus.DRAFT
@pytest.mark.asyncio
async def test_get_contract_by_id(self, db_session: AsyncSession):
"""Test getting a contract by ID."""
service = ContractService(db_session)
created = await service.create_contract(
title="测试合同",
party_a="甲方",
party_b="乙方",
content="合同内容",
created_by=1,
)
contract = await service.get_contract_by_id(created.id)
assert contract is not None
assert contract.title == "测试合同"
@pytest.mark.asyncio
async def test_submit_for_approval(self, db_session: AsyncSession):
"""Test submitting a contract for approval."""
service = ContractService(db_session)
contract = await service.create_contract(
title="测试合同",
party_a="甲方",
party_b="乙方",
content="合同内容",
created_by=1,
)
updated = await service.submit_for_approval(contract.id)
assert updated.status == ContractStatus.PENDING_APPROVAL
@pytest.mark.asyncio
async def test_approve_contract(self, db_session: AsyncSession):
"""Test approving a contract."""
service = ContractService(db_session)
contract = await service.create_contract(
title="测试合同",
party_a="甲方",
party_b="乙方",
content="合同内容",
created_by=1,
)
# Submit for approval first
await service.submit_for_approval(contract.id)
approval = await service.approve_contract(contract.id, 1, "同意")
assert approval.status == ContractStatus.APPROVED
class TestContractTemplateService:
"""Test cases for ContractTemplate service."""
@pytest.mark.asyncio
async def test_create_template(self, db_session: AsyncSession):
"""Test creating a contract template."""
service = ContractTemplateService(db_session)
template = await service.create_template(
name="测试模板",
content="模板内容",
created_by=1,
contract_type="销售合同",
)
assert template.id is not None
assert template.name == "测试模板"