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.

110 lines
5.0 KiB

2 years ago
2 years ago
2 years ago
2 years ago
  1. import unittest
  2. from random import randint
  3. from unittest.mock import patch
  4. from gooey.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. # Initial Value cases
  26. [{'widget': 'IntegerField', 'gooey_options': {'initial_value': 0}}, 0],
  27. [{'widget': 'IntegerField', 'gooey_options': {'initial_value': 10}}, 10],
  28. [{'widget': 'IntegerField', 'gooey_options': {'initial_value': 76}}, 76],
  29. # note that WX caps the value
  30. # unless explicitly widened via gooey_options
  31. [{'widget': 'IntegerField', 'gooey_options': {'initial_value': 81234}}, 100],
  32. # here we set the max to something higher than
  33. # the default and all works as expected.
  34. # this is a TODO for validation
  35. [{'widget': 'IntegerField', 'gooey_options': {'initial_value': 81234, 'max': 99999}}, 81234],
  36. [{'widget': 'DecimalField'}, 0],
  37. [{'default': 0, 'widget': 'DecimalField'}, 0],
  38. [{'default': 81234, 'widget': 'DecimalField'}, 100],
  39. [{'default': 81234, 'widget': 'DecimalField', 'gooey_options': {'max': 99999}}, 81234],
  40. # Initial Value cases
  41. [{'widget': 'DecimalField', 'gooey_options': {'initial_value': 0}}, 0],
  42. [{'widget': 'DecimalField', 'gooey_options': {'initial_value': 10}}, 10],
  43. [{'widget': 'DecimalField', 'gooey_options': {'initial_value': 76}}, 76],
  44. # note that WX caps the value
  45. # unless explicitly widened via gooey_options
  46. [{'widget': 'DecimalField', 'gooey_options': {'initial_value': 81234}}, 100],
  47. # here we set the max to something higher than
  48. # the default and all works as expected.
  49. # this is a TODO for validation
  50. [{'widget': 'DecimalField', 'gooey_options': {'initial_value': 81234, 'max': 99999}}, 81234],
  51. ]
  52. for inputs, expected in cases:
  53. with self.subTest(inputs):
  54. parser = self.makeParser(**inputs)
  55. with instrumentGooey(parser) as (app, frame, gapp):
  56. input = gapp.getActiveConfig().reifiedWidgets[0]
  57. self.assertEqual(input.getValue()['rawValue'], expected)
  58. def testGooeyOptions(self):
  59. cases = [
  60. {'widget': 'DecimalField', 'gooey_options': {'min': -100, 'max': 1234, 'increment': 1.240}},
  61. {'widget': 'DecimalField', 'gooey_options': {'min': 1234, 'max': 3456, 'increment': 2.2}},
  62. {'widget': 'IntegerField', 'gooey_options': {'min': -100, 'max': 1234}},
  63. {'widget': 'IntegerField', 'gooey_options': {'min': 1234, 'max': 3456}}
  64. ];
  65. using = {
  66. 'min': lambda widget: widget.GetMin(),
  67. 'max': lambda widget: widget.GetMax(),
  68. 'increment': lambda widget: widget.GetIncrement(),
  69. }
  70. for case in cases:
  71. with self.subTest(case):
  72. parser = self.makeParser(**case)
  73. with instrumentGooey(parser) as (app, frame, gapp):
  74. wxWidget = gapp.getActiveConfig().reifiedWidgets[0].widget
  75. for option, value in case['gooey_options'].items():
  76. self.assertEqual(using[option](wxWidget), value)
  77. def testZerosAreReturned(self):
  78. """
  79. Originally the formatter was dropping '0' due to
  80. it being interpreted as falsey
  81. """
  82. parser = self.makeParser(widget='IntegerField')
  83. with instrumentGooey(parser) as (app, frame, gapp):
  84. field = gapp.getActiveConfig().reifiedWidgets[0]
  85. result = field.getValue()
  86. self.assertEqual(result['rawValue'], 0)
  87. self.assertIsNotNone(result['cmd'])
  88. def testNoLossOfPrecision(self):
  89. parser = self.makeParser(widget='DecimalField', default=12.23534, gooey_options={'precision': 20})
  90. with instrumentGooey(parser) as (app, frame, gapp):
  91. field = gapp.getActiveConfig().reifiedWidgets[0]
  92. result = field.getValue()
  93. self.assertEqual(result['rawValue'], 12.23534)
  94. self.assertIsNotNone(result['cmd'])
  95. if __name__ == '__main__':
  96. unittest.main()