修改多个问题,优化代码结构

This commit is contained in:
strepsiades
2026-04-01 16:23:43 +08:00
parent 8f4727a772
commit 7c49df94fc
29 changed files with 968 additions and 504 deletions
+2 -2
View File
@@ -110,7 +110,7 @@ async def test_api_client_logs_one_line_request_summary_in_debug_mode(caplog: py
fake_client = _FakeAsyncClient()
client._client = fake_client
with caplog.at_level(logging.DEBUG, logger=logger_name):
with caplog.at_level(logging.INFO, logger=logger_name):
await client.request("GET", "/ping")
assert "ApiClient request: method=GET endpoint=/ping elapsed=" in caplog.text
assert "🔎 ApiClient request: method=GET endpoint=/ping elapsed=" in caplog.text
@@ -20,7 +20,7 @@ class _DS(BaseDataSource):
class _FailStrategy(DefaultSyncStrategy):
schema = object # type: ignore
async def bind(self, **kwargs):
async def bind(self):
raise RuntimeError("bind failed")
async def create(self):
@@ -39,7 +39,7 @@ class _OkStrategy(DefaultSyncStrategy):
self.create_calls = 0
self.update_calls = 0
async def bind(self, **kwargs):
async def bind(self):
self.bound = True
async def create(self):
+96 -17
View File
@@ -1,26 +1,62 @@
from types import SimpleNamespace
from typing import cast
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
import pytest_asyncio
import sync_state_machine.domain # noqa: F401
from sync_state_machine.common.binding import BindingManager
from sync_state_machine.common.collection import DataCollection
from sync_state_machine.common.sync_node import SyncNode
from sync_state_machine.common.sqlite_backend import SQLitePersistenceBackend
from sync_state_machine.common.types import BindingStatus
from sync_state_machine.domain.project_detail.sync_strategy import ProjectDetailSyncStrategy
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
def _fake_node(*, node_id: str, key: str):
data = {"id": node_id, "key": key}
return SimpleNamespace(
node_id=node_id,
data_id=node_id,
action=None,
context={},
get_data=lambda: data,
return cast(
SyncNode,
SimpleNamespace(
node_id=node_id,
data_id=node_id,
action=None,
context={},
get_data=lambda: data,
),
)
def _build_project_detail_node(*, node_id: str, data_id: str, key: str) -> ProjectDetailSyncNode:
return ProjectDetailSyncNode(
node_id=node_id,
data_id=data_id,
data={
"id": data_id,
"project_id": "project-1",
"key": key,
"value": [{"year": 2026, "capacity": 4.0}],
},
)
@pytest_asyncio.fixture
async def project_detail_env(tmp_path):
persistence = SQLitePersistenceBackend(str(tmp_path / "project-detail.db"))
await persistence.initialize()
local = DataCollection("local", persistence=persistence, auto_persist=False)
remote = DataCollection("remote", persistence=persistence, auto_persist=False)
binding_manager = BindingManager(persistence, auto_persist=False)
yield local, remote, binding_manager, persistence
await persistence.close()
@pytest.mark.asyncio
async def test_project_detail_update_partitions_group_production_plans_before_generic_logic():
async def test_project_detail_process_update_pairs_partitions_group_production_plans_before_generic_logic():
strategy = ProjectDetailSyncStrategy("project_detail_data", MagicMock(), MagicMock(), MagicMock())
pull_pair = BoundNodePair(
@@ -35,13 +71,6 @@ async def test_project_detail_update_partitions_group_production_plans_before_ge
generic_node = SimpleNamespace(node_id="generic-update")
with patch.object(
strategy,
"collect_update_pairs",
new=AsyncMock(return_value=[pull_pair, generic_pair]),
), patch(
"sync_state_machine.domain.project_detail.sync_strategy.build_data_id_normalization_map",
new=AsyncMock(return_value={}),
), patch.object(
strategy,
"prepare_update_for_direction",
new=AsyncMock(return_value=pull_node),
@@ -50,9 +79,12 @@ async def test_project_detail_update_partitions_group_production_plans_before_ge
"update_pair",
new=AsyncMock(return_value=[generic_node]),
) as mock_update_pair:
updated_nodes = await strategy.update()
updated_by_id = await strategy.process_update_pairs([pull_pair, generic_pair], {})
assert updated_nodes == [pull_node, generic_node]
assert updated_by_id == {
"pull-update": pull_node,
"generic-update": generic_node,
}
mock_prepare.assert_awaited_once_with(
pull_pair.local_node,
pull_pair.remote_node,
@@ -65,4 +97,51 @@ async def test_project_detail_update_partitions_group_production_plans_before_ge
def test_project_detail_domain_option_is_typed_schema() -> None:
strategy = ProjectDetailSyncStrategy("project_detail_data", MagicMock(), MagicMock(), MagicMock())
assert strategy.domain_option.pull_group_production_plans is True
assert strategy.domain_option.pull_group_production_plans is True
@pytest.mark.asyncio
async def test_project_detail_bind_create_pulls_group_production_plan_from_remote(project_detail_env) -> None:
local, remote, binding_manager, _ = project_detail_env
strategy = ProjectDetailSyncStrategy("project_detail_data", local, remote, binding_manager)
strategy.config.depend_fields = {}
remote_node = _build_project_detail_node(
node_id="remote-pull",
data_id="detail-remote-pull",
key="ensure_production",
)
await remote.add(remote_node)
await strategy.bind()
created_nodes = await strategy.create()
assert len(created_nodes) == 1
created_node = created_nodes[0]
created_data = created_node.get_data()
assert created_node.node_type == "project_detail_data"
assert created_data is not None
assert created_data["key"] == "ensure_production"
assert remote_node.binding_status == BindingStatus.PEER_CREATING
assert await binding_manager.get_local_id("project_detail_data", remote_node.node_id) == created_node.node_id
@pytest.mark.asyncio
async def test_project_detail_bind_does_not_push_group_production_plan_from_local(project_detail_env) -> None:
local, remote, binding_manager, _ = project_detail_env
strategy = ProjectDetailSyncStrategy("project_detail_data", local, remote, binding_manager)
strategy.config.depend_fields = {}
local_node = _build_project_detail_node(
node_id="local-pull",
data_id="detail-local-pull",
key="ensure_production",
)
await local.add(local_node)
await strategy.bind()
created_nodes = await strategy.create()
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
+49 -12
View File
@@ -2,34 +2,32 @@ import pytest
from types import SimpleNamespace
from unittest.mock import AsyncMock, patch, MagicMock
from sync_state_machine.domain.company.sync_strategy import CompanySyncStrategy
from sync_state_machine.sync_system.strategy_ops.update_ops import BoundNodePair, build_merged_update_payload, run_update
from sync_state_machine.sync_system.strategy_ops.update_ops import BoundNodePair, build_merged_update_payload
from sync_state_machine.domain.construction.sync_strategy import ConstructionTaskSyncStrategy
from sync_state_machine.domain.project_detail.sync_strategy import ProjectDetailSyncStrategy
from sync_state_machine.sync_system.config import UpdateDirection
from schemas.project_extensions.project_detail import ProjectDetailKey
@pytest.mark.asyncio
async def test_run_update_delegates_to_strategy_pair_planner():
async def test_default_strategy_update_orchestrates_pairs_in_strategy_layer():
local_node = SimpleNamespace(node_id="local-1")
remote_node = SimpleNamespace(node_id="remote-1")
strategy = MagicMock()
strategy.node_type = "TestNode"
strategy.local_collection = MagicMock()
strategy.remote_collection = MagicMock()
strategy.binding_manager = MagicMock()
strategy.reset_schema_diff_validator = MagicMock()
strategy.emit_schema_diff_report = MagicMock()
strategy = CompanySyncStrategy("company", MagicMock(), MagicMock(), MagicMock())
strategy.ensure_runtime = MagicMock()
updated_node = SimpleNamespace(node_id="target-1")
strategy.update_pair = AsyncMock(return_value=[updated_node])
with patch(
'sync_state_machine.sync_system.strategy_ops.update_ops.build_data_id_normalization_map',
'sync_state_machine.sync_system.strategy.build_data_id_normalization_map',
new=AsyncMock(return_value={"project": "remote-project"}),
), patch(
'sync_state_machine.sync_system.strategy_ops.update_ops.collect_bound_node_pairs',
'sync_state_machine.sync_system.strategy.collect_bound_node_pairs',
new=AsyncMock(return_value=[BoundNodePair(local_node=local_node, remote_node=remote_node)]),
), patch(
'sync_state_machine.sync_system.strategy.emit_schema_diff_report',
):
updated = await run_update(strategy)
updated = await strategy.update()
assert updated == [updated_node]
@@ -155,3 +153,42 @@ def test_project_detail_compare_payload_normalizes_enum_key_values():
assert normalized["key"] == "plan_construction"
assert normalized["value"] == [{"date": "2023-06-28", "capacity": 4.0}]
@pytest.mark.asyncio
async def test_project_detail_process_update_pairs_partitions_group_production_plans_before_generic_logic():
strategy = ProjectDetailSyncStrategy("project_detail_data", MagicMock(), MagicMock(), MagicMock())
pull_pair = BoundNodePair(
local_node=SimpleNamespace(node_id="local-pull", get_data=lambda: {"key": "ensure_production"}),
remote_node=SimpleNamespace(node_id="remote-pull", get_data=lambda: {"key": "ensure_production"}),
)
generic_pair = BoundNodePair(
local_node=SimpleNamespace(node_id="local-generic", get_data=lambda: {"key": "production"}),
remote_node=SimpleNamespace(node_id="remote-generic", get_data=lambda: {"key": "production"}),
)
pull_node = SimpleNamespace(node_id="pull-update")
generic_node = SimpleNamespace(node_id="generic-update")
with patch.object(
strategy,
"prepare_update_for_direction",
new=AsyncMock(return_value=pull_node),
) as mock_prepare, patch.object(
strategy,
"update_pair",
new=AsyncMock(return_value=[generic_node]),
) as mock_update_pair:
updated_by_id = await strategy.process_update_pairs([pull_pair, generic_pair], {})
assert updated_by_id == {
"pull-update": pull_node,
"generic-update": generic_node,
}
mock_prepare.assert_awaited_once_with(
pull_pair.local_node,
pull_pair.remote_node,
UpdateDirection.PULL,
{},
)
mock_update_pair.assert_awaited_once_with(generic_pair.local_node, generic_pair.remote_node, {})