test_param_widget.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import functools
  2. from pathlib import Path
  3. from PySide6.QtWidgets import QWidget
  4. from ..param_widgets import (
  5. BoolParamWidget,
  6. StrParamWidget,
  7. PosIntParamWidget,
  8. ChoiceParamWidget,
  9. PathParamWidget,
  10. load_parameter_widget,
  11. )
  12. from ..param_multival_widget import MultiValueInputWidget
  13. from ..utils import _NoValue
  14. def test_GooeyParamWidgetMixin():
  15. # can we set and get a supported value to/from any widget
  16. # through the GooeyParamWidgetMixin API
  17. for pw_factory, val, default in (
  18. (BoolParamWidget, False, True),
  19. (BoolParamWidget, False, None),
  20. (PosIntParamWidget, 4, 1),
  21. (functools.partial(PosIntParamWidget, True), 4, None),
  22. (StrParamWidget, 'dummy', 'mydefault'),
  23. (functools.partial(ChoiceParamWidget, ['a', 'b', 'c']), 'b', 'c'),
  24. (PathParamWidget, str(Path.cwd()), 'mypath'),
  25. (PathParamWidget, str(Path.cwd()), None),
  26. # cannot include MultiValueInputWidget, leads to Python segfault
  27. # on garbage collection?!
  28. (functools.partial(
  29. MultiValueInputWidget, PathParamWidget),
  30. [str(Path.cwd()), 'temp'],
  31. 'mypath'),
  32. (functools.partial(
  33. MultiValueInputWidget, PathParamWidget),
  34. [str(Path.cwd()), 'temp'],
  35. None),
  36. ):
  37. pname = 'peewee'
  38. # this is how all parameter widgets are instantiated
  39. parent = QWidget() # we need parent to stick around,
  40. # so nothing gets picked up by GC
  41. pw = load_parameter_widget(
  42. parent,
  43. pw_factory,
  44. name=pname,
  45. docs='EXPLAIN!',
  46. default=default,
  47. )
  48. # If nothing was set yet, we expect `_NoValue` as the "representation
  49. # of default" here:
  50. assert pw.get_gooey_param_spec() == {pname: _NoValue}, \
  51. f"Default value not retrieved from {pw_factory}"
  52. # If nothing other than the default was set yet,
  53. # we still expect `_NoValue` as the "representation of default" here:
  54. pw.init_gooey_from_params({pname: default})
  55. assert pw.get_gooey_param_spec() == {pname: _NoValue}, \
  56. f"Default value not retrieved from {pw_factory}"
  57. # with a different value set, we get the set value back,
  58. # not the default
  59. pw.init_gooey_from_params({pname: val})
  60. assert pw.get_gooey_param_spec() == {pname: val}