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.2 KiB

2 years ago
2 years ago
  1. import unittest
  2. from argparse import ArgumentParser
  3. from unittest.mock import patch
  4. from gooey import GooeyParser
  5. from gooey.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. #
  36. # for choices, default, initalSelection, dynamicUpdate, expectedFinalSelection in testcases:
  37. # parser = self.makeParser(choices=choices, default=default)
  38. # with instrumentGooey(parser) as (app, frame):
  39. # dropdown = frame.configs[0].reifiedWidgets[0]
  40. # # ensure that default values (when supplied) are selected in the UI
  41. # self.assertEqual(dropdown.widget.GetValue(), initalSelection)
  42. # # fire a dynamic update with the mock values
  43. # mock.return_value = {'--dropdown': dynamicUpdate}
  44. # frame.fetchExternalUpdates()
  45. # # the values in the UI now reflect those returned from the update
  46. # # note: we're appending the ['select option'] bit here as it gets automatically added
  47. # # in the UI.
  48. # expectedValues = ['Select Option'] + dynamicUpdate
  49. # self.assertEqual(dropdown.widget.GetItems(), expectedValues)
  50. # # and our selection is what we expect
  51. # self.assertEqual(dropdown.widget.GetValue(), expectedFinalSelection)
  52. def testInitialValue(self):
  53. cases = [
  54. # `initial` should supersede `default`
  55. {'inputs': {'default': 'b',
  56. 'choices': ['a', 'b', 'c'],
  57. 'gooey_options': {'initial_value': 'a'}},
  58. 'expect': 'a'},
  59. {'inputs': {'choices': ['a', 'b', 'c'],
  60. 'gooey_options': {'initial_value': 'a'}},
  61. 'expect': 'a'},
  62. {'inputs': {'choices': ['a', 'b', 'c'],
  63. 'default': 'b',
  64. 'gooey_options': {}},
  65. 'expect': 'b'},
  66. {'inputs': {'choices': ['a', 'b', 'c'],
  67. 'default': 'b'},
  68. 'expect': 'b'},
  69. {'inputs': {'choices': ['a', 'b', 'c']},
  70. 'expect': None}
  71. ]
  72. for case in cases:
  73. with self.subTest(case):
  74. parser = self.makeParser(**case['inputs'])
  75. with instrumentGooey(parser) as (app, frame, gapp):
  76. widget = gapp.getActiveConfig().reifiedWidgets[0]
  77. self.assertEqual(widget.getValue()['rawValue'], case['expect'])
  78. if __name__ == '__main__':
  79. unittest.main()