Cleanup test database unit tests
All checks were successful
AWS Deploy on Push / build (push) Successful in 1m8s
All checks were successful
AWS Deploy on Push / build (push) Successful in 1m8s
This commit is contained in:
@@ -57,59 +57,33 @@ class MockChannelURL(MockBase):
|
||||
updated_at = Column(DateTime, default=lambda: datetime.now(timezone.utc), onupdate=lambda: datetime.now(timezone.utc))
|
||||
|
||||
# Create test engine
|
||||
TEST_ENGINE = create_engine(
|
||||
engine_mock = create_engine(
|
||||
"sqlite:///:memory:",
|
||||
connect_args={"check_same_thread": False},
|
||||
poolclass=StaticPool
|
||||
)
|
||||
|
||||
# Create test session
|
||||
TEST_SESSION = sessionmaker(autocommit=False, autoflush=False, bind=TEST_ENGINE)
|
||||
session_mock = sessionmaker(autocommit=False, autoflush=False, bind=engine_mock)
|
||||
|
||||
# Mock the actual database functions
|
||||
def mock_get_db():
|
||||
db = TEST_SESSION()
|
||||
db = session_mock()
|
||||
try:
|
||||
yield db
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
@pytest.fixture(scope="session", autouse=True)
|
||||
def create_test_tables():
|
||||
"""Create test database tables for all tests"""
|
||||
MockBase.metadata.create_all(bind=TEST_ENGINE)
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def mock_db_engine(monkeypatch):
|
||||
"""Fixture to mock database engine and session for each test"""
|
||||
# First mock get_db_credentials to prevent real connection attempts
|
||||
def mock_credentials():
|
||||
return "sqlite:///:memory:"
|
||||
monkeypatch.setattr('app.utils.database.get_db_credentials', mock_credentials)
|
||||
|
||||
# Then patch the actual database functions
|
||||
monkeypatch.setattr('app.utils.database.engine', TEST_ENGINE)
|
||||
monkeypatch.setattr('app.utils.database.SessionLocal', TEST_SESSION)
|
||||
monkeypatch.setattr('app.utils.database.get_db', mock_get_db)
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def mock_env(monkeypatch):
|
||||
"""Fixture for mocking environment variables"""
|
||||
# Clear any existing env vars first
|
||||
monkeypatch.delenv("MOCK_AUTH", raising=False)
|
||||
monkeypatch.delenv("DB_USER", raising=False)
|
||||
monkeypatch.delenv("DB_PASSWORD", raising=False)
|
||||
monkeypatch.delenv("DB_HOST", raising=False)
|
||||
monkeypatch.delenv("DB_NAME", raising=False)
|
||||
|
||||
# Set mock values
|
||||
monkeypatch.setenv("MOCK_AUTH", "true")
|
||||
monkeypatch.setenv("DB_USER", "testuser")
|
||||
monkeypatch.setenv("DB_PASSWORD", "testpass")
|
||||
monkeypatch.setenv("DB_HOST", "localhost")
|
||||
monkeypatch.setenv("DB_NAME", "testdb")
|
||||
monkeypatch.setenv("AWS_REGION", "us-east-1") # Mock AWS region
|
||||
|
||||
monkeypatch.setenv("AWS_REGION", "us-east-1")
|
||||
|
||||
@pytest.fixture
|
||||
def mock_ssm():
|
||||
"""Fixture for mocking boto3 SSM client"""
|
||||
@@ -135,7 +109,7 @@ def test_get_db_credentials_ssm(mock_ssm):
|
||||
|
||||
def test_session_creation():
|
||||
"""Test database session creation"""
|
||||
session = TEST_SESSION()
|
||||
session = session_mock()
|
||||
assert isinstance(session, Session)
|
||||
session.close()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user