refactor(status): introduce OrderStatus enum, replace string literals

Centralized order status values in api/app/constants.py via a
str-valued Enum so comparisons keep working. Replaced literals in:
- services: sync_service, sqlite_service, retry_service
- routers: sync, dashboard
- templates: dashboard.html, logs.html
- static JS: shared (ORDER_STATUS mirror), dashboard, logs
- tests: requirements, order_items_overwrite, business_rules

MALFORMED intentionally NOT added — introduced in follow-up PR2
(per-order failure isolation).

Full test suite: 231 unit + 33 e2e pass.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Claude Agent
2026-04-22 08:45:32 +00:00
parent 51790accf9
commit f6d283b743
14 changed files with 171 additions and 133 deletions

View File

@@ -36,6 +36,7 @@ from unittest.mock import MagicMock, patch
from app.services.import_service import build_articles_json, compute_discount_split
from app.services.order_reader import OrderData, OrderItem
from app.constants import OrderStatus
# ---------------------------------------------------------------------------
@@ -857,14 +858,14 @@ class TestRefreshOrderAddress:
def test_null_address_ids_returns_422(self, client, db):
"""Orders without Oracle address IDs return 422."""
db.execute("INSERT OR IGNORE INTO orders (order_number, status) VALUES ('test-no-addr', 'IMPORTED')")
db.execute(f"INSERT OR IGNORE INTO orders (order_number, status) VALUES ('test-no-addr', '{OrderStatus.IMPORTED.value}')")
db.commit()
res = client.post("/api/orders/test-no-addr/refresh-address")
assert res.status_code == 422
def test_oracle_unavailable_returns_503(self, client, db, monkeypatch):
"""Oracle connection failure returns 503."""
db.execute("INSERT OR IGNORE INTO orders (order_number, status, id_adresa_livrare) VALUES ('test-oracle-fail', 'IMPORTED', 4116)")
db.execute(f"INSERT OR IGNORE INTO orders (order_number, status, id_adresa_livrare) VALUES ('test-oracle-fail', '{OrderStatus.IMPORTED.value}', 4116)")
db.commit()
import asyncio as _asyncio
@@ -878,7 +879,7 @@ class TestRefreshOrderAddress:
def test_refresh_returns_8_fields(self, client, db, monkeypatch):
"""Successful refresh returns 8-field address dict."""
db.execute("INSERT OR IGNORE INTO orders (order_number, status, id_adresa_livrare) VALUES ('test-refresh-ok', 'IMPORTED', 4116)")
db.execute(f"INSERT OR IGNORE INTO orders (order_number, status, id_adresa_livrare) VALUES ('test-refresh-ok', '{OrderStatus.IMPORTED.value}', 4116)")
db.commit()
mock_result = (
@@ -908,7 +909,7 @@ class TestRefreshOrderAddress:
from unittest.mock import AsyncMock # noqa: E402 (already imported MagicMock/patch above)
def _make_order_detail(status='IMPORTED', id_comanda=12345, factura_numar=None):
def _make_order_detail(status=OrderStatus.IMPORTED.value, id_comanda=12345, factura_numar=None):
return {
"order": {
"order_number": "1001",
@@ -983,7 +984,7 @@ class TestResyncDeleteSafetyGates:
from app.services import retry_service
with patch('app.services.sqlite_service.get_order_detail',
new=AsyncMock(return_value=_make_order_detail(status='ERROR'))), \
new=AsyncMock(return_value=_make_order_detail(status=OrderStatus.ERROR.value))), \
patch('app.services.sync_service._sync_lock', new=_unlocked_lock()):
result = await retry_service.resync_single_order("1001", {})
@@ -1051,7 +1052,7 @@ class TestResyncDeleteHappyPaths:
from app.services import retry_service
with patch('app.services.sqlite_service.get_order_detail',
new=AsyncMock(return_value=_make_order_detail(status='DELETED_IN_ROA'))), \
new=AsyncMock(return_value=_make_order_detail(status=OrderStatus.DELETED_IN_ROA.value))), \
patch('app.services.sync_service._sync_lock', new=_unlocked_lock()), \
patch('app.services.retry_service._download_and_reimport',
new=AsyncMock(return_value={"success": True, "message": "ok"})):

View File

@@ -35,6 +35,7 @@ if _api_dir not in sys.path:
from app import database
from app.services import sqlite_service
from app.constants import OrderStatus
@pytest.fixture(autouse=True)
@@ -69,7 +70,7 @@ async def _seed_order(order_number="TEST-001"):
order_number=order_number,
order_date="2026-01-01",
customer_name="Test",
status="IMPORTED",
status=OrderStatus.IMPORTED.value,
)
@@ -192,5 +193,5 @@ async def test_mark_order_deleted_removes_items():
finally:
await db.close()
assert row is not None
assert row["status"] == "DELETED_IN_ROA"
assert row["status"] == OrderStatus.DELETED_IN_ROA.value
assert row["id_comanda"] is None

View File

@@ -36,6 +36,7 @@ import pytest_asyncio
from app.database import init_sqlite
from app.services import sqlite_service
from app.constants import OrderStatus
# Initialize SQLite once before any tests run
init_sqlite()
@@ -70,10 +71,10 @@ def seed_baseline_data():
# Add the first order (IMPORTED) with items
await sqlite_service.upsert_order(
"RUN001", "ORD001", "2025-01-15", "Test Client", "IMPORTED",
"RUN001", "ORD001", "2025-01-15", "Test Client", OrderStatus.IMPORTED.value,
id_comanda=100, id_partener=200, items_count=2
)
await sqlite_service.add_sync_run_order("RUN001", "ORD001", "IMPORTED")
await sqlite_service.add_sync_run_order("RUN001", "ORD001", OrderStatus.IMPORTED.value)
items = [
{
@@ -103,15 +104,15 @@ def seed_baseline_data():
# Add more orders for filter tests
await sqlite_service.upsert_order(
"RUN001", "ORD002", "2025-01-16", "Client 2", "SKIPPED",
"RUN001", "ORD002", "2025-01-16", "Client 2", OrderStatus.SKIPPED.value,
missing_skus=["SKU99"], items_count=1
)
await sqlite_service.add_sync_run_order("RUN001", "ORD002", "SKIPPED")
await sqlite_service.add_sync_run_order("RUN001", "ORD002", OrderStatus.SKIPPED.value)
await sqlite_service.upsert_order(
"RUN001", "ORD003", "2025-01-17", "Client 3", "ERROR",
"RUN001", "ORD003", "2025-01-17", "Client 3", OrderStatus.ERROR.value,
error_message="Test error", items_count=3
)
await sqlite_service.add_sync_run_order("RUN001", "ORD003", "ERROR")
await sqlite_service.add_sync_run_order("RUN001", "ORD003", OrderStatus.ERROR.value)
asyncio.run(_seed())
yield
@@ -212,7 +213,7 @@ async def test_get_order_detail_not_found():
async def test_get_order_detail_status():
"""Seeded ORD001 should have IMPORTED status."""
detail = await sqlite_service.get_order_detail("ORD001")
assert detail["order"]["status"] == "IMPORTED"
assert detail["order"]["status"] == OrderStatus.IMPORTED.value
# ---------------------------------------------------------------------------
@@ -232,7 +233,7 @@ async def test_get_run_orders_filtered_all():
@pytest.mark.asyncio
async def test_get_run_orders_filtered_imported():
"""Filter IMPORTED should return only ORD001."""
result = await sqlite_service.get_run_orders_filtered("RUN001", "IMPORTED", 1, 50)
result = await sqlite_service.get_run_orders_filtered("RUN001", OrderStatus.IMPORTED.value, 1, 50)
assert result["total"] == 1
assert result["orders"][0]["order_number"] == "ORD001"
@@ -240,7 +241,7 @@ async def test_get_run_orders_filtered_imported():
@pytest.mark.asyncio
async def test_get_run_orders_filtered_skipped():
"""Filter SKIPPED should return only ORD002."""
result = await sqlite_service.get_run_orders_filtered("RUN001", "SKIPPED", 1, 50)
result = await sqlite_service.get_run_orders_filtered("RUN001", OrderStatus.SKIPPED.value, 1, 50)
assert result["total"] == 1
assert result["orders"][0]["order_number"] == "ORD002"
@@ -248,7 +249,7 @@ async def test_get_run_orders_filtered_skipped():
@pytest.mark.asyncio
async def test_get_run_orders_filtered_error():
"""Filter ERROR should return only ORD003."""
result = await sqlite_service.get_run_orders_filtered("RUN001", "ERROR", 1, 50)
result = await sqlite_service.get_run_orders_filtered("RUN001", OrderStatus.ERROR.value, 1, 50)
assert result["total"] == 1
assert result["orders"][0]["order_number"] == "ORD003"
@@ -360,10 +361,10 @@ def test_api_sync_run_orders(client):
def test_api_sync_run_orders_filtered(client):
"""R1: Filtering by status=IMPORTED returns only IMPORTED orders."""
resp = client.get("/api/sync/run/RUN001/orders?status=IMPORTED")
resp = client.get(f"/api/sync/run/RUN001/orders?status={OrderStatus.IMPORTED.value}")
assert resp.status_code == 200
data = resp.json()
assert all(o["status"] == "IMPORTED" for o in data["orders"])
assert all(o["status"] == OrderStatus.IMPORTED.value for o in data["orders"])
def test_api_sync_run_orders_pagination_fields(client):