1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143
|
import datetime
import inspect
from enum import Enum
from pathlib import Path
from typing import List, Optional, Tuple
from unittest.mock import MagicMock, patch
import pytest
from typing_extensions import Annotated
from magicgui import magicgui, types, use_app, widgets
from magicgui.widgets import Container, request_values
from magicgui.widgets.bases import DialogWidget, ValueWidget
from tests import MyInt
# it's important that "qt" be last here, so that it's used for
# the rest of the tests
@pytest.fixture(scope="module", params=["ipynb", "qt"])
def backend(request):
return request.param
# FIXME: this test needs to come before we start swapping backends between qt and ipynb
# in other tests, otherwise it causes a stack overflow in windows...
# I'm not sure why that is, but it likely means that switching apps mid-process is
# not a good idea. This should be explored further and perhaps prevented... and
# testing might need to be reorganized to avoid this problem.
def test_bound_callable_catches_recursion():
"""Test that accessing widget.value raises an informative error message.
(... rather than a recursion error)
"""
# this should NOT raise here. the function should not be called greedily
@magicgui(x={"bind": lambda x: x.value * 2})
def f(x: int = 5):
return x
with pytest.raises(RuntimeError):
assert f() == 10
f.x.unbind()
assert f() == 5
# use `get_value` within the callback if you need to access widget.value
f.x.bind(lambda x: x.get_value() * 4)
assert f() == 20
@pytest.mark.parametrize(
"WidgetClass",
[
getattr(widgets, n)
for n in widgets.__all__
if n
not in (
"Widget",
"TupleEdit",
"FunctionGui",
"MainFunctionGui",
"show_file_dialog",
"request_values",
"create_widget",
)
],
)
def test_widgets(WidgetClass, backend):
"""Test that we can retrieve getters, setters, and signals for most Widgets."""
app = use_app(backend)
if not hasattr(app.backend_module, WidgetClass.__name__):
pytest.skip(f"no {WidgetClass.__name__!r} in backend {backend!r}")
wdg: widgets.Widget = WidgetClass()
wdg.close()
expectations = (
[{"value": 1}, widgets.SpinBox],
[{"value": 1.0}, widgets.FloatSpinBox],
[{"value": "hi"}, widgets.LineEdit],
[{"value": "a", "options": {"choices": ["a", "b"]}}, widgets.Combobox],
[{"value": 1, "widget_type": "Slider"}, widgets.Slider],
)
@pytest.mark.parametrize("kwargs, expect_type", expectations)
def test_create_widget(kwargs, expect_type):
"""Test that various values get turned into widgets."""
wdg = widgets.create_widget(**kwargs)
assert isinstance(wdg, expect_type)
wdg.close()
expectations_annotation = (
(int, widgets.SpinBox),
(float, widgets.FloatSpinBox),
(range, widgets.RangeEdit),
(str, widgets.LineEdit),
(bool, widgets.CheckBox),
(slice, widgets.SliceEdit),
(datetime.date, widgets.DateEdit),
(datetime.time, widgets.TimeEdit),
(datetime.datetime, widgets.DateTimeEdit),
)
@pytest.mark.parametrize("annotation, expected_type", expectations_annotation)
def test_create_widget_annotation(annotation, expected_type):
wdg = widgets.create_widget(annotation=annotation)
assert isinstance(wdg, expected_type)
wdg.close()
def test_create_widget_annotation_overwrite_parrams():
wdg1 = widgets.create_widget(annotation=widgets.ProgressBar)
assert isinstance(wdg1, widgets.ProgressBar)
assert wdg1.visible
wdg2 = widgets.create_widget(
annotation=Annotated[widgets.ProgressBar, {"visible": False}]
)
assert isinstance(wdg2, widgets.ProgressBar)
assert not wdg2.visible
# fmt: off
class MyBadWidget:
"""INCOMPLETE widget implementation and will error."""
def _mgui_close_widget(self): ...
def _mgui_get_visible(self): ...
def _mgui_set_visible(self): ...
def _mgui_get_enabled(self): ...
def _mgui_set_enabled(self, enabled): ...
def _mgui_get_parent(self): ...
def _mgui_set_parent(self, widget): ...
def _mgui_get_native_widget(self): return MagicMock()
def _mgui_get_root_native_widget(self): ...
def _mgui_bind_parent_change_callback(self, callback): ...
def _mgui_render(self): ...
def _mgui_get_width(self): ...
def _mgui_set_width(self, value: int): ...
def _mgui_get_min_width(self): ...
def _mgui_set_min_width(self, value: int): ...
def _mgui_get_max_width(self): ...
def _mgui_set_max_width(self, value: int): ...
def _mgui_get_height(self): ...
def _mgui_set_height(self, value: int): ...
def _mgui_get_min_height(self): ...
def _mgui_set_min_height(self, value: int): ...
def _mgui_get_max_height(self): ...
def _mgui_set_max_height(self, value: int): ...
def _mgui_get_value(self): ...
def _mgui_set_value(self, value): ...
def _mgui_bind_change_callback(self, callback): ...
def _mgui_get_tooltip(self, value): ...
# def _mgui_set_tooltip(self, value): ...
class MyValueWidget(MyBadWidget):
"""Complete protocol implementation... should work."""
def _mgui_set_tooltip(self, value): ...
# fmt: on
def test_custom_widget():
"""Test that create_widget works with arbitrary backend implementations."""
# by implementing the ValueWidgetProtocol, magicgui will know to wrap the above
# widget with a widgets._bases.ValueWidget
with pytest.warns(UserWarning, match="must accept a `parent` Argument"):
wdg = widgets.create_widget(1, widget_type=MyValueWidget) # type:ignore
assert isinstance(wdg, ValueWidget)
wdg.close()
def test_custom_widget_fails():
"""Test that create_widget works with arbitrary backend implementations."""
with pytest.raises(TypeError) as err:
widgets.create_widget(1, widget_type=MyBadWidget) # type: ignore
assert "does not implement 'WidgetProtocol'" in str(err)
assert "Missing methods: {'_mgui_set_tooltip'}" in str(err)
def test_extra_kwargs_error():
"""Test that unrecognized kwargs gives a FutureWarning."""
with pytest.raises(TypeError) as wrn:
widgets.Label(unknown_kwarg="hi")
assert "unexpected keyword argument" in str(wrn)
def test_autocall_no_runtime_error():
"""Make sure changing a value doesn't cause an autocall infinite loop."""
@magicgui(auto_call=True, result_widget=True)
def func(input=1):
return round(input, 4)
func.input.value = 2
def test_basic_widget_attributes():
"""Basic test coverage for getting/setting attributes."""
widget = widgets.create_widget(value=1, name="my_name")
container = widgets.Container(labels=False)
assert widget.enabled
widget.enabled = False
assert not widget.enabled
assert not widget.visible
widget.show()
assert widget.visible
assert widget.parent is None
container.append(widget)
assert widget.parent is container
widget.parent = None
assert widget.parent is None
assert widget.label == "my name"
widget.label = "A different label"
assert widget.label == "A different label"
assert widget.param_kind == inspect.Parameter.POSITIONAL_OR_KEYWORD
widget.param_kind = inspect.Parameter.KEYWORD_ONLY
widget.param_kind = "positional_only"
assert widget.param_kind == inspect.Parameter.POSITIONAL_ONLY
with pytest.raises(KeyError):
widget.param_kind = "not a proper param type"
with pytest.raises(TypeError):
widget.param_kind = 1
assert repr(widget) == "SpinBox(value=1, annotation=None, name='my_name')"
assert widget.options == {
"max": 999,
"min": 0,
"step": None,
"enabled": False,
"visible": False,
}
widget.close()
def test_width_height():
widget = widgets.create_widget(value=1, name="my_name")
widget.show()
assert widget.visible
assert widget.width < 100
widget.width = 150.01
assert widget.width == 150
widget.min_width = 100.01
assert widget.min_width == 100
widget.max_width = 200.01
assert widget.max_width == 200
widget.height = 150.01
assert widget.height == 150
widget.min_height = 100.01
assert widget.min_height == 100
widget.max_height = 200.01
assert widget.max_height == 200
def test_tooltip():
label = widgets.Label()
assert not label.tooltip
label.tooltip = "My Tooltip"
assert label.tooltip == "My Tooltip"
def test_widget_resolves_forward_ref():
"""The annotation on a widget should always be a resolved type."""
@magicgui
def widget(x: "tests.MyInt"): # type: ignore # noqa
pass
assert widget.x.annotation is MyInt
def test_unhashable_choice_data():
"""Test that providing unhashable choice data is ok."""
combo = widgets.ComboBox()
assert not combo.choices
combo.choices = ("a", "b", "c")
assert combo.choices == ("a", "b", "c")
combo.choices = (("a", [1, 2, 3]), ("b", [1, 2, 5]))
assert combo.choices == ([1, 2, 3], [1, 2, 5])
combo.choices = ("x", "y", "z")
assert combo.choices == ("x", "y", "z")
combo.close()
def test_ambiguous_eq_choice_data():
"""Test that providing choice data with an ambiguous equal operation is ok."""
import numpy as np
combo = widgets.ComboBox()
assert not combo.choices
combo.choices = (("a", np.array([0, 0, 0])), ("b", np.array([1, 2, 3])))
assert len(combo.choices) == 2
assert np.all(combo.choices[0] == [0, 0, 0])
assert np.all(combo.choices[1] == [1, 2, 3])
combo.close()
def test_bound_values():
"""Test that we can bind a "permanent" value override to a parameter."""
@magicgui(x={"bind": 10})
def f(x: int = 5):
return x
# bound values hide the widget by default
assert not f.x.visible
assert f() == 10
f.x.unbind()
assert f() == 5
def test_bound_unknown_type_annotation():
"""Test that we can bind a "permanent" value override to a parameter."""
import numpy as np
def _provide_value(_):
return np.array(1)
@magicgui(arr={"bind": _provide_value})
def f(arr: np.ndarray) -> np.ndarray:
return arr
assert f() == np.array(1)
def test_binding_None():
"""Test that we can bind a "permanent" value override to a parameter."""
@magicgui(x={"bind": None})
def f(x: int = 5):
return x
assert f() is None
f.x.unbind()
assert f() == 5
def test_bound_values_visible():
"""Test that we force a bound widget to be visible."""
@magicgui(x={"bind": 10, "visible": True})
def f(x: int = 5):
return x
f.show()
assert f.x.visible
assert f() == 10
f.x.unbind()
assert f() == 5
def test_bound_callables():
"""Test that we can use a callable as a bound value."""
@magicgui(x={"bind": lambda x: 10})
def f(x: int = 5):
return x
assert f() == 10
f.x.unbind()
assert f() == 5
def test_bound_callable_without_calling():
"""Test that we can use a callable as a bound value, but return it directly."""
def callback():
return "hi"
@magicgui
def f(x: int = 5):
return x
assert f() == 5
f.x.bind(callback, call=False)
assert f() == callback
assert f()() == "hi"
def test_bound_not_called():
"""Test that"""
mock = MagicMock()
f = magicgui(lambda a: None, a={"bind": mock})
# the bind function should not be called when creating the widget
mock.assert_not_called()
# the bind function should be called when getting the value
_ = f.a.value
mock.assert_called_once_with(f.a)
def test_progressbar():
"""Test manually controlling a progressbar."""
@magicgui(pbar={"min": 20, "max": 40, "step": 2, "value": 30})
def t(pbar: widgets.ProgressBar):
assert pbar.get_value() == 30
pbar.decrement()
assert pbar.get_value() == 28
pbar.step = 5
assert pbar.get_value() == 28
pbar.increment()
assert pbar.get_value() == 33
pbar.decrement(10)
assert pbar.get_value() == 23
return pbar.get_value()
assert t() == 23
def test_main_function_gui():
"""Test that main_window makes the widget a top level main window with menus."""
@magicgui(main_window=True)
def add(num1: int, num2: int) -> int:
"""Adds the given two numbers, returning the result.
The function assumes that the two numbers can be added and does
not perform any prior checks.
Parameters
----------
num1 , num2 : int
Numbers to be added
Returns
-------
int
Resulting integer
"""
assert not add.visible
add.show()
assert add.visible
assert isinstance(add, widgets.MainFunctionGui)
add._show_docs()
assert isinstance(add._help_text_edit, widgets.TextEdit)
assert add._help_text_edit.value.startswith("Adds the given two numbers")
assert add._help_text_edit.read_only
add.close()
def test_range_widget():
args = (-100, 1000, 2)
rw = widgets.RangeEdit(*args)
v = rw.value
assert isinstance(v, range)
assert (v.start, v.stop, v.step) == args
def test_range_widget_max():
# max will override and restrict the possible values
rw = widgets.RangeEdit(-100, 250, 1, max=(0, 500, 1))
v = rw.value
assert isinstance(v, range)
assert (rw.start.max, rw.stop.max, rw.step.max) == (0, 500, 1)
with pytest.raises(ValueError):
rw = widgets.RangeEdit(100, 300, 5, max=(0, 500, 5))
def test_range_widget_min():
# max will override and restrict the possible values
rw = widgets.RangeEdit(2, 1000, 5, min=(0, 500, 5))
v = rw.value
assert isinstance(v, range)
assert (rw.start.min, rw.stop.min, rw.step.min) == (0, 500, 5)
with pytest.raises(ValueError):
rw = widgets.RangeEdit(-100, 1000, 5, min=(0, 500, 5))
def test_range_value_none():
"""Test that arg: int = None defaults to 0"""
@magicgui
def f(x: Optional[int] = None): # type: ignore
...
assert f.x.value == 0
rw = widgets.SpinBox(value=None)
assert rw.value == 0
@pytest.mark.parametrize(
"value,maksimum", [(10, 999), (None, 999), (1000, 9999), (1500, 9999)]
)
def test_range_big_value(value, maksimum):
rw = widgets.SpinBox(value=value)
assert rw.value == (value if value is not None else 0)
rw.max = maksimum
def test_range_negative_value():
rw = widgets.SpinBox(value=-10)
assert rw.value == -10
assert rw.min == -10
def test_adaptive():
"""Turn on and off adaptive step."""
rw = widgets.SpinBox()
assert rw.adaptive_step
assert rw.step is None
rw.adaptive_step = False
assert not rw.adaptive_step
assert rw.step == 1
rw.step = None
assert rw.adaptive_step
assert rw.step is None
rw.step = 3
assert not rw.adaptive_step
assert rw.step == 3
rw = widgets.SpinBox(step=2)
assert not rw.adaptive_step
assert rw.step == 2
rw.adaptive_step = True
assert rw.adaptive_step
assert rw.step is None
rw.adaptive_step = False
assert not rw.adaptive_step
assert rw.step == 2
def test_exception_range_out_of_range():
with pytest.raises(ValueError):
widgets.SpinBox(value=10000, max=1000)
with pytest.raises(ValueError):
widgets.SpinBox(value=-10, min=0)
def test_file_dialog_events():
"""Test that file dialog events emit the value of the line_edit."""
fe = widgets.FileEdit(value="hi")
mock = MagicMock()
fe.changed.connect(mock)
fe.line_edit.value = "world"
mock.assert_called_once_with(Path("world"))
def test_file_dialog_button_events():
"""Test that clicking the file dialog button doesn't emit an event."""
fe = widgets.FileEdit(value="hi")
mock = MagicMock()
fe.changed.connect(mock)
with patch.object(fe, "_show_file_dialog", return_value=""):
fe.choose_btn.changed.emit("value")
mock.assert_not_called()
assert fe.value == Path("hi")
def test_file_edit_values():
cwd = Path(".").absolute()
fe = widgets.FileEdit(mode=types.FileDialogMode.EXISTING_FILE)
assert isinstance(fe.value, Path)
fe.value = Path("hi")
assert fe.value == cwd / "hi"
fe = widgets.FileEdit(mode=types.FileDialogMode.EXISTING_FILE, nullable=True)
assert fe.value is None
fe.value = Path("hi")
assert fe.value == cwd / "hi"
fe.value = None
assert fe.value is None
fe = widgets.FileEdit(mode=types.FileDialogMode.EXISTING_FILES)
assert fe.value == ()
fe.value = Path("hi")
assert fe.value == (cwd / "hi",)
fe.value = (Path("hi"), Path("world"))
assert fe.value == (cwd / "hi", cwd / "world")
fe.value = ()
assert fe.value == ()
def test_null_events():
"""Test that nullable widgets emit events when their null value is set"""
wdg = widgets.ComboBox(choices=["a", "b"], nullable=True)
mock = MagicMock()
wdg.changed.connect(mock)
wdg.value = "b"
mock.assert_called_once()
mock.reset_mock()
wdg.value = None
mock.assert_called_once()
mock.reset_mock()
wdg._nullable = False
wdg.value = "a"
mock.assert_called_once()
mock.reset_mock()
mock.assert_not_called()
wdg.value = None
mock.assert_not_called()
@pytest.mark.parametrize("WdgClass", [widgets.FloatSlider, widgets.FloatSpinBox])
@pytest.mark.parametrize("value", [1, 1e6, 1e12, 1e16, 1e22])
def test_extreme_floats(WdgClass, value):
wdg = WdgClass(value=value, max=value * 10)
assert round(wdg.value / value, 4) == 1
assert round(wdg.max / value, 4) == 10
mock = MagicMock()
wdg.changed.connect(mock)
wdg.value = value * 2
mock.assert_called_once()
assert round(mock.call_args[0][0] / value, 4) == 2
_value = 1 / value
wdg2 = WdgClass(value=_value, step=_value / 10, max=_value * 100)
assert round(wdg2.value / _value, 4) == 1.0
wdg.close()
wdg2.close()
@pytest.mark.parametrize("Cls", [widgets.ComboBox, widgets.RadioButtons])
def test_categorical_widgets(Cls):
wdg = Cls(
value=1,
choices=[("first option", 1), ("second option", 2), ("third option", 3)],
)
mock = MagicMock()
wdg.changed.connect(mock)
assert isinstance(wdg, widgets.bases.CategoricalWidget)
assert wdg.value == 1
assert wdg.current_choice == "first option"
mock.assert_not_called()
wdg.value = 2
mock.assert_called_once_with(2)
assert wdg.value == 2
assert wdg.current_choice == "second option"
assert wdg.choices == (1, 2, 3)
wdg.del_choice("third option")
assert wdg.choices == (1, 2)
@pytest.mark.parametrize(
"Cls,value", [(widgets.ComboBox, "c3"), (widgets.Select, ["c2", "c3"])]
)
def test_reset_choices_emits_once(Cls, value):
data = ["c1", "c2", "c3"]
wdg = Cls(
value=value,
choices=lambda w: data,
)
mock = MagicMock()
wdg.changed.connect(mock)
mock.assert_not_called()
data = ["d2", "d4"]
wdg.reset_choices()
mock.assert_called_once()
data = ["d2", "d4", "d5"]
wdg.reset_choices()
mock.assert_called_once()
@pytest.mark.parametrize(
"Cls,value1,value2",
[(widgets.ComboBox, "c4", "c1"), (widgets.Select, ["c3", "c4"], ["c1", "c2"])],
)
def test_set_value_emits_once(Cls, value1, value2):
wdg = Cls(
value=value1,
choices=["c1", "c2", "c3", "c4"],
)
mock = MagicMock()
wdg.changed.connect(mock)
mock.assert_not_called()
wdg.value = value2
mock.assert_called_once()
wdg.value = value2
mock.assert_called_once()
@pytest.mark.parametrize(
"Cls,value", [(widgets.ComboBox, "c3"), (widgets.Select, ["c3", "c4"])]
)
def test_set_choices_emits_once(Cls, value):
wdg = Cls(
value=value,
choices=["c1", "c2", "c3", "c4"],
)
mock = MagicMock()
wdg.changed.connect(mock)
mock.assert_not_called()
wdg.choices = ["d2", "d4", "d5"]
mock.assert_called_once()
class MyEnum(Enum):
A = "a"
B = "b"
@pytest.mark.parametrize("Cls", [widgets.ComboBox, widgets.RadioButtons])
def test_categorical_widgets_with_enums(Cls):
wdg = Cls(value=MyEnum.A, choices=MyEnum)
mock = MagicMock()
wdg.changed.connect(mock)
assert isinstance(wdg, widgets.bases.CategoricalWidget)
assert wdg.value == MyEnum.A
assert wdg.current_choice == "A"
mock.assert_not_called()
wdg.value = MyEnum.B
mock.assert_called_once_with(MyEnum.B)
assert wdg.value == MyEnum.B
assert wdg.current_choice == "B"
assert wdg.choices == tuple(MyEnum.__members__.values())
wdg.close()
@pytest.mark.parametrize("Cls", [widgets.ComboBox, widgets.RadioButtons])
def test_categorical_change_choices(Cls):
"""Make sure we can change choices to more or fewer options."""
a = tuple(range(10))
wdg = Cls(choices=a)
assert wdg.choices == a
b = tuple(range(5))
wdg.choices = b
assert wdg.choices == b
c = tuple(range(15))
wdg.choices = c
assert wdg.choices == c
@pytest.mark.parametrize("Cls", [widgets.ComboBox, widgets.RadioButtons])
def test_categorical_change_choices_callable(Cls):
first_choices = ("a", "b")
def get_choices(wdg):
return ("c", "d")
wdg = Cls(choices=first_choices)
assert wdg.choices == first_choices
assert wdg._default_choices == first_choices
wdg.choices = get_choices
assert wdg.choices == ("c", "d")
assert wdg._default_choices == get_choices
@pytest.mark.skipif(use_app().backend_name != "qt", reason="only on qt")
def test_radiobutton_reset_choices():
"""Test that reset_choices doesn't change the number of buttons."""
from qtpy.QtWidgets import QRadioButton
wdg = widgets.RadioButtons(choices=["a", "b", "c"])
assert len(wdg.native.findChildren(QRadioButton)) == 3
wdg.reset_choices()
assert len(wdg.native.findChildren(QRadioButton)) == 3
def test_tracking():
slider = widgets.Slider(tracking=False)
assert slider.tracking is False
slider.tracking = True
assert slider.tracking
def test_select_set_value():
sel = widgets.Select(value=[1, 3, 4], choices=list(range(10)))
assert sel.value == [1, 3, 4]
sel.value = [1, 4, 8]
assert sel.value == [1, 4, 8]
def test_slider_readeout():
"""Test that the slider readout spinbox visibility works."""
# FIXME: ugly direct backend access.
sld = widgets.Slider()
sld.show()
backend_slider = sld.native.children()[1]
assert "SpinBox" in type(backend_slider).__name__
assert backend_slider.isVisible()
sld = widgets.Slider(readout=False)
sld.show()
backend_slider = sld.native.children()[1]
assert not backend_slider.isVisible()
sld = widgets.Slider(readout=True)
sld.show()
assert sld.native.children()[1].isVisible()
sld.readout = False
assert not sld.native.children()[1].isVisible()
def test_slice_edit_events():
"""Test that changed events of spin boxes inside a slice edit are
observable from its parent."""
start, stop, step = 0, 10, 1
sl = widgets.SliceEdit(start, stop, step)
container = widgets.Container(widgets=[sl])
mock = MagicMock()
container.changed.connect(mock)
sl.start.changed.emit(sl.value)
mock.assert_called()
assert sl.value == slice(start, stop, step)
def test_pushbutton_click_signal():
btn = widgets.PushButton(text="click me")
mock = MagicMock()
mock2 = MagicMock()
btn.changed.connect(mock)
btn.clicked.connect(mock2)
btn.native.click()
mock.assert_called_once()
mock2.assert_called_once()
def test_pushbutton_icon(backend: str):
use_app(backend)
btn = widgets.PushButton(icon="mdi:folder")
btn.set_icon("play", "red")
btn.set_icon(None)
if backend == "qt":
with pytest.warns(UserWarning, match="Could not set iconify icon"):
btn.set_icon("bad:key")
def test_list_edit():
"""Test ListEdit."""
mock = MagicMock()
list_edit = widgets.ListEdit(value=[1, 2, 3])
list_edit.changed.connect(mock)
assert list_edit.value == [1, 2, 3]
assert list_edit.data == [1, 2, 3]
assert mock.call_count == 0
list_edit.btn_plus.changed()
assert list_edit.value == [1, 2, 3, 3]
assert list_edit.data == [1, 2, 3, 3]
assert mock.call_count == 1
mock.assert_called_with([1, 2, 3, 3])
list_edit[1].btn_minus.changed()
assert list_edit.value == [1, 3, 3]
assert list_edit.data == [1, 3, 3]
assert mock.call_count == 2
mock.assert_called_with([1, 3, 3])
list_edit.data[0] = 0
assert list_edit.value == [0, 3, 3]
assert list_edit.data == [0, 3, 3]
assert mock.call_count == 3
mock.assert_called_with([0, 3, 3])
list_edit[0].value = 10
assert list_edit.value == [10, 3, 3]
assert list_edit.data == [10, 3, 3]
assert mock.call_count == 4
mock.assert_called_with([10, 3, 3])
list_edit.data[:2] = [6, 5] # type: ignore
assert list_edit.value == [6, 5, 3]
assert list_edit.data == [6, 5, 3]
assert mock.call_count == 5
mock.assert_called_with([6, 5, 3])
del list_edit.data[0]
assert list_edit.value == [5, 3]
assert list_edit.data == [5, 3]
assert mock.call_count == 6
mock.assert_called_with([5, 3])
list_edit.value = [2, 1]
assert list_edit.value == [2, 1]
assert list_edit.data == [2, 1]
# NOTE: changed.blocked() does not restore
assert mock.call_count == 7
mock.assert_called_with([2, 1])
def test_list_edit_only_values():
@magicgui
def f1(x=[2, 4, 6]): # noqa: B006
pass
assert type(f1.x) is widgets.ListEdit
assert f1.x._args_type is int
assert f1.x.value == [2, 4, 6]
def test_list_edit_annotations():
@magicgui
def f2(x: List[int]):
pass
assert type(f2.x) is widgets.ListEdit
assert f2.x.annotation == List[int]
assert f2.x._args_type is int
assert f2.x.value == []
f2.x.btn_plus.changed()
assert f2.x.value == [0]
@magicgui(
x={"options": {"widget_type": "Slider", "min": -10, "max": 10, "step": 5}}
)
def f3(x: List[int] = [0]): # noqa: B006
pass
assert type(f3.x) is widgets.ListEdit
assert type(f3.x[0].value_widget) is widgets.Slider
assert f3.x[0].value_widget.min == -10
assert f3.x[0].value_widget.max == 10
assert f3.x[0].value_widget.step == 5
@magicgui
def f4(x: List[int] = ()): # type: ignore
pass
assert type(f4.x) is widgets.ListEdit
assert f4.x.annotation == List[int]
assert f4.x._args_type is int
assert f4.x.value == []
f4.x.btn_plus.changed()
assert type(f4.x[0].value_widget) is widgets.SpinBox
assert f4.x.value == [0]
@magicgui
def f5(x: List[Annotated[int, {"max": 3}]]):
pass
assert type(f5.x) is widgets.ListEdit
assert f5.x.annotation == List[int]
f5.x.btn_plus.changed()
assert f5.x[0].value_widget.max == 3
def test_tuple_edit():
"""Test TupleEdit."""
from typing import Tuple
mock = MagicMock()
tuple_edit = widgets.TupleEdit(value=(1, "a", 2.5))
tuple_edit.changed.connect(mock)
assert tuple_edit.value == (1, "a", 2.5)
assert mock.call_count == 0
tuple_edit[0].value = 2
assert tuple_edit.value == (2, "a", 2.5)
assert mock.call_count == 1
mock.assert_called_with((2, "a", 2.5))
tuple_edit.value = (2, "xyz", 1.0)
assert tuple_edit.value == (2, "xyz", 1.0)
assert mock.call_count == 2
mock.assert_called_with((2, "xyz", 1.0))
with pytest.raises(ValueError):
tuple_edit.value = (2, "x")
@magicgui
def f1(x=(2, 4, 6)):
pass
assert type(f1.x) is widgets.TupleEdit
assert f1.x.value == (2, 4, 6)
@magicgui
def f2(x: Tuple[int, str]):
pass
assert type(f2.x) is widgets.TupleEdit
assert f2.x.annotation == Tuple[int, str]
assert f2.x.value == (0, "")
@magicgui
def f3(x: Tuple[Annotated[int, {"max": 3}], str]):
pass
assert type(f3.x) is widgets.TupleEdit
assert f2.x.annotation == Tuple[int, str]
assert f3.x[0].max == 3
def test_request_values(monkeypatch):
from unittest.mock import Mock
container = Container()
mock = Mock()
def _exec(self, **k):
mock()
assert self.native.parent() is container.native
return True
monkeypatch.setattr(DialogWidget, "exec", _exec)
vals = request_values(
age={"value": 40},
name={"annotation": str, "label": "Enter your name:"},
title="Hi! Who are you?",
parent=container,
)
assert vals == {"age": 40, "name": ""}
mock.assert_called_once()
mock.reset_mock()
vals = request_values(
values={"age": int, "name": str}, title="Hi! Who are you?", parent=container
)
assert vals == {"age": 0, "name": ""}
mock.assert_called_once()
def test_range_slider():
@magicgui(auto_call=True, range_value={"widget_type": "RangeSlider", "max": 500})
def func(range_value: Tuple[int, int] = (20, 380)):
print(range_value)
assert isinstance(func.range_value, widgets.RangeSlider)
assert func.range_value.max == 500
assert func.range_value.value == (20, 380)
def test_float_range_slider():
@magicgui(auto_call=True, range_value={"widget_type": "FloatRangeSlider", "max": 1})
def func(range_value: Tuple[float, float] = (0.2, 0.8)):
print(range_value)
assert isinstance(func.range_value, widgets.FloatRangeSlider)
assert func.range_value.max == 1
assert func.range_value.value == (0.2, 0.8)
def test_literal():
from typing import Literal, Set
from typing_extensions import get_args
Lit = Literal[None, "a", 1, True, b"bytes"]
@magicgui
def f(x: Lit): ...
cbox = f.x
assert type(cbox) is widgets.ComboBox
assert cbox.choices == get_args(Lit)
@magicgui
def f(x: Set[Lit]): ...
sel = f.x
assert type(sel) is widgets.Select
assert sel.choices == get_args(Lit)
def test_separator_singleton():
from magicgui.types import Separator, _Separator
sep1 = Separator
sep2 = _Separator()
assert sep1 is sep2
def test_separator(backend: str):
from magicgui.types import Separator
use_app(backend)
sep = [[Separator] * (i + 1) for i in range(4)]
a = [1, 2, *sep[0], 4, *sep[1], 6, 7, *sep[2], 9, *sep[3], 11, 12, *sep[0], 14, *sep[0]]
b = [1, 2, *sep[0], 4, *sep[1], 6, 7, *sep[2], 9, *sep[3], 6, 7, *sep[0], 9, *sep[0]]
b2 = [1, 2, *sep[0], 4, *sep[1], 6, 7, *sep[2], 9, *sep[3], *sep[0], *sep[0]]
combo_a = widgets.ComboBox(choices=a, value=a[0])
combo_b = widgets.ComboBox(choices=b, value=b[0])
assert len(combo_a) == len(combo_a.choices)
assert len(combo_b) == len(combo_b.choices)
if backend == "qt":
def get_all_itemdata(combo_box):
return [combo_box.itemData(index) for index in range(combo_box.count())]
# Count returns the number of all items including separator items
assert combo_a.native.count() == 21
assert combo_b.native.count() == 18
# Separator singletons themselves are used as separator item data
assert get_all_itemdata(combo_a.native) == a
assert get_all_itemdata(combo_b.native) == b2
# Choices only returns unique, non-separator items
assert combo_a.choices == (1, 2, 4, 6, 7, 9, 11, 12, 14)
assert combo_b.choices == (1, 2, 4, 6, 7, 9)
if backend == "ipynb":
# Separator singletons themselves are used as separator item data
assert combo_a.options['choices'] == a
assert combo_b.options['choices'] == b # items are not unique
# Choices only returns duplicated, non-separator items
assert combo_a.choices == (1, 2, 4, 6, 7, 9, 11, 12, 14)
assert combo_b.choices == (1, 2, 4, 6, 7, 9, 6, 7, 9)
def test_float_slider_readout():
sld = widgets.FloatSlider(value=4, min=0.5, max=10.5)
assert sld.value == 4
assert sld._widget._readout_widget.value() == 4
assert sld._widget._readout_widget.minimum() == 0.5
assert sld._widget._readout_widget.maximum() == 10.5
def test_toolbar():
tb = widgets.ToolBar()
tb.add_button("test", callback=lambda: None)
tb.add_separator()
tb.add_spacer()
tb.add_button("test2", callback=lambda: None)
tb.icon_size = 26
assert tb.icon_size == (26, 26)
tb.clear()
|