Files
set50-system/backend/tests/test_backtest_store.py

54 lines
1.5 KiB
Python

"""Tests for the durable backtest run store (Task 6)."""
from __future__ import annotations
import tempfile
import unittest
from pathlib import Path
from app.backtest_store import BacktestStore
class StoreTest(unittest.TestCase):
def tearDown(self):
self._dir = getattr(self, "_dir", None)
if self._dir:
import shutil
shutil.rmtree(self._dir, ignore_errors=True)
def _store(self):
self._dir = tempfile.mkdtemp()
return BacktestStore(Path(self._dir) / "runs.json")
def test_add_persists_and_assigns_id(self):
s = self._store()
rec = s.add({"start": "2026-01-01", "end": "2026-06-01", "final_equity": 123.0})
self.assertIn("id", rec)
self.assertIn("ran_at", rec)
self.assertEqual(len(s.all()), 1)
def test_survives_restart(self):
path = Path(tempfile.mkdtemp()) / "runs.json"
s1 = BacktestStore(path)
rec = s1.add({"start": "2026-01-01", "end": "2026-06-01"})
# new instance reads from disk
s2 = BacktestStore(path)
self.assertEqual(len(s2.all()), 1)
got = s2.get(rec["id"])
assert got is not None
self.assertEqual(got["start"], "2026-01-01")
def test_get_returns_none_for_unknown(self):
s = self._store()
self.assertIsNone(s.get("nope"))
def test_all_returns_copies(self):
s = self._store()
s.add({"x": 1})
s.all()[0]["x"] = 999
self.assertEqual(s.all()[0]["x"], 1)
if __name__ == "__main__":
unittest.main()