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

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 7fb471d was 7fb471d, checked in by Piotr Rozyczko <rozyczko@…>, 6 years ago

Update for unit tests and minor functionality quirks

  • Property mode set to 100644
File size: 6.9 KB
Line 
1import sys
2import unittest
3from bumps import options
4
5from PyQt4 import QtGui
6from PyQt4 import QtWebKit
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 QtGui.QApplication.instance():
19    app = QtGui.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, QtGui.QDialog)
35        # Default title
36        self.assertEqual(self.widget.windowTitle(), "Fit Algorithms")
37
38        # The combo box
39        self.assertIsInstance(self.widget.cbAlgorithm, QtGui.QComboBox)
40        self.assertEqual(self.widget.cbAlgorithm.count(), 5)
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(QtGui.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    def testOnHelp(self):
116        ''' Test help display'''
117        #Mock the QWebView method
118        QtWebKit.QWebView.show = MagicMock()
119        QtWebKit.QWebView.load = MagicMock()
120
121        # Invoke the action on default tab
122        self.widget.onHelp()
123        # Check if show() got called
124        self.assertTrue(QtWebKit.QWebView.show.called)
125        # Assure the filename is correct
126        self.assertIn("optimizer.html", QtWebKit.QWebView.load.call_args[0][0].toString())
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(QtWebKit.QWebView.show.call_count, 2)
133        # Assure the filename is correct
134        self.assertIn("fit-dream", QtWebKit.QWebView.load.call_args[0][0].toString())
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(QtWebKit.QWebView.show.call_count, 3)
141        # Assure the filename is correct
142        self.assertIn("fit-lm", QtWebKit.QWebView.load.call_args[0][0].toString())
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(QtGui.QMainWindow()))
151
152        # Switch to DREAM
153        self.widget.cbAlgorithm.setCurrentIndex(2)
154        # test smart call
155        self.assertIsInstance(self.widget.widgetFromOption('samples'), QtGui.QLineEdit)
156        self.assertIsInstance(self.widget.widgetFromOption('init'), QtGui.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.