Skip to content

Commit aa18ab7

Browse files
committed
fix: fixed the pytest and mock test env
1 parent eec04e3 commit aa18ab7

5 files changed

Lines changed: 370 additions & 335 deletions

File tree

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,3 +174,7 @@ book/
174174

175175
node_modules*
176176
exports/
177+
test.db-shm
178+
test.db-wal
179+
test_export.db-journal
180+
*.db-journal

tests/conftest.py

Lines changed: 90 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,97 @@
1+
import os
12
import pytest
23
from fastapi.testclient import TestClient
4+
from unittest.mock import Mock, patch
5+
from sqlalchemy import create_engine
6+
from sqlalchemy.orm import sessionmaker
37

8+
# Set test environment variables before importing the app
9+
os.environ["DATABASE_URL"] = "sqlite:///test.db"
10+
os.environ["REDIS_URL"] = "redis://localhost:6379/1"
11+
os.environ["USE_CELERY"] = "false"
12+
os.environ["ENABLE_CACHING"] = "false"
13+
os.environ["DEBUG"] = "true"
14+
15+
# Import app after environment variables are set
416
from app.main import app
17+
from app.db.database import Base, get_db
18+
19+
20+
@pytest.fixture(scope="session")
21+
def test_engine():
22+
"""Create test database engine and tables"""
23+
engine = create_engine("sqlite:///test.db", connect_args={"check_same_thread": False})
24+
25+
# Create all tables
26+
Base.metadata.create_all(bind=engine)
27+
28+
yield engine
29+
30+
# Clean up - drop all tables after tests
31+
Base.metadata.drop_all(bind=engine)
32+
33+
34+
@pytest.fixture
35+
def db_session(test_engine):
36+
"""Create a database session for testing"""
37+
TestingSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=test_engine)
38+
session = TestingSessionLocal()
39+
40+
try:
41+
yield session
42+
finally:
43+
session.rollback()
44+
session.close()
45+
46+
47+
@pytest.fixture
48+
def client(db_session):
49+
"""Create a test client for the FastAPI app with mocked dependencies"""
50+
def override_get_db():
51+
try:
52+
yield db_session
53+
finally:
54+
pass
55+
56+
app.dependency_overrides[get_db] = override_get_db
57+
58+
# Mock Redis and other external services
59+
with patch('app.core.redis_client.RedisClient') as mock_redis_class:
60+
mock_redis_instance = Mock()
61+
mock_redis_instance.ping.return_value = True
62+
mock_redis_instance.get.return_value = None
63+
mock_redis_instance.set.return_value = True
64+
mock_redis_instance.incr.return_value = 1
65+
mock_redis_instance.expire.return_value = True
66+
mock_redis_instance.lpush.return_value = 1
67+
mock_redis_instance.ltrim.return_value = True
68+
mock_redis_instance.lrange.return_value = []
69+
mock_redis_instance.hset.return_value = True
70+
mock_redis_instance.hgetall.return_value = {}
71+
mock_redis_instance.llen.return_value = 0
72+
mock_redis_class.return_value = mock_redis_instance
73+
74+
with patch('app.core.cache.cache_manager') as mock_cache:
75+
mock_cache.redis = mock_redis_instance
76+
77+
with patch('app.db.database.get_connection_pool_status') as mock_pool_status:
78+
mock_pool_status.return_value = {
79+
"pool_size": 20,
80+
"checked_in": 15,
81+
"pool_status": "healthy"
82+
}
83+
84+
with TestClient(app) as test_client:
85+
yield test_client
86+
87+
# Clean up dependency override
88+
app.dependency_overrides.clear()
589

690

791
@pytest.fixture
8-
def client():
9-
"""Create a test client for the FastAPI app"""
10-
with TestClient(app) as test_client:
11-
yield test_client
92+
def mock_db():
93+
"""Mock database session for testing"""
94+
with patch('app.db.database.get_db') as mock_get_db:
95+
mock_session = Mock()
96+
mock_get_db.return_value = mock_session
97+
yield mock_session

0 commit comments

Comments
 (0)