0
0
mirror of https://github.com/bec-project/bec_widgets.git synced 2025-07-14 03:31:50 +02:00

test: test_hierarchy.py added

This commit is contained in:
wyzula-jan
2023-10-31 15:11:49 +01:00
parent de23c28e40
commit f396f98e73
2 changed files with 127 additions and 6 deletions

99
tests/test_hierarchy.py Normal file
View File

@ -0,0 +1,99 @@
import pytest
from PyQt5.QtWidgets import (
QWidget,
QVBoxLayout,
QLineEdit,
QComboBox,
QTableWidget,
QSpinBox,
)
from bec_widgets.qt_utils.widget_hierarchy import (
export_config_to_dict,
import_config_from_dict,
)
@pytest.fixture(scope="function")
def example_widget(qtbot):
# Create a widget with a few child widgets
main_widget = QWidget()
layout = QVBoxLayout(main_widget)
line_edit = QLineEdit(main_widget)
combo_box = QComboBox(main_widget)
table_widget = QTableWidget(2, 2, main_widget)
spin_box = QSpinBox(main_widget)
layout.addWidget(line_edit)
layout.addWidget(combo_box)
layout.addWidget(table_widget)
layout.addWidget(spin_box)
# Add text items to the combo box
combo_box.addItems(["Option 1", "Option 2", "Option 3"])
qtbot.addWidget(main_widget)
qtbot.waitExposed(main_widget)
yield main_widget
def test_export_import_config(example_widget):
initial_config = {
"QWidget ()": {
"QLineEdit ()": {"value": "New Text"},
"QComboBox ()": {"value": 1},
"QTableWidget ()": {"value": [["a", "b"], ["c", "d"]]},
"QSpinBox ()": {"value": 10},
}
}
import_config_from_dict(example_widget, initial_config, set_values=True)
exported_config_full = export_config_to_dict(example_widget, grab_values=True)
exported_config_reduced = export_config_to_dict(
example_widget, grab_values=True, save_all=False
)
expected_full = {
"QWidget ()": {
"QVBoxLayout ()": {},
"QLineEdit ()": {"value": "New Text", "QObject ()": {}},
"QComboBox ()": {"value": 1, "QStandardItemModel ()": {}},
"QTableWidget ()": {
"value": [["a", "b"], ["c", "d"]],
"QWidget (qt_scrollarea_viewport)": {},
"QStyledItemDelegate ()": {},
"QHeaderView ()": {
"QWidget (qt_scrollarea_viewport)": {},
"QWidget (qt_scrollarea_hcontainer)": {
"QScrollBar ()": {},
"QBoxLayout ()": {},
},
"QWidget (qt_scrollarea_vcontainer)": {
"QScrollBar ()": {},
"QBoxLayout ()": {},
},
"QItemSelectionModel ()": {},
},
"QAbstractButton ()": {},
"QAbstractTableModel ()": {},
"QItemSelectionModel ()": {},
"QWidget (qt_scrollarea_hcontainer)": {"QScrollBar ()": {}, "QBoxLayout ()": {}},
"QWidget (qt_scrollarea_vcontainer)": {"QScrollBar ()": {}, "QBoxLayout ()": {}},
},
"QSpinBox ()": {
"value": 10,
"QLineEdit (qt_spinbox_lineedit)": {"value": "10", "QObject ()": {}},
"QValidator (qt_spinboxvalidator)": {},
},
}
}
expected_reduced = {
"QWidget ()": {
"QLineEdit ()": {"value": "New Text"},
"QComboBox ()": {"value": 1},
"QTableWidget ()": {"value": [["a", "b"], ["c", "d"]]},
"QSpinBox ()": {"value": 10, "QLineEdit (qt_spinbox_lineedit)": {"value": "10"}},
}
}
assert exported_config_full == expected_full
assert exported_config_reduced == expected_reduced