修改问题
This commit is contained in:
@@ -263,7 +263,7 @@ async def test_create_all_marks_duplicate_project_key_in_same_batch_as_error() -
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_create_node_reuses_existing_node_by_project_key_and_rebinds_data_id() -> None:
|
||||
async def test_create_node_raises_on_existing_project_key_with_different_data_id() -> None:
|
||||
collection = DataCollection("remote")
|
||||
|
||||
handler = ProjectDetailApiHandler()
|
||||
@@ -278,17 +278,15 @@ async def test_create_node_reuses_existing_node_by_project_key_and_rebinds_data_
|
||||
)
|
||||
await collection.add(existing)
|
||||
|
||||
created = handler._create_node(
|
||||
{
|
||||
"id": "remote-detail-99",
|
||||
"project_id": "project-1",
|
||||
"key": "plan_construction",
|
||||
"value": [{"date": "2026-01-01", "capacity": 21.0}],
|
||||
}
|
||||
)
|
||||
|
||||
assert created.node_id == "detail-node-old"
|
||||
assert created.data_id == "remote-detail-99"
|
||||
with pytest.raises(RuntimeError, match="project_detail_data load conflict"):
|
||||
handler._create_node(
|
||||
{
|
||||
"id": "remote-detail-99",
|
||||
"project_id": "project-1",
|
||||
"key": "plan_construction",
|
||||
"value": [{"date": "2026-01-01", "capacity": 21.0}],
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
|
||||
@@ -34,3 +34,20 @@ async def test_collection_filter_by_project_ids() -> None:
|
||||
assert collection.get("c1") is not None
|
||||
assert collection.get("p2_node") is not None
|
||||
assert collection.get("c2") is None
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_collection_update_clears_stale_data_id_index_for_same_node() -> None:
|
||||
register_test_domain()
|
||||
|
||||
collection = DataCollection("local")
|
||||
node = build_project_node("p1_node", "p1", name="P1", code="P1")
|
||||
await collection.add(node)
|
||||
|
||||
with node.allow_core_state_write("test_rebind"):
|
||||
node.data_id = "p2"
|
||||
|
||||
await collection.update(node)
|
||||
|
||||
assert collection.get_by_data_id("test_project", "p1") is None
|
||||
assert collection.get_by_data_id("test_project", "p2") is node
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
|
||||
import pytest
|
||||
|
||||
from sync_state_machine.common.collection import DataCollection
|
||||
@@ -95,14 +97,14 @@ class _DS(BaseDataSource):
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_load_all_continue_when_one_handler_fails(capsys) -> None:
|
||||
async def test_load_all_continue_when_one_handler_fails(caplog) -> None:
|
||||
ds = _DS()
|
||||
ds.add_handler(_FailHandler())
|
||||
ds.add_handler(_OkHandler())
|
||||
ds.set_collection(DataCollection("local"))
|
||||
|
||||
await ds.load_all(order=["fail", "ok"])
|
||||
with caplog.at_level(logging.INFO):
|
||||
await ds.load_all(order=["fail", "ok"])
|
||||
|
||||
out = capsys.readouterr().out
|
||||
assert "[fail] 加载失败" in out
|
||||
assert "Loaded nodes for ok: 0" in out
|
||||
assert "[fail] 加载失败" in caplog.text
|
||||
assert "Loaded nodes for ok: 0" in caplog.text
|
||||
|
||||
@@ -49,6 +49,10 @@ class _StubStrategy:
|
||||
async def update(self):
|
||||
return []
|
||||
|
||||
def normalize_compare_payload(self, data, data_id_map=None):
|
||||
del data_id_map
|
||||
return dict(data or {})
|
||||
|
||||
|
||||
class _FakePersistence:
|
||||
async def close(self) -> None:
|
||||
|
||||
@@ -150,11 +150,11 @@ async def test_post_check_uses_same_id_resolver_as_create_update_for_depend_ids(
|
||||
|
||||
result = await evaluate_consistency_for_node_type(
|
||||
node_type="construction_task",
|
||||
strategy=strategy,
|
||||
local_collection=local_collection,
|
||||
remote_collection=remote_collection,
|
||||
binding_manager=binding_manager,
|
||||
logger=logging.getLogger("sync_state_machine"),
|
||||
normalize_compare_payload=strategy.normalize_compare_payload,
|
||||
depend_fields={"contract_id": "contract"},
|
||||
compare_to_remote=True,
|
||||
)
|
||||
@@ -237,11 +237,11 @@ async def test_post_check_ignores_only_explicit_configured_fields(tmp_path: Path
|
||||
|
||||
result = await evaluate_consistency_for_node_type(
|
||||
node_type="contract",
|
||||
strategy=strategy,
|
||||
local_collection=local_collection,
|
||||
remote_collection=remote_collection,
|
||||
binding_manager=binding_manager,
|
||||
logger=logging.getLogger("sync_state_machine"),
|
||||
normalize_compare_payload=strategy.normalize_compare_payload,
|
||||
compare_to_remote=True,
|
||||
ignore_fields=["code"],
|
||||
)
|
||||
|
||||
@@ -94,3 +94,41 @@ async def test_project_detail_create_failure_preserves_push_id_and_biz_id() -> N
|
||||
assert result.metadata["biz_id"] == "project-1"
|
||||
assert result.metadata["project_id"] == "project-1"
|
||||
assert result.metadata["key"] == "production"
|
||||
|
||||
|
||||
def test_project_detail_load_conflict_raises_on_same_project_key_different_data_id() -> None:
|
||||
collection = DataCollection("remote")
|
||||
|
||||
project = _ProjectNode(
|
||||
node_id="project-node-1",
|
||||
data_id="project-1",
|
||||
data={"id": "project-1"},
|
||||
)
|
||||
collection._nodes[project.node_id] = project
|
||||
|
||||
existing_detail = ProjectDetailSyncNode(
|
||||
node_id="detail-node-1",
|
||||
data_id="detail-old",
|
||||
data={
|
||||
"id": "detail-old",
|
||||
"project_id": "project-1",
|
||||
"key": "actual_construction",
|
||||
"value": [{"date": "2026-01-01", "capacity": 1.0}],
|
||||
},
|
||||
context={"project_id": "project-1"},
|
||||
)
|
||||
collection._nodes[existing_detail.node_id] = existing_detail
|
||||
collection._data_id_to_node_id[existing_detail.data_id] = existing_detail.node_id
|
||||
|
||||
handler = ProjectDetailApiHandler()
|
||||
handler.set_collection(collection)
|
||||
|
||||
with pytest.raises(RuntimeError, match="project_detail_data load conflict"):
|
||||
handler._create_node(
|
||||
{
|
||||
"id": "detail-new",
|
||||
"project_id": "project-1",
|
||||
"key": "actual_construction",
|
||||
"value": [{"date": "2026-01-02", "capacity": 2.0}],
|
||||
}
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user