"""Invalid Part Number drafts stay editable and identify their offending cell.""" from dataclasses import replace from pathlib import Path import pytest from PySide6.QtCore import Qt from PySide6.QtWidgets import QLineEdit from dx_productivity.trial_task_center.errors import TaskCenterError from dx_productivity.trial_task_center.target_plan import ComponentTarget, TargetPlanRow, compile_target_rules from dx_productivity.trial_task_center.ui.mapping_table import MappingTable from dx_productivity.trial_task_center.ui.target_table import TargetTable def target_row(): return TargetPlanRow(ComponentTarget(Path('board.prj'), 'MAIN', '1', 'uid', 'R1', 'OLD', ''), True, 'NEW') @pytest.mark.parametrize('bulk', [False, True]) def test_invalid_pn_highlight_and_tooltip_clear_after_correction(qtbot, bulk): table = TargetTable() qtbot.addWidget(table) table.populate((target_row(),)) index = table.model().index(0, 6) if bulk: table.selectRow(0) table.apply_to_selected('BAD PART !!@#') else: assert table.model().setData(index, 'BAD PART !!@#') assert table.rows()[0].new_part_number == 'BAD PART !!@#' and table.dirty assert index.data(Qt.ItemDataRole.BackgroundRole) is not None assert '料号' in index.data(Qt.ItemDataRole.ToolTipRole) assert '空白' in index.data(Qt.ItemDataRole.ToolTipRole) with pytest.raises(TaskCenterError): compile_target_rules(table.rows()) assert table.rows()[0].new_part_number == 'BAD PART !!@#' assert table.model().setData(index, 'NEW-OK') assert index.data(Qt.ItemDataRole.BackgroundRole) is None assert compile_target_rules(table.rows())[0].new_part_number == 'NEW-OK' def test_mapping_pn_error_is_visible_without_flagging_free_value(qtbot): from types import SimpleNamespace table = MappingTable() qtbot.addWidget(table) table.populate((SimpleNamespace(rule_id='rule', old_part_number='OLD', new_part_number='BAD PART !!@#', custom_value_enabled=True, custom_value='free !@#', enabled=True, position=0),)) assert table.model().index(0, 1).data(Qt.ItemDataRole.BackgroundRole) is not None assert '空白' in table.model().index(0, 1).data(Qt.ItemDataRole.ToolTipRole) assert table.model().index(0, 2).data(Qt.ItemDataRole.BackgroundRole) is None def test_part_number_editor_has_an_accessible_name(qtbot): table = TargetTable() qtbot.addWidget(table) table.populate((target_row(),)) table.show() table.edit(table.model().index(0, 6)) editor = table.findChild(QLineEdit) assert editor is not None and '新料号' in editor.accessibleName() def test_active_editor_commits_invalid_text_to_draft_without_losing_it(qtbot): table = TargetTable() qtbot.addWidget(table) table.populate((target_row(),)) table.show() table.edit(table.model().index(0, 6)) editor = table.findChild(QLineEdit) editor.setText('BAD PART !!@#') assert table.has_pending_editor() assert table.commit_editor() assert table.rows()[0].new_part_number == 'BAD PART !!@#' and table.dirty with pytest.raises(TaskCenterError): compile_target_rules(table.rows()) assert not table.has_pending_editor() def test_rejected_readonly_editor_commit_preserves_text_and_allows_retry(qtbot): table = TargetTable() qtbot.addWidget(table) table.populate((target_row(),)) table.show() table.edit(table.model().index(0, 6)) editor = table.findChild(QLineEdit) editor.setText('PENDING-NEW') table.set_plan_editable(False) with pytest.raises(TaskCenterError, match='editor_commit'): table.commit_editor() assert table.rows()[0].new_part_number == 'NEW' assert table.has_pending_editor() and editor.text() == 'PENDING-NEW' and editor.isVisible() table.set_plan_editable(True) assert table.commit_editor() assert table.rows()[0].new_part_number == 'PENDING-NEW' assert not table.has_pending_editor() def test_unchanged_after_trim_editor_can_close_without_false_rejection(qtbot): table = TargetTable() qtbot.addWidget(table) table.populate((target_row(),)) table.show() table.edit(table.model().index(0, 6)) table.findChild(QLineEdit).setText(' NEW ') assert table.commit_editor() assert table.rows()[0].new_part_number == 'NEW' and not table.dirty