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.

88 lines
3.6 KiB

  1. import unittest
  2. from random import randint
  3. from unittest.mock import patch
  4. from tests.harness import instrumentGooey
  5. from gooey import GooeyParser
  6. from gooey.tests import *
  7. class TestNumbericInputs(unittest.TestCase):
  8. def makeParser(self, **kwargs):
  9. parser = GooeyParser(description='description')
  10. parser.add_argument('--input', **kwargs)
  11. return parser
  12. def testDefault(self):
  13. cases = [
  14. [{'widget': 'IntegerField'}, 0],
  15. [{'default': 0, 'widget': 'IntegerField'}, 0],
  16. [{'default': 10, 'widget': 'IntegerField'}, 10],
  17. [{'default': 76, 'widget': 'IntegerField'}, 76],
  18. # note that WX caps the value
  19. # unless explicitly widened via gooey_options
  20. [{'default': 81234, 'widget': 'IntegerField'}, 100],
  21. # here we set the max to something higher than
  22. # the default and all works as expected.
  23. # this is a TODO for validation
  24. [{'default': 81234, 'widget': 'IntegerField', 'gooey_options': {'max': 99999}}, 81234],
  25. [{'widget': 'DecimalField'}, 0],
  26. [{'default': 0, 'widget': 'DecimalField'}, 0],
  27. [{'default': 81234, 'widget': 'DecimalField'}, 100],
  28. [{'default': 81234, 'widget': 'DecimalField', 'gooey_options': {'max': 99999}}, 81234],
  29. ]
  30. for inputs, expected in cases:
  31. with self.subTest(inputs):
  32. parser = self.makeParser(**inputs)
  33. with instrumentGooey(parser) as (app, gooeyApp):
  34. input = gooeyApp.configs[0].reifiedWidgets[0]
  35. self.assertEqual(input.getValue()['rawValue'], expected)
  36. def testGooeyOptions(self):
  37. cases = [
  38. {'widget': 'DecimalField', 'gooey_options': {'min': -100, 'max': 1234, 'increment': 1.240}},
  39. {'widget': 'DecimalField', 'gooey_options': {'min': 1234, 'max': 3456, 'increment': 2.2}},
  40. {'widget': 'IntegerField', 'gooey_options': {'min': -100, 'max': 1234}},
  41. {'widget': 'IntegerField', 'gooey_options': {'min': 1234, 'max': 3456}}
  42. ];
  43. using = {
  44. 'min': lambda widget: widget.GetMin(),
  45. 'max': lambda widget: widget.GetMax(),
  46. 'increment': lambda widget: widget.GetIncrement(),
  47. }
  48. for case in cases:
  49. with self.subTest(case):
  50. parser = self.makeParser(**case)
  51. with instrumentGooey(parser) as (app, gooeyApp):
  52. wxWidget = gooeyApp.configs[0].reifiedWidgets[0].widget
  53. for option, value in case['gooey_options'].items():
  54. self.assertEqual(using[option](wxWidget), value)
  55. def testZerosAreReturned(self):
  56. """
  57. Originally the formatter was dropping '0' due to
  58. it being interpreted as falsey
  59. """
  60. parser = self.makeParser(widget='IntegerField')
  61. with instrumentGooey(parser) as (app, gooeyApp):
  62. field = gooeyApp.configs[0].reifiedWidgets[0]
  63. result = field.getValue()
  64. self.assertEqual(result['rawValue'], 0)
  65. self.assertIsNotNone(result['cmd'])
  66. def testNoLossOfPrecision(self):
  67. parser = self.makeParser(widget='DecimalField', default=12.23534, gooey_options={'precision': 20})
  68. with instrumentGooey(parser) as (app, gooeyApp):
  69. field = gooeyApp.configs[0].reifiedWidgets[0]
  70. result = field.getValue()
  71. self.assertEqual(result['rawValue'], 12.23534)
  72. self.assertIsNotNone(result['cmd'])
  73. if __name__ == '__main__':
  74. unittest.main()