"""Shared fixtures for API handler tests.""" import sys from pathlib import Path from unittest.mock import MagicMock import pytest from flask import Flask # Add apps directory to path for imports apps_dir = Path(__file__).parent.parent sys.path.insert(0, str(apps_dir)) @pytest.fixture def flask_app(): """Create Flask app context for testing.""" app = Flask(__name__) app.config["TESTING"] = True return app @pytest.fixture def app_context(flask_app): """Provide Flask application context.""" with flask_app.app_context(): yield flask_app @pytest.fixture def request_context(flask_app): """Provide Flask request context.""" with flask_app.test_request_context(): yield flask_app @pytest.fixture def mock_db_connection(mocker): """Mock database connection with cursor that has description attribute.""" mock_cursor = MagicMock() mock_cursor.description = [ MagicMock(name="id"), MagicMock(name="name"), MagicMock(name="dob"), MagicMock(name="email"), MagicMock(name="gender"), MagicMock(name="created"), MagicMock(name="modified"), ] # Set name attribute on description items for i, col_name in enumerate(["id", "name", "dob", "email", "gender", "created", "modified"]): mock_cursor.description[i].name = col_name mock_conn = MagicMock() mock_conn.cursor.return_value.__enter__ = MagicMock(return_value=mock_cursor) mock_conn.cursor.return_value.__exit__ = MagicMock(return_value=False) mocker.patch("helpers.init_db_connection", return_value=mock_conn) return {"connection": mock_conn, "cursor": mock_cursor} @pytest.fixture def mock_secrets(mocker): """Mock get_secret to return test values.""" secrets = { "PG_HOST": "localhost", "PG_PORT": "5432", "PG_DB": "test_db", "PG_USER": "test_user", "PG_PASS": "test_pass", } mocker.patch("helpers.get_secret", side_effect=lambda key, default=None: secrets.get(key, default)) return secrets @pytest.fixture def sample_user_data(): """Sample user data for testing.""" return { "name": "Test User", "email": "test@example.com", "dob": "1990-01-15", "gender": "male", } @pytest.fixture def sample_db_row(): """Sample database row tuple.""" return ( "550e8400-e29b-41d4-a716-446655440000", # id "Test User", # name "1990-01-15", # dob "test@example.com", # email "male", # gender "2024-01-01T10:00:00", # created "2024-01-01T10:00:00", # modified )