test_config.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. """配置加载/合并测试。"""
  2. import json
  3. import os
  4. import tempfile
  5. import unittest
  6. from portcv.config import DEFAULTS, Config, deep_update, load_config
  7. class ConfigBasicsTests(unittest.TestCase):
  8. def test_defaults_are_available(self):
  9. cfg = Config()
  10. self.assertEqual(cfg.get_path("run.mode"), "composed")
  11. self.assertEqual(cfg.get_path("plc.modbus.port"), 1502)
  12. self.assertEqual(cfg.get_path("scene.mode"), "composed")
  13. self.assertIn("container", cfg.labels)
  14. self.assertEqual(cfg.input_size, (640, 640))
  15. self.assertEqual(cfg.roi[0], (0.03, 0.10))
  16. self.assertEqual(cfg.roi[2], (0.97, 0.99))
  17. def test_get_path_default(self):
  18. cfg = Config()
  19. self.assertEqual(cfg.get_path("nope.nothing", "fallback"), "fallback")
  20. self.assertIsNone(cfg.get_path("run.nothing"))
  21. def test_override_merges_deeply(self):
  22. cfg = Config({"run": {"max_fps": 5.0}, "plc": {"modbus": {"unit_id": 3}}})
  23. self.assertAlmostEqual(cfg.get_path("run.max_fps"), 5.0)
  24. self.assertEqual(cfg.get_path("run.mode"), "composed") # 未被覆盖的键保留
  25. self.assertEqual(cfg.get_path("plc.modbus.unit_id"), 3)
  26. self.assertEqual(cfg.get_path("plc.modbus.port"), 1502)
  27. def test_defaults_are_not_mutated(self):
  28. cfg = Config({"run": {"max_fps": 1.0}})
  29. cfg.set_path("run.mode", "direct")
  30. self.assertAlmostEqual(cfg.get_path("run.max_fps"), 1.0)
  31. self.assertEqual(DEFAULTS["run"]["mode"], "composed")
  32. self.assertAlmostEqual(DEFAULTS["run"]["max_fps"], 15.0)
  33. def test_set_path_creates_nested_keys(self):
  34. cfg = Config()
  35. cfg.set_path("custom.deep.value", 42)
  36. self.assertEqual(cfg.get_path("custom.deep.value"), 42)
  37. self.assertIn("custom", cfg)
  38. def test_section_returns_copy(self):
  39. cfg = Config()
  40. section = cfg.section("run")
  41. section["mode"] = "changed"
  42. self.assertEqual(cfg.get_path("run.mode"), "composed")
  43. self.assertEqual(cfg.to_dict()["run"]["mode"], "composed")
  44. def test_deep_update_replaces_lists(self):
  45. base = {"a": {"b": 1, "c": [1, 2]}}
  46. deep_update(base, {"a": {"c": [9], "d": 2}})
  47. self.assertEqual(base, {"a": {"b": 1, "c": [9], "d": 2}})
  48. self.assertEqual(base["a"]["c"], [9])
  49. class LoadConfigTests(unittest.TestCase):
  50. def test_missing_file(self):
  51. with self.assertRaises(FileNotFoundError):
  52. load_config("definitely/not/here.json")
  53. def test_unsupported_extension(self):
  54. with tempfile.TemporaryDirectory() as tmp:
  55. path = os.path.join(tmp, "cfg.txt")
  56. with open(path, "w", encoding="utf-8") as fh:
  57. fh.write("{}")
  58. with self.assertRaises(ValueError):
  59. load_config(path)
  60. def test_json_roundtrip_and_overrides(self):
  61. with tempfile.TemporaryDirectory() as tmp:
  62. path = os.path.join(tmp, "sub", "cfg.json")
  63. cfg = Config({"run": {"backend": "onnx", "max_fps": 8.0}})
  64. cfg.dump(path)
  65. self.assertTrue(os.path.isfile(path))
  66. with open(path, "r", encoding="utf-8") as fh:
  67. self.assertEqual(json.load(fh)["run"]["backend"], "onnx")
  68. loaded = load_config(path)
  69. self.assertEqual(loaded.get_path("run.backend"), "onnx")
  70. self.assertAlmostEqual(loaded.get_path("run.max_fps"), 8.0)
  71. overridden = load_config(path, {"run": {"max_fps": 12.0}})
  72. self.assertAlmostEqual(overridden.get_path("run.max_fps"), 12.0)
  73. self.assertEqual(overridden.get_path("run.backend"), "onnx")
  74. def test_none_path_is_pure_defaults(self):
  75. self.assertEqual(load_config(None).get_path("run.mode"), "composed")
  76. def test_yaml_requires_pyyaml(self):
  77. with tempfile.TemporaryDirectory() as tmp:
  78. path = os.path.join(tmp, "cfg.yaml")
  79. with open(path, "w", encoding="utf-8") as fh:
  80. fh.write("run:\n max_fps: 3.0\n")
  81. try:
  82. import yaml # noqa: F401
  83. except ImportError:
  84. with self.assertRaises(RuntimeError):
  85. load_config(path)
  86. else: # pragma: no cover - 取决于环境是否装了 pyyaml
  87. self.assertAlmostEqual(load_config(path).get_path("run.max_fps"), 3.0)
  88. if __name__ == "__main__":
  89. unittest.main()