Files
trex-core/tests/runtime/test_account_snapshot_config.py
T

56 lines
1.6 KiB
Python

"""Account snapshot configuration stays opt-in and bounded."""
from __future__ import annotations
import pytest
from trex_core.runtime.config import AccountSnapshotConfig, DaemonConfig
_KEYS = (
"TREX_ACCOUNT_SNAPSHOT_ENABLED",
"TREX_ACCOUNT_SNAPSHOT_BUY_MAX_AGE_SEC",
"TREX_ACCOUNT_SNAPSHOT_SELL_MAX_AGE_SEC",
"TREX_ACCOUNT_SNAPSHOT_REFRESH_INTERVAL_SEC",
)
@pytest.fixture(autouse=True)
def _clean(monkeypatch):
for key in _KEYS:
monkeypatch.delenv(key, raising=False)
monkeypatch.setenv("TREX_POSTGRESQL_DSN", "dbname=x")
def test_defaults_are_disabled_and_bounded():
config = DaemonConfig.from_env().account_snapshot
assert config == AccountSnapshotConfig()
assert config.enabled is False
def test_env_enables_and_tunes_account_snapshot():
import os
os.environ["TREX_ACCOUNT_SNAPSHOT_ENABLED"] = "true"
os.environ["TREX_ACCOUNT_SNAPSHOT_BUY_MAX_AGE_SEC"] = "300"
os.environ["TREX_ACCOUNT_SNAPSHOT_SELL_MAX_AGE_SEC"] = "1800"
os.environ["TREX_ACCOUNT_SNAPSHOT_REFRESH_INTERVAL_SEC"] = "240"
config = DaemonConfig.from_env().account_snapshot
assert config.enabled is True
assert config.buy_max_age_sec == 300
assert config.sell_max_age_sec == 1800
assert config.refresh_interval_sec == 240
@pytest.mark.parametrize(
"kwargs",
[
{"buy_max_age_sec": -1},
{"buy_max_age_sec": 601, "sell_max_age_sec": 600},
{"sell_max_age_sec": 21_601},
{"refresh_interval_sec": 0},
],
)
def test_invalid_age_or_interval_policy_is_rejected(kwargs):
with pytest.raises(ValueError):
AccountSnapshotConfig(**kwargs)