chore: pre-existing in-tree work (event-study/research/vintages/prices + migration script + integrity docs)
Committing the prior uncommitted working-tree state that predates this session's data-source work (was already modified/untracked at session start) so the tree is clean before push. Includes: event-study + research report integrity/forward observation work, prices tests, research hash migration script, and the 2026-08-23/24 engineering-log + test-evidence notes. Verified green as part of the full 362-test suite.
This commit is contained in:
1 parent
dbb787c50a
commit
ead9aeb25c
22 files changed
+2326
-66
No files matched your search
@@ -94,6 +94,39 @@ class EventStudyTests(unittest.TestCase):
|
||||
result = run_event_study(events, prices, windows=(1,), min_events=1)
|
||||
self.assertAlmostEqual(result["windows"]["1"]["gross_return"], 101 / 100 - 1, places=8)
|
||||
|
||||
def test_event_study_requires_known_at_for_point_in_time_prices(self):
|
||||
events = [{"event_id": "pit", "published_at": "2026-01-01T08:00:00+07:00", "signals": [{"symbol": "AOT", "target_weight": 1.0}]}]
|
||||
prices = {
|
||||
"AOT": [
|
||||
{"session_date": "2026-01-01", "close": 100},
|
||||
{"session_date": "2026-01-02", "close": 105, "known_at": "2026-01-02T17:00:00+07:00"},
|
||||
]
|
||||
}
|
||||
with self.assertRaisesRegex(EventStudyError, "known_at"):
|
||||
run_event_study(events, prices, windows=(1,), min_events=1, require_price_known_at=True)
|
||||
|
||||
def test_event_study_rejects_price_known_after_session_as_lookahead(self):
|
||||
events = [{"event_id": "lookahead", "published_at": "2026-01-01T08:00:00+07:00", "signals": [{"symbol": "AOT", "target_weight": 1.0}]}]
|
||||
prices = {
|
||||
"AOT": [
|
||||
{"session_date": "2026-01-01", "close": 100, "known_at": "2026-01-01T17:00:00+07:00", "market_timezone": "Asia/Bangkok"},
|
||||
{"session_date": "2026-01-02", "close": 105, "known_at": "2026-01-03T09:00:00+07:00", "market_timezone": "Asia/Bangkok"},
|
||||
]
|
||||
}
|
||||
with self.assertRaisesRegex(EventStudyError, "known_at"):
|
||||
run_event_study(events, prices, windows=(1,), min_events=1, require_price_known_at=True)
|
||||
|
||||
def test_event_study_checks_known_at_in_market_timezone(self):
|
||||
events = [{"event_id": "timezone-lookahead", "published_at": "2026-01-01T08:00:00+07:00", "signals": [{"symbol": "AOT", "target_weight": 1.0}]}]
|
||||
prices = {
|
||||
"AOT": [
|
||||
{"session_date": "2026-01-01", "close": 100, "known_at": "2026-01-01T18:30:00Z", "market_timezone": "Asia/Bangkok"},
|
||||
{"session_date": "2026-01-02", "close": 105, "known_at": "2026-01-02T17:00:00+07:00", "market_timezone": "Asia/Bangkok"},
|
||||
]
|
||||
}
|
||||
with self.assertRaisesRegex(EventStudyError, "known_at"):
|
||||
run_event_study(events, prices, windows=(1,), min_events=1, require_price_known_at=True)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -5,11 +5,15 @@ import tempfile
|
||||
import unittest
|
||||
from datetime import datetime, timezone
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from app.prices import (
|
||||
DEFAULT_SYMBOL_MAP,
|
||||
PIT_ARCHIVE_CONTRACT,
|
||||
PriceSnapshotStore,
|
||||
PriceSourceError,
|
||||
_normalized_snapshot_hash,
|
||||
_observation_id,
|
||||
collect_price_snapshot,
|
||||
normalize_yahoo_chart,
|
||||
trading_dates,
|
||||
@@ -19,6 +23,33 @@ FIXTURE = Path(__file__).parent / "fixtures" / "yahoo_chart_aot.json"
|
||||
PAYLOAD = json.loads(FIXTURE.read_text(encoding="utf-8"))
|
||||
|
||||
|
||||
def valid_non_pit_snapshot(snapshot_id="prices-valid", retrieved_at="2026-02-01T00:00:00+00:00"):
|
||||
return {
|
||||
"schema_version": 1,
|
||||
"source": {
|
||||
"source_id": "test.prices",
|
||||
"source_url": "https://example.test/prices",
|
||||
"snapshot_id": snapshot_id,
|
||||
"retrieved_at": retrieved_at,
|
||||
"period_start": "2026-01-01",
|
||||
"period_end": "2026-01-02",
|
||||
"quality": "revised_vendor_history",
|
||||
"point_in_time": False,
|
||||
"parser_version": "test-v1",
|
||||
"adjusted_prices": False,
|
||||
},
|
||||
"series": {
|
||||
"AOT": {
|
||||
"canonical_symbol": "AOT",
|
||||
"provider_symbol": "AOT.BK",
|
||||
"raw_payload_hash": hashlib.sha256(b"aot").hexdigest(),
|
||||
"timezone": "Asia/Bangkok",
|
||||
"bars": [{"date": "2026-01-01", "close": 100.0}],
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
class FakePriceProvider:
|
||||
def __init__(self, payload):
|
||||
self.payload = payload
|
||||
@@ -75,6 +106,43 @@ class PriceSnapshotTests(unittest.TestCase):
|
||||
with self.assertRaisesRegex(PriceSourceError, "result"):
|
||||
normalize_yahoo_chart(payload, canonical_symbol="AOT", provider_symbol="AOT.BK", retrieved_at="2026-08-23T06:00:00+00:00", raw_payload_hash="a" * 64)
|
||||
|
||||
def test_normalize_yahoo_chart_rejects_malformed_provider_shapes_and_numbers(self):
|
||||
cases = []
|
||||
|
||||
payload = copy.deepcopy(PAYLOAD)
|
||||
payload["chart"]["result"][0]["meta"] = []
|
||||
cases.append(("meta", payload))
|
||||
|
||||
payload = copy.deepcopy(PAYLOAD)
|
||||
payload["chart"]["result"][0]["indicators"]["quote"][0] = []
|
||||
cases.append(("quote", payload))
|
||||
|
||||
payload = copy.deepcopy(PAYLOAD)
|
||||
payload["chart"]["result"][0]["timestamp"][0] = 10**100
|
||||
cases.append(("timestamp overflow", payload))
|
||||
|
||||
for label, field, value in (
|
||||
("boolean timestamp", "timestamp", True),
|
||||
("boolean close", "close", True),
|
||||
("boolean volume", "volume", True),
|
||||
):
|
||||
payload = copy.deepcopy(PAYLOAD)
|
||||
if field == "timestamp":
|
||||
payload["chart"]["result"][0][field][0] = value
|
||||
else:
|
||||
payload["chart"]["result"][0]["indicators"]["quote"][0][field][0] = value
|
||||
cases.append((label, payload))
|
||||
|
||||
for label, payload in cases:
|
||||
with self.subTest(label=label), self.assertRaises(PriceSourceError):
|
||||
normalize_yahoo_chart(
|
||||
payload,
|
||||
canonical_symbol="AOT",
|
||||
provider_symbol="AOT.BK",
|
||||
retrieved_at="2026-08-23T06:00:00+00:00",
|
||||
raw_payload_hash="a" * 64,
|
||||
)
|
||||
|
||||
def test_collector_persists_revised_vendor_history_snapshot(self):
|
||||
provider = FakePriceProvider(PAYLOAD)
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
@@ -93,6 +161,231 @@ class PriceSnapshotTests(unittest.TestCase):
|
||||
self.assertEqual(len(manifest["snapshots"]), 1)
|
||||
self.assertEqual(len(provider.calls), 2)
|
||||
|
||||
def test_collector_records_observation_history_for_unchanged_payload(self):
|
||||
provider = FakePriceProvider(PAYLOAD)
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
root = Path(temp_dir)
|
||||
collect_price_snapshot(
|
||||
root,
|
||||
start="2026-01-01",
|
||||
end="2026-01-06",
|
||||
symbol_map={"AOT": "AOT.BK", "SET50": "^SET.BK"},
|
||||
provider=provider,
|
||||
retrieved_at="2026-08-23T06:00:00+00:00",
|
||||
)
|
||||
collect_price_snapshot(
|
||||
root,
|
||||
start="2026-01-01",
|
||||
end="2026-01-06",
|
||||
symbol_map={"AOT": "AOT.BK", "SET50": "^SET.BK"},
|
||||
provider=provider,
|
||||
retrieved_at="2026-08-24T06:00:00+00:00",
|
||||
)
|
||||
|
||||
store = PriceSnapshotStore(root)
|
||||
manifest = store.load_manifest()
|
||||
observations = manifest["observations"]
|
||||
self.assertEqual(len(observations), 2)
|
||||
self.assertEqual(observations[0]["revision_status"], "initial")
|
||||
self.assertEqual(observations[1]["revision_status"], "unchanged")
|
||||
self.assertEqual(observations[1]["previous_raw_payload_hash"], observations[0]["raw_payload_hash"])
|
||||
self.assertFalse(observations[1]["diff"]["normalized_content_changed"])
|
||||
entry = store.latest_snapshot_entry()
|
||||
self.assertEqual(entry["first_seen_at"], "2026-08-23T06:00:00+00:00")
|
||||
self.assertEqual(entry["last_seen_at"], "2026-08-24T06:00:00+00:00")
|
||||
self.assertEqual(entry["observation_count"], 2)
|
||||
|
||||
def test_collector_records_revision_diff_without_overwriting_previous_snapshot(self):
|
||||
changed_payload = copy.deepcopy(PAYLOAD)
|
||||
changed_payload["chart"]["result"][0]["indicators"]["quote"][0]["close"][0] = 61.0
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
root = Path(temp_dir)
|
||||
collect_price_snapshot(
|
||||
root,
|
||||
start="2026-01-01",
|
||||
end="2026-01-06",
|
||||
symbol_map={"AOT": "AOT.BK", "SET50": "^SET.BK"},
|
||||
provider=FakePriceProvider(PAYLOAD),
|
||||
retrieved_at="2026-08-23T06:00:00+00:00",
|
||||
)
|
||||
revised = collect_price_snapshot(
|
||||
root,
|
||||
start="2026-01-01",
|
||||
end="2026-01-06",
|
||||
symbol_map={"AOT": "AOT.BK", "SET50": "^SET.BK"},
|
||||
provider=FakePriceProvider(changed_payload),
|
||||
retrieved_at="2026-08-24T06:00:00+00:00",
|
||||
)
|
||||
|
||||
store = PriceSnapshotStore(root)
|
||||
manifest = store.load_manifest()
|
||||
observations = manifest["observations"]
|
||||
self.assertEqual(len(manifest["snapshots"]), 2)
|
||||
self.assertEqual(len(observations), 2)
|
||||
self.assertEqual(observations[1]["revision_status"], "revised")
|
||||
self.assertTrue(observations[1]["diff"]["normalized_content_changed"])
|
||||
self.assertIn("series.AOT.bars[0].close", observations[1]["diff"]["changed_paths"])
|
||||
self.assertEqual(observations[1]["previous_snapshot_id"], observations[0]["snapshot_id"])
|
||||
self.assertNotEqual(revised["source"]["snapshot_id"], observations[0]["snapshot_id"])
|
||||
self.assertEqual(store.load_snapshot(observations[0]["snapshot_id"])["series"]["AOT"]["bars"][0]["close"], 61.5)
|
||||
|
||||
def test_price_store_rejects_tampered_observation_audit_entry(self):
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
root = Path(temp_dir)
|
||||
collect_price_snapshot(
|
||||
root,
|
||||
start="2026-01-01",
|
||||
end="2026-01-06",
|
||||
symbol_map={"AOT": "AOT.BK", "SET50": "^SET.BK"},
|
||||
provider=FakePriceProvider(PAYLOAD),
|
||||
retrieved_at="2026-08-23T06:00:00+00:00",
|
||||
)
|
||||
store = PriceSnapshotStore(root)
|
||||
manifest = store.load_manifest()
|
||||
manifest["observations"][0]["diff"]["changed_paths"] = ["series.AOT.bars[0].close"]
|
||||
store.manifest_path.write_text(json.dumps(manifest), encoding="utf-8")
|
||||
|
||||
with self.assertRaises(PriceSourceError):
|
||||
store.list_observations()
|
||||
|
||||
def test_price_store_rejects_recomputed_observation_id_with_mismatched_predecessor(self):
|
||||
changed_payload = copy.deepcopy(PAYLOAD)
|
||||
changed_payload["chart"]["result"][0]["indicators"]["quote"][0]["close"][0] = 61.0
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
root = Path(temp_dir)
|
||||
collect_price_snapshot(
|
||||
root,
|
||||
start="2026-01-01",
|
||||
end="2026-01-06",
|
||||
symbol_map={"AOT": "AOT.BK", "SET50": "^SET.BK"},
|
||||
provider=FakePriceProvider(PAYLOAD),
|
||||
retrieved_at="2026-08-23T06:00:00+00:00",
|
||||
)
|
||||
collect_price_snapshot(
|
||||
root,
|
||||
start="2026-01-01",
|
||||
end="2026-01-06",
|
||||
symbol_map={"AOT": "AOT.BK", "SET50": "^SET.BK"},
|
||||
provider=FakePriceProvider(changed_payload),
|
||||
retrieved_at="2026-08-24T06:00:00+00:00",
|
||||
)
|
||||
store = PriceSnapshotStore(root)
|
||||
manifest = store.load_manifest()
|
||||
tampered = manifest["observations"][1]
|
||||
tampered["previous_raw_payload_hash"] = "0" * 64
|
||||
tampered["observation_id"] = _observation_id({key: value for key, value in tampered.items() if key != "observation_id"})
|
||||
store.manifest_path.write_text(json.dumps(manifest), encoding="utf-8")
|
||||
|
||||
with self.assertRaisesRegex(PriceSourceError, "previous.*hash|observation"):
|
||||
store.list_observations()
|
||||
|
||||
def test_price_store_rejects_recomputed_observation_id_with_tampered_diff(self):
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
root = Path(temp_dir)
|
||||
collect_price_snapshot(
|
||||
root,
|
||||
start="2026-01-01",
|
||||
end="2026-01-06",
|
||||
symbol_map={"AOT": "AOT.BK", "SET50": "^SET.BK"},
|
||||
provider=FakePriceProvider(PAYLOAD),
|
||||
retrieved_at="2026-08-23T06:00:00+00:00",
|
||||
)
|
||||
store = PriceSnapshotStore(root)
|
||||
manifest = store.load_manifest()
|
||||
tampered = manifest["observations"][0]
|
||||
tampered["diff"]["changed_paths"] = ["series.AOT.bars[0].close"]
|
||||
tampered["observation_id"] = _observation_id({key: value for key, value in tampered.items() if key != "observation_id"})
|
||||
store.manifest_path.write_text(json.dumps(manifest), encoding="utf-8")
|
||||
|
||||
with self.assertRaisesRegex(PriceSourceError, "diff|observation"):
|
||||
store.list_observations()
|
||||
|
||||
def test_price_store_rejects_malformed_observation_id_types_without_type_error(self):
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
root = Path(temp_dir)
|
||||
collect_price_snapshot(
|
||||
root,
|
||||
start="2026-01-01",
|
||||
end="2026-01-06",
|
||||
symbol_map={"AOT": "AOT.BK", "SET50": "^SET.BK"},
|
||||
provider=FakePriceProvider(PAYLOAD),
|
||||
retrieved_at="2026-08-23T06:00:00+00:00",
|
||||
)
|
||||
store = PriceSnapshotStore(root)
|
||||
manifest = store.load_manifest()
|
||||
tampered = manifest["observations"][0]
|
||||
tampered["snapshot_id"] = []
|
||||
tampered["observation_id"] = _observation_id({key: value for key, value in tampered.items() if key != "observation_id"})
|
||||
store.manifest_path.write_text(json.dumps(manifest), encoding="utf-8")
|
||||
|
||||
with self.assertRaisesRegex(PriceSourceError, "observation"):
|
||||
store.list_observations()
|
||||
|
||||
def test_price_store_rejects_observation_history_truncation(self):
|
||||
changed_payload = copy.deepcopy(PAYLOAD)
|
||||
changed_payload["chart"]["result"][0]["indicators"]["quote"][0]["close"][0] = 61.0
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
root = Path(temp_dir)
|
||||
collect_price_snapshot(
|
||||
root,
|
||||
start="2026-01-01",
|
||||
end="2026-01-06",
|
||||
symbol_map={"AOT": "AOT.BK", "SET50": "^SET.BK"},
|
||||
provider=FakePriceProvider(PAYLOAD),
|
||||
retrieved_at="2026-08-23T06:00:00+00:00",
|
||||
)
|
||||
collect_price_snapshot(
|
||||
root,
|
||||
start="2026-01-01",
|
||||
end="2026-01-06",
|
||||
symbol_map={"AOT": "AOT.BK", "SET50": "^SET.BK"},
|
||||
provider=FakePriceProvider(changed_payload),
|
||||
retrieved_at="2026-08-24T06:00:00+00:00",
|
||||
)
|
||||
store = PriceSnapshotStore(root)
|
||||
manifest = store.load_manifest()
|
||||
manifest["observations"] = manifest["observations"][:-1]
|
||||
store.manifest_path.write_text(json.dumps(manifest), encoding="utf-8")
|
||||
|
||||
with self.assertRaisesRegex(PriceSourceError, "observation"):
|
||||
store.list_observations()
|
||||
|
||||
def test_collector_rejects_out_of_order_observation_after_future_capture(self):
|
||||
revised_payload = copy.deepcopy(PAYLOAD)
|
||||
revised_payload["chart"]["result"][0]["indicators"]["quote"][0]["close"][0] = 61.0
|
||||
late_payload = copy.deepcopy(PAYLOAD)
|
||||
late_payload["chart"]["result"][0]["indicators"]["quote"][0]["close"][0] = 62.0
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
root = Path(temp_dir)
|
||||
collect_price_snapshot(
|
||||
root,
|
||||
start="2026-01-01",
|
||||
end="2026-01-06",
|
||||
symbol_map={"AOT": "AOT.BK", "SET50": "^SET.BK"},
|
||||
provider=FakePriceProvider(PAYLOAD),
|
||||
retrieved_at="2026-08-23T06:00:00+00:00",
|
||||
)
|
||||
collect_price_snapshot(
|
||||
root,
|
||||
start="2026-01-01",
|
||||
end="2026-01-06",
|
||||
symbol_map={"AOT": "AOT.BK", "SET50": "^SET.BK"},
|
||||
provider=FakePriceProvider(revised_payload),
|
||||
retrieved_at="2026-08-24T06:00:00+00:00",
|
||||
)
|
||||
before = json.loads((root / "manifest.json").read_text(encoding="utf-8"))
|
||||
with self.assertRaisesRegex(PriceSourceError, "immutable predecessor"):
|
||||
collect_price_snapshot(
|
||||
root,
|
||||
start="2026-01-01",
|
||||
end="2026-01-06",
|
||||
symbol_map={"AOT": "AOT.BK", "SET50": "^SET.BK"},
|
||||
provider=FakePriceProvider(late_payload),
|
||||
retrieved_at="2026-08-22T06:00:00+00:00",
|
||||
)
|
||||
after = json.loads((root / "manifest.json").read_text(encoding="utf-8"))
|
||||
self.assertEqual(after, before)
|
||||
|
||||
def test_price_store_rejects_snapshot_with_mismatched_raw_hash(self):
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
store = PriceSnapshotStore(Path(temp_dir))
|
||||
@@ -100,6 +393,469 @@ class PriceSnapshotTests(unittest.TestCase):
|
||||
with self.assertRaisesRegex(PriceSourceError, "raw payload hash"):
|
||||
store.persist(snapshot, {"AOT.BK": b"different"})
|
||||
|
||||
def test_price_store_rejects_same_raw_hash_with_changed_stable_content(self):
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
root = Path(temp_dir)
|
||||
store = PriceSnapshotStore(root)
|
||||
snapshot = {
|
||||
"schema_version": 1,
|
||||
"source": {
|
||||
"source_id": "test.prices",
|
||||
"snapshot_id": "prices-same-raw",
|
||||
"retrieved_at": "2026-02-01T00:00:00+00:00",
|
||||
"period_start": "2026-01-01",
|
||||
"period_end": "2026-01-01",
|
||||
"quality": "revised_vendor_history",
|
||||
"point_in_time": False,
|
||||
},
|
||||
"series": {
|
||||
"AOT": {
|
||||
"canonical_symbol": "AOT",
|
||||
"provider_symbol": "AOT.BK",
|
||||
"raw_payload_hash": hashlib.sha256(b"aot").hexdigest(),
|
||||
"timezone": "Asia/Bangkok",
|
||||
"bars": [{"date": "2026-01-01", "close": 100.0}],
|
||||
}
|
||||
},
|
||||
}
|
||||
raw = {"AOT.BK": b"aot"}
|
||||
store.persist(snapshot, raw)
|
||||
changed = copy.deepcopy(snapshot)
|
||||
changed["series"]["AOT"]["bars"][0]["close"] = 101.0
|
||||
|
||||
with self.assertRaisesRegex(PriceSourceError, "immutable"):
|
||||
store.persist(changed, raw)
|
||||
|
||||
def test_price_store_rejects_same_raw_payload_with_changed_normalized_content(self):
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
root = Path(temp_dir)
|
||||
store = PriceSnapshotStore(root)
|
||||
initial = valid_non_pit_snapshot("prices-same-raw-initial", "2026-02-01T00:00:00+00:00")
|
||||
store.persist(initial, {"AOT.BK": b"aot"})
|
||||
|
||||
revised = valid_non_pit_snapshot("prices-same-raw-revised", "2026-02-02T00:00:00+00:00")
|
||||
revised["series"]["AOT"]["bars"][0]["close"] = 101.0
|
||||
|
||||
with self.assertRaisesRegex(PriceSourceError, "normalized content"):
|
||||
store.persist(revised, {"AOT.BK": b"aot"})
|
||||
|
||||
def test_price_store_rejects_point_in_time_flag_without_archive_contract(self):
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
store = PriceSnapshotStore(Path(temp_dir))
|
||||
snapshot = {
|
||||
"schema_version": 1,
|
||||
"source": {
|
||||
"source_id": "test.prices",
|
||||
"snapshot_id": "prices-pit-missing-contract",
|
||||
"retrieved_at": "2026-02-01T00:00:00+00:00",
|
||||
"period_start": "2026-01-01",
|
||||
"period_end": "2026-01-02",
|
||||
"quality": "forward_market_archive",
|
||||
"point_in_time": True,
|
||||
},
|
||||
"series": {"AOT": {"bars": [{"date": "2026-01-01", "close": 100.0}]}},
|
||||
}
|
||||
with self.assertRaisesRegex(PriceSourceError, "point-in-time archive contract"):
|
||||
store.persist(snapshot, {"AOT.BK": b"aot"})
|
||||
|
||||
def test_price_store_requires_known_at_for_each_pit_bar(self):
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
store = PriceSnapshotStore(Path(temp_dir))
|
||||
snapshot = {
|
||||
"schema_version": 1,
|
||||
"source": {
|
||||
"source_id": "test.prices",
|
||||
"source_url": "https://example.test/prices",
|
||||
"snapshot_id": "prices-pit-missing-known-at",
|
||||
"retrieved_at": "2026-02-01T00:00:00+00:00",
|
||||
"period_start": "2026-01-01",
|
||||
"period_end": "2026-01-01",
|
||||
"quality": "point_in_time_archive",
|
||||
"point_in_time": True,
|
||||
"archive_contract": PIT_ARCHIVE_CONTRACT,
|
||||
"provider_release_id": "release-2026-01-01",
|
||||
"parser_version": "pit-test-v1",
|
||||
"point_in_time_evidence": {
|
||||
"known_at_field": "known_at",
|
||||
"known_at_semantics": "provider_release_time",
|
||||
"provider_release_id": "release-2026-01-01",
|
||||
"release_published_at": "2026-01-01T17:00:00+07:00",
|
||||
},
|
||||
},
|
||||
"series": {"AOT": {"canonical_symbol": "AOT", "timezone": "Asia/Bangkok", "bars": [{"session_date": "2026-01-01", "close": 100.0}]}},
|
||||
}
|
||||
with self.assertRaisesRegex(PriceSourceError, "known_at"):
|
||||
store.persist(snapshot, {"AOT.BK": b"aot"})
|
||||
|
||||
def test_price_store_persists_and_loads_explicit_pit_archive_contract(self):
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
store = PriceSnapshotStore(Path(temp_dir))
|
||||
snapshot = {
|
||||
"schema_version": 1,
|
||||
"source": {
|
||||
"source_id": "test.prices",
|
||||
"source_url": "https://example.test/prices",
|
||||
"snapshot_id": "prices-pit-valid",
|
||||
"retrieved_at": "2026-02-01T00:00:00+00:00",
|
||||
"period_start": "2026-01-01",
|
||||
"period_end": "2026-01-01",
|
||||
"quality": "point_in_time_archive",
|
||||
"point_in_time": True,
|
||||
"archive_contract": PIT_ARCHIVE_CONTRACT,
|
||||
"provider_release_id": "release-2026-01-01",
|
||||
"parser_version": "pit-test-v1",
|
||||
"point_in_time_evidence": {
|
||||
"known_at_field": "known_at",
|
||||
"known_at_semantics": "provider_release_time",
|
||||
"provider_release_id": "release-2026-01-01",
|
||||
"release_published_at": "2026-01-01T17:00:00+07:00",
|
||||
},
|
||||
},
|
||||
"series": {
|
||||
"AOT": {
|
||||
"canonical_symbol": "AOT",
|
||||
"timezone": "Asia/Bangkok",
|
||||
"bars": [{
|
||||
"session_date": "2026-01-01",
|
||||
"open": 99.0,
|
||||
"high": 101.0,
|
||||
"low": 98.0,
|
||||
"close": 100.0,
|
||||
"adjusted_close": 100.0,
|
||||
"volume": 1000,
|
||||
"known_at": "2026-01-01T17:00:00+07:00",
|
||||
}],
|
||||
}
|
||||
},
|
||||
}
|
||||
loaded = store.persist(snapshot, {"AOT.BK": b"aot"})
|
||||
|
||||
self.assertEqual(loaded["source"]["archive_contract"], PIT_ARCHIVE_CONTRACT)
|
||||
self.assertEqual(store.load_snapshot("prices-pit-valid")["source"]["provider_release_id"], "release-2026-01-01")
|
||||
manifest_entry = store.load_manifest()["snapshots"]["prices-pit-valid"]
|
||||
self.assertEqual(manifest_entry["archive_contract"], PIT_ARCHIVE_CONTRACT)
|
||||
self.assertTrue(manifest_entry["point_in_time"])
|
||||
|
||||
snapshot["series"]["AOT"]["bars"][0]["close"] = 101.0
|
||||
with self.assertRaisesRegex(PriceSourceError, "immutable"):
|
||||
store.persist(snapshot, {"AOT.BK": b"aot-revision"})
|
||||
|
||||
manifest = store.load_manifest()
|
||||
manifest["snapshots"]["prices-pit-valid"]["period_end"] = "2026-01-02"
|
||||
(Path(temp_dir) / "manifest.json").write_text(json.dumps(manifest), encoding="utf-8")
|
||||
with self.assertRaisesRegex(PriceSourceError, "point-in-time metadata"):
|
||||
store.load_snapshot("prices-pit-valid")
|
||||
|
||||
def test_price_store_totalizes_extreme_pit_numeric_values(self):
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
store = PriceSnapshotStore(Path(temp_dir))
|
||||
snapshot = {
|
||||
"schema_version": 1,
|
||||
"source": {
|
||||
"source_id": "test.prices",
|
||||
"source_url": "https://example.test/prices",
|
||||
"snapshot_id": "prices-pit-extreme-number",
|
||||
"retrieved_at": "2026-02-01T00:00:00+00:00",
|
||||
"period_start": "2026-01-01",
|
||||
"period_end": "2026-01-01",
|
||||
"quality": "point_in_time_archive",
|
||||
"point_in_time": True,
|
||||
"archive_contract": PIT_ARCHIVE_CONTRACT,
|
||||
"provider_release_id": "release-2026-01-01",
|
||||
"parser_version": "pit-test-v1",
|
||||
"point_in_time_evidence": {
|
||||
"known_at_field": "known_at",
|
||||
"known_at_semantics": "provider_release_time",
|
||||
"provider_release_id": "release-2026-01-01",
|
||||
"release_published_at": "2026-01-01T17:00:00+07:00",
|
||||
},
|
||||
},
|
||||
"series": {
|
||||
"AOT": {
|
||||
"canonical_symbol": "AOT",
|
||||
"timezone": "Asia/Bangkok",
|
||||
"bars": [{
|
||||
"session_date": "2026-01-01",
|
||||
"open": 99.0,
|
||||
"high": 101.0,
|
||||
"low": 98.0,
|
||||
"close": 100.0,
|
||||
"adjusted_close": 100.0,
|
||||
"known_at": "2026-01-01T17:00:00+07:00",
|
||||
"volume": 10**1000,
|
||||
}],
|
||||
}
|
||||
},
|
||||
}
|
||||
with self.assertRaisesRegex(PriceSourceError, "point-in-time archive volume"):
|
||||
store.persist(snapshot, {"AOT.BK": b"aot"})
|
||||
|
||||
def test_price_store_rejects_non_object_manifest_before_persist(self):
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
root = Path(temp_dir)
|
||||
(root / "manifest.json").write_text("[]", encoding="utf-8")
|
||||
store = PriceSnapshotStore(root)
|
||||
snapshot = {
|
||||
"schema_version": 1,
|
||||
"source": {"source_id": "test.prices", "snapshot_id": "prices-malformed-manifest", "point_in_time": False},
|
||||
"series": {},
|
||||
}
|
||||
with self.assertRaisesRegex(PriceSourceError, "manifest"):
|
||||
store.persist(snapshot, {"AOT.BK": b"aot"})
|
||||
|
||||
def test_price_store_rejects_invalid_raw_payload_symbol(self):
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
store = PriceSnapshotStore(Path(temp_dir))
|
||||
snapshot = {
|
||||
"schema_version": 1,
|
||||
"source": {"source_id": "test.prices", "snapshot_id": "prices-invalid-raw-symbol", "point_in_time": False},
|
||||
"series": {},
|
||||
}
|
||||
raw_payloads: Any = {1: b"aot"}
|
||||
with self.assertRaisesRegex(PriceSourceError, "raw payload"):
|
||||
store.persist(snapshot, raw_payloads)
|
||||
def test_price_store_rejects_series_provenance_mismatch_after_normalized_hash_refresh(self):
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
root = Path(temp_dir)
|
||||
store = PriceSnapshotStore(root)
|
||||
snapshot = valid_non_pit_snapshot()
|
||||
store.persist(snapshot, {"AOT.BK": b"aot"})
|
||||
snapshot_path = root / "snapshots" / "prices-valid.json"
|
||||
original = json.loads(snapshot_path.read_text(encoding="utf-8"))
|
||||
|
||||
for field, value in (("provider_symbol", "PTT.BK"), ("raw_payload_hash", "0" * 64)):
|
||||
tampered = copy.deepcopy(original)
|
||||
tampered["series"]["AOT"][field] = value
|
||||
tampered["source"]["normalized_snapshot_hash"] = _normalized_snapshot_hash(tampered)
|
||||
snapshot_path.write_text(json.dumps(tampered), encoding="utf-8")
|
||||
with self.subTest(field=field), self.assertRaisesRegex(PriceSourceError, "series"):
|
||||
store.load_snapshot("prices-valid")
|
||||
|
||||
def test_price_store_rejects_missing_required_metadata_and_malformed_non_pit_series(self):
|
||||
cases = []
|
||||
snapshot = valid_non_pit_snapshot("prices-missing-schema")
|
||||
snapshot.pop("schema_version")
|
||||
cases.append(("schema_version", snapshot))
|
||||
|
||||
snapshot = valid_non_pit_snapshot("prices-missing-pit-flag")
|
||||
snapshot["source"].pop("point_in_time")
|
||||
cases.append(("point_in_time", snapshot))
|
||||
|
||||
snapshot = valid_non_pit_snapshot("prices-missing-source-id")
|
||||
snapshot["source"].pop("source_id")
|
||||
cases.append(("source identity", snapshot))
|
||||
|
||||
snapshot = valid_non_pit_snapshot("prices-malformed-series")
|
||||
snapshot["series"]["AOT"]["bars"] = "not-a-list"
|
||||
cases.append(("normalized series", snapshot))
|
||||
|
||||
snapshot = valid_non_pit_snapshot("prices-duplicate-bars")
|
||||
snapshot["series"]["AOT"]["bars"].append(copy.deepcopy(snapshot["series"]["AOT"]["bars"][0]))
|
||||
cases.append(("duplicate normalized dates", snapshot))
|
||||
|
||||
for label, snapshot in cases:
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
with self.subTest(label=label), self.assertRaises(PriceSourceError):
|
||||
PriceSnapshotStore(Path(temp_dir)).persist(snapshot, {"AOT.BK": b"aot"})
|
||||
|
||||
def test_price_store_rejects_non_pit_manifest_metadata_tampering(self):
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
root = Path(temp_dir)
|
||||
store = PriceSnapshotStore(root)
|
||||
store.persist(valid_non_pit_snapshot(), {"AOT.BK": b"aot"})
|
||||
manifest_path = root / "manifest.json"
|
||||
original = json.loads(manifest_path.read_text(encoding="utf-8"))
|
||||
|
||||
for field, value in (("parser_version", "tampered-parser"), ("symbols", ["PTT"])):
|
||||
manifest = copy.deepcopy(original)
|
||||
manifest["snapshots"]["prices-valid"][field] = value
|
||||
manifest_path.write_text(json.dumps(manifest), encoding="utf-8")
|
||||
with self.subTest(field=field), self.assertRaisesRegex(PriceSourceError, "metadata"):
|
||||
store.load_snapshot("prices-valid")
|
||||
|
||||
manifest = copy.deepcopy(original)
|
||||
manifest["snapshots"]["prices-valid"].pop("source_url")
|
||||
manifest_path.write_text(json.dumps(manifest), encoding="utf-8")
|
||||
with self.assertRaisesRegex(PriceSourceError, "metadata"):
|
||||
store.load_snapshot("prices-valid")
|
||||
|
||||
def test_price_store_rejects_out_of_order_insertion_without_changing_manifest(self):
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
root = Path(temp_dir)
|
||||
store = PriceSnapshotStore(root)
|
||||
late = valid_non_pit_snapshot("prices-late", "2026-02-01T18:30:00-07:00")
|
||||
early = valid_non_pit_snapshot("prices-early", "2026-02-02T00:00:00+00:00")
|
||||
store.persist(late, {"AOT.BK": b"aot"})
|
||||
before = json.loads((root / "manifest.json").read_text(encoding="utf-8"))
|
||||
|
||||
with self.assertRaisesRegex(PriceSourceError, "immutable predecessor"):
|
||||
store.persist(early, {"AOT.BK": b"aot"})
|
||||
|
||||
after = json.loads((root / "manifest.json").read_text(encoding="utf-8"))
|
||||
self.assertEqual(after, before)
|
||||
self.assertFalse((root / "snapshots" / "prices-early.json").exists())
|
||||
|
||||
def test_price_store_uses_deterministic_predecessor_for_equal_timestamps(self):
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
root = Path(temp_dir)
|
||||
store = PriceSnapshotStore(root)
|
||||
timestamp = "2026-02-01T00:00:00+00:00"
|
||||
store.persist(valid_non_pit_snapshot("prices-a", timestamp), {"AOT.BK": b"aot"})
|
||||
store.persist(valid_non_pit_snapshot("prices-b", timestamp), {"AOT.BK": b"aot"})
|
||||
|
||||
observation = next(item for item in store.load_manifest()["observations"] if item["snapshot_id"] == "prices-b")
|
||||
self.assertEqual(observation["previous_snapshot_id"], "prices-a")
|
||||
|
||||
def test_price_store_rejects_reverse_arrival_for_equal_timestamp(self):
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
root = Path(temp_dir)
|
||||
store = PriceSnapshotStore(root)
|
||||
timestamp = "2026-02-01T00:00:00+00:00"
|
||||
store.persist(valid_non_pit_snapshot("prices-b", timestamp), {"AOT.BK": b"aot"})
|
||||
before = json.loads((root / "manifest.json").read_text(encoding="utf-8"))
|
||||
|
||||
with self.assertRaisesRegex(PriceSourceError, "immutable predecessor"):
|
||||
store.persist(valid_non_pit_snapshot("prices-a", timestamp), {"AOT.BK": b"aot"})
|
||||
|
||||
after = json.loads((root / "manifest.json").read_text(encoding="utf-8"))
|
||||
self.assertEqual(after, before)
|
||||
|
||||
def test_price_store_treats_same_snapshot_same_timestamp_as_idempotent(self):
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
root = Path(temp_dir)
|
||||
store = PriceSnapshotStore(root)
|
||||
snapshot = valid_non_pit_snapshot("prices-idempotent", "2026-02-01T00:00:00+00:00")
|
||||
store.persist(snapshot, {"AOT.BK": b"aot"})
|
||||
store.persist(copy.deepcopy(snapshot), {"AOT.BK": b"aot"})
|
||||
|
||||
self.assertEqual(len(store.list_observations()), 1)
|
||||
|
||||
def test_price_store_rejects_equal_timestamp_initial_node_with_canonical_predecessor(self):
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
root = Path(temp_dir)
|
||||
store = PriceSnapshotStore(root)
|
||||
timestamp = "2026-02-01T00:00:00+00:00"
|
||||
store.persist(valid_non_pit_snapshot("prices-a", timestamp), {"AOT.BK": b"aot"})
|
||||
store.persist(valid_non_pit_snapshot("prices-b", timestamp), {"AOT.BK": b"aot"})
|
||||
manifest_path = root / "manifest.json"
|
||||
manifest = json.loads(manifest_path.read_text(encoding="utf-8"))
|
||||
observation = next(item for item in manifest["observations"] if item["snapshot_id"] == "prices-b")
|
||||
observation.pop("observation_id")
|
||||
observation["previous_snapshot_id"] = None
|
||||
observation["previous_raw_payload_hash"] = None
|
||||
observation["revision_status"] = "initial"
|
||||
observation["observation_id"] = _observation_id(observation)
|
||||
manifest_path.write_text(json.dumps(manifest), encoding="utf-8")
|
||||
|
||||
with self.assertRaisesRegex(PriceSourceError, "predecessor chain"):
|
||||
store.load_manifest()
|
||||
|
||||
def test_price_store_rejects_duplicate_equal_time_observations_for_one_snapshot(self):
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
root = Path(temp_dir)
|
||||
store = PriceSnapshotStore(root)
|
||||
snapshot_id = "prices-duplicate-time"
|
||||
store.persist(valid_non_pit_snapshot(snapshot_id, "2026-02-01T00:00:00+00:00"), {"AOT.BK": b"aot"})
|
||||
manifest_path = root / "manifest.json"
|
||||
manifest = json.loads(manifest_path.read_text(encoding="utf-8"))
|
||||
original = manifest["observations"][0]
|
||||
duplicate_diff = copy.deepcopy(original["diff"])
|
||||
duplicate_diff["raw_payload_changed"] = False
|
||||
observations = []
|
||||
for retrieved_at in ("2026-02-01T00:00:00+00:00", "2026-02-01T00:00:00Z"):
|
||||
observation = copy.deepcopy(original)
|
||||
observation["retrieved_at"] = retrieved_at
|
||||
observation["previous_snapshot_id"] = snapshot_id
|
||||
observation["previous_raw_payload_hash"] = original["raw_payload_hash"]
|
||||
observation["revision_status"] = "unchanged"
|
||||
observation["diff"] = copy.deepcopy(duplicate_diff)
|
||||
observation["observation_id"] = _observation_id(
|
||||
{key: value for key, value in observation.items() if key != "observation_id"}
|
||||
)
|
||||
observations.append(observation)
|
||||
manifest["observations"] = observations
|
||||
entry = manifest["snapshots"][snapshot_id]
|
||||
entry["observation_count"] = 2
|
||||
manifest_path.write_text(json.dumps(manifest), encoding="utf-8")
|
||||
|
||||
with self.assertRaisesRegex(PriceSourceError, "ambiguous observation"):
|
||||
store.load_manifest()
|
||||
|
||||
def test_price_store_orders_latest_snapshot_by_utc_time(self):
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
root = Path(temp_dir)
|
||||
store = PriceSnapshotStore(root)
|
||||
store.persist(valid_non_pit_snapshot("prices-early", "2026-02-02T00:00:00+00:00"), {"AOT.BK": b"aot"})
|
||||
store.persist(valid_non_pit_snapshot("prices-late", "2026-02-01T18:30:00-07:00"), {"AOT.BK": b"aot"})
|
||||
|
||||
self.assertEqual(store.latest_snapshot_entry()["snapshot_id"], "prices-late")
|
||||
|
||||
def test_price_store_validates_timing_metadata_when_observation_count_is_omitted(self):
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
root = Path(temp_dir)
|
||||
store = PriceSnapshotStore(root)
|
||||
store.persist(valid_non_pit_snapshot(), {"AOT.BK": b"aot"})
|
||||
manifest_path = root / "manifest.json"
|
||||
manifest = json.loads(manifest_path.read_text(encoding="utf-8"))
|
||||
entry = manifest["snapshots"]["prices-valid"]
|
||||
entry.pop("observation_count")
|
||||
entry["first_seen_at"] = "not-a-timestamp"
|
||||
entry["last_seen_at"] = "not-a-timestamp"
|
||||
manifest_path.write_text(json.dumps(manifest), encoding="utf-8")
|
||||
|
||||
with self.assertRaisesRegex(PriceSourceError, "timing"):
|
||||
store.load_manifest()
|
||||
|
||||
def test_price_store_rejects_observation_before_snapshot_first_capture(self):
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
root = Path(temp_dir)
|
||||
store = PriceSnapshotStore(root)
|
||||
store.persist(valid_non_pit_snapshot("prices-temporal", "2026-02-01T00:00:00+00:00"), {"AOT.BK": b"aot"})
|
||||
manifest_path = root / "manifest.json"
|
||||
manifest = json.loads(manifest_path.read_text(encoding="utf-8"))
|
||||
observation = manifest["observations"][0]
|
||||
observation["retrieved_at"] = "2026-01-31T00:00:00+00:00"
|
||||
observation["observation_id"] = _observation_id({key: value for key, value in observation.items() if key != "observation_id"})
|
||||
entry = manifest["snapshots"]["prices-temporal"]
|
||||
entry["first_seen_at"] = "2026-01-31T00:00:00+00:00"
|
||||
entry["last_seen_at"] = "2026-01-31T00:00:00+00:00"
|
||||
manifest_path.write_text(json.dumps(manifest), encoding="utf-8")
|
||||
|
||||
with self.assertRaisesRegex(PriceSourceError, "before snapshot"):
|
||||
store.load_manifest()
|
||||
|
||||
def test_price_store_rejects_unreferenced_contract_snapshot(self):
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
root = Path(temp_dir)
|
||||
store = PriceSnapshotStore(root)
|
||||
store.persist(valid_non_pit_snapshot(), {"AOT.BK": b"aot"})
|
||||
manifest_path = root / "manifest.json"
|
||||
manifest = json.loads(manifest_path.read_text(encoding="utf-8"))
|
||||
unreferenced = copy.deepcopy(manifest["snapshots"]["prices-valid"])
|
||||
unreferenced["snapshot_id"] = "prices-unreferenced"
|
||||
unreferenced["snapshot_file"] = "prices-unreferenced.json"
|
||||
unreferenced.pop("observation_count")
|
||||
unreferenced.pop("first_seen_at")
|
||||
unreferenced.pop("last_seen_at")
|
||||
manifest["snapshots"]["prices-unreferenced"] = unreferenced
|
||||
manifest_path.write_text(json.dumps(manifest), encoding="utf-8")
|
||||
|
||||
with self.assertRaisesRegex(PriceSourceError, "observation count"):
|
||||
store.load_manifest()
|
||||
|
||||
def test_price_store_rejects_malformed_snapshot_manifest_entry(self):
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
root = Path(temp_dir)
|
||||
store = PriceSnapshotStore(root)
|
||||
store.persist(valid_non_pit_snapshot(), {"AOT.BK": b"aot"})
|
||||
manifest_path = root / "manifest.json"
|
||||
manifest = json.loads(manifest_path.read_text(encoding="utf-8"))
|
||||
manifest["snapshots"]["prices-malformed-entry"] = []
|
||||
manifest_path.write_text(json.dumps(manifest), encoding="utf-8")
|
||||
|
||||
with self.assertRaisesRegex(PriceSourceError, "snapshot manifest entry"):
|
||||
store.load_manifest()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -1,11 +1,13 @@
|
||||
import copy
|
||||
import hashlib
|
||||
import json
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
from typing import Any, cast
|
||||
|
||||
from app.prices import PriceSnapshotStore, PriceSourceError
|
||||
from app.research import ResearchRunError, ResearchRunStore, run_tourism_research
|
||||
from app.research import ResearchRunError, ResearchRunStore, _manifest_entry_hash, _report_hash, _validate_config, run_tourism_research
|
||||
from app.vintages import VintageStore
|
||||
|
||||
|
||||
@@ -27,7 +29,7 @@ def tourism_snapshot(vintage_id: str, published_at: str) -> dict:
|
||||
"source_url": "https://example.test/bot",
|
||||
"vintage_id": vintage_id,
|
||||
"published_at": published_at,
|
||||
"retrieved_at": "2026-02-01T00:00:00+00:00",
|
||||
"retrieved_at": "2026-02-02T00:00:00+00:00",
|
||||
"release_status": "provisional",
|
||||
"parser_version": "test-v1",
|
||||
},
|
||||
@@ -35,23 +37,51 @@ def tourism_snapshot(vintage_id: str, published_at: str) -> dict:
|
||||
|
||||
|
||||
def price_snapshot(point_in_time: bool) -> tuple[dict, dict[str, bytes]]:
|
||||
def bar(session_date: str, close: float) -> dict:
|
||||
if not point_in_time:
|
||||
return {"date": session_date, "close": close}
|
||||
return {
|
||||
"date": session_date,
|
||||
"session_date": session_date,
|
||||
"open": close,
|
||||
"high": close + 1,
|
||||
"low": close - 1,
|
||||
"close": close,
|
||||
"adjusted_close": close,
|
||||
"volume": 1000,
|
||||
"known_at": f"{session_date}T17:00:00+07:00",
|
||||
}
|
||||
|
||||
pit_source = {
|
||||
"source_url": "https://example.test/prices",
|
||||
"parser_version": "pit-test-v1",
|
||||
"archive_contract": "pit-daily-v1",
|
||||
"provider_release_id": "release-2026-02-01",
|
||||
"point_in_time_evidence": {
|
||||
"known_at_field": "known_at",
|
||||
"known_at_semantics": "provider_release_time",
|
||||
"provider_release_id": "release-2026-02-01",
|
||||
"release_published_at": "2026-02-01T17:00:00+07:00",
|
||||
},
|
||||
} if point_in_time else {}
|
||||
return (
|
||||
{
|
||||
"schema_version": 1,
|
||||
"source": {
|
||||
"source_id": "test.prices",
|
||||
"snapshot_id": "prices-test",
|
||||
"retrieved_at": "2026-02-01T00:00:00+00:00",
|
||||
"retrieved_at": "2026-02-02T00:00:00+00:00",
|
||||
"period_start": "2026-01-01",
|
||||
"period_end": "2026-01-06",
|
||||
"quality": "forward_market_archive" if point_in_time else "revised_vendor_history",
|
||||
"quality": "point_in_time_archive" if point_in_time else "revised_vendor_history",
|
||||
"point_in_time": point_in_time,
|
||||
"adjusted_prices": False,
|
||||
**pit_source,
|
||||
},
|
||||
"series": {
|
||||
"AOT": {"bars": [{"date": "2026-01-01", "close": 100.0}, {"date": "2026-01-02", "close": 102.0}, {"date": "2026-01-05", "close": 104.0}, {"date": "2026-01-06", "close": 106.0}]},
|
||||
"PTT": {"bars": [{"date": "2026-01-01", "close": 100.0}, {"date": "2026-01-02", "close": 99.0}, {"date": "2026-01-05", "close": 98.0}, {"date": "2026-01-06", "close": 97.0}]},
|
||||
"SET50": {"bars": [{"date": "2026-01-01", "close": 100.0}, {"date": "2026-01-02", "close": 101.0}, {"date": "2026-01-05", "close": 102.0}, {"date": "2026-01-06", "close": 103.0}]},
|
||||
"AOT": {"canonical_symbol": "AOT", "provider_symbol": "AOT.BK", "raw_payload_hash": hashlib.sha256(b"aot").hexdigest(), "timezone": "Asia/Bangkok", "bars": [bar("2026-01-01", 100.0), bar("2026-01-02", 102.0), bar("2026-01-05", 104.0), bar("2026-01-06", 106.0)]},
|
||||
"PTT": {"canonical_symbol": "PTT", "provider_symbol": "PTT.BK", "raw_payload_hash": hashlib.sha256(b"ptt").hexdigest(), "timezone": "Asia/Bangkok", "bars": [bar("2026-01-01", 100.0), bar("2026-01-02", 99.0), bar("2026-01-05", 98.0), bar("2026-01-06", 97.0)]},
|
||||
"SET50": {"canonical_symbol": "SET50", "provider_symbol": "^SET.BK", "raw_payload_hash": hashlib.sha256(b"set").hexdigest(), "timezone": "Asia/Bangkok", "bars": [bar("2026-01-01", 100.0), bar("2026-01-02", 101.0), bar("2026-01-05", 102.0), bar("2026-01-06", 103.0)]},
|
||||
},
|
||||
"benchmark_symbol": "SET50",
|
||||
},
|
||||
@@ -60,6 +90,364 @@ def price_snapshot(point_in_time: bool) -> tuple[dict, dict[str, bytes]]:
|
||||
|
||||
|
||||
class ResearchRunTests(unittest.TestCase):
|
||||
def test_research_config_rejects_malformed_values_with_domain_error(self):
|
||||
cases = (
|
||||
((None,), 20.0, 1, 1),
|
||||
((float("nan"),), 20.0, 1, 1),
|
||||
((1,), float("inf"), 1, 1),
|
||||
((1,), 20.0, None, 1),
|
||||
((1,), 20.0, 1, float("inf")),
|
||||
)
|
||||
for windows, cost_bps, min_events, execution_lag in cases:
|
||||
with self.subTest(windows=windows, cost_bps=cost_bps, min_events=min_events, execution_lag=execution_lag):
|
||||
with self.assertRaises(ResearchRunError):
|
||||
_validate_config(
|
||||
cast(Any, windows),
|
||||
cast(Any, cost_bps),
|
||||
cast(Any, min_events),
|
||||
cast(Any, execution_lag),
|
||||
)
|
||||
|
||||
def test_research_run_store_binds_report_hash_and_rejects_tampering(self):
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
root = Path(temp_dir)
|
||||
store = ResearchRunStore(root)
|
||||
report = {
|
||||
"schema_version": 1,
|
||||
"run_id": "tourism-run-test",
|
||||
"theme": "tourism",
|
||||
"status": "blocked",
|
||||
"reason": "test",
|
||||
"generated_at": "2026-02-01T00:00:00+00:00",
|
||||
"config": {},
|
||||
"gates": {},
|
||||
"inputs": {},
|
||||
}
|
||||
|
||||
stored = store.persist(report)
|
||||
|
||||
self.assertRegex(stored["report_hash"], r"^[a-f0-9]{64}$")
|
||||
report_path = root / "reports" / "tourism-run-test.json"
|
||||
tampered = json.loads(report_path.read_text(encoding="utf-8"))
|
||||
tampered["reason"] = "tampered"
|
||||
report_path.write_text(json.dumps(tampered), encoding="utf-8")
|
||||
with self.assertRaisesRegex(ResearchRunError, "integrity"):
|
||||
store.load("tourism-run-test")
|
||||
|
||||
def test_research_run_store_rejects_manifest_hash_tampering(self):
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
root = Path(temp_dir)
|
||||
store = ResearchRunStore(root)
|
||||
report = {
|
||||
"schema_version": 1,
|
||||
"run_id": "tourism-run-manifest-test",
|
||||
"theme": "tourism",
|
||||
"status": "blocked",
|
||||
"reason": "test",
|
||||
"generated_at": "2026-02-01T00:00:00+00:00",
|
||||
"config": {},
|
||||
"gates": {},
|
||||
"inputs": {},
|
||||
}
|
||||
store.persist(report)
|
||||
|
||||
manifest_path = root / "manifest.json"
|
||||
manifest = json.loads(manifest_path.read_text(encoding="utf-8"))
|
||||
manifest["runs"]["tourism-run-manifest-test"]["report_hash"] = "0" * 64
|
||||
manifest_path.write_text(json.dumps(manifest), encoding="utf-8")
|
||||
with self.assertRaisesRegex(ResearchRunError, "integrity"):
|
||||
store.load("tourism-run-manifest-test")
|
||||
|
||||
def test_research_run_store_migrates_legacy_reports_explicitly(self):
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
root = Path(temp_dir)
|
||||
store = ResearchRunStore(root)
|
||||
report = {
|
||||
"schema_version": 1,
|
||||
"run_id": "tourism-run-legacy-test",
|
||||
"theme": "tourism",
|
||||
"status": "blocked",
|
||||
"reason": "test",
|
||||
"generated_at": "2026-02-01T00:00:00+00:00",
|
||||
"config": {},
|
||||
"gates": {},
|
||||
"inputs": {},
|
||||
}
|
||||
legacy_manifest = {
|
||||
"schema_version": 1,
|
||||
"runs": {
|
||||
"tourism-run-legacy-test": {
|
||||
"run_id": "tourism-run-legacy-test",
|
||||
"status": "blocked",
|
||||
"reason": "test",
|
||||
"generated_at": "2026-02-01T00:00:00+00:00",
|
||||
"report_file": "tourism-run-legacy-test.json",
|
||||
}
|
||||
},
|
||||
}
|
||||
(root / "reports").mkdir(parents=True)
|
||||
(root / "reports" / "tourism-run-legacy-test.json").write_text(json.dumps(report), encoding="utf-8")
|
||||
(root / "manifest.json").write_text(json.dumps(legacy_manifest), encoding="utf-8")
|
||||
|
||||
with self.assertRaisesRegex(ResearchRunError, "integrity"):
|
||||
store.load("tourism-run-legacy-test")
|
||||
|
||||
self.assertEqual(store.migrate_legacy(), 1)
|
||||
migrated = store.load("tourism-run-legacy-test")
|
||||
manifest_entry = store.load_manifest()["runs"]["tourism-run-legacy-test"]
|
||||
self.assertEqual(migrated["report_hash_algorithm"], "sha256-json-canonical-v1")
|
||||
self.assertRegex(migrated["report_hash"], r"^[a-f0-9]{64}$")
|
||||
self.assertRegex(manifest_entry["manifest_entry_hash"], r"^[a-f0-9]{64}$")
|
||||
self.assertEqual(store.migrate_legacy(), 0)
|
||||
|
||||
def test_research_run_store_completes_interrupted_legacy_migration(self):
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
root = Path(temp_dir)
|
||||
store = ResearchRunStore(root)
|
||||
report = {
|
||||
"schema_version": 1,
|
||||
"run_id": "tourism-run-partial-migration-test",
|
||||
"theme": "tourism",
|
||||
"status": "blocked",
|
||||
"reason": "test",
|
||||
"generated_at": "2026-02-01T00:00:00+00:00",
|
||||
"config": {},
|
||||
"gates": {},
|
||||
"inputs": {},
|
||||
}
|
||||
partial_report = copy.deepcopy(report)
|
||||
partial_report["report_hash_algorithm"] = "sha256-json-canonical-v1"
|
||||
partial_report["report_hash"] = _report_hash(partial_report)
|
||||
legacy_entry = {
|
||||
"run_id": report["run_id"],
|
||||
"status": report["status"],
|
||||
"reason": report["reason"],
|
||||
"generated_at": report["generated_at"],
|
||||
"report_file": f"{report['run_id']}.json",
|
||||
}
|
||||
(root / "reports").mkdir(parents=True)
|
||||
(root / "reports" / f"{report['run_id']}.json").write_text(json.dumps(partial_report), encoding="utf-8")
|
||||
(root / "manifest.json").write_text(json.dumps({"schema_version": 1, "runs": {report["run_id"]: legacy_entry}}), encoding="utf-8")
|
||||
|
||||
self.assertEqual(store.migrate_legacy(), 1)
|
||||
self.assertEqual(store.load(report["run_id"])["report_hash"], partial_report["report_hash"])
|
||||
self.assertEqual(store.migrate_legacy(), 0)
|
||||
|
||||
def test_research_run_store_rejects_manifest_identity_tampering(self):
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
root = Path(temp_dir)
|
||||
store = ResearchRunStore(root)
|
||||
report = {
|
||||
"schema_version": 1,
|
||||
"run_id": "tourism-run-identity-test",
|
||||
"theme": "tourism",
|
||||
"status": "blocked",
|
||||
"reason": "test",
|
||||
"generated_at": "2026-02-01T00:00:00+00:00",
|
||||
"config": {},
|
||||
"gates": {},
|
||||
"inputs": {},
|
||||
}
|
||||
store.persist(report)
|
||||
|
||||
manifest_path = root / "manifest.json"
|
||||
manifest = json.loads(manifest_path.read_text(encoding="utf-8"))
|
||||
entry = manifest["runs"]["tourism-run-identity-test"]
|
||||
entry["run_id"] = "tourism-run-other"
|
||||
entry["manifest_entry_hash"] = _manifest_entry_hash(entry)
|
||||
manifest_path.write_text(json.dumps(manifest), encoding="utf-8")
|
||||
|
||||
with self.assertRaisesRegex(ResearchRunError, "integrity"):
|
||||
store.load("tourism-run-identity-test")
|
||||
|
||||
def test_research_run_store_rejects_manifest_metadata_tampering(self):
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
root = Path(temp_dir)
|
||||
store = ResearchRunStore(root)
|
||||
report = {
|
||||
"schema_version": 1,
|
||||
"run_id": "tourism-run-metadata-test",
|
||||
"theme": "tourism",
|
||||
"status": "blocked",
|
||||
"reason": "test",
|
||||
"generated_at": "2026-02-01T00:00:00+00:00",
|
||||
"config": {},
|
||||
"gates": {},
|
||||
"inputs": {},
|
||||
}
|
||||
store.persist(report)
|
||||
|
||||
manifest_path = root / "manifest.json"
|
||||
manifest = json.loads(manifest_path.read_text(encoding="utf-8"))
|
||||
manifest["runs"]["tourism-run-metadata-test"]["generated_at"] = "2099-02-01T00:00:00+00:00"
|
||||
manifest_path.write_text(json.dumps(manifest), encoding="utf-8")
|
||||
with self.assertRaisesRegex(ResearchRunError, "integrity"):
|
||||
store.load("tourism-run-metadata-test")
|
||||
|
||||
def test_research_run_store_rejects_non_object_report(self):
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
root = Path(temp_dir)
|
||||
store = ResearchRunStore(root)
|
||||
report = {
|
||||
"schema_version": 1,
|
||||
"run_id": "tourism-run-shape-test",
|
||||
"theme": "tourism",
|
||||
"status": "blocked",
|
||||
"reason": "test",
|
||||
"generated_at": "2026-02-01T00:00:00+00:00",
|
||||
"config": {},
|
||||
"gates": {},
|
||||
"inputs": {},
|
||||
}
|
||||
store.persist(report)
|
||||
report_path = root / "reports" / "tourism-run-shape-test.json"
|
||||
report_path.write_text("[\"tampered\"]", encoding="utf-8")
|
||||
with self.assertRaisesRegex(ResearchRunError, "invalid"):
|
||||
store.load("tourism-run-shape-test")
|
||||
|
||||
def test_research_run_store_rejects_non_object_manifest(self):
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
root = Path(temp_dir)
|
||||
store = ResearchRunStore(root)
|
||||
(root / "manifest.json").write_text("[]", encoding="utf-8")
|
||||
|
||||
with self.assertRaisesRegex(ResearchRunError, "schema"):
|
||||
store.load_manifest()
|
||||
|
||||
def test_research_run_store_rejects_unreadable_manifest_bytes(self):
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
root = Path(temp_dir)
|
||||
store = ResearchRunStore(root)
|
||||
(root / "manifest.json").write_bytes(b"\xff")
|
||||
|
||||
with self.assertRaisesRegex(ResearchRunError, "unreadable"):
|
||||
store.load_manifest()
|
||||
|
||||
def test_research_run_store_rejects_unreadable_report_bytes(self):
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
root = Path(temp_dir)
|
||||
store = ResearchRunStore(root)
|
||||
report = {
|
||||
"schema_version": 1,
|
||||
"run_id": "tourism-run-encoding-test",
|
||||
"theme": "tourism",
|
||||
"status": "blocked",
|
||||
"reason": "test",
|
||||
"generated_at": "2026-02-01T00:00:00+00:00",
|
||||
"config": {},
|
||||
"gates": {},
|
||||
"inputs": {},
|
||||
}
|
||||
store.persist(report)
|
||||
(root / "reports" / "tourism-run-encoding-test.json").write_bytes(b"\xff")
|
||||
|
||||
with self.assertRaisesRegex(ResearchRunError, "unreadable"):
|
||||
store.load("tourism-run-encoding-test")
|
||||
|
||||
def test_research_run_store_list_runs_rejects_manifest_metadata_tampering(self):
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
root = Path(temp_dir)
|
||||
store = ResearchRunStore(root)
|
||||
report = {
|
||||
"schema_version": 1,
|
||||
"run_id": "tourism-run-list-test",
|
||||
"theme": "tourism",
|
||||
"status": "blocked",
|
||||
"reason": "test",
|
||||
"generated_at": "2026-02-01T00:00:00+00:00",
|
||||
"config": {},
|
||||
"gates": {},
|
||||
"inputs": {},
|
||||
}
|
||||
store.persist(report)
|
||||
manifest_path = root / "manifest.json"
|
||||
manifest = json.loads(manifest_path.read_text(encoding="utf-8"))
|
||||
manifest["runs"]["tourism-run-list-test"]["generated_at"] = "2099-02-01T00:00:00+00:00"
|
||||
manifest_path.write_text(json.dumps(manifest), encoding="utf-8")
|
||||
|
||||
with self.assertRaisesRegex(ResearchRunError, "integrity"):
|
||||
store.list_runs()
|
||||
|
||||
def test_research_run_store_latest_and_list_reject_malformed_manifest_entry(self):
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
root = Path(temp_dir)
|
||||
store = ResearchRunStore(root)
|
||||
report = {
|
||||
"schema_version": 1,
|
||||
"run_id": "tourism-run-entry-shape-test",
|
||||
"theme": "tourism",
|
||||
"status": "blocked",
|
||||
"reason": "test",
|
||||
"generated_at": "2026-02-01T00:00:00+00:00",
|
||||
"config": {},
|
||||
"gates": {},
|
||||
"inputs": {},
|
||||
}
|
||||
store.persist(report)
|
||||
manifest_path = root / "manifest.json"
|
||||
manifest = json.loads(manifest_path.read_text(encoding="utf-8"))
|
||||
manifest["runs"]["tourism-run-entry-shape-test"] = []
|
||||
manifest_path.write_text(json.dumps(manifest), encoding="utf-8")
|
||||
|
||||
with self.assertRaisesRegex(ResearchRunError, "entry"):
|
||||
store.list_runs()
|
||||
with self.assertRaisesRegex(ResearchRunError, "entry"):
|
||||
store.latest()
|
||||
|
||||
def test_research_run_store_persist_rejects_malformed_existing_manifest_entry(self):
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
root = Path(temp_dir)
|
||||
store = ResearchRunStore(root)
|
||||
report = {
|
||||
"schema_version": 1,
|
||||
"run_id": "tourism-run-persist-entry-test",
|
||||
"theme": "tourism",
|
||||
"status": "blocked",
|
||||
"reason": "test",
|
||||
"generated_at": "2026-02-01T00:00:00+00:00",
|
||||
"config": {},
|
||||
"gates": {},
|
||||
"inputs": {},
|
||||
}
|
||||
store.persist(report)
|
||||
manifest_path = root / "manifest.json"
|
||||
manifest = json.loads(manifest_path.read_text(encoding="utf-8"))
|
||||
manifest["runs"]["tourism-run-persist-entry-test"] = []
|
||||
manifest_path.write_text(json.dumps(manifest), encoding="utf-8")
|
||||
|
||||
with self.assertRaisesRegex(ResearchRunError, "entry"):
|
||||
store.persist(report)
|
||||
|
||||
def test_research_run_store_persist_validates_unrelated_manifest_entries_before_write(self):
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
root = Path(temp_dir)
|
||||
store = ResearchRunStore(root)
|
||||
existing_report = {
|
||||
"schema_version": 1,
|
||||
"run_id": "tourism-run-persist-unrelated-existing",
|
||||
"theme": "tourism",
|
||||
"status": "blocked",
|
||||
"reason": "test",
|
||||
"generated_at": "2026-02-01T00:00:00+00:00",
|
||||
"config": {},
|
||||
"gates": {},
|
||||
"inputs": {},
|
||||
}
|
||||
new_report = {
|
||||
**existing_report,
|
||||
"run_id": "tourism-run-persist-unrelated-new",
|
||||
}
|
||||
store.persist(existing_report)
|
||||
|
||||
manifest_path = root / "manifest.json"
|
||||
manifest = json.loads(manifest_path.read_text(encoding="utf-8"))
|
||||
manifest["runs"]["tourism-run-persist-unrelated-malformed"] = []
|
||||
manifest_path.write_text(json.dumps(manifest), encoding="utf-8")
|
||||
|
||||
with self.assertRaisesRegex(ResearchRunError, "entry"):
|
||||
store.persist(new_report)
|
||||
self.assertFalse((root / "reports" / "tourism-run-persist-unrelated-new.json").exists())
|
||||
|
||||
def test_price_store_loads_snapshot_and_verifies_raw_payload(self):
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
store = PriceSnapshotStore(Path(temp_dir))
|
||||
@@ -100,6 +488,78 @@ class ResearchRunTests(unittest.TestCase):
|
||||
self.assertNotIn("result", report)
|
||||
self.assertEqual(run_store.latest()["run_id"], report["run_id"])
|
||||
|
||||
def test_runner_allows_explicit_exploratory_mode_with_revised_prices(self):
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
root = Path(temp_dir)
|
||||
vintage_store = VintageStore(root / "tourism")
|
||||
vintage_store.persist(b"tourism", tourism_snapshot("v1", "2026-01-01T08:00:00+07:00"))
|
||||
price_store = PriceSnapshotStore(root / "prices")
|
||||
snapshot, raw = price_snapshot(point_in_time=False)
|
||||
price_store.persist(snapshot, raw)
|
||||
run_store = ResearchRunStore(root / "runs")
|
||||
|
||||
report = run_tourism_research(
|
||||
vintage_store,
|
||||
price_store,
|
||||
run_store,
|
||||
mode="exploratory",
|
||||
min_events=1,
|
||||
windows=(1,),
|
||||
)
|
||||
|
||||
self.assertEqual(report["status"], "descriptive_only")
|
||||
self.assertEqual(report["research_mode"], "exploratory")
|
||||
self.assertEqual(report["result_scope"], "non_pit_descriptive_only")
|
||||
self.assertEqual(report["gates"]["prices"]["status"], "descriptive_only")
|
||||
self.assertEqual(report["gates"]["prices"]["reason"], "price_series_not_point_in_time")
|
||||
self.assertEqual(report["result"]["event_count"], 1)
|
||||
|
||||
def test_runner_keeps_exploratory_pit_inputs_descriptive_only(self):
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
root = Path(temp_dir)
|
||||
vintage_store = VintageStore(root / "tourism")
|
||||
vintage_store.persist(b"tourism", tourism_snapshot("v1", "2026-01-01T08:00:00+07:00"))
|
||||
price_store = PriceSnapshotStore(root / "prices")
|
||||
snapshot, raw = price_snapshot(point_in_time=True)
|
||||
price_store.persist(snapshot, raw)
|
||||
run_store = ResearchRunStore(root / "runs")
|
||||
|
||||
report = run_tourism_research(
|
||||
vintage_store,
|
||||
price_store,
|
||||
run_store,
|
||||
mode="exploratory",
|
||||
min_events=1,
|
||||
windows=(1,),
|
||||
)
|
||||
|
||||
self.assertEqual(report["status"], "descriptive_only")
|
||||
self.assertEqual(report["result_scope"], "non_pit_descriptive_only")
|
||||
self.assertEqual(report["gates"]["prices"]["status"], "descriptive_only")
|
||||
self.assertFalse(report["gates"]["prices"]["validated"])
|
||||
self.assertTrue(report["gates"]["prices"]["point_in_time"])
|
||||
self.assertEqual(report["gates"]["prices"]["reason"], "exploratory_mode_non_validated")
|
||||
|
||||
def test_runner_reuses_same_run_when_only_observation_time_changes(self):
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
root = Path(temp_dir)
|
||||
vintage_store = VintageStore(root / "tourism")
|
||||
vintage_store.persist(b"tourism", tourism_snapshot("v1", "2026-01-01T08:00:00+07:00"))
|
||||
price_store = PriceSnapshotStore(root / "prices")
|
||||
snapshot, raw = price_snapshot(point_in_time=False)
|
||||
price_store.persist(snapshot, raw)
|
||||
run_store = ResearchRunStore(root / "runs")
|
||||
|
||||
first = run_tourism_research(vintage_store, price_store, run_store, mode="exploratory", min_events=1, windows=(1,))
|
||||
observed_again = copy.deepcopy(snapshot)
|
||||
observed_again["source"]["retrieved_at"] = "2026-02-03T00:00:00+00:00"
|
||||
price_store.persist(observed_again, raw)
|
||||
second = run_tourism_research(vintage_store, price_store, run_store, mode="exploratory", min_events=1, windows=(1,))
|
||||
|
||||
self.assertEqual(second["run_id"], first["run_id"])
|
||||
self.assertEqual(second, first)
|
||||
self.assertEqual(len(price_store.list_observations()), 2)
|
||||
|
||||
def test_runner_computes_replayable_result_when_both_gates_pass(self):
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
root = Path(temp_dir)
|
||||
|
||||
@@ -5,11 +5,12 @@ import tempfile
|
||||
import unittest
|
||||
from datetime import datetime, timezone
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
from urllib.parse import parse_qs
|
||||
|
||||
from app.bot_tourism import BotTourismSource
|
||||
from app.collector import collect_bot_vintage
|
||||
from app.vintages import VintageStore, filter_vintages
|
||||
from app.vintages import VintageStore, VintageStoreError, filter_vintages
|
||||
|
||||
FIXTURES = Path(__file__).parent / "fixtures"
|
||||
REPORT_HTML = (FIXTURES / "bot_tourism_report.html").read_text(encoding="utf-8")
|
||||
@@ -128,6 +129,50 @@ class VintageStoreTests(unittest.TestCase):
|
||||
with self.assertRaisesRegex(ValueError, "hash"):
|
||||
store.load_snapshot(snapshot["source"]["vintage_id"])
|
||||
|
||||
def test_snapshot_load_rejects_non_object_json(self):
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
root = Path(temp_dir)
|
||||
store = VintageStore(root)
|
||||
snapshot = sample_snapshot()
|
||||
store.persist(REPORT_HTML.encode("utf-8"), snapshot)
|
||||
snapshot_path = root / "snapshots" / f"{snapshot['source']['vintage_id']}.json"
|
||||
snapshot_path.write_text(json.dumps(["not", "a", "snapshot"]), encoding="utf-8")
|
||||
|
||||
with self.assertRaisesRegex(VintageStoreError, "invalid"):
|
||||
store.load_snapshot(snapshot["source"]["vintage_id"])
|
||||
|
||||
def test_snapshot_load_rejects_unreadable_utf8(self):
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
root = Path(temp_dir)
|
||||
store = VintageStore(root)
|
||||
snapshot = sample_snapshot()
|
||||
store.persist(REPORT_HTML.encode("utf-8"), snapshot)
|
||||
snapshot_path = root / "snapshots" / f"{snapshot['source']['vintage_id']}.json"
|
||||
snapshot_path.write_bytes(b"\xff")
|
||||
|
||||
with self.assertRaisesRegex(VintageStoreError, "unreadable"):
|
||||
store.load_snapshot(snapshot["source"]["vintage_id"])
|
||||
|
||||
def test_snapshot_load_rejects_unreadable_raw_payload(self):
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
root = Path(temp_dir)
|
||||
store = VintageStore(root)
|
||||
snapshot = sample_snapshot()
|
||||
store.persist(REPORT_HTML.encode("utf-8"), snapshot)
|
||||
|
||||
with patch("pathlib.Path.read_bytes", side_effect=OSError("read failed")):
|
||||
with self.assertRaisesRegex(VintageStoreError, "unreadable"):
|
||||
store.load_snapshot(snapshot["source"]["vintage_id"])
|
||||
|
||||
def test_manifest_load_rejects_unreadable_utf8(self):
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
root = Path(temp_dir)
|
||||
store = VintageStore(root)
|
||||
(root / "manifest.json").write_bytes(b"\xff")
|
||||
|
||||
with self.assertRaisesRegex(VintageStoreError, "unreadable"):
|
||||
store.load_manifest()
|
||||
|
||||
def test_collector_returns_manifested_snapshot_summary(self):
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
result = collect_bot_vintage(
|
||||
|
||||
Reference in new issue
Block a user