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

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalc
Last change on this file since a90c9c5 was 14ec91c5, checked in by Piotr Rozyczko <rozyczko@…>, 6 years ago

More code review related fixes

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