ai-legal-assistant/backend/tests/unit/test_law_service.py
root 656f596d7e feat: implement AI legal assistant system MVP
Core modules:
- Laws: CRUD, search, AI-powered QA
- Analysis: legal research and case management
- Contracts: lifecycle management with templates
- Signatures: electronic signature workflow

Infrastructure:
- FastAPI + SQLite + async SQLAlchemy
- Docker deployment support
- 54 unit tests passing

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

145 lines
4.4 KiB
Python

"""Unit tests for Law service."""
import pytest
from datetime import date
from unittest.mock import AsyncMock, patch, MagicMock
from sqlalchemy.ext.asyncio import AsyncSession
from app.models.law import Law, LawArticle, LawType, LawStatus
from app.services.law_service import LawService
class TestLawService:
"""Test cases for Law service."""
@pytest.mark.asyncio
async def test_create_law(self, db_session: AsyncSession):
"""Test creating a law."""
service = LawService(db_session)
law = await service.create_law(
title="测试法律",
law_type=LawType.LAW,
promulgation_date=date(2024, 1, 1),
effective_date=date(2024, 2, 1),
issuing_authority="测试机关",
content="测试内容",
)
assert law.id is not None
assert law.title == "测试法律"
assert law.law_type == LawType.LAW
@pytest.mark.asyncio
async def test_get_law_by_id(self, db_session: AsyncSession):
"""Test getting a law by ID."""
service = LawService(db_session)
# Create a law first
created = await service.create_law(
title="测试法律",
law_type=LawType.LAW,
promulgation_date=date(2024, 1, 1),
effective_date=date(2024, 2, 1),
issuing_authority="测试机关",
content="测试内容",
)
# Get the law
law = await service.get_law_by_id(created.id)
assert law is not None
assert law.title == "测试法律"
@pytest.mark.asyncio
async def test_get_laws_list(self, db_session: AsyncSession):
"""Test getting list of laws."""
service = LawService(db_session)
# Create multiple laws
for i in range(3):
await service.create_law(
title=f"测试法律{i}",
law_type=LawType.LAW,
promulgation_date=date(2024, 1, 1),
effective_date=date(2024, 2, 1),
issuing_authority="测试机关",
content=f"测试内容{i}",
)
laws = await service.get_laws_list(skip=0, limit=10)
assert len(laws) == 3
@pytest.mark.asyncio
async def test_update_law(self, db_session: AsyncSession):
"""Test updating a law."""
service = LawService(db_session)
# Create a law
law = await service.create_law(
title="原标题",
law_type=LawType.LAW,
promulgation_date=date(2024, 1, 1),
effective_date=date(2024, 2, 1),
issuing_authority="测试机关",
content="原内容",
)
# Update the law
updated = await service.update_law(law.id, title="新标题")
assert updated.title == "新标题"
@pytest.mark.asyncio
async def test_delete_law(self, db_session: AsyncSession):
"""Test deleting a law."""
service = LawService(db_session)
# Create a law
law = await service.create_law(
title="待删除法律",
law_type=LawType.LAW,
promulgation_date=date(2024, 1, 1),
effective_date=date(2024, 2, 1),
issuing_authority="测试机关",
content="测试内容",
)
# Delete the law
result = await service.delete_law(law.id)
assert result is True
# Verify it's deleted
deleted = await service.get_law_by_id(law.id)
assert deleted is None
@pytest.mark.asyncio
async def test_search_laws_by_keyword(self, db_session: AsyncSession):
"""Test searching laws by keyword."""
service = LawService(db_session)
# Create laws with different content
await service.create_law(
title="民法典",
law_type=LawType.LAW,
promulgation_date=date(2024, 1, 1),
effective_date=date(2024, 2, 1),
issuing_authority="全国人大",
content="民法典全文内容",
)
await service.create_law(
title="刑法",
law_type=LawType.LAW,
promulgation_date=date(2024, 1, 1),
effective_date=date(2024, 2, 1),
issuing_authority="全国人大",
content="刑法全文内容",
)
results = await service.search_laws_by_keyword("民法")
assert len(results) >= 1
assert any("民法典" in law.title for law in results)