Files
radar_system/python_app/tests/test_gui_profile_codec.py
2026-06-11 13:02:02 +03:00

88 lines
3.4 KiB
Python

"""Tests for GUI profile persistence."""
from __future__ import annotations
import unittest
from python_app.models.gui_profile_model import (
GuiPassThroughStateModel,
GuiProcessingStateModel,
GuiProfileModel,
GuiStateModel,
)
class GuiProfileCodecTest(unittest.TestCase):
def test_pass_through_combo_filter_round_trips(self) -> None:
profile = GuiProfileModel(
gui=GuiStateModel(
processing=GuiProcessingStateModel(
pass_through=GuiPassThroughStateModel(combo_filter="0:0,1:0")
)
)
)
encoded = profile.to_dict()
decoded = GuiProfileModel.from_dict(encoded)
self.assertIsNotNone(decoded.gui)
assert decoded.gui is not None
self.assertEqual(decoded.gui.processing.pass_through.combo_filter, "0:0,1:0")
self.assertEqual(encoded["gui"]["processing"]["pass_through"]["combo_filter"], "0:0,1:0")
def test_pass_through_unwrap_phase_round_trips(self) -> None:
profile = GuiProfileModel(
gui=GuiStateModel(
processing=GuiProcessingStateModel(
pass_through=GuiPassThroughStateModel(unwrap_phase=True)
)
)
)
encoded = profile.to_dict()
decoded = GuiProfileModel.from_dict(encoded)
assert decoded.gui is not None
self.assertTrue(decoded.gui.processing.pass_through.unwrap_phase)
self.assertTrue(encoded["gui"]["processing"]["pass_through"]["unwrap_phase"])
def test_default_profile_round_trips_idempotently(self) -> None:
# Full-subtree idempotence catches field-drop/mis-map regressions across every
# sub-model, which the single combo_filter round-trip above cannot.
once = GuiProfileModel().to_dict()
twice = GuiProfileModel.from_dict(once).to_dict()
self.assertEqual(once["gui"], twice["gui"])
def test_missing_gui_section_yields_none(self) -> None:
self.assertIsNone(GuiProfileModel.from_dict({}).gui)
def test_unsupported_version_is_rejected(self) -> None:
with self.assertRaisesRegex(ValueError, "version"):
GuiProfileModel.from_dict({"gui": {"version": 2}})
def test_invalid_combo_mode_is_rejected(self) -> None:
with self.assertRaisesRegex(ValueError, "combo_mode"):
GuiProfileModel.from_dict({"gui": {"switches": {"combo_mode": "bogus"}}})
def test_wrong_typed_field_is_rejected(self) -> None:
with self.assertRaises(ValueError): # combos_text must be a JSON string, not a number
GuiProfileModel.from_dict({"gui": {"switches": {"combos_text": 123}}})
def test_legacy_gpr_payload_migrates_selected_mode(self) -> None:
# An old payload that selects 'gpr' but carries a legacy root gpr.mode is migrated.
decoded = GuiProfileModel.from_dict({
"gui": {"processing": {"selected_mode": "gpr"}},
"gpr": {"relative_permittivity": 4.0, "mode": "point"},
})
assert decoded.gui is not None
self.assertEqual(decoded.gui.processing.selected_mode, "legacy_gpr")
def test_modern_gpr_selection_is_not_migrated(self) -> None:
decoded = GuiProfileModel.from_dict({"gui": {"processing": {"selected_mode": "gpr"}}})
assert decoded.gui is not None
self.assertEqual(decoded.gui.processing.selected_mode, "gpr")
if __name__ == "__main__":
unittest.main()