File size: 1,914 Bytes
e2ce547 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | from __future__ import annotations
import os
import tempfile
import unittest
from pathlib import Path
from unittest.mock import patch
from polymarket_collector.config import Config
class ConfigTests(unittest.TestCase):
def test_loads_dotenv_and_respects_process_environment(self) -> None:
with tempfile.TemporaryDirectory() as temporary:
env_file = Path(temporary) / ".env"
env_file.write_text(
"MAX_MARKETS=12\nHEALTH_PORT=9090\nMIN_DAILY_REWARD=2.5\n",
encoding="utf-8",
)
clean = {
key: value
for key, value in os.environ.items()
if key
not in {"MAX_MARKETS", "HEALTH_PORT", "MIN_DAILY_REWARD"}
}
clean["ENV_FILE"] = str(env_file)
clean["MAX_MARKETS"] = "15"
with patch.dict(os.environ, clean, clear=True):
config = Config.from_env()
self.assertEqual(config.max_markets, 15)
self.assertEqual(config.health_port, 9090)
self.assertEqual(config.min_daily_reward, 2.5)
def test_port_is_used_when_health_port_is_absent(self) -> None:
clean = {
key: value
for key, value in os.environ.items()
if key not in {"HEALTH_PORT", "PORT", "ENV_FILE"}
}
clean["ENV_FILE"] = "/does/not/exist"
clean["PORT"] = "8765"
with patch.dict(os.environ, clean, clear=True):
config = Config.from_env()
self.assertEqual(config.health_port, 8765)
def test_rejects_invalid_limits(self) -> None:
with patch.dict(
os.environ,
{"ENV_FILE": "/does/not/exist", "MAX_MARKETS": "0"},
clear=True,
):
with self.assertRaises(ValueError):
Config.from_env()
if __name__ == "__main__":
unittest.main()
|