Files
ai-exchange/tests/unit/test_range_service.py
T

67 lines
1.9 KiB
Python

import os
import unittest
os.environ.setdefault("DATABASE_URL", "postgresql+psycopg://user:pass@localhost:5432/ai_ict_test")
from src.services.ranges import DealingRangeService, RangeCandidate, range_matches_candidate
class RangeIdempotencyTests(unittest.TestCase):
def test_range_matches_candidate_when_same_structure(self) -> None:
candidate = RangeCandidate(
high=110.0,
low=90.0,
equilibrium=100.0,
high_swing_id=10,
low_swing_id=11,
ts_start="t",
)
latest = {
"high": 110.0,
"low": 90.0,
"equilibrium": 100.0,
"high_swing_id": 10,
"low_swing_id": 11,
"ts_start": "t",
}
self.assertTrue(range_matches_candidate(latest, candidate))
def test_persist_active_range_skips_equivalent_latest_row(self) -> None:
class StubRepo:
def __init__(self):
self.upsert_calls = 0
def fetch_latest_valid_range(self, instrument_id, timeframe):
return {
"high": 110.0,
"low": 90.0,
"equilibrium": 100.0,
"high_swing_id": 10,
"low_swing_id": 11,
"ts_start": "t",
}
def upsert_range(self, **kwargs):
self.upsert_calls += 1
repo = StubRepo()
service = DealingRangeService(repository=repo)
candidate = RangeCandidate(
high=110.0,
low=90.0,
equilibrium=100.0,
high_swing_id=10,
low_swing_id=11,
ts_start="t",
)
persisted = service.persist_active_range(instrument_id=1, timeframe="1m", candidate=candidate)
self.assertEqual(persisted, 0)
self.assertEqual(repo.upsert_calls, 0)
if __name__ == "__main__":
unittest.main()