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

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

Fixing unit tests + removal of unnecessary files

  • 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 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(), "Fit Algorithms")
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        # bottom value for floats and ints
65        self.assertEqual(self.widget.steps_de.validator().bottom(), 0)
66        self.assertEqual(self.widget.CR_de.validator().bottom(), 0)
67
68        # Behaviour on empty cell
69        self.widget.onAlgorithmChange(3)
70        self.widget.steps_de.setText("")
71        # This should disable the OK button
72        ## self.assertFalse(self.widget.buttonBox.button(QtGui.QDialogButtonBox.Ok).isEnabled())
73        # Let's put some valid value in lineedit
74        self.widget.steps_de.setText("1")
75        # This should enable the OK button
76        self.assertTrue(self.widget.buttonBox.button(QtGui.QDialogButtonBox.Ok).isEnabled())
77
78    def testOnAlgorithmChange(self):
79        '''Test the combo box change callback'''
80        # Current ID
81        self.assertEqual(self.widget.current_fitter_id, 'lm')
82        # index = 0
83        self.widget.onAlgorithmChange(0)
84        # Check Nelder-Mead
85        self.assertEqual(self.widget.stackedWidget.currentIndex(), 1)
86        self.assertEqual(self.widget.current_fitter_id, 'lm')
87
88        # index = 4
89        self.widget.onAlgorithmChange(4)
90        # Check Levenberg-Marquad
91        self.assertEqual(self.widget.stackedWidget.currentIndex(), 1)
92        self.assertEqual(self.widget.current_fitter_id, 'lm')
93
94    def testOnApply(self):
95        '''Test bumps update'''
96        # Spy on the update signal
97        spy_apply = QtSignalSpy(self.widget, self.widget.fit_option_changed)
98
99        # Set the DREAM optimizer
100        self.widget.cbAlgorithm.setCurrentIndex(2)
101        # Change some values
102        self.widget.init_dream.setCurrentIndex(2)
103        self.widget.steps_dream.setText("50")
104        # Apply the new values
105        self.widget.onApply()
106
107        self.assertEqual(spy_apply.count(), 1)
108        self.assertIn('DREAM', spy_apply.called()[0]['args'][0])
109
110        # Check the parameters
111        self.assertEqual(options.FIT_CONFIG.values['dream']['steps'], 50.0)
112        self.assertEqual(options.FIT_CONFIG.values['dream']['init'], 'cov')
113
114    def testOnHelp(self):
115        ''' Test help display'''
116        #Mock the QWebView method
117        QtWebKit.QWebView.show = MagicMock()
118        QtWebKit.QWebView.load = MagicMock()
119
120        # Invoke the action on default tab
121        self.widget.onHelp()
122        # Check if show() got called
123        self.assertTrue(QtWebKit.QWebView.show.called)
124        # Assure the filename is correct
125        self.assertIn("optimizer.html", QtWebKit.QWebView.load.call_args[0][0].toString())
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(QtWebKit.QWebView.show.call_count, 2)
132        # Assure the filename is correct
133        self.assertIn("fit-dream", QtWebKit.QWebView.load.call_args[0][0].toString())
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(QtWebKit.QWebView.show.call_count, 3)
140        # Assure the filename is correct
141        self.assertIn("fit-lm", QtWebKit.QWebView.load.call_args[0][0].toString())
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(QtGui.QMainWindow()))
150
151        # Switch to DREAM
152        self.widget.cbAlgorithm.setCurrentIndex(2)
153        # test smart call
154        self.assertIsInstance(self.widget.widgetFromOption('samples'), QtGui.QLineEdit)
155        self.assertIsInstance(self.widget.widgetFromOption('init'), QtGui.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.