import pytest from fastapi.testclient import TestClient from jira_webhook_llm import create_app from database.database import get_db_session, Base # Import get_db_session and Base from sqlalchemy.orm import sessionmaker import os from sqlalchemy import create_engine @pytest.fixture(scope="function") def setup_db(monkeypatch): # Use in-memory SQLite for tests test_db_url = "sqlite:///:memory:" monkeypatch.setenv("DATABASE_URL", test_db_url) from database import database as db from database.models import Base # Import Base from models test_engine = create_engine(test_db_url, connect_args={"check_same_thread": False}) # Update the global engine and SessionLocal db.engine = test_engine db.SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=test_engine) # Create all tables Base.metadata.create_all(bind=test_engine) yield test_engine # Cleanup Base.metadata.drop_all(bind=test_engine) @pytest.fixture def mock_full_jira_payload(setup_db): mock_data = { "issueKey": "PROJ-123", "summary": "Test Issue", "description": "Test Description", "comment": "Test Comment", "labels": ["test"], "status": "open", "assignee": "Tester", "updated": "2025-07-13T12:00:00Z", "payloadData": {"key1": "value1"} } return mock_data @pytest.fixture(scope="function") def test_client(setup_db, monkeypatch): # Prevent signal handling in tests to avoid "set_wakeup_fd" error monkeypatch.setattr("jira_webhook_llm.handle_shutdown_signal", lambda *args: None) # Create a test app instance app = create_app() # Override the get_db_session dependency to use the test database def override_get_db_session(): with setup_db.connect() as connection: with sessionmaker(autocommit=False, autoflush=False, bind=connection)() as session: yield session app.dependency_overrides[get_db_session] = override_get_db_session with TestClient(app) as client: yield client # Clean up dependency override app.dependency_overrides.clear() @pytest.fixture def mock_jira_payload(): return { "issueKey": "TEST-123", "summary": "Test Issue", "description": "Test Description", "comment": "Test Comment", "labels": ["test"], "status": "Open", "assignee": "Tester", "updated": "2025-07-13T12:00:00Z" } # return { # "issueKey": "TEST-123", # "summary": "Test Issue", # "description": "Test Description", # "comment": "Test Comment", # "labels": ["test"], # "status": "Open", # "assignee": "Tester", # "updated": "2025-07-13T12:00:00Z" # }