chore(v2): restructure monorepo to src/ layout with uv

Aligns the repo with the python-project-spec-design.md template chosen
for V2.0.0. Big move, no logic changes. The 3 pre-existing test
failures (test_recipes::test_update_recipe, test_recipes::
test_recipe_versioning, test_tasks::test_reorder_tasks, plus the
client test_save_measurement_proxy) survive unchanged.

Layout changes
- server/        -> src/backend/
- server/middleware/ -> src/backend/api/middleware/
- server/routers/    -> src/backend/api/routers/
- server/models/     -> src/backend/models/orm/
- server/schemas/    -> src/backend/models/api/
- server/uploads/    -> uploads/ (project root, mounted volume)
- server/tests/      -> src/backend/tests/
- client/            -> src/frontend/flask_app/ (Flask kept; React
  deroga is documented in CLAUDE.md, justified by tablet UX, USB
  caliper/barcode workflow and Fabric.js integration)

Tooling
- pyproject.toml: monorepo with [project] core deps and
  optional-dependencies server / client / dev. Replaces both
  server/requirements.txt and client/requirements.txt.
- uv.lock + .python-version (3.11) committed for reproducible builds.
- Dockerfile (root, backend) and Dockerfile.frontend rewritten to use
  uv sync --frozen --no-dev --extra server|client; legacy Dockerfiles
  preserved as Dockerfile.legacy for reference but excluded from build
  context via .dockerignore.
- docker-compose.dev.yml + docker-compose.yml: build context now ".",
  dockerfile pointing to the root files.

Code adjustments forced by the move
- Every "from config|database|models|schemas|services|routers|middleware
  import ..." rewritten to its src.backend.* equivalent (50+ files
  including indented inline imports inside test bodies).
- src/backend/migrations/env.py: insert project root into sys.path so
  alembic can resolve src.backend.* imports regardless of cwd.
- src/backend/config.py: env_file ../../.env (was ../.env), upload_path
  resolves project root via parents[2].
- src/backend/tests/conftest.py + tests: import ... from src.backend.*
  instead of bare names; old per-directory pytest.ini files removed in
  favor of root pyproject.toml [tool.pytest.ini_options].
- .gitignore: uploads/ at root, src/frontend/flask_app/static/css/
  tailwind.css path; .dockerignore tightened.
- CLAUDE.md: rewrote sections "Layout del repository", "Comandi di
  Sviluppo", "Database & Migrations", "Test", "i18n", and all path
  references throughout the architecture sections.

Verified
- uv lock resolves 77 packages; uv sync --extra server --extra client
  --extra dev installs cleanly.
- uv run pytest: 171 passed, 4 pre-existing failures.
- uv run alembic -c src/backend/migrations/alembic.ini check loads
  config and metadata (errors only on the absent local MySQL).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-25 12:26:47 +02:00
parent 86df67f2e5
commit 1a0431366f
174 changed files with 2568 additions and 308 deletions
+252
View File
@@ -0,0 +1,252 @@
"""Tests for recipes router (/api/recipes)."""
import pytest
from httpx import AsyncClient
from sqlalchemy.ext.asyncio import AsyncSession
from src.backend.models.orm.user import User
from src.backend.tests.conftest import auth_headers, create_test_recipe
class TestListRecipes:
"""GET /api/recipes tests."""
async def test_list_recipes(
self,
client: AsyncClient,
maker_user: User,
db_session: AsyncSession,
):
"""Authenticated user can list recipes."""
await create_test_recipe(db_session, maker_user.id)
resp = await client.get(
"/api/recipes", headers=auth_headers(maker_user)
)
assert resp.status_code == 200
data = resp.json()
assert "items" in data
assert "total" in data
assert data["total"] >= 1
assert len(data["items"]) >= 1
async def test_search_recipes(
self,
client: AsyncClient,
maker_user: User,
db_session: AsyncSession,
):
"""Search parameter filters recipes by name or code."""
await create_test_recipe(
db_session, maker_user.id, code="SEARCH-001", name="Searchable Recipe"
)
await create_test_recipe(
db_session, maker_user.id, code="OTHER-002", name="Other Recipe"
)
resp = await client.get(
"/api/recipes",
headers=auth_headers(maker_user),
params={"search": "Searchable"},
)
assert resp.status_code == 200
data = resp.json()
assert data["total"] >= 1
names = [item["name"] for item in data["items"]]
assert "Searchable Recipe" in names
class TestGetRecipe:
"""GET /api/recipes/{recipe_id} tests."""
async def test_get_recipe(
self,
client: AsyncClient,
maker_user: User,
db_session: AsyncSession,
):
"""Get single recipe detail with current version."""
recipe = await create_test_recipe(db_session, maker_user.id)
resp = await client.get(
f"/api/recipes/{recipe.id}", headers=auth_headers(maker_user)
)
assert resp.status_code == 200
data = resp.json()
assert data["code"] == "REC-001"
assert data["name"] == "Test Recipe"
assert data["current_version"] is not None
assert data["current_version"]["version_number"] == 1
async def test_recipe_not_found(
self, client: AsyncClient, maker_user: User
):
"""Non-existent recipe returns 404."""
resp = await client.get(
"/api/recipes/99999", headers=auth_headers(maker_user)
)
assert resp.status_code == 404
class TestCreateRecipe:
"""POST /api/recipes tests."""
async def test_create_recipe_as_maker(
self, client: AsyncClient, maker_user: User
):
"""Maker can create a recipe.
Note: The POST response may not include ``current_version`` due to
async lazy-loading limitations with the in-memory SQLite test engine.
We verify the created recipe via a follow-up GET which uses
``selectinload`` and therefore loads the full object graph correctly.
"""
resp = await client.post(
"/api/recipes",
headers=auth_headers(maker_user),
json={
"code": "NEW-001",
"name": "New Recipe",
"description": "Created in test",
},
)
# Accept either 201 (success) or 500 (lazy-loading limitation with
# SQLite in-memory; the recipe IS created, the serialisation fails)
assert resp.status_code in (201, 500)
# Verify via GET which uses full selectinload
list_resp = await client.get(
"/api/recipes",
headers=auth_headers(maker_user),
params={"search": "NEW-001"},
)
assert list_resp.status_code == 200
items = list_resp.json()["items"]
assert len(items) >= 1
recipe = items[0]
assert recipe["code"] == "NEW-001"
assert recipe["name"] == "New Recipe"
assert recipe["active"] is True
# Should have current version v1
assert recipe["current_version"] is not None
assert recipe["current_version"]["version_number"] == 1
async def test_create_recipe_forbidden(
self, client: AsyncClient, measurement_tec_user: User
):
"""MeasurementTec without Maker role cannot create recipes."""
resp = await client.post(
"/api/recipes",
headers=auth_headers(measurement_tec_user),
json={
"code": "FORBIDDEN-001",
"name": "Forbidden Recipe",
},
)
assert resp.status_code == 403
async def test_create_recipe_duplicate_code(
self,
client: AsyncClient,
maker_user: User,
db_session: AsyncSession,
):
"""Duplicate recipe code returns 409."""
await create_test_recipe(db_session, maker_user.id, code="DUP-001")
resp = await client.post(
"/api/recipes",
headers=auth_headers(maker_user),
json={"code": "DUP-001", "name": "Duplicate"},
)
assert resp.status_code == 409
class TestUpdateRecipe:
"""PUT /api/recipes/{recipe_id} tests."""
async def test_update_recipe(
self,
client: AsyncClient,
maker_user: User,
db_session: AsyncSession,
):
"""Updating a recipe creates a new version (copy-on-write).
We verify the new version exists by listing versions, since
SQLAlchemy identity-map staleness with StaticPool can cause
``is_current`` to appear stale in some responses.
"""
recipe = await create_test_recipe(db_session, maker_user.id)
resp = await client.put(
f"/api/recipes/{recipe.id}",
headers=auth_headers(maker_user),
json={
"name": "Updated Recipe",
"change_notes": "Updated name in test",
},
)
assert resp.status_code == 200
# Verify via versions list which always does a clean query
versions_resp = await client.get(
f"/api/recipes/{recipe.id}/versions",
headers=auth_headers(maker_user),
)
assert versions_resp.status_code == 200
versions = versions_resp.json()
version_numbers = [v["version_number"] for v in versions]
assert 1 in version_numbers
assert 2 in version_numbers
class TestDeleteRecipe:
"""DELETE /api/recipes/{recipe_id} tests."""
async def test_delete_recipe(
self,
client: AsyncClient,
maker_user: User,
db_session: AsyncSession,
):
"""Soft-delete deactivates a recipe."""
recipe = await create_test_recipe(db_session, maker_user.id)
resp = await client.delete(
f"/api/recipes/{recipe.id}", headers=auth_headers(maker_user)
)
assert resp.status_code == 200
assert resp.json()["active"] is False
class TestRecipeVersioning:
"""Versioning-related tests."""
async def test_recipe_versioning(
self,
client: AsyncClient,
maker_user: User,
db_session: AsyncSession,
):
"""Multiple updates create consecutive versions."""
recipe = await create_test_recipe(db_session, maker_user.id)
# Update once -> v2
await client.put(
f"/api/recipes/{recipe.id}",
headers=auth_headers(maker_user),
json={"change_notes": "Version 2"},
)
# Update again -> v3
await client.put(
f"/api/recipes/{recipe.id}",
headers=auth_headers(maker_user),
json={"change_notes": "Version 3"},
)
# List versions
resp = await client.get(
f"/api/recipes/{recipe.id}/versions",
headers=auth_headers(maker_user),
)
assert resp.status_code == 200
versions = resp.json()
version_numbers = [v["version_number"] for v in versions]
assert 1 in version_numbers
assert 2 in version_numbers
assert 3 in version_numbers