| import pytest |
| import asyncio |
| from typing import Generator, Any |
| from fastapi import FastAPI |
| from fastapi.testclient import TestClient |
| from sqlalchemy import create_engine |
| from sqlalchemy.orm import sessionmaker |
| from app import app as main_app |
| from services.database_service import DatabaseService |
| import os |
| import tempfile |
| import base64 |
| from PIL import Image |
| import io |
|
|
| |
| TEST_DATABASE_URL = "sqlite:///./test.db" |
|
|
| @pytest.fixture(scope="session") |
| def app() -> Generator[FastAPI, Any, None]: |
| """ |
| Create a FastAPI test app |
| """ |
| yield main_app |
|
|
| @pytest.fixture(scope="session") |
| def client(app: FastAPI) -> Generator[TestClient, Any, None]: |
| """ |
| Create a test client |
| """ |
| with TestClient(app) as test_client: |
| yield test_client |
|
|
| @pytest.fixture(scope="function") |
| def db_service(): |
| """ |
| Create a database service for testing |
| """ |
| service = DatabaseService() |
| |
| service.supabase = None |
| yield service |
|
|
| @pytest.fixture(scope="function") |
| def auth_headers(): |
| """ |
| Create authentication headers for testing |
| """ |
| return {"Authorization": "Bearer test_token"} |
|
|
| @pytest.fixture(scope="function") |
| def sample_image(): |
| """ |
| Create a sample image for testing |
| """ |
| |
| img = Image.new('RGB', (100, 100), color='red') |
| img_byte_arr = io.BytesIO() |
| img.save(img_byte_arr, format='JPEG') |
| img_byte_arr = img_byte_arr.getvalue() |
| |
| return base64.b64encode(img_byte_arr).decode('utf-8') |
|
|
| @pytest.fixture(scope="function") |
| def sample_skin_tone(): |
| """ |
| Sample skin tone data |
| """ |
| return { |
| "primary_tone": "medium", |
| "undertone": "warm", |
| "melanin_level": 45.5, |
| "rgb_values": { |
| "r": 180, |
| "g": 120, |
| "b": 80, |
| "hex": "B47850" |
| } |
| } |
|
|
| @pytest.fixture(scope="function") |
| def sample_conditions(): |
| """ |
| Sample skin conditions |
| """ |
| return [ |
| {"condition": "blackheads", "severity": "mild", "confidence": 85.5}, |
| {"condition": "oily_skin", "severity": "moderate", "confidence": 92.0} |
| ] |
|
|
| @pytest.fixture(scope="function") |
| def sample_user(): |
| """ |
| Sample user data |
| """ |
| return { |
| "id": "test_user_123", |
| "email": "test@example.com", |
| "name": "Test User", |
| "created_at": "2024-01-01T00:00:00Z" |
| } |
|
|
| @pytest.fixture(scope="function") |
| def event_loop(): |
| """ |
| Create an event loop for async tests |
| """ |
| loop = asyncio.get_event_loop_policy().new_event_loop() |
| yield loop |
| loop.close() |