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