72 lines
1.7 KiB
Python
72 lines
1.7 KiB
Python
"""
|
|
Test configuration and fixtures for HXBooks.
|
|
|
|
Provides isolated test database, Flask app instances, and CLI testing utilities.
|
|
"""
|
|
|
|
from collections.abc import Generator
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
from click.testing import CliRunner
|
|
from flask import Flask
|
|
from flask.testing import FlaskClient
|
|
from sqlalchemy.orm import Session
|
|
|
|
from hxbooks import cli
|
|
from hxbooks.app import create_app
|
|
from hxbooks.db import db
|
|
from hxbooks.models import User
|
|
|
|
|
|
@pytest.fixture
|
|
def app(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Flask:
|
|
"""Create Flask app with test configuration."""
|
|
test_db_path = tmp_path / "test.db"
|
|
test_config = {
|
|
"TESTING": True,
|
|
"SQLALCHEMY_DATABASE_URI": f"sqlite:///{test_db_path}",
|
|
"SECRET_KEY": "test-secret-key",
|
|
"WTF_CSRF_ENABLED": False,
|
|
}
|
|
|
|
app = create_app(test_config)
|
|
|
|
with app.app_context():
|
|
db.create_all()
|
|
|
|
monkeypatch.setattr(cli, "get_app", lambda: app)
|
|
return app
|
|
|
|
|
|
@pytest.fixture
|
|
def client(app: Flask) -> FlaskClient:
|
|
"""Create test client for Flask app."""
|
|
return app.test_client()
|
|
|
|
|
|
@pytest.fixture
|
|
def cli_runner() -> CliRunner:
|
|
"""Create Click CLI test runner."""
|
|
return CliRunner()
|
|
|
|
|
|
@pytest.fixture
|
|
def test_user(app: Flask) -> User:
|
|
"""Create a test user in the database."""
|
|
with app.app_context():
|
|
user = User(username="testuser")
|
|
db.session.add(user)
|
|
db.session.commit()
|
|
|
|
# Refresh to get the ID
|
|
db.session.refresh(user)
|
|
return user
|
|
|
|
|
|
@pytest.fixture
|
|
def db_session(app: Flask) -> Generator[Session]:
|
|
"""Create database session for direct database testing."""
|
|
with app.app_context():
|
|
yield db.session
|