- imptune/api/printers.py: POST /printers (all form fields, checkbox->bool,
FK resolution), DELETE /printers/{id}, grouped list renderer with LEFT JOIN
- imptune/api/clients.py: POST /clients with duplicate-name handling
- imptune/api/pages.py: GET /printers and GET /clients page routes
- imptune/main.py: register printers + clients routers; close db on shutdown
- imptune/db/database.py: close existing connection before re-init (test isolation)
- templates: printers.html, clients.html, printer_form.html (Alpine.js port
auto-derivation), printer_list.html (grouped by client), client_list.html
- tests/conftest.py: close test-thread db connection in fixture teardown
- tests/test_printer_crud.py: updated to use list(select().where()) for DB
queries (avoids Peewee thread-local cursor caching across test boundaries)
Auto-fix [Rule 1 - Bug]: DB test isolation — Peewee thread-local connections
persisted across tests causing stale DB reads; fixed via conftest teardown and
lifespan db.close() on shutdown.
36 lines
1.0 KiB
Python
36 lines
1.0 KiB
Python
import os
|
|
from contextlib import asynccontextmanager
|
|
from pathlib import Path
|
|
|
|
from fastapi import FastAPI
|
|
from fastapi.staticfiles import StaticFiles
|
|
|
|
from imptune.api import clients, drivers, health, pages, printers
|
|
from imptune.config import DATA_DIR, DRIVERS_DIR
|
|
from imptune.db.database import db, init_db
|
|
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(app: FastAPI):
|
|
os.makedirs(DATA_DIR, exist_ok=True)
|
|
os.makedirs(DRIVERS_DIR, exist_ok=True)
|
|
init_db()
|
|
yield
|
|
# Close DB connection on shutdown so test fixtures can re-initialize cleanly
|
|
if not db.is_closed():
|
|
db.close()
|
|
|
|
|
|
app = FastAPI(title="ImpTune", lifespan=lifespan)
|
|
|
|
# Serve baked-in static assets (pico.min.css, htmx.min.js, alpine.min.js)
|
|
_static_dir = Path(__file__).parent / "static"
|
|
app.mount("/static", StaticFiles(directory=str(_static_dir)), name="static")
|
|
|
|
# Register routers
|
|
app.include_router(health.router)
|
|
app.include_router(pages.router)
|
|
app.include_router(drivers.router)
|
|
app.include_router(printers.router)
|
|
app.include_router(clients.router)
|