fix(sector-radar): 增加安全源契约诊断
This commit is contained in:
@@ -1,7 +1,10 @@
|
||||
import logging
|
||||
from collections.abc import Sequence
|
||||
from datetime import UTC, date, datetime, timedelta
|
||||
from decimal import Decimal
|
||||
|
||||
import pytest
|
||||
|
||||
from zhixing_server.modules.sector_radar.application.build import (
|
||||
BuildSectorRadar,
|
||||
BuildSectorRadarCommand,
|
||||
@@ -19,6 +22,7 @@ from zhixing_server.modules.sector_radar.domain.source import (
|
||||
MoneyflowDcRow,
|
||||
SectorIndexRow,
|
||||
SectorMemberRow,
|
||||
SourceContractError,
|
||||
SourceResult,
|
||||
SourceSnapshot,
|
||||
StockBasicRow,
|
||||
@@ -284,6 +288,37 @@ def test_successful_build_is_idempotent_and_failed_retry_preserves_last_good() -
|
||||
assert any(item.status is PublicationStatus.FAILED for item in repository.publications.values())
|
||||
|
||||
|
||||
def test_source_contract_failure_is_logged_with_safe_build_context(
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
) -> None:
|
||||
class InvalidDailySource(FakeRadarSource):
|
||||
def fetch_daily(self, trade_date: date) -> SourceResult[DailyRow]:
|
||||
self.calls.append("daily")
|
||||
raise SourceContractError("daily returned duplicate business keys")
|
||||
|
||||
repository = InMemorySectorRadarRepository()
|
||||
caplog.set_level(
|
||||
logging.ERROR,
|
||||
logger="zhixing_server.modules.sector_radar.application.build",
|
||||
)
|
||||
|
||||
failed = BuildSectorRadar(
|
||||
InvalidDailySource(),
|
||||
repository,
|
||||
today=TARGET_DATE,
|
||||
now_fn=lambda: NOW,
|
||||
).execute(BuildSectorRadarCommand(trade_date=TARGET_DATE))
|
||||
|
||||
messages = "\n".join(record.getMessage() for record in caplog.records)
|
||||
assert failed.status == "failed"
|
||||
assert failed.outcomes[0].error_message == "input or source contract validation failed"
|
||||
assert "sector_radar_source_group_contract_failed" in messages
|
||||
assert "source_group=daily" in messages
|
||||
assert "publication_id=radar-20260828-running-" in messages
|
||||
assert "validation=daily returned duplicate business keys" in messages
|
||||
assert len(caplog.records) == 1
|
||||
|
||||
|
||||
def test_partial_coverage_and_lock_have_distinct_exit_codes() -> None:
|
||||
repository = InMemorySectorRadarRepository()
|
||||
partial = BuildSectorRadar(
|
||||
|
||||
Reference in New Issue
Block a user