Rework core functionality, add CLI and tests

This commit is contained in:
2026-03-16 00:34:32 +01:00
parent c30ad57051
commit 40ca08359f
9 changed files with 3118 additions and 35 deletions

70
tests/conftest.py Normal file
View File

@@ -0,0 +1,70 @@
"""
Test configuration and fixtures for HXBooks.
Provides isolated test database, Flask app instances, and CLI testing utilities.
"""
import tempfile
from pathlib import Path
from typing import Generator
import pytest
from click.testing import CliRunner
from flask import Flask
from flask.testing import FlaskClient
from hxbooks import cli, 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):
"""Create database session for direct database testing."""
with app.app_context():
yield db.session