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.

101 lines
4.9 KiB

3 years ago
  1. import sys
  2. import unittest
  3. from copy import deepcopy
  4. from gooey import Events
  5. from gooey.tests.harness import instrumentGooey
  6. from gooey.tests import *
  7. class TestLiveDynamicUpdates(unittest.TestCase):
  8. def test_validate_form(self):
  9. """
  10. Integration testing the Dynamic Validation features.
  11. """
  12. # Because it's a live test, nothing is mocked. This basic.py file
  13. # will be called via subprocess as part of the test. As such, we
  14. # grab both its path on disk (for use as a target for Gooey) as
  15. # well as its parser instance (so that we can bootstrap)
  16. from gooey.tests.dynamics.files import basic
  17. params = {
  18. 'target': '{} -u {}'.format(sys.executable, basic.__file__),
  19. 'use_events': [Events.VALIDATE_FORM],
  20. }
  21. with instrumentGooey(basic.make_parser(), **params) as (app, frame, gapp):
  22. # the parser has a single arg of type int.
  23. # We purposefully give it invalid input for the sake of the test.
  24. gapp.getActiveConfig().widgetsMap['foo'].setValue('not a number')
  25. # and make sure we're not somehow starting with an error
  26. self.assertEqual(gapp.getActiveFormState()[0]['error'], '')
  27. gapp.onStart()
  28. # All subprocess calls ultimately pump though wx's event queue
  29. # so we have to kick off the mainloop and let it run long enough
  30. # to let the subprocess complete and the event queue flush
  31. wx.CallLater(2500, app.ExitMainLoop)
  32. app.MainLoop()
  33. # after the subprocess call is complete, our UI should have
  34. # been updated with the data dynamically returned from the
  35. # basic.py invocation.
  36. self.assertIn('invalid literal', gapp.getActiveFormState()[0]['error'])
  37. def test_validate_form_without_errors(self):
  38. from gooey.tests.dynamics.files import basic
  39. params = {
  40. 'target': '{} -u {}'.format(sys.executable, basic.__file__),
  41. 'use_events': [Events.VALIDATE_FORM],
  42. # setting to false because it interferes with the test
  43. 'show_success_modal': False
  44. }
  45. with instrumentGooey(basic.make_parser(), **params) as (app, frame, gapp):
  46. gapp.getActiveConfig().widgetsMap['foo'].setValue('10') # valid int
  47. self.assertEqual(gapp.getActiveFormState()[0]['error'], '')
  48. gapp.onStart()
  49. wx.CallLater(2500, app.ExitMainLoop)
  50. app.MainLoop()
  51. # no errors blocked the run, so we should have executed and finished.
  52. # we're now on the success screen.
  53. self.assertEqual(gapp.state['image'], gapp.state['images']['successIcon'])
  54. # and indeed no errors were written to the UI
  55. self.assertEqual(gapp.getActiveFormState()[0]['error'], '')
  56. # and we find the expected output written to the console
  57. # rather than some unexpected error
  58. self.assertIn('DONE', frame.FindWindowByName("console").getText())
  59. def test_lifecycle_handlers(self):
  60. cases = [
  61. {'input': 'happy path', 'expected_stdout': 'DONE', 'expected_update': 'success'},
  62. {'input': 'fail', 'expected_stdout': 'EXCEPTION', 'expected_update': 'error'}
  63. ]
  64. from gooey.tests.dynamics.files import lifecycles
  65. params = {
  66. 'target': '{} -u {}'.format(sys.executable, lifecycles.__file__),
  67. 'use_events': [Events.ON_SUCCESS, Events.ON_ERROR],
  68. 'show_success_modal': False,
  69. 'show_failure_modal': False
  70. }
  71. for case in cases:
  72. with self.subTest(case):
  73. with instrumentGooey(lifecycles.make_parser(), **params) as (app, frame, gapp):
  74. gapp.getActiveConfig().widgetsMap['foo'].setValue(case['input'])
  75. gapp.onStart()
  76. # give everything a chance to run
  77. wx.CallLater(2000, app.ExitMainLoop)
  78. app.MainLoop()
  79. # `lifecycle.py` is set up to raise an exception for certain inputs
  80. # so we check that we find our expected stdout here
  81. console = frame.FindWindowByName("console")
  82. self.assertIn(case['expected_stdout'], console.getText())
  83. # Now, based on what happened during the run (success/exception) our
  84. # respective lifecycle handler should have been called. These are
  85. # configured to update the form field in the UI with a relevant value.
  86. # Thus we we're checking here to see that out input has changed, and now
  87. # matches the value we expect from the handler
  88. textfield = gapp.getActiveFormState()[0]
  89. print(case['expected_update'], textfield['value'])
  90. self.assertEqual(case['expected_update'], textfield['value'])