File size: 3,302 Bytes
c62301e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
"""Tests for DS-09: Replace raw string fields with enums."""

from sqlalchemy import inspect

from app.core.database import SessionLocal, engine
from app.core.enums import ProjectStatus, UserRole
from app.core.models import Project, User


class TestUserRoleEnum:
    """User.role should accept only valid UserRole values."""

    def test_user_role_accepts_valid_enum(self) -> None:
        """Creating a User with a valid enum role should work."""
        user = User(
            google_id="enum-test-1",
            email="enum1@test.com",
            role=UserRole.ADMIN,
        )
        assert user.role is UserRole.ADMIN
        assert user.role.value == "admin"

    def test_user_role_defaults_in_db(self) -> None:
        """Default User.role should be UserRole.USER when persisted."""
        user = User(
            google_id="enum-test-2",
            email="enum2@test.com",
        )
        db = SessionLocal()
        try:
            db.add(user)
            db.flush()  # Apply defaults
            assert user.role == UserRole.USER
        finally:
            db.rollback()
            db.close()

    def test_user_role_column_type(self) -> None:
        """The DB column should be an Enum type (or String for SQLite compat)."""
        insp = inspect(engine)
        columns = {c["name"]: c for c in insp.get_columns("users")}
        role_col = columns["role"]
        assert role_col["type"] is not None

    def test_user_role_python_type_safety(self) -> None:
        """Type-annotated field ensures only UserRole values are expected."""
        user = User(
            google_id="enum-test-4",
            email="enum4@test.com",
            role=UserRole.USER,
        )
        assert isinstance(user.role, UserRole)
        # Setting to a non-enum via attribute is allowed at runtime (Python dynamic),
        # but the column type in the DB schema is now an enum type
        # (PostgreSQL will enforce; SQLite stores as text)


class TestProjectStatusEnum:
    """Project.status should accept only valid ProjectStatus values."""

    def test_project_status_accepts_valid_enum(self) -> None:
        """Creating a Project with a valid enum status should work."""
        project = Project(
            user_id=1,
            title="Test Project",
            artifacts={},
            status=ProjectStatus.ARCHIVED,
        )
        assert project.status is ProjectStatus.ARCHIVED
        assert project.status.value == "archived"

    def test_project_status_defaults_in_db(self) -> None:
        """Default Project.status should be ProjectStatus.DRAFT when persisted."""
        project = Project(
            user_id=1,
            title="Test Project",
            artifacts={},
        )
        db = SessionLocal()
        try:
            db.add(project)
            db.flush()
            assert project.status == ProjectStatus.DRAFT
        finally:
            db.rollback()
            db.close()

    def test_project_status_python_type_safety(self) -> None:
        """Type-annotated field ensures only ProjectStatus values are expected."""
        project = Project(
            user_id=1,
            title="Test",
            artifacts={},
            status=ProjectStatus.ACTIVE,
        )
        assert isinstance(project.status, ProjectStatus)