import pytest from fastapi.testclient import TestClient from jira_webhook_llm import create_app, app # Assuming app is created via factory from database.database import engine, Base # Add import 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", "projectKey": "PROJ", "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 def test_client(setup_db): # Ensure the database is set up before creating the app test_engine = setup_db # Import and patch the database module to use test database from database import database as db db.engine = test_engine db.SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=test_engine) # Create fresh app instance with test DB from jira_webhook_llm import create_app app = create_app() return TestClient(app) @pytest.fixture def mock_jira_payload(): return { "projectKey": "TEST-PROJECT", "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" # }