test_app_config.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import tempfile
  2. import unittest
  3. from pathlib import Path
  4. from app_config import load_config
  5. class AppConfigTest(unittest.TestCase):
  6. def test_load_config_with_defaults(self):
  7. with tempfile.TemporaryDirectory() as tmp_dir:
  8. path = Path(tmp_dir) / "config.yaml"
  9. path.write_text(
  10. """
  11. db:
  12. host: 127.0.0.1
  13. port: 5432
  14. database: test_db
  15. user: postgres
  16. password: secret
  17. """.strip(),
  18. encoding="utf-8",
  19. )
  20. config = load_config(path)
  21. self.assertEqual(config.db.host, "127.0.0.1")
  22. self.assertEqual(config.db.port, 5432)
  23. self.assertEqual(config.modbus.host, "0.0.0.0")
  24. self.assertEqual(config.modbus.port, 502)
  25. self.assertEqual(config.modbus.interval, 5)
  26. self.assertEqual(config.logging.retention_days, 3)
  27. def test_load_modbus_interval(self):
  28. with tempfile.TemporaryDirectory() as tmp_dir:
  29. path = Path(tmp_dir) / "config.yaml"
  30. path.write_text(
  31. """
  32. db:
  33. host: 127.0.0.1
  34. port: 5432
  35. database: test_db
  36. user: postgres
  37. password: secret
  38. modbus:
  39. interval: 10
  40. """.strip(),
  41. encoding="utf-8",
  42. )
  43. config = load_config(path)
  44. self.assertEqual(config.modbus.interval, 10)
  45. if __name__ == "__main__":
  46. unittest.main()