arch-design-agent-skill-das.../backend/tests/test_project.py
openclaw ab3dd6da1c feat(project): add ProjectService with CRUD and path validation
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-23 16:05:23 +00:00

84 lines
2.6 KiB
Python

import json
from pathlib import Path
import pytest
from app.modules.project.domain.entities import Project
from app.modules.project.infrastructure.json_repository import JsonProjectRepository
@pytest.fixture
def repo(tmp_path: Path) -> JsonProjectRepository:
return JsonProjectRepository(tmp_path / "projects.json")
def test_empty_repo_returns_empty_list(repo: JsonProjectRepository):
assert repo.list_all() == []
def test_save_and_get(repo: JsonProjectRepository):
from datetime import datetime
p = Project(id="id1", name="test", design_dir="/tmp/d", code_dir=None, created_at=datetime(2026, 1, 1))
repo.save(p)
assert repo.get_by_id("id1") is not None
assert repo.get_by_id("id1").name == "test"
def test_list_all(repo: JsonProjectRepository):
from datetime import datetime
p1 = Project(id="id1", name="a", design_dir="/d1", code_dir=None, created_at=datetime(2026, 1, 1))
p2 = Project(id="id2", name="b", design_dir="/d2", code_dir=None, created_at=datetime(2026, 1, 2))
repo.save(p1)
repo.save(p2)
assert len(repo.list_all()) == 2
def test_delete(repo: JsonProjectRepository):
from datetime import datetime
p = Project(id="id1", name="test", design_dir="/d", code_dir=None, created_at=datetime(2026, 1, 1))
repo.save(p)
repo.delete("id1")
assert repo.get_by_id("id1") is None
def test_get_nonexistent_returns_none(repo: JsonProjectRepository):
assert repo.get_by_id("nope") is None
# --- Service tests ---
from app.modules.project.application.services import ProjectService
from app.shared.kernel.exceptions import NotFoundError, ValidationError
@pytest.fixture
def service(tmp_path: Path) -> ProjectService:
repo = JsonProjectRepository(tmp_path / "projects.json")
return ProjectService(repo)
def test_create_project_validates_design_dir(service: ProjectService, tmp_path: Path):
design_dir = tmp_path / "design"
design_dir.mkdir()
project = service.create_project("test", str(design_dir))
assert project.name == "test"
assert project.id # UUID generated
def test_create_project_rejects_missing_dir(service: ProjectService):
with pytest.raises(ValidationError):
service.create_project("test", "/nonexistent/path")
def test_get_project_not_found(service: ProjectService):
with pytest.raises(NotFoundError):
service.get_project("nonexistent")
def test_delete_project(service: ProjectService, tmp_path: Path):
design_dir = tmp_path / "design"
design_dir.mkdir()
p = service.create_project("test", str(design_dir))
service.delete_project(p.id)
with pytest.raises(NotFoundError):
service.get_project(p.id)