Skip to content

Commit 4b8132c

Browse files
committed
refactor(component editor): Improve separation of concerns
Update the pytests
1 parent 856c018 commit 4b8132c

2 files changed

Lines changed: 14 additions & 12 deletions

File tree

ardupilot_methodic_configurator/frontend_tkinter_component_editor_base.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -261,9 +261,7 @@ def _create_save_frame(self) -> None:
261261
save_frame = ttk.Frame(self.main_frame)
262262
save_frame.pack(side=tk.TOP, fill="x", expand=False)
263263

264-
self.save_button = ttk.Button(
265-
save_frame, text=_("Save data and start configuration"), command=self.validate_and_save_component_json
266-
)
264+
self.save_button = ttk.Button(save_frame, text=_("Save data and start configuration"), command=self.on_save_pressed)
267265
show_tooltip(
268266
self.save_button,
269267
_("Save component data to the vehicle_components.json file\nand start parameter value configuration and tuning."),
@@ -519,14 +517,15 @@ def validate_data_and_highlight_errors_in_red(self) -> str:
519517

520518
return ""
521519

522-
def validate_and_save_component_json(self) -> None:
523-
"""Validate and save the component JSON data."""
520+
def on_save_pressed(self) -> None:
521+
"""Validate component data, save the component JSON file and close the window."""
524522
error_msg = self.validate_data_and_highlight_errors_in_red()
525523
if error_msg:
526524
show_error_message(_("Error"), error_msg)
527525
return
528526
if self._confirm_component_properties():
529527
self.save_component_json()
528+
self.root.destroy()
530529

531530
def _confirm_component_properties(self) -> bool:
532531
"""Show confirmation dialog for component properties."""

tests/test_frontend_tkinter_component_editor_base.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -576,17 +576,16 @@ def test_user_can_successfully_save_component_data(self, editor_for_save_tests:
576576
577577
GIVEN: A user has completed their component configuration
578578
WHEN: They save the configuration and it succeeds
579-
THEN: The data should be saved and the window should close
579+
THEN: The data should be saved
580580
"""
581581
# Arrange: Configure successful save operation
582582
editor_for_save_tests.data_model.save_to_filesystem.return_value = (False, "")
583583

584584
# Act: Save component data
585585
editor_for_save_tests.save_component_json()
586586

587-
# Assert: Save operation should be attempted and window should close
587+
# Assert: Save operation should be attempted
588588
editor_for_save_tests.data_model.save_to_filesystem.assert_called_once_with(editor_for_save_tests.local_filesystem)
589-
editor_for_save_tests.root.destroy.assert_called_once()
590589

591590
def test_user_receives_error_feedback_when_save_fails(self, editor_for_save_tests: ComponentEditorWindowBase) -> None:
592591
"""
@@ -620,7 +619,7 @@ def test_user_must_confirm_before_saving_component_data(self, editor_for_save_te
620619
"ardupilot_methodic_configurator.frontend_tkinter_component_editor_base.messagebox.askyesno", return_value=True
621620
):
622621
# Act: Trigger validate and save operation
623-
editor_for_save_tests.validate_and_save_component_json()
622+
editor_for_save_tests.on_save_pressed()
624623

625624
# Assert: Validation and save should proceed
626625
editor_for_save_tests.validate_data_and_highlight_errors_in_red.assert_called_once()
@@ -644,7 +643,8 @@ def test_user_can_save_before_closing_when_prompted(self, editor_for_closing_tes
644643
WHEN: They choose to save their changes in the confirmation dialog
645644
THEN: The save operation should be executed before closing
646645
"""
647-
# Arrange: Mock user choosing to save
646+
# Arrange: Mock user choosing to save and a successful save operation
647+
editor_for_closing_tests.save_component_json = MagicMock(return_value=False)
648648
with (
649649
patch(
650650
"ardupilot_methodic_configurator.frontend_tkinter_component_editor_base.messagebox.askyesnocancel",
@@ -1021,6 +1021,9 @@ def test_user_benefits_from_consistent_ui_styling(
10211021
# Arrange: Mock the style instance
10221022
mock_style = MagicMock()
10231023
mock_style_class.return_value = mock_style
1024+
# Provide a sensible default DPI scaling for tests so style font sizes are predictable
1025+
# 9pt base with 1.5 scaling -> int(9 * 1.5) == 13, matching test expectations
1026+
editor_for_ui_tests.dpi_scaling_factor = 1.5
10241027

10251028
# Act: Setup styles
10261029
editor_for_ui_tests._setup_styles()
@@ -1442,7 +1445,7 @@ def test_user_receives_validation_errors_before_save(
14421445
editor_for_validation_tests.validate_data_and_highlight_errors_in_red.return_value = error_message
14431446

14441447
# Act: Attempt to validate and save
1445-
editor_for_validation_tests.validate_and_save_component_json()
1448+
editor_for_validation_tests.on_save_pressed()
14461449

14471450
# Assert: Error should be displayed and save should not proceed
14481451
mock_error.assert_called_once_with(_("Error"), error_message)
@@ -1469,7 +1472,7 @@ def test_user_must_confirm_component_properties_before_save(
14691472
)
14701473

14711474
# Act: Attempt to validate and save
1472-
editor_for_validation_tests.validate_and_save_component_json()
1475+
editor_for_validation_tests.on_save_pressed()
14731476

14741477
# Assert: Confirmation should be requested but save should not proceed
14751478
mock_confirm.assert_called_once()

0 commit comments

Comments
 (0)