1
1
import pytest
2
+ from typing import Generator
2
3
from dotenv import load_dotenv
3
4
from sqlmodel import create_engine , Session , select
4
5
from sqlalchemy import Engine
14
15
# Define a custom exception for test setup errors
15
16
class SetupError (Exception ):
16
17
"""Exception raised for errors in the test setup process."""
17
- pass
18
+ def __init__ (self , message = "An error occurred during test setup" ):
19
+ self .message = message
20
+ super ().__init__ (self .message )
18
21
19
22
20
23
@pytest .fixture (scope = "session" )
@@ -30,7 +33,7 @@ def engine() -> Engine:
30
33
31
34
32
35
@pytest .fixture (scope = "session" , autouse = True )
33
- def set_up_database (engine ):
36
+ def set_up_database (engine ) -> Generator [ None , None , None ] :
34
37
"""
35
38
Set up the test database before running the test suite.
36
39
Drop all tables and recreate them to ensure a clean state.
@@ -41,7 +44,7 @@ def set_up_database(engine):
41
44
42
45
43
46
@pytest .fixture
44
- def session (engine ):
47
+ def session (engine ) -> Generator [ Session , None , None ] :
45
48
"""
46
49
Provide a session for database operations in tests.
47
50
"""
@@ -50,7 +53,7 @@ def session(engine):
50
53
51
54
52
55
@pytest .fixture (autouse = True )
53
- def clean_db (session : Session ):
56
+ def clean_db (session : Session ) -> None :
54
57
"""
55
58
Cleans up the database tables before each test.
56
59
"""
@@ -61,9 +64,8 @@ def clean_db(session: Session):
61
64
session .commit ()
62
65
63
66
64
- # Test user fixture
65
67
@pytest .fixture ()
66
- def test_user (session : Session ):
68
+ def test_user (session : Session ) -> User :
67
69
"""
68
70
Creates a test user in the database.
69
71
"""
@@ -78,9 +80,8 @@ def test_user(session: Session):
78
80
return user
79
81
80
82
81
- # Unauthenticated client fixture
82
83
@pytest .fixture ()
83
- def unauth_client (session : Session ):
84
+ def unauth_client (session : Session ) -> Generator [ TestClient , None , None ] :
84
85
"""
85
86
Provides a TestClient instance without authentication.
86
87
"""
@@ -93,9 +94,8 @@ def get_session_override():
93
94
app .dependency_overrides .clear ()
94
95
95
96
96
- # Authenticated client fixture
97
97
@pytest .fixture ()
98
- def auth_client (session : Session , test_user : User ):
98
+ def auth_client (session : Session , test_user : User ) -> Generator [ TestClient , None , None ] :
99
99
"""
100
100
Provides a TestClient instance with valid authentication tokens.
101
101
"""
@@ -117,7 +117,7 @@ def get_session_override():
117
117
118
118
119
119
@pytest .fixture
120
- def test_organization (session : Session ):
120
+ def test_organization (session : Session ) -> Organization :
121
121
"""Create a test organization for use in tests"""
122
122
organization = Organization (name = "Test Organization" )
123
123
session .add (organization )
0 commit comments