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.

59 lines
2.9 KiB

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