# ============================================================ # MD-04 Excel/CSV 导入管线黄金测试 # ============================================================ from __future__ import annotations from server.aps_domain.importers import apply_import_commit, preview_file, validate_batch from server.state.seed import seed_world def _next_id_factory(world): tables = { "audit": "auditEvents", "salesOrder": "salesOrders", "material": "materials", } counters: dict[str, int] = {} def next_id(kind: str) -> int: if kind not in counters: rows = world.get(tables.get(kind, kind + "s"), []) counters[kind] = max((r.get("id", 0) for r in rows if isinstance(r, dict)), default=0) counters[kind] += 1 return counters[kind] return next_id def test_validate_equipment_batch(): world = seed_world() rows = [ {"code": "EQ-X1", "name": "试验压接机", "capabilities": "OP-PRESS,OP-WELD", "opStdTime": "OP-PRESS:2,OP-WELD:5", "movable": "是", "zone": "ZONE-A", "availabilityRate": "0.95", "status": "RUNNING"}, ] batch = validate_batch("equipment", rows, world) assert batch["okCount"] == 1 assert batch["errorCount"] == 0 assert "OP-PRESS" in batch["okRows"][0]["capabilities"] def test_preview_csv_orders_and_commit(): world = seed_world() product = next(m for m in world["materials"] if m.get("type") == "FINISHED_PRODUCT") before = len(world["salesOrders"]) csv_text = ( "订单号,客户,产品编码,数量,交期,优先级\n" f"SO-IMP-1,导入客户,{product['code']},50,2026-08-01,3\n" ).encode("utf-8") preview = preview_file("orders.csv", csv_text, world) assert preview["canCommit"] is True assert preview["totalOk"] >= 1 assert preview["batches"][0]["kind"] == "orders" applied = apply_import_commit(world, _next_id_factory(world), preview["batches"]) assert applied["total"] >= 1 assert len(world["salesOrders"]) == before + 1 assert any(o.get("customerName") == "导入客户" for o in world["salesOrders"]) def test_commit_flex_equipment(): world = seed_world() batches = [{ "kind": "equipment", "sheet": "设备", "okRows": [{ "code": "EQ-NEW-99", "name": "新压接", "capabilities": ["OP-PRESS"], "opStdTime": {"OP-PRESS": 1.5}, "movable": True, "moveTimeMin": 15, "zone": "ZONE-A", "availabilityRate": 1.0, "status": "RUNNING", "adaptableMolds": [], }], }] applied = apply_import_commit(world, _next_id_factory(world), batches) assert applied["summary"].get("flexEquipment", 0) == 1 assert any(e["code"] == "EQ-NEW-99" for e in world["flexEquipment"])