You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
4.1 KiB

  1. import unittest
  2. from argparse import ArgumentParser
  3. from unittest.mock import patch
  4. from gooey import GooeyParser
  5. from tests.harness import instrumentGooey
  6. from gooey.tests import *
  7. class TestGooeyDropdown(unittest.TestCase):
  8. def makeParser(self, **kwargs):
  9. parser = GooeyParser(description='description')
  10. parser.add_argument('--dropdown', **kwargs)
  11. return parser
  12. @patch("gui.containers.application.seeder.fetchDynamicProperties")
  13. def test_dropdown_behavior(self, mock):
  14. """
  15. Testing that:
  16. - default values are used as the initial selection (when present)
  17. - Initial selection defaults to placeholder when no defaults supplied
  18. - selection is preserved (when possible) across dynamic updates
  19. """
  20. testcases = [
  21. # tuples of [choices, default, initalSelection, dynamicUpdate, expectedFinalSelection]
  22. [['1', '2'], None, 'Select Option', ['1', '2','3'], 'Select Option'],
  23. [['1', '2'], '2', '2', ['1', '2','3'], '2'],
  24. [['1', '2'], '1', '1', ['1', '2','3'], '1'],
  25. # dynamic updates removed our selected value; defaults back to placeholder
  26. [['1', '2'], '2', '2', ['1', '3'], 'Select Option'],
  27. # TODO: this test case is currently passing wrong data for the dynamic
  28. # TODO: update due to a bug where Gooey doesn't apply the same ingestion
  29. # TODO: rules for data received dynamically as it does for parsers.
  30. # TODO: In short, Gooey should be able to handle a list of bools [True, False]
  31. # TODO: from dynamics just like it does in parser land. It doesn't currently
  32. # TODO: do this, so I'm manually casting it to strings for now.
  33. [[True, False], True, 'True', ['True', 'False'], 'True']
  34. ]
  35. for choices, default, initalSelection, dynamicUpdate, expectedFinalSelection in testcases:
  36. parser = self.makeParser(choices=choices, default=default)
  37. with instrumentGooey(parser) as (app, gooeyApp):
  38. dropdown = gooeyApp.configs[0].reifiedWidgets[0]
  39. # ensure that default values (when supplied) are selected in the UI
  40. self.assertEqual(dropdown.widget.GetValue(), initalSelection)
  41. # fire a dynamic update with the mock values
  42. mock.return_value = {'--dropdown': dynamicUpdate}
  43. gooeyApp.fetchExternalUpdates()
  44. # the values in the UI now reflect those returned from the update
  45. # note: we're appending the ['select option'] bit here as it gets automatically added
  46. # in the UI.
  47. expectedValues = ['Select Option'] + dynamicUpdate
  48. self.assertEqual(dropdown.widget.GetItems(), expectedValues)
  49. # and our selection is what we expect
  50. self.assertEqual(dropdown.widget.GetValue(), expectedFinalSelection)
  51. def testInitialValue(self):
  52. cases = [
  53. # `initial` should supersede `default`
  54. {'inputs': {'default': 'b',
  55. 'choices': ['a', 'b', 'c'],
  56. 'gooey_options': {'initial_value': 'a'}},
  57. 'expect': 'a'},
  58. {'inputs': {'choices': ['a', 'b', 'c'],
  59. 'gooey_options': {'initial_value': 'a'}},
  60. 'expect': 'a'},
  61. {'inputs': {'choices': ['a', 'b', 'c'],
  62. 'default': 'b',
  63. 'gooey_options': {}},
  64. 'expect': 'b'},
  65. {'inputs': {'choices': ['a', 'b', 'c'],
  66. 'default': 'b'},
  67. 'expect': 'b'},
  68. {'inputs': {'choices': ['a', 'b', 'c']},
  69. 'expect': None}
  70. ]
  71. for case in cases:
  72. with self.subTest(case):
  73. parser = self.makeParser(**case['inputs'])
  74. with instrumentGooey(parser) as (app, gooeyApp):
  75. widget = gooeyApp.configs[0].reifiedWidgets[0]
  76. self.assertEqual(widget.getValue()['rawValue'], case['expect'])
  77. if __name__ == '__main__':
  78. unittest.main()