优化大量推送问题
This commit is contained in:
@@ -12,9 +12,12 @@ from sync_state_machine.datasource.task_result import TaskStatus
|
||||
|
||||
class _MaterialDetailSchema(BaseModel):
|
||||
id: str
|
||||
project_id: str | None = None
|
||||
parent_id: str
|
||||
date: str | None = None
|
||||
quantity: float
|
||||
remark: str | None = None
|
||||
is_complete: bool | None = None
|
||||
|
||||
|
||||
class _MaterialDetailNode(SyncNode[_MaterialDetailSchema]):
|
||||
@@ -31,6 +34,12 @@ class _MockApiClient:
|
||||
return {"code": 200, "message": "OK", "data": {"biz_id": kwargs.get("data", {}).get("biz_id")}}
|
||||
|
||||
|
||||
class _FailingApiClient(_MockApiClient):
|
||||
async def request(self, method: str, endpoint: str, **kwargs: Any) -> dict[str, Any]:
|
||||
self.requests.append({"method": method, "endpoint": endpoint, **kwargs})
|
||||
raise Exception("API error: failed request")
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_material_detail_update_returns_in_progress_with_shared_push_id() -> None:
|
||||
handler = MaterialDetailApiHandler()
|
||||
@@ -60,3 +69,61 @@ async def test_material_detail_update_returns_in_progress_with_shared_push_id()
|
||||
assert results[0].status == TaskStatus.IN_PROGRESS
|
||||
assert results[0].task_id
|
||||
assert handler.api_client.requests[0]["endpoint"] == "/material/detail/update"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_material_detail_create_failure_preserves_push_id_for_trace_lookup() -> None:
|
||||
handler = MaterialDetailApiHandler()
|
||||
handler.api_client = _FailingApiClient()
|
||||
|
||||
node = _MaterialDetailNode(
|
||||
node_id="detail-node-create-1",
|
||||
data_id="",
|
||||
data={
|
||||
"id": "runtime-detail-1",
|
||||
"project_id": "project-1",
|
||||
"parent_id": "material-1",
|
||||
"date": "2025-03-01",
|
||||
"quantity": 4.0,
|
||||
"remark": None,
|
||||
"is_complete": False,
|
||||
},
|
||||
)
|
||||
|
||||
results = await handler.create_all([node])
|
||||
|
||||
assert len(results) == 1
|
||||
assert results[0].status == TaskStatus.FAILED
|
||||
assert results[0].push_id
|
||||
assert handler.api_client.requests[0]["endpoint"] == "/material/detail/create"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_material_detail_update_failure_preserves_push_id_for_trace_lookup() -> None:
|
||||
handler = MaterialDetailApiHandler()
|
||||
handler.api_client = _FailingApiClient()
|
||||
|
||||
node = _MaterialDetailNode(
|
||||
node_id="detail-node-update-fail-1",
|
||||
data_id="detail-1",
|
||||
data={
|
||||
"id": "detail-1",
|
||||
"parent_id": "material-1",
|
||||
"quantity": 10.0,
|
||||
"remark": "new",
|
||||
},
|
||||
origin_data={
|
||||
"id": "detail-1",
|
||||
"parent_id": "material-1",
|
||||
"quantity": 5.0,
|
||||
"remark": None,
|
||||
},
|
||||
context={"material_id": "material-1"},
|
||||
)
|
||||
|
||||
results = await handler.update_all([node])
|
||||
|
||||
assert len(results) == 1
|
||||
assert results[0].status == TaskStatus.FAILED
|
||||
assert results[0].push_id
|
||||
assert handler.api_client.requests[0]["endpoint"] == "/material/detail/update"
|
||||
|
||||
@@ -57,6 +57,23 @@ class _FailingApiClient:
|
||||
return {"code": 500, "message": "boom"}
|
||||
|
||||
|
||||
class _RecordingApiClient:
|
||||
def __init__(self) -> None:
|
||||
self.calls = []
|
||||
|
||||
async def request(self, method, endpoint, params=None, data=None, **kwargs):
|
||||
self.calls.append(
|
||||
{
|
||||
"method": method,
|
||||
"endpoint": endpoint,
|
||||
"params": params,
|
||||
"data": data,
|
||||
"kwargs": kwargs,
|
||||
}
|
||||
)
|
||||
return {"code": 200, "message": "OK"}
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_project_detail_create_failure_preserves_push_id_and_biz_id() -> None:
|
||||
collection = DataCollection("remote")
|
||||
@@ -96,6 +113,117 @@ async def test_project_detail_create_failure_preserves_push_id_and_biz_id() -> N
|
||||
assert result.metadata["key"] == "production"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_project_detail_production_payload_excludes_ic_plan_dates() -> None:
|
||||
collection = DataCollection("remote")
|
||||
|
||||
project = _ProjectNode(
|
||||
node_id="project-node-1",
|
||||
data_id="project-1",
|
||||
data={"id": "project-1"},
|
||||
context={"all_info": {"project_detail": []}},
|
||||
)
|
||||
await collection.add(project)
|
||||
|
||||
node = ProjectDetailSyncNode(
|
||||
node_id="detail-node-1",
|
||||
data_id="local-detail-1",
|
||||
data={
|
||||
"id": "local-detail-1",
|
||||
"project_id": "project-1",
|
||||
"key": "production",
|
||||
"value": [{"date": "2026-01-01", "capacity": 1.0}],
|
||||
"plan_production_date": "2026-01-01",
|
||||
"plan_full_production_date": "2026-06-01",
|
||||
"ic_plan_first_production_date": "2026-02-01",
|
||||
"ic_plan_full_production_date": "2026-07-01",
|
||||
"actual_production_date": "2026-01-15",
|
||||
"actual_full_production_date": "2026-03-15",
|
||||
},
|
||||
)
|
||||
|
||||
handler = ProjectDetailApiHandler()
|
||||
handler.set_collection(collection)
|
||||
handler.api_client = _RecordingApiClient()
|
||||
|
||||
results = await handler.create_all([node])
|
||||
|
||||
assert len(results) == 1
|
||||
assert results[0].status == TaskStatus.IN_PROGRESS
|
||||
assert len(handler.api_client.calls) == 1
|
||||
|
||||
request_payload = handler.api_client.calls[0]["data"]["data"]
|
||||
assert request_payload["plan_production_date"] == "2026-01-01"
|
||||
assert request_payload["plan_full_production_date"] == "2026-06-01"
|
||||
assert request_payload["actual_production_date"] == "2026-01-15"
|
||||
assert request_payload["actual_full_production_date"] == "2026-03-15"
|
||||
assert "ic_plan_first_production_date" not in request_payload
|
||||
assert "ic_plan_full_production_date" not in request_payload
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_project_detail_create_all_keeps_partial_results_when_schema_check_errors() -> None:
|
||||
collection = DataCollection("remote")
|
||||
|
||||
project = _ProjectNode(
|
||||
node_id="project-node-1",
|
||||
data_id="project-1",
|
||||
data={"id": "project-1"},
|
||||
context={"all_info": {"project_detail": []}},
|
||||
)
|
||||
await collection.add(project)
|
||||
|
||||
valid_node = ProjectDetailSyncNode(
|
||||
node_id="detail-node-1",
|
||||
data_id="local-detail-1",
|
||||
data={
|
||||
"id": "local-detail-1",
|
||||
"project_id": "project-1",
|
||||
"key": "plan_construction",
|
||||
"value": [{"date": "2026-01-01", "capacity": 1.0}],
|
||||
},
|
||||
)
|
||||
invalid_node = ProjectDetailSyncNode(
|
||||
node_id="detail-node-2",
|
||||
data_id="local-detail-2",
|
||||
data={
|
||||
"id": "local-detail-2",
|
||||
"project_id": "project-1",
|
||||
"key": "production",
|
||||
"value": [{"date": "2026-01-02", "capacity": 2.0}],
|
||||
},
|
||||
)
|
||||
|
||||
handler = ProjectDetailApiHandler()
|
||||
handler.set_collection(collection)
|
||||
handler.api_client = _RecordingApiClient()
|
||||
|
||||
class _ExplodingSchema:
|
||||
@classmethod
|
||||
def model_validate(cls, data):
|
||||
raise RuntimeError("schema exploded")
|
||||
|
||||
original_get_update_config = handler._get_update_config
|
||||
|
||||
def _patched_get_update_config(key):
|
||||
config = original_get_update_config(key)
|
||||
if key == "production":
|
||||
assert config is not None
|
||||
return _ExplodingSchema, config[1]
|
||||
return config
|
||||
|
||||
handler._get_update_config = _patched_get_update_config
|
||||
|
||||
results = await handler.create_all([valid_node, invalid_node])
|
||||
|
||||
assert len(results) == 2
|
||||
assert results[0].status == TaskStatus.IN_PROGRESS
|
||||
assert results[1].status == TaskStatus.FAILED
|
||||
assert results[1].error == "schema exploded"
|
||||
assert len(handler.api_client.calls) == 1
|
||||
assert handler.api_client.calls[0]["endpoint"] == "/project/plan_construction"
|
||||
|
||||
|
||||
def test_project_detail_load_allows_same_project_key_with_different_data_id() -> None:
|
||||
collection = DataCollection("remote")
|
||||
|
||||
|
||||
@@ -15,6 +15,7 @@ from sync_state_machine.domain.project_detail.sync_strategy import ProjectDetail
|
||||
from sync_state_machine.domain.project_detail.sync_node import ProjectDetailSyncNode
|
||||
from sync_state_machine.sync_system.config import UpdateDirection
|
||||
from sync_state_machine.sync_system.strategy_ops import BoundNodePair
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
def _fake_node(*, node_id: str, key: str):
|
||||
@@ -44,6 +45,19 @@ def _build_project_detail_node(*, node_id: str, data_id: str, key: str) -> Proje
|
||||
)
|
||||
|
||||
|
||||
class _ProjectSchema(BaseModel):
|
||||
id: str
|
||||
plan_production_date: str | None = None
|
||||
plan_full_production_date: str | None = None
|
||||
actual_production_date: str | None = None
|
||||
actual_full_production_date: str | None = None
|
||||
|
||||
|
||||
class _ProjectNode(SyncNode[_ProjectSchema]):
|
||||
node_type = "project"
|
||||
schema = _ProjectSchema
|
||||
|
||||
|
||||
@pytest_asyncio.fixture
|
||||
async def project_detail_env(tmp_path):
|
||||
persistence = SQLitePersistenceBackend(str(tmp_path / "project-detail.db"))
|
||||
@@ -144,4 +158,65 @@ async def test_project_detail_bind_does_not_push_group_production_plan_from_loca
|
||||
|
||||
assert created_nodes == []
|
||||
assert remote.filter(node_type="project_detail_data") == []
|
||||
assert await binding_manager.get_remote_id("project_detail_data", local_node.node_id) is None
|
||||
assert await binding_manager.get_remote_id("project_detail_data", local_node.node_id) is None
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_project_detail_create_remote_production_carries_project_plan_dates(project_detail_env) -> None:
|
||||
local, remote, binding_manager, _ = project_detail_env
|
||||
strategy = ProjectDetailSyncStrategy("project_detail_data", local, remote, binding_manager)
|
||||
|
||||
local_project = _ProjectNode(
|
||||
node_id="local-project-node",
|
||||
data_id="local-project-id",
|
||||
data={
|
||||
"id": "local-project-id",
|
||||
"plan_production_date": "2023-12-30",
|
||||
"plan_full_production_date": "2024-12-30",
|
||||
"actual_production_date": "2023-12-30",
|
||||
"actual_full_production_date": "2024-12-30",
|
||||
},
|
||||
)
|
||||
remote_project = _ProjectNode(
|
||||
node_id="remote-project-node",
|
||||
data_id="remote-project-id",
|
||||
data={"id": "remote-project-id"},
|
||||
)
|
||||
await local.add(local_project)
|
||||
await remote.add(remote_project)
|
||||
await binding_manager.bind("project", local_project.node_id, remote_project.node_id)
|
||||
|
||||
local_detail = ProjectDetailSyncNode(
|
||||
node_id="local-detail-node",
|
||||
data_id="local-detail-id",
|
||||
data={
|
||||
"id": "local-detail-id",
|
||||
"project_id": "local-project-id",
|
||||
"key": "production",
|
||||
"value": [{"date": "2023-12-30", "capacity": 3.0}],
|
||||
},
|
||||
)
|
||||
await local.add(local_detail)
|
||||
|
||||
created_remote = ProjectDetailSyncNode(
|
||||
node_id="remote-detail-node",
|
||||
data_id="",
|
||||
data={
|
||||
"id": "",
|
||||
"project_id": "remote-project-id",
|
||||
"key": "production",
|
||||
"value": [{"date": "2023-12-30", "capacity": 3.0}],
|
||||
},
|
||||
)
|
||||
await remote.add(created_remote)
|
||||
await binding_manager.bind("project_detail_data", local_detail.node_id, created_remote.node_id)
|
||||
|
||||
await strategy._enrich_created_production_nodes([created_remote], source_is_local=True)
|
||||
|
||||
created_data = created_remote.get_data()
|
||||
assert created_data is not None
|
||||
assert created_data["project_id"] == "remote-project-id"
|
||||
assert created_data["plan_production_date"] == "2023-12-30"
|
||||
assert created_data["plan_full_production_date"] == "2024-12-30"
|
||||
assert created_data["actual_production_date"] == "2023-12-30"
|
||||
assert created_data["actual_full_production_date"] == "2024-12-30"
|
||||
Reference in New Issue
Block a user