feat: honor temporal read context
This commit is contained in:
+66
-1
@@ -17,8 +17,17 @@ from govoplan_core.core.institutional import (
|
||||
TemporalRevision,
|
||||
revise_formal_decision,
|
||||
)
|
||||
from govoplan_core.core.temporal import (
|
||||
TemporalDataContext,
|
||||
bind_temporal_data_context,
|
||||
reset_temporal_data_context,
|
||||
)
|
||||
from govoplan_decisions.backend.db.models import FormalDecisionRevision
|
||||
from govoplan_decisions.backend.service import DecisionStoreError, SqlDecisionRegistry
|
||||
from govoplan_decisions.backend.service import (
|
||||
DecisionStoreError,
|
||||
SqlDecisionRegistry,
|
||||
list_decisions,
|
||||
)
|
||||
|
||||
|
||||
NOW = datetime(2026, 8, 1, 12, 0, tzinfo=UTC)
|
||||
@@ -96,6 +105,62 @@ class DecisionTests(unittest.TestCase):
|
||||
with self.assertRaisesRegex(Exception, "same-tenant"):
|
||||
registry.get_decision(self.session, Principal("tenant-2"), reference=first.reference)
|
||||
|
||||
def test_temporal_context_selects_valid_and_recorded_state(self) -> None:
|
||||
registry = SqlDecisionRegistry()
|
||||
first = registry.record_decision(self.session, self.principal, decision=decision())
|
||||
temporal = TemporalRevision(
|
||||
revision="2",
|
||||
valid_from=NOW,
|
||||
recorded_at=NOW + timedelta(minutes=1),
|
||||
change_reason="Effect confirmed.",
|
||||
)
|
||||
revised = revise_formal_decision(
|
||||
first,
|
||||
expected_revision="1",
|
||||
temporal=temporal,
|
||||
state="effective",
|
||||
)
|
||||
registry.record_decision(
|
||||
self.session,
|
||||
self.principal,
|
||||
decision=revised,
|
||||
expected_revision="1",
|
||||
)
|
||||
|
||||
token = bind_temporal_data_context(
|
||||
TemporalDataContext(
|
||||
validity_mode="at",
|
||||
valid_at=NOW + timedelta(hours=1),
|
||||
recorded_at=NOW + timedelta(seconds=30),
|
||||
)
|
||||
)
|
||||
try:
|
||||
historical = registry.get_decision(
|
||||
self.session,
|
||||
self.principal,
|
||||
reference=reference("decision", "decision-1", "decisions", None),
|
||||
)
|
||||
self.assertEqual("1", historical.temporal.revision if historical else None)
|
||||
finally:
|
||||
reset_temporal_data_context(token)
|
||||
|
||||
token = bind_temporal_data_context(
|
||||
TemporalDataContext(
|
||||
validity_mode="at",
|
||||
valid_at=NOW - timedelta(days=1),
|
||||
)
|
||||
)
|
||||
try:
|
||||
self.assertEqual((), list_decisions(self.session, self.principal))
|
||||
finally:
|
||||
reset_temporal_data_context(token)
|
||||
|
||||
token = bind_temporal_data_context(TemporalDataContext(validity_mode="all"))
|
||||
try:
|
||||
self.assertEqual(1, len(list_decisions(self.session, self.principal)))
|
||||
finally:
|
||||
reset_temporal_data_context(token)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user