source: sasview/src/sas/qtgui/Perspectives/Fitting/UnitTesting/FittingOptionsTest.py @ f3e5956

ESS_GUIESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalc
Last change on this file since f3e5956 was bfb5d9e, checked in by Torin Cooper-Bennun <torin.cooper-bennun@…>, 6 years ago

fix testNew1DPlot, testNew2DPlot, testDefaults

  • Property mode set to 100644
File size: 6.9 KB
Line 
1import sys
2import unittest
3import webbrowser
4from bumps import options
5
6from PyQt5 import QtGui, QtWidgets
7
8from unittest.mock import MagicMock
9
10# set up import paths
11import path_prepare
12
13from UnitTesting.TestUtils import QtSignalSpy
14
15# Local
16from sas.qtgui.Perspectives.Fitting.FittingOptions import FittingOptions
17
18if not QtWidgets.QApplication.instance():
19    app = QtWidgets.QApplication(sys.argv)
20
21class FittingOptionsTest(unittest.TestCase):
22    '''Test the FittingOptions dialog'''
23    def setUp(self):
24        '''Create FittingOptions dialog'''
25        self.widget = FittingOptions(None, config=options.FIT_CONFIG)
26
27    def tearDown(self):
28        '''Destroy the GUI'''
29        self.widget.close()
30        self.widget = None
31
32    def testDefaults(self):
33        '''Test the GUI in its default state'''
34        self.assertIsInstance(self.widget, QtWidgets.QDialog)
35        # Default title
36        self.assertEqual(self.widget.windowTitle(), "Fit Algorithms")
37
38        # The combo box
39        self.assertIsInstance(self.widget.cbAlgorithm, QtWidgets.QComboBox)
40        self.assertEqual(self.widget.cbAlgorithm.count(), 6)
41        self.assertEqual(self.widget.cbAlgorithm.itemText(0), 'Nelder-Mead Simplex')
42        self.assertEqual(self.widget.cbAlgorithm.itemText(4), 'Levenberg-Marquardt')
43        self.assertEqual(self.widget.cbAlgorithm.currentIndex(), 4)
44
45    def testAssignValidators(self):
46        """
47        Check that line edits got correct validators
48        """
49        # Can't reliably test the method in action, but can easily check the results
50       
51        # DREAM
52        self.assertIsInstance(self.widget.samples_dream.validator(), QtGui.QIntValidator)
53        self.assertIsInstance(self.widget.burn_dream.validator(), QtGui.QIntValidator)
54        self.assertIsInstance(self.widget.pop_dream.validator(), QtGui.QDoubleValidator)
55        self.assertIsInstance(self.widget.thin_dream.validator(), QtGui.QIntValidator)
56        self.assertIsInstance(self.widget.steps_dream.validator(), QtGui.QIntValidator)
57        # DE
58        self.assertIsInstance(self.widget.steps_de.validator(), QtGui.QIntValidator)
59        self.assertIsInstance(self.widget.CR_de.validator(), QtGui.QDoubleValidator)
60        self.assertIsInstance(self.widget.pop_de.validator(), QtGui.QDoubleValidator)
61        self.assertIsInstance(self.widget.F_de.validator(), QtGui.QDoubleValidator)
62        self.assertIsInstance(self.widget.ftol_de.validator(), QtGui.QDoubleValidator)
63        self.assertIsInstance(self.widget.xtol_de.validator(), QtGui.QDoubleValidator)
64
65        # bottom value for floats and ints
66        self.assertEqual(self.widget.steps_de.validator().bottom(), 0)
67        self.assertEqual(self.widget.CR_de.validator().bottom(), 0)
68
69        # Behaviour on empty cell
70        self.widget.onAlgorithmChange(3)
71        self.widget.steps_de.setText("")
72        # This should disable the OK button
73        ## self.assertFalse(self.widget.buttonBox.button(QtGui.QDialogButtonBox.Ok).isEnabled())
74        # Let's put some valid value in lineedit
75        self.widget.steps_de.setText("1")
76        # This should enable the OK button
77        self.assertTrue(self.widget.buttonBox.button(QtWidgets.QDialogButtonBox.Ok).isEnabled())
78
79    def testOnAlgorithmChange(self):
80        '''Test the combo box change callback'''
81        # Current ID
82        self.assertEqual(self.widget.current_fitter_id, 'lm')
83        # index = 0
84        self.widget.onAlgorithmChange(0)
85        # Check Nelder-Mead
86        self.assertEqual(self.widget.stackedWidget.currentIndex(), 1)
87        self.assertEqual(self.widget.current_fitter_id, 'lm')
88
89        # index = 4
90        self.widget.onAlgorithmChange(4)
91        # Check Levenberg-Marquad
92        self.assertEqual(self.widget.stackedWidget.currentIndex(), 1)
93        self.assertEqual(self.widget.current_fitter_id, 'lm')
94
95    def testOnApply(self):
96        '''Test bumps update'''
97        # Spy on the update signal
98        spy_apply = QtSignalSpy(self.widget, self.widget.fit_option_changed)
99
100        # Set the DREAM optimizer
101        self.widget.cbAlgorithm.setCurrentIndex(2)
102        # Change some values
103        self.widget.init_dream.setCurrentIndex(2)
104        self.widget.steps_dream.setText("50")
105        # Apply the new values
106        self.widget.onApply()
107
108        self.assertEqual(spy_apply.count(), 1)
109        self.assertIn('DREAM', spy_apply.called()[0]['args'][0])
110
111        # Check the parameters
112        self.assertEqual(options.FIT_CONFIG.values['dream']['steps'], 50.0)
113        self.assertEqual(options.FIT_CONFIG.values['dream']['init'], 'cov')
114
115    # test disabled until pyQt5 works well
116    def testOnHelp(self):
117        ''' Test help display'''
118        webbrowser.open = MagicMock()
119
120        # Invoke the action on default tab
121        self.widget.onHelp()
122        # Check if show() got called
123        self.assertTrue(webbrowser.open.called)
124        # Assure the filename is correct
125        self.assertIn("optimizer.html", webbrowser.open.call_args[0][0])
126
127        # Change the combo index
128        self.widget.cbAlgorithm.setCurrentIndex(2)
129        self.widget.onHelp()
130        # Check if show() got called
131        self.assertEqual(webbrowser.open.call_count, 2)
132        # Assure the filename is correct
133        self.assertIn("fit-dream", webbrowser.open.call_args[0][0])
134
135        # Change the index again
136        self.widget.cbAlgorithm.setCurrentIndex(4)
137        self.widget.onHelp()
138        # Check if show() got called
139        self.assertEqual(webbrowser.open.call_count, 3)
140        # Assure the filename is correct
141        self.assertIn("fit-lm", webbrowser.open.call_args[0][0])
142
143    def testWidgetFromOptions(self):
144        '''Test the helper function'''
145        # test empty call
146        self.assertIsNone(self.widget.widgetFromOption(None))
147        # test silly call
148        self.assertIsNone(self.widget.widgetFromOption('poop'))
149        self.assertIsNone(self.widget.widgetFromOption(QtWidgets.QMainWindow()))
150
151        # Switch to DREAM
152        self.widget.cbAlgorithm.setCurrentIndex(2)
153        # test smart call
154        self.assertIsInstance(self.widget.widgetFromOption('samples'), QtWidgets.QLineEdit)
155        self.assertIsInstance(self.widget.widgetFromOption('init'), QtWidgets.QComboBox)
156
157    def testUpdateWidgetFromBumps(self):
158        '''Test the widget update'''
159        # modify some value
160        options.FIT_CONFIG.values['newton']['steps'] = 1234
161        options.FIT_CONFIG.values['newton']['starts'] = 666
162        options.FIT_CONFIG.values['newton']['xtol'] = 0.01
163
164        # Invoke the method for the changed
165        self.widget.updateWidgetFromBumps('newton')
166
167        # See that the widget picked up the right values
168        self.assertEqual(self.widget.steps_newton.text(), '1234')
169        self.assertEqual(self.widget.starts_newton.text(), '666')
170        self.assertEqual(self.widget.ftol_newton.text(), '1e-06') # default
171        self.assertEqual(self.widget.xtol_newton.text(), '0.01')
172
173if __name__ == "__main__":
174    unittest.main()
Note: See TracBrowser for help on using the repository browser.