Download backend/tests/conftest.py from Jessedbrown/schedule-manager: direct link, hf CLI and curl.
- Browser
- Download file 1.9 kB
-
https://huggingface.co/Jessedbrown/schedule-manager/resolve/main/backend/tests/conftest.py
- Command line
-
hf download hf://Jessedbrown/schedule-manager/backend/tests/conftest.py
-
curl -L -o conftest.py https://huggingface.co/Jessedbrown/schedule-manager/resolve/main/backend/tests/conftest.py
1.9 kB
| """Pytest fixtures for testing.""" | |
| import pytest | |
| from fastapi.testclient import TestClient | |
| from sqlalchemy import create_engine | |
| from sqlalchemy.orm import sessionmaker | |
| from sqlalchemy.pool import StaticPool | |
| from app.db import Base, get_db | |
| from app.main import app | |
| from app.models import Instructor, Room, Student | |
| # Use in-memory SQLite for tests | |
| SQLALCHEMY_DATABASE_URL = "sqlite://" | |
| engine = create_engine( | |
| SQLALCHEMY_DATABASE_URL, | |
| connect_args={"check_same_thread": False}, | |
| poolclass=StaticPool, | |
| ) | |
| TestingSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) | |
| def db(): | |
| """Create a fresh database for each test.""" | |
| Base.metadata.create_all(bind=engine) | |
| db = TestingSessionLocal() | |
| try: | |
| yield db | |
| finally: | |
| db.close() | |
| Base.metadata.drop_all(bind=engine) | |
| def client(db): | |
| """Create a test client with the test database.""" | |
| def override_get_db(): | |
| try: | |
| yield db | |
| finally: | |
| pass | |
| app.dependency_overrides[get_db] = override_get_db | |
| with TestClient(app) as test_client: | |
| yield test_client | |
| app.dependency_overrides.clear() | |
| def seed_data(db): | |
| """Seed basic data for testing.""" | |
| # Create rooms | |
| rooms = [ | |
| Room(name="Room A", capacity=30), | |
| Room(name="Room B", capacity=20), | |
| Room(name="Small Room", capacity=5), | |
| ] | |
| db.add_all(rooms) | |
| # Create instructors | |
| instructors = [ | |
| Instructor(name="Dr. Smith"), | |
| Instructor(name="Prof. Jones"), | |
| ] | |
| db.add_all(instructors) | |
| # Create students | |
| students = [ | |
| Student(name=f"Student {i}") for i in range(1, 11) | |
| ] | |
| db.add_all(students) | |
| db.commit() | |
| return { | |
| "rooms": rooms, | |
| "instructors": instructors, | |
| "students": students, | |
| } | |