feat(backend): sync endpoints + all models + seed + order workflow
- All business models: Vehicle, Order, OrderLine, Invoice, Appointment, CatalogMarca/Model/Ansamblu/Norma/Pret/TipDeviz/TipMotor, Mecanic - Sync endpoints: GET /sync/full, GET /sync/changes?since=, POST /sync/push with tenant isolation and last-write-wins conflict resolution - Order CRUD with state machine: DRAFT -> VALIDAT -> FACTURAT Auto-recalculates totals (manopera + materiale) - Vehicle CRUD: list, create, get, update - Seed data: 24 marci, 11 ansamble, 6 tipuri deviz, 5 tipuri motoare, 3 preturi - Alembic migration for all business models - 13 passing tests (auth + sync + orders) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import pytest
|
||||
import pytest_asyncio
|
||||
from httpx import ASGITransport, AsyncClient
|
||||
from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine
|
||||
|
||||
from app.db.base import Base
|
||||
@@ -22,3 +23,40 @@ async def setup_test_db():
|
||||
yield
|
||||
app.dependency_overrides.clear()
|
||||
await engine.dispose()
|
||||
|
||||
|
||||
@pytest_asyncio.fixture
|
||||
async def client():
|
||||
async with AsyncClient(
|
||||
transport=ASGITransport(app=app), base_url="http://test"
|
||||
) as c:
|
||||
yield c
|
||||
|
||||
|
||||
@pytest_asyncio.fixture
|
||||
async def auth_headers(client):
|
||||
r = await client.post(
|
||||
"/api/auth/register",
|
||||
json={
|
||||
"email": "test@service.ro",
|
||||
"password": "testpass123",
|
||||
"tenant_name": "Test Service",
|
||||
"telefon": "0722000000",
|
||||
},
|
||||
)
|
||||
token = r.json()["access_token"]
|
||||
return {"Authorization": f"Bearer {token}"}
|
||||
|
||||
|
||||
@pytest_asyncio.fixture
|
||||
async def tenant_id(client):
|
||||
r = await client.post(
|
||||
"/api/auth/register",
|
||||
json={
|
||||
"email": "tenant@service.ro",
|
||||
"password": "testpass123",
|
||||
"tenant_name": "Tenant Service",
|
||||
"telefon": "0722000001",
|
||||
},
|
||||
)
|
||||
return r.json()["tenant_id"]
|
||||
|
||||
145
backend/tests/test_orders.py
Normal file
145
backend/tests/test_orders.py
Normal file
@@ -0,0 +1,145 @@
|
||||
import uuid
|
||||
from datetime import UTC, datetime
|
||||
|
||||
import pytest
|
||||
from httpx import ASGITransport, AsyncClient
|
||||
|
||||
from app.main import app
|
||||
|
||||
|
||||
async def _create_vehicle(client, auth_headers):
|
||||
"""Helper to create a vehicle via sync push and return its id."""
|
||||
me = await client.get("/api/auth/me", headers=auth_headers)
|
||||
tenant_id = me.json()["tenant_id"]
|
||||
vid = str(uuid.uuid4())
|
||||
now = datetime.now(UTC).isoformat()
|
||||
await client.post(
|
||||
"/api/sync/push",
|
||||
headers=auth_headers,
|
||||
json={
|
||||
"operations": [
|
||||
{
|
||||
"table": "vehicles",
|
||||
"id": vid,
|
||||
"operation": "INSERT",
|
||||
"data": {
|
||||
"id": vid,
|
||||
"tenant_id": tenant_id,
|
||||
"nr_inmatriculare": "CT01TST",
|
||||
"client_nume": "Test Client",
|
||||
"created_at": now,
|
||||
"updated_at": now,
|
||||
},
|
||||
"timestamp": now,
|
||||
}
|
||||
]
|
||||
},
|
||||
)
|
||||
return vid
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_order_workflow(client, auth_headers):
|
||||
vid = await _create_vehicle(client, auth_headers)
|
||||
|
||||
# Create order (DRAFT)
|
||||
r = await client.post(
|
||||
"/api/orders",
|
||||
headers=auth_headers,
|
||||
json={"vehicle_id": vid},
|
||||
)
|
||||
assert r.status_code == 200
|
||||
order_id = r.json()["id"]
|
||||
|
||||
# Add manopera line: 2h x 150 = 300
|
||||
r = await client.post(
|
||||
f"/api/orders/{order_id}/lines",
|
||||
headers=auth_headers,
|
||||
json={
|
||||
"tip": "manopera",
|
||||
"descriere": "Reparatie motor",
|
||||
"ore": 2,
|
||||
"pret_ora": 150,
|
||||
},
|
||||
)
|
||||
assert r.status_code == 200
|
||||
|
||||
# Add material line: 2 buc x 50 = 100
|
||||
r = await client.post(
|
||||
f"/api/orders/{order_id}/lines",
|
||||
headers=auth_headers,
|
||||
json={
|
||||
"tip": "material",
|
||||
"descriere": "Filtru ulei",
|
||||
"cantitate": 2,
|
||||
"pret_unitar": 50,
|
||||
"um": "buc",
|
||||
},
|
||||
)
|
||||
assert r.status_code == 200
|
||||
|
||||
# Validate order
|
||||
r = await client.post(
|
||||
f"/api/orders/{order_id}/validate",
|
||||
headers=auth_headers,
|
||||
)
|
||||
assert r.status_code == 200
|
||||
assert r.json()["status"] == "VALIDAT"
|
||||
|
||||
# Get order details
|
||||
r = await client.get(
|
||||
f"/api/orders/{order_id}",
|
||||
headers=auth_headers,
|
||||
)
|
||||
assert r.status_code == 200
|
||||
data = r.json()
|
||||
assert data["total_manopera"] == 300
|
||||
assert data["total_materiale"] == 100
|
||||
assert data["total_general"] == 400
|
||||
assert data["status"] == "VALIDAT"
|
||||
assert len(data["lines"]) == 2
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_cannot_add_line_to_validated_order(client, auth_headers):
|
||||
vid = await _create_vehicle(client, auth_headers)
|
||||
|
||||
# Create and validate order
|
||||
r = await client.post(
|
||||
"/api/orders",
|
||||
headers=auth_headers,
|
||||
json={"vehicle_id": vid},
|
||||
)
|
||||
order_id = r.json()["id"]
|
||||
await client.post(
|
||||
f"/api/orders/{order_id}/validate",
|
||||
headers=auth_headers,
|
||||
)
|
||||
|
||||
# Try to add line to validated order
|
||||
r = await client.post(
|
||||
f"/api/orders/{order_id}/lines",
|
||||
headers=auth_headers,
|
||||
json={
|
||||
"tip": "manopera",
|
||||
"descriere": "Should fail",
|
||||
"ore": 1,
|
||||
"pret_ora": 100,
|
||||
},
|
||||
)
|
||||
assert r.status_code == 422
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_list_orders(client, auth_headers):
|
||||
vid = await _create_vehicle(client, auth_headers)
|
||||
|
||||
await client.post(
|
||||
"/api/orders",
|
||||
headers=auth_headers,
|
||||
json={"vehicle_id": vid},
|
||||
)
|
||||
|
||||
r = await client.get("/api/orders", headers=auth_headers)
|
||||
assert r.status_code == 200
|
||||
assert len(r.json()) == 1
|
||||
128
backend/tests/test_sync.py
Normal file
128
backend/tests/test_sync.py
Normal file
@@ -0,0 +1,128 @@
|
||||
import uuid
|
||||
from datetime import UTC, datetime
|
||||
|
||||
import pytest
|
||||
from httpx import ASGITransport, AsyncClient
|
||||
|
||||
from app.main import app
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_full_sync_returns_all_tables(client, auth_headers):
|
||||
r = await client.get("/api/sync/full", headers=auth_headers)
|
||||
assert r.status_code == 200
|
||||
data = r.json()
|
||||
assert "tables" in data and "synced_at" in data
|
||||
assert "vehicles" in data["tables"]
|
||||
assert "catalog_marci" in data["tables"]
|
||||
assert "orders" in data["tables"]
|
||||
assert "mecanici" in data["tables"]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_sync_push_insert_vehicle(client, auth_headers):
|
||||
# Get tenant_id from /me
|
||||
me = await client.get("/api/auth/me", headers=auth_headers)
|
||||
tenant_id = me.json()["tenant_id"]
|
||||
|
||||
vid = str(uuid.uuid4())
|
||||
now = datetime.now(UTC).isoformat()
|
||||
r = await client.post(
|
||||
"/api/sync/push",
|
||||
headers=auth_headers,
|
||||
json={
|
||||
"operations": [
|
||||
{
|
||||
"table": "vehicles",
|
||||
"id": vid,
|
||||
"operation": "INSERT",
|
||||
"data": {
|
||||
"id": vid,
|
||||
"tenant_id": tenant_id,
|
||||
"nr_inmatriculare": "CTA01ABC",
|
||||
"client_nume": "Popescu",
|
||||
"created_at": now,
|
||||
"updated_at": now,
|
||||
},
|
||||
"timestamp": now,
|
||||
}
|
||||
]
|
||||
},
|
||||
)
|
||||
assert r.status_code == 200
|
||||
assert r.json()["applied"] == 1
|
||||
|
||||
# Verify via full sync
|
||||
full = await client.get("/api/sync/full", headers=auth_headers)
|
||||
vehicles = full.json()["tables"]["vehicles"]
|
||||
assert len(vehicles) == 1
|
||||
assert vehicles[0]["nr_inmatriculare"] == "CTA01ABC"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_sync_changes_since(client, auth_headers):
|
||||
me = await client.get("/api/auth/me", headers=auth_headers)
|
||||
tenant_id = me.json()["tenant_id"]
|
||||
|
||||
before = datetime.now(UTC).isoformat()
|
||||
|
||||
vid = str(uuid.uuid4())
|
||||
now = datetime.now(UTC).isoformat()
|
||||
await client.post(
|
||||
"/api/sync/push",
|
||||
headers=auth_headers,
|
||||
json={
|
||||
"operations": [
|
||||
{
|
||||
"table": "vehicles",
|
||||
"id": vid,
|
||||
"operation": "INSERT",
|
||||
"data": {
|
||||
"id": vid,
|
||||
"tenant_id": tenant_id,
|
||||
"nr_inmatriculare": "B99XYZ",
|
||||
"created_at": now,
|
||||
"updated_at": now,
|
||||
},
|
||||
"timestamp": now,
|
||||
}
|
||||
]
|
||||
},
|
||||
)
|
||||
|
||||
r = await client.get(
|
||||
f"/api/sync/changes?since={before}", headers=auth_headers
|
||||
)
|
||||
assert r.status_code == 200
|
||||
data = r.json()
|
||||
assert "vehicles" in data["tables"]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_sync_push_rejects_wrong_tenant(client, auth_headers):
|
||||
now = datetime.now(UTC).isoformat()
|
||||
vid = str(uuid.uuid4())
|
||||
r = await client.post(
|
||||
"/api/sync/push",
|
||||
headers=auth_headers,
|
||||
json={
|
||||
"operations": [
|
||||
{
|
||||
"table": "vehicles",
|
||||
"id": vid,
|
||||
"operation": "INSERT",
|
||||
"data": {
|
||||
"id": vid,
|
||||
"tenant_id": "wrong-tenant-id",
|
||||
"nr_inmatriculare": "HACK",
|
||||
"created_at": now,
|
||||
"updated_at": now,
|
||||
},
|
||||
"timestamp": now,
|
||||
}
|
||||
]
|
||||
},
|
||||
)
|
||||
assert r.status_code == 200
|
||||
# Wrong tenant_id is rejected (skipped)
|
||||
assert r.json()["applied"] == 0
|
||||
Reference in New Issue
Block a user