Files
AareDAQ/tests/unit/gui/test_message_box.py
T
David Perl df3ddc5c84
Build and Publish / test (pull_request_target) Successful in 2m59s
Build and Publish / build (pull_request_target) Successful in 19s
Build and Publish / Build and Deploy Docs (pull_request_target) Successful in 35s
Live changes from early July, PX-III
2026-07-27 10:33:04 +02:00

78 lines
2.7 KiB
Python

from unittest.mock import MagicMock, patch
from PySide6.QtWidgets import QMessageBox
from aare.gui.widgets.message_box import (
reply_box,
timer_box,
ring_current_low_check,
experiment_hutch_shutter_check,
ring_current_auto_check,
)
import pytest
def test_reply_box(qtbot):
parent = MagicMock()
with patch("PySide6.QtWidgets.QMessageBox.question") as mock_question:
mock_question.return_value = QMessageBox.StandardButton.Yes
res = reply_box(parent, "Title", "Message")
assert res == QMessageBox.StandardButton.Yes
mock_question.assert_called_once()
def test_timer_box_auto_accept(qtbot):
parent = None
condition_func = MagicMock(return_value=True)
# We need to process events for timer to fire
box = timer_box(parent, condition_func=condition_func)
# Wait until box is closed by check()
qtbot.waitUntil(lambda: not box.isVisible(), timeout=2000)
assert box.result() == QMessageBox.StandardButton.Yes
@pytest.mark.timeout(10)
def test_ring_current_low_check_ok(qtbot):
# Should return True immediately if current is high enough
assert ring_current_low_check(None, 361.0) is True
def test_ring_current_low_check_low_yes(qtbot):
with patch("aare.gui.widgets.message_box.reply_box") as mock_reply:
mock_reply.return_value = QMessageBox.StandardButton.Yes
assert ring_current_low_check(None, 50.0) is True
mock_reply.assert_called_once()
def test_ring_current_low_check_low_no(qtbot):
with patch("aare.gui.widgets.message_box.reply_box") as mock_reply:
mock_reply.return_value = QMessageBox.StandardButton.No
assert ring_current_low_check(None, 50.0) is False
def test_experiment_hutch_shutter_check_open(qtbot):
assert experiment_hutch_shutter_check(None, True) is True
def test_experiment_hutch_shutter_check_closed_yes(qtbot):
with patch("aare.gui.widgets.message_box.reply_box") as mock_reply:
mock_reply.return_value = QMessageBox.StandardButton.Yes
assert experiment_hutch_shutter_check(None, False) is True
def test_ring_current_auto_check_yes(qtbot):
# This one uses a nested event loop, which can be tricky to test.
# We'll mock timer_box to return a box that we can close manually.
with patch("aare.gui.widgets.message_box.timer_box") as mock_timer_box:
box = QMessageBox()
box.setStandardButtons(QMessageBox.StandardButton.Yes)
mock_timer_box.return_value = box
# We need to close the box after some time to break the loop
from PySide6.QtCore import QTimer
QTimer.singleShot(100, lambda: box.done(QMessageBox.StandardButton.Yes))
res = ring_current_auto_check(None, 50.0, lambda: False)
assert res is True