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.

51 lines
2.3 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. ]
  26. for choices, default, initalSelection, dynamicUpdate, expectedFinalSelection in testcases:
  27. parser = self.make_parser(choices=choices, default=default)
  28. with instrumentGooey(parser) as (app, gooeyApp):
  29. dropdown = gooeyApp.configs[0].reifiedWidgets[0]
  30. # ensure that default values (when supplied) are selected in the UI
  31. self.assertEqual(dropdown.widget.GetValue(), initalSelection)
  32. # fire a dynamic update with the mock values
  33. mock.return_value = {'--dropdown': dynamicUpdate}
  34. gooeyApp.fetchExternalUpdates()
  35. # the values in the UI now reflect those returned from the update
  36. # note: we're appending the ['select option'] bit here as it gets automatically added
  37. # in the UI.
  38. expectedValues = ['Select Option'] + dynamicUpdate
  39. self.assertEqual(dropdown.widget.GetItems(), expectedValues)
  40. # and our selection is what we expect
  41. self.assertEqual(dropdown.widget.GetValue(), expectedFinalSelection)
  42. if __name__ == '__main__':
  43. unittest.main()