Skip to content

Commit

Permalink
Merge pull request #1151 from nstelter-slac/property_tests
Browse files Browse the repository at this point in the history
TST: add tests for objects the use PyDMPrimitiveWidget's RULE_PROPERTIES
  • Loading branch information
jbellister-slac authored Jan 14, 2025
2 parents f5ce657 + 4b94724 commit 96e8cfd
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 3 deletions.
13 changes: 13 additions & 0 deletions pydm/tests/widgets/test_drawing.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
PyDMDrawingPolyline,
PyDMDrawingIrregularPolygon,
)
from ...utilities import checkObjectProperties


# --------------------
Expand Down Expand Up @@ -84,6 +85,15 @@ def test_qt_to_deg(qt_deg, expected_deg):
assert qt_to_deg(qt_deg) == expected_deg


# additional props we expect to get added to PyDMDrawing class RULE_PROPERTIES
expected_drawing_properties = {
"Set Pen Color": ["penColor", QColor],
"Set Pen Style": ["penStyle", int],
"Set Pen Width": ["penWidth", float],
"Set Brush Color": ["brush", QBrush],
}


def test_pydmdrawing_construct(qtbot):
"""
Test the construction of a PyDM base object.
Expand All @@ -97,6 +107,8 @@ def test_pydmdrawing_construct(qtbot):
Window for widget testing
"""
pydm_drawing = PyDMDrawing()
# should be ok to just test this on a single instance of the created object
assert checkObjectProperties(pydm_drawing, expected_drawing_properties) is True
qtbot.addWidget(pydm_drawing)

assert pydm_drawing.alarmSensitiveBorder is False
Expand Down Expand Up @@ -482,6 +494,7 @@ def test_pydmdrawingline_draw_item(qtbot, signals, alarm_sensitive_content):
True if the widget will be redraw with a different color if an alarm is triggered; False otherwise
"""
pydm_drawingline = PyDMDrawingLine(init_channel="fake://tst")
assert checkObjectProperties(pydm_drawingline, expected_drawing_properties) is True
qtbot.addWidget(pydm_drawingline)

pydm_drawingline.alarmSensitiveContent = alarm_sensitive_content
Expand Down
7 changes: 7 additions & 0 deletions pydm/tests/widgets/test_label.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from ...widgets.label import PyDMLabel
from ...widgets.base import PyDMWidget
from ...widgets.display_format import parse_value_for_display, DisplayFormat
from ...utilities import checkObjectProperties

from qtpy.QtWidgets import QApplication, QStyleOption
from qtpy.QtCore import Qt
Expand All @@ -18,6 +19,10 @@
# --------------------


# additional props we expect to get added to PyDMLabel class RULE_PROPERTIES
expected_label_properties = {"Text": ["value_changed", str]}


def test_construct(qtbot):
"""
Test the basic instantiation of the widget.
Expand All @@ -33,6 +38,8 @@ def test_construct(qtbot):
Window for widget testing
"""
pydm_label = PyDMLabel()
assert checkObjectProperties(pydm_label, expected_label_properties) is True

qtbot.addWidget(pydm_label)

display_format_type = pydm_label.displayFormat
Expand Down
7 changes: 6 additions & 1 deletion pydm/tests/widgets/test_related_display_button.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from qtpy.QtWidgets import QApplication
from ...utilities.stylesheet import global_style
from ...widgets.related_display_button import PyDMRelatedDisplayButton
from ...utilities import IconFont
from ...utilities import IconFont, checkObjectProperties

test_ui_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "../test_data", "test.ui")
test_ui_path_with_stylesheet = os.path.join(
Expand All @@ -17,6 +17,10 @@
)


# additional props we expect to get added to PyDMRelatedDisplayButton class RULE_PROPERTIES
expected_related_display_button_properties = {"Text": ["setText", str], "Filenames": ["filenames", list]}


def test_old_display_filename_property(qtbot):
# This test is mostly only checking that the related display button
# doesn't totally explode when the old 'displayFilename' property is used.
Expand All @@ -25,6 +29,7 @@ def test_old_display_filename_property(qtbot):
main_window.setWindowTitle("Related Display Button Test")
qtbot.addWidget(main_window)
button = PyDMRelatedDisplayButton(parent=main_window)
assert checkObjectProperties(button, expected_related_display_button_properties) is True
with warnings.catch_warnings(record=True) as record:
button.displayFilename = test_ui_path
assert len(record) >= 1
Expand Down
6 changes: 6 additions & 0 deletions pydm/tests/widgets/test_slider.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from qtpy.QtGui import QMouseEvent
from ...widgets.slider import PyDMSlider, PyDMPrimitiveSlider
from ...widgets.base import PyDMWidget
from ...utilities import checkObjectProperties

# Unit Tests for the PyDMPrimitiveSlider class

Expand Down Expand Up @@ -199,6 +200,10 @@ def test_getSliderPosition(slider_fixture, request):
# Unit Tests for the PyDMSlider Widget


# additional props we expect to get added to PyDMSlider class RULE_PROPERTIES
expected_slider_properties = {"Set Step Size ": ["step_size", float]}


def test_construct(qtbot):
"""
Test the construction of the widget.
Expand All @@ -212,6 +217,7 @@ def test_construct(qtbot):
Window for widget testing
"""
pydm_slider = PyDMSlider()
assert checkObjectProperties(pydm_slider, expected_slider_properties) is True
qtbot.addWidget(pydm_slider)

assert pydm_slider.alarmSensitiveContent is True
Expand Down
7 changes: 7 additions & 0 deletions pydm/tests/widgets/test_symbol_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@

from ...widgets.symbol import PyDMSymbol
from ...widgets.symbol_editor import SymbolEditor
from ...utilities import checkObjectProperties


# additional props we expect to get added to PyDMSymbol class RULE_PROPERTIES
expected_symbol_properties = {"Index": ["set_current_key", int]}


def test_symbol_editor(qtbot, monkeypatch):
Expand All @@ -21,6 +26,8 @@ def test_symbol_editor(qtbot, monkeypatch):
"""
# Create the base widget
widget = PyDMSymbol()
assert checkObjectProperties(widget, expected_symbol_properties) is True

qtbot.addWidget(widget)

# Ensure that no rules are set
Expand Down
42 changes: 42 additions & 0 deletions pydm/utilities/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,3 +560,45 @@ def get_clipboard_text() -> str:
if text:
return text
return ""


def checkObjectProperties(class_object, extra_properties):
"""
Check that an object has the expected RULE_PROPERTIES map.
This function should only be used on objects derived from PyDMWidget and
therefore PyDMPrimitiveWidget (which defines the RULE_PROPERTIES).
The expected properties are the base properties of PyDMWidget and any extra ones applied to the subclass.
Parameters
----------
class_object : Type[PyDMWidget]
The object to check the properties of.
extra_properties : dict
Map of the additional properties we expect applied to the class_object.
These should be only the extra props applied to the derived class itself,
not including the props applied to PyDMWidget and PyDMPrimitiveWidget.
"""

# Properties that all PyDMWidget derived objects should all have, since they are applied
# at the definition of PyDMWidget and PyDMPrimitiveWidget classes.
pydm_widget_props = {
"Enable": ["setEnabled", bool],
"Visible": ["setVisible", bool],
"Opacity": ["set_opacity", float],
"Position - X": ["setX", int],
"Position - Y": ["setY", int],
}

# Combine the properties into one map
pydm_widget_props.update(extra_properties)

# Check if object's properties are as expected
for key, value in pydm_widget_props.items():
if key not in class_object.RULE_PROPERTIES:
print("Missing property: ", key)
return False
if class_object.RULE_PROPERTIES[key] != value:
print(f"Mismatch for property '{key}': expected {value}, got {class_object.RULE_PROPERTIES[key]}")
return False
return True
3 changes: 1 addition & 2 deletions pydm/widgets/related_display_button.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@

class PyDMRelatedDisplayButton(QPushButton, PyDMWidget, new_properties=_relatedDisplayRuleProperties):
"""
A QPushButton capable of opening a new Display at the same of at a
new window.
A QPushButton capable of opening a new Display in the same or a new window.
Parameters
----------
Expand Down

0 comments on commit 96e8cfd

Please sign in to comment.