"""配置加载/合并测试。""" import json import os import tempfile import unittest from portcv.config import DEFAULTS, Config, deep_update, load_config class ConfigBasicsTests(unittest.TestCase): def test_defaults_are_available(self): cfg = Config() self.assertEqual(cfg.get_path("run.mode"), "composed") self.assertEqual(cfg.get_path("plc.modbus.port"), 1502) self.assertEqual(cfg.get_path("scene.mode"), "composed") self.assertIn("container", cfg.labels) self.assertEqual(cfg.input_size, (640, 640)) self.assertEqual(cfg.roi[0], (0.03, 0.10)) self.assertEqual(cfg.roi[2], (0.97, 0.99)) def test_get_path_default(self): cfg = Config() self.assertEqual(cfg.get_path("nope.nothing", "fallback"), "fallback") self.assertIsNone(cfg.get_path("run.nothing")) def test_override_merges_deeply(self): cfg = Config({"run": {"max_fps": 5.0}, "plc": {"modbus": {"unit_id": 3}}}) self.assertAlmostEqual(cfg.get_path("run.max_fps"), 5.0) self.assertEqual(cfg.get_path("run.mode"), "composed") # 未被覆盖的键保留 self.assertEqual(cfg.get_path("plc.modbus.unit_id"), 3) self.assertEqual(cfg.get_path("plc.modbus.port"), 1502) def test_defaults_are_not_mutated(self): cfg = Config({"run": {"max_fps": 1.0}}) cfg.set_path("run.mode", "direct") self.assertAlmostEqual(cfg.get_path("run.max_fps"), 1.0) self.assertEqual(DEFAULTS["run"]["mode"], "composed") self.assertAlmostEqual(DEFAULTS["run"]["max_fps"], 15.0) def test_set_path_creates_nested_keys(self): cfg = Config() cfg.set_path("custom.deep.value", 42) self.assertEqual(cfg.get_path("custom.deep.value"), 42) self.assertIn("custom", cfg) def test_section_returns_copy(self): cfg = Config() section = cfg.section("run") section["mode"] = "changed" self.assertEqual(cfg.get_path("run.mode"), "composed") self.assertEqual(cfg.to_dict()["run"]["mode"], "composed") def test_deep_update_replaces_lists(self): base = {"a": {"b": 1, "c": [1, 2]}} deep_update(base, {"a": {"c": [9], "d": 2}}) self.assertEqual(base, {"a": {"b": 1, "c": [9], "d": 2}}) self.assertEqual(base["a"]["c"], [9]) class LoadConfigTests(unittest.TestCase): def test_missing_file(self): with self.assertRaises(FileNotFoundError): load_config("definitely/not/here.json") def test_unsupported_extension(self): with tempfile.TemporaryDirectory() as tmp: path = os.path.join(tmp, "cfg.txt") with open(path, "w", encoding="utf-8") as fh: fh.write("{}") with self.assertRaises(ValueError): load_config(path) def test_json_roundtrip_and_overrides(self): with tempfile.TemporaryDirectory() as tmp: path = os.path.join(tmp, "sub", "cfg.json") cfg = Config({"run": {"backend": "onnx", "max_fps": 8.0}}) cfg.dump(path) self.assertTrue(os.path.isfile(path)) with open(path, "r", encoding="utf-8") as fh: self.assertEqual(json.load(fh)["run"]["backend"], "onnx") loaded = load_config(path) self.assertEqual(loaded.get_path("run.backend"), "onnx") self.assertAlmostEqual(loaded.get_path("run.max_fps"), 8.0) overridden = load_config(path, {"run": {"max_fps": 12.0}}) self.assertAlmostEqual(overridden.get_path("run.max_fps"), 12.0) self.assertEqual(overridden.get_path("run.backend"), "onnx") def test_none_path_is_pure_defaults(self): self.assertEqual(load_config(None).get_path("run.mode"), "composed") def test_yaml_requires_pyyaml(self): with tempfile.TemporaryDirectory() as tmp: path = os.path.join(tmp, "cfg.yaml") with open(path, "w", encoding="utf-8") as fh: fh.write("run:\n max_fps: 3.0\n") try: import yaml # noqa: F401 except ImportError: with self.assertRaises(RuntimeError): load_config(path) else: # pragma: no cover - 取决于环境是否装了 pyyaml self.assertAlmostEqual(load_config(path).get_path("run.max_fps"), 3.0) if __name__ == "__main__": unittest.main()