Files
dxdesigner-part-replacement/tests/v1_1/test_target_table.py
T

170 lines
7.8 KiB
Python

import os
os.environ.setdefault('QT_QPA_PLATFORM', 'offscreen')
from pathlib import Path
from PySide6.QtCore import Qt
from PySide6.QtWidgets import QApplication
from dx_productivity.trial_task_center.target_plan import ComponentTarget, TargetPlanRow
from dx_productivity.trial_task_center.ui.target_table import TargetTable
def test_copy_material_numbers_works_when_locked_and_filtered(qtbot):
table = TargetTable()
qtbot.addWidget(table)
rows = tuple(TargetPlanRow(ComponentTarget(Path('board.prj'), 'MAIN', '1', str(i),
f'R{i}', f'OLD-{i}', 'V'), True, f'NEW-{i}') for i in range(3))
table.populate(rows)
table.set_plan_editable(False)
table.show()
table.setCurrentIndex(table.model().index(1, 4))
qtbot.keyClick(table, Qt.Key.Key_C, Qt.KeyboardModifier.ControlModifier)
assert QApplication.clipboard().text() == 'OLD-1'
table.clearSelection()
table.setCurrentIndex(table.model().index(1, 6))
qtbot.keyClick(table, Qt.Key.Key_C, Qt.KeyboardModifier.ControlModifier)
assert QApplication.clipboard().text() == 'NEW-1'
table.selectAll()
table.copy_column(6)
assert QApplication.clipboard().text() == 'NEW-0\nNEW-1\nNEW-2'
table.set_find_text('R2')
table.clearSelection()
table.setCurrentIndex(table.model().index(0, 4))
table.copy_column()
assert QApplication.clipboard().text() == 'OLD-2'
assert table.rows() == rows and not table.dirty
def test_missing_library_part_is_visible_in_status_and_highlighted(qtbot):
table = TargetTable()
qtbot.addWidget(table)
model = table.plan_model
model.workspace_status = True
model.library_issues = {'NEW': '中心库未找到该物料号:NEW'}
table.populate((TargetPlanRow(ComponentTarget(Path('board.prj'), 'MAIN', '1', 'u1', 'R1', 'OLD', 'V'), True, 'NEW'),))
assert model.data(model.index(0, 10)) == '中心库未找到该物料号:NEW'
assert model.data(model.index(0, 6), Qt.ItemDataRole.BackgroundRole) is not None
def test_rectangular_copy_has_tsv_feedback_and_checkboxes_remain_editable(qtbot):
from PySide6.QtCore import QItemSelection, QItemSelectionModel
table = TargetTable()
qtbot.addWidget(table)
table.plan_model.workspace_status = True
table.populate(tuple(TargetPlanRow(ComponentTarget(Path('board.prj'), 'MAIN', '1', str(i),
f'R{i}', f'OLD-{i}', 'V'), True, f'NEW-{i}') for i in range(2)))
table.show()
table.setCurrentIndex(table.model().index(0, 4))
table.selectionModel().select(QItemSelection(table.model().index(0, 4), table.model().index(1, 6)),
QItemSelectionModel.SelectionFlag.ClearAndSelect)
qtbot.keyClick(table, Qt.Key.Key_C, Qt.KeyboardModifier.ControlModifier)
assert QApplication.clipboard().text() == 'OLD-0\tV\tNEW-0\nOLD-1\tV\tNEW-1'
assert len(table._copy_cells) == 6
qtbot.waitUntil(lambda: not table._copy_cells)
table.set_show_unselected(False)
assert table.model().setData(table.model().index(0, 0), Qt.CheckState.Unchecked, Qt.ItemDataRole.CheckStateRole)
assert not table.rows()[0].enabled and table.rowCount() == 2
table.plan_model.library_issues = {'NEW-0': '中心库未找到该物料号:NEW-0'}
assert not table.model().setData(table.model().index(0, 0), Qt.CheckState.Checked, Qt.ItemDataRole.CheckStateRole)
def test_edit_targets_and_nc_in_original_view_without_excel():
app = QApplication.instance() or QApplication([])
table = TargetTable()
rows = tuple(TargetPlanRow(ComponentTarget(Path('C:/board.prj'), 'MAIN', '1', uid,
refdes, 'OLD', value), False, 'NEW')
for uid, refdes, value in [('u1', 'R1', 'A/NC'), ('u2', 'R2', 'B')])
table.populate(rows)
table.model().setData(table.model().index(0, 0), Qt.CheckState.Checked, Qt.ItemDataRole.CheckStateRole)
table.model().setData(table.model().index(0, 6), 'NEW-2')
table.model().setData(table.model().index(0, 7), Qt.CheckState.Checked, Qt.ItemDataRole.CheckStateRole)
table.model().setData(table.model().index(0, 8), '100nF/10V')
table.model().setData(table.model().index(0, 9), Qt.CheckState.Unchecked, Qt.ItemDataRole.CheckStateRole)
edited = table.rows()
assert edited[0].target == rows[0].target
assert edited[0].enabled and edited[0].new_part_number == 'NEW-2'
assert edited[0].custom_value == '100nF/10V' and not edited[0].sync_nc
assert not edited[1].enabled and edited[1].target.uid == 'u2'
assert table.dirty
assert not table.model().flags(table.model().index(0, 3)) & Qt.ItemFlag.ItemIsEditable
table.close()
def test_table_lock_blocks_editing_and_selection():
app = QApplication.instance() or QApplication([])
table = TargetTable()
table.populate((TargetPlanRow(ComponentTarget(Path('C:/board.prj'), 'MAIN', '1',
'u1', 'R1', 'OLD', 'A')),))
table.set_plan_editable(False)
assert not table.model().flags(table.model().index(0, 0)) & Qt.ItemFlag.ItemIsUserCheckable
assert not table.model().flags(table.model().index(0, 6)) & Qt.ItemFlag.ItemIsEditable
table.close()
def test_target_table_find_filters_rows_and_bulk_actions_keep_hidden_rows(qtbot):
table = TargetTable()
qtbot.addWidget(table)
rows = (
TargetPlanRow(ComponentTarget(Path("board.prj"), "MAIN", "1", "u1", "R1", "OLD-A", "10nF")),
TargetPlanRow(ComponentTarget(Path("board.prj"), "MAIN", "2", "u2", "R2", "OLD-B", "22nF")),
TargetPlanRow(ComponentTarget(Path("board.prj"), "AUX", "1", "u3", "C1", "OLD-A", "10uF")),
)
table.populate(rows)
table.set_find_text("R2")
assert table.rowCount() == 1
table.selectRow(0)
table.apply_to_selected("NEW-R2")
table.set_find_text("")
assert table.rowCount() == 3
assert table.rows()[1].new_part_number == "NEW-R2"
assert table.rows()[0].new_part_number == ""
def test_target_table_find_next_selects_next_matching_row(qtbot):
table = TargetTable()
qtbot.addWidget(table)
rows = tuple(
TargetPlanRow(ComponentTarget(Path("board.prj"), "MAIN", str(index), f"u{index}", f"R{index}", "OLD", "V"))
for index in range(1, 4)
)
table.populate(rows)
table.set_find_text("R")
assert table.find_next() == 0
assert table.find_next() == 1
assert table.find_next() == 2
assert table.find_next() == 0
def test_header_sort_edit_and_bulk_actions_keep_uid_binding(qtbot):
table = TargetTable()
qtbot.addWidget(table)
rows = tuple(TargetPlanRow(ComponentTarget(Path('board.prj'), 'MAIN', '1', uid, ref, 'OLD', 'V'), enabled=True)
for uid, ref in [('u2', 'R2'), ('u1', 'R1')])
table.populate(rows)
assert table.isSortingEnabled()
table.sortByColumn(3, Qt.SortOrder.AscendingOrder)
assert table.model().index(0, 3).data() == 'R1'
table.model().setData(table.model().index(0, 6), 'NEW-R1')
table.selectRow(1)
table.apply_to_selected('NEW-R2')
assert {r.target.uid: r.new_part_number for r in table.rows()} == {'u1': 'NEW-R1', 'u2': 'NEW-R2'}
table.set_find_text('R1')
table.select_all_targets(False)
assert {r.target.uid: r.enabled for r in table.rows()} == {'u1': False, 'u2': True}
def test_click_checkbox_cell_center_toggles_once_and_preserves_locked_state(qtbot):
table = TargetTable()
qtbot.addWidget(table)
table.populate((TargetPlanRow(ComponentTarget(Path('board.prj'), 'MAIN', '1', 'u1', 'R1', 'OLD', 'V'), True, 'NEW'),))
table.show()
point = table.visualRect(table.model().index(0, 0)).center()
qtbot.mouseClick(table.viewport(), Qt.MouseButton.LeftButton, pos=point)
assert not table.rows()[0].enabled
qtbot.mouseClick(table.viewport(), Qt.MouseButton.LeftButton, pos=point)
assert table.rows()[0].enabled
table.set_plan_editable(False)
qtbot.mouseClick(table.viewport(), Qt.MouseButton.LeftButton, pos=point)
assert table.rows()[0].enabled