Files
radar_system/python_app/tests/test_antenna_geometry.py
T
2026-10-05 16:46:28 +03:00

135 lines
6.6 KiB
Python

"""Geometry edits preserve XYZ and reject stale or invalid processing results."""
from __future__ import annotations
from dataclasses import asdict
import os
from pathlib import Path
import unittest
from unittest import mock
import numpy as np
os.environ.setdefault("QT_QPA_PLATFORM", "offscreen")
from PyQt6.QtCore import QSignalBlocker
from PyQt6.QtWidgets import QApplication
from python_app.gui.antenna_geometry_editor import AntennaGeometryEditor
from python_app.tests.gui_test_support import isolated_window
from python_app.models.dataset_model import ResultCollection, ResultPayload
from python_app.models.gui_profile_model import GuiProfileModel
from python_app.models.run_config_model import GprTxGeometryModel, GprRxGeometryModel
class AntennaGeometryTest(unittest.TestCase):
@classmethod
def setUpClass(cls) -> None:
cls.app = QApplication.instance() or QApplication([])
def test_coordinates_keep_full_precision_and_default_missing_axes(self) -> None:
entry = GprTxGeometryModel(output_pos=2, x_m=0.12345678912345, y_m=-2.5, z_m=3.5)
editor = AntennaGeometryEditor("output_pos", [entry], position_count=4)
self.assertEqual(editor.records(), [asdict(entry)])
editor.set_records([{"output_pos": 1, "x_m": -1}])
self.assertEqual(editor.records(), [{"output_pos": 1, "x_m": -1.0, "y_m": 0.0, "z_m": 0.0}])
def test_invalid_edit_rolls_back_without_emitting_a_change(self) -> None:
editor = AntennaGeometryEditor("input_pos", [GprRxGeometryModel(input_pos=0, x_m=1.0)], position_count=2)
changed = mock.Mock()
editor.changed.connect(changed)
with mock.patch("python_app.gui.antenna_geometry_editor.QMessageBox.warning") as warning:
editor._table.item(0, 1).setText("nan")
warning.assert_called_once()
changed.assert_not_called()
self.assertEqual(editor.records()[0]["x_m"], 1.0)
def test_full_table_disables_add_and_removal_frees_a_position(self) -> None:
editor = AntennaGeometryEditor("input_pos", [GprRxGeometryModel(input_pos=0, x_m=1.0)], position_count=1)
self.assertFalse(editor._add_button.isEnabled())
editor._add_row()
self.assertEqual(len(editor.records()), 1)
editor._table.selectRow(0)
editor._remove_button.click()
self.assertEqual(editor.records(), [])
self.assertTrue(editor._add_button.isEnabled())
editor._add_button.click()
self.assertEqual(editor.records()[0]["input_pos"], 0)
def test_web_geometry_rejects_duplicates_and_out_of_range_before_mutating(self) -> None:
editor = AntennaGeometryEditor("output_pos", [], position_count=2)
for records in ([{"output_pos": 2}], [{"output_pos": 0}, {"output_pos": 0}],
[{"output_pos": True}], [{"output_pos": 0, "x_m": True}]):
with self.subTest(records=records), self.assertRaises(ValueError):
editor.set_records(records)
self.assertEqual(editor.records(), [])
class WindowGeometryTest(unittest.TestCase):
@classmethod
def setUpClass(cls) -> None:
cls.app = QApplication.instance() or QApplication([])
cls.window = isolated_window(cls.addClassCleanup)
cls.window._timer.stop()
cls.window._shutdown_web_ui()
cls.initial_config = cls.window._build_config()
@classmethod
def tearDownClass(cls) -> None:
cls.window.close()
def setUp(self) -> None:
self.w = self.window
self.w._apply_loaded_profile(GuiProfileModel(run_config=self.initial_config.clone()), Path("run_config.json"))
def test_profile_and_live_config_preserve_all_coordinates(self) -> None:
config = self.initial_config.clone()
config.gpr.tx_geometry = [GprTxGeometryModel(output_pos=0, x_m=1.25, y_m=-2.0, z_m=3.0)]
config.gpr.rx_geometry = [GprRxGeometryModel(input_pos=0, x_m=-1.25, y_m=4.0, z_m=-5.0)]
self.w._apply_loaded_profile(GuiProfileModel(run_config=config), Path("run_config.json"))
rebuilt = self.w._build_config()
self.assertEqual(rebuilt.gpr, config.gpr)
live = self.w._live_processing_config().to_dict()
self.assertEqual(live["tx_geometry"], [asdict(entry) for entry in config.gpr.tx_geometry])
self.assertEqual(live["rx_geometry"], [asdict(entry) for entry in config.gpr.rx_geometry])
schema = self.w._web_settings_schema()
self.assertFalse(any(entry.get("applies_on_start") for entry in schema))
def test_malformed_web_batch_restores_every_changed_widget(self) -> None:
original_gain = self.w._bscan_gain.value()
original_mode = self.w._processing_mode.currentText()
with mock.patch.object(self.w, "_show_exception") as error, mock.patch.object(self.w._live_config_writer, "write") as write:
self.w._apply_web_live_settings({
"processor_mode": "bscan", "bscan_gain": original_gain + 1,
"tx_geometry": [{"output_pos": 0}, {"output_pos": 0}],
})
error.assert_called_once()
write.assert_not_called()
self.assertEqual(self.w._bscan_gain.value(), original_gain)
self.assertEqual(self.w._processing_mode.currentText(), original_mode)
def test_stale_results_cannot_draw_over_new_geometry(self) -> None:
revision = (1 << 63) + 123456789
self.w._required_processing_revision = revision
with QSignalBlocker(self.w._processing_mode):
self.w._processing_mode.setCurrentText("gpr")
words = [(revision >> shift) & 0xffff for shift in range(0, 64, 16)]
fresh = ResultCollection(collection_id=1, monotonic_ns=1, collection_payloads=[ResultPayload(
processing_name="processing_revision", kind=4, table=np.asarray([words], dtype=np.float32),
)])
with mock.patch.object(self.w, "_draw_gpr_map", return_value=True) as draw:
self.assertFalse(self.w._draw_results(ResultCollection(collection_id=1, monotonic_ns=1)))
self.assertTrue(self.w._draw_results(fresh))
draw.assert_called_once_with(fresh)
self.assertFalse(self.w._result_collection_has_trace(fresh))
def test_invalid_or_mixed_history_commands_do_not_clear_data(self) -> None:
with mock.patch.object(self.w, "_show_exception") as error, mock.patch.object(self.w, "_clear_all_runtime_history") as clear:
self.w._apply_web_live_settings({"history_command": "bogus"})
self.w._apply_web_live_settings({"history_command": "clear_all", "bscan_gain": 2})
self.assertEqual(error.call_count, 2)
clear.assert_not_called()
if __name__ == "__main__":
unittest.main()