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

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

Unit tests for fitting options dialog SASVIEW-514

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