[b0c5e8c] | 1 | import sys |
---|
| 2 | import unittest |
---|
| 3 | from bumps import options |
---|
| 4 | |
---|
[53c771e] | 5 | from PyQt5 import QtGui, QtWidgets |
---|
| 6 | from PyQt5 import QtWebKit |
---|
[b0c5e8c] | 7 | |
---|
[7fb471d] | 8 | from unittest.mock import MagicMock |
---|
[b0c5e8c] | 9 | |
---|
| 10 | # set up import paths |
---|
| 11 | import path_prepare |
---|
| 12 | |
---|
| 13 | from UnitTesting.TestUtils import QtSignalSpy |
---|
| 14 | |
---|
| 15 | # Local |
---|
| 16 | from sas.qtgui.Perspectives.Fitting.FittingOptions import FittingOptions |
---|
| 17 | |
---|
[53c771e] | 18 | if not QtWidgets.QApplication.instance(): |
---|
| 19 | app = QtWidgets.QApplication(sys.argv) |
---|
[b0c5e8c] | 20 | |
---|
| 21 | class 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''' |
---|
[53c771e] | 34 | self.assertIsInstance(self.widget, QtWidgets.QDialog) |
---|
[b0c5e8c] | 35 | # Default title |
---|
[85487ebd] | 36 | self.assertEqual(self.widget.windowTitle(), "Fit Algorithms") |
---|
[b0c5e8c] | 37 | |
---|
| 38 | # The combo box |
---|
[53c771e] | 39 | self.assertIsInstance(self.widget.cbAlgorithm, QtWidgets.QComboBox) |
---|
[b0c5e8c] | 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 | |
---|
[72f4834] | 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 |
---|
[377ade1] | 73 | ## self.assertFalse(self.widget.buttonBox.button(QtGui.QDialogButtonBox.Ok).isEnabled()) |
---|
[72f4834] | 74 | # Let's put some valid value in lineedit |
---|
| 75 | self.widget.steps_de.setText("1") |
---|
| 76 | # This should enable the OK button |
---|
[53c771e] | 77 | self.assertTrue(self.widget.buttonBox.button(QtWidgets.QDialogButtonBox.Ok).isEnabled()) |
---|
[72f4834] | 78 | |
---|
[b0c5e8c] | 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 | |
---|
[53c771e] | 115 | # test disabled until pyQt5 works well |
---|
| 116 | def notestOnHelp(self): |
---|
[b0c5e8c] | 117 | ''' Test help display''' |
---|
| 118 | #Mock the QWebView method |
---|
| 119 | QtWebKit.QWebView.show = MagicMock() |
---|
| 120 | QtWebKit.QWebView.load = MagicMock() |
---|
| 121 | |
---|
| 122 | # Invoke the action on default tab |
---|
| 123 | self.widget.onHelp() |
---|
| 124 | # Check if show() got called |
---|
| 125 | self.assertTrue(QtWebKit.QWebView.show.called) |
---|
| 126 | # Assure the filename is correct |
---|
| 127 | self.assertIn("optimizer.html", QtWebKit.QWebView.load.call_args[0][0].toString()) |
---|
| 128 | |
---|
| 129 | # Change the combo index |
---|
| 130 | self.widget.cbAlgorithm.setCurrentIndex(2) |
---|
| 131 | self.widget.onHelp() |
---|
| 132 | # Check if show() got called |
---|
| 133 | self.assertEqual(QtWebKit.QWebView.show.call_count, 2) |
---|
| 134 | # Assure the filename is correct |
---|
| 135 | self.assertIn("fit-dream", QtWebKit.QWebView.load.call_args[0][0].toString()) |
---|
| 136 | |
---|
| 137 | # Change the index again |
---|
| 138 | self.widget.cbAlgorithm.setCurrentIndex(4) |
---|
| 139 | self.widget.onHelp() |
---|
| 140 | # Check if show() got called |
---|
| 141 | self.assertEqual(QtWebKit.QWebView.show.call_count, 3) |
---|
| 142 | # Assure the filename is correct |
---|
| 143 | self.assertIn("fit-lm", QtWebKit.QWebView.load.call_args[0][0].toString()) |
---|
| 144 | |
---|
| 145 | def testWidgetFromOptions(self): |
---|
| 146 | '''Test the helper function''' |
---|
| 147 | # test empty call |
---|
| 148 | self.assertIsNone(self.widget.widgetFromOption(None)) |
---|
| 149 | # test silly call |
---|
| 150 | self.assertIsNone(self.widget.widgetFromOption('poop')) |
---|
[53c771e] | 151 | self.assertIsNone(self.widget.widgetFromOption(QtWidgets.QMainWindow())) |
---|
[b0c5e8c] | 152 | |
---|
| 153 | # Switch to DREAM |
---|
| 154 | self.widget.cbAlgorithm.setCurrentIndex(2) |
---|
| 155 | # test smart call |
---|
[53c771e] | 156 | self.assertIsInstance(self.widget.widgetFromOption('samples'), QtWidgets.QLineEdit) |
---|
| 157 | self.assertIsInstance(self.widget.widgetFromOption('init'), QtWidgets.QComboBox) |
---|
[b0c5e8c] | 158 | |
---|
| 159 | def testUpdateWidgetFromBumps(self): |
---|
| 160 | '''Test the widget update''' |
---|
| 161 | # modify some value |
---|
| 162 | options.FIT_CONFIG.values['newton']['steps'] = 1234 |
---|
| 163 | options.FIT_CONFIG.values['newton']['starts'] = 666 |
---|
| 164 | options.FIT_CONFIG.values['newton']['xtol'] = 0.01 |
---|
| 165 | |
---|
| 166 | # Invoke the method for the changed |
---|
| 167 | self.widget.updateWidgetFromBumps('newton') |
---|
| 168 | |
---|
| 169 | # See that the widget picked up the right values |
---|
| 170 | self.assertEqual(self.widget.steps_newton.text(), '1234') |
---|
| 171 | self.assertEqual(self.widget.starts_newton.text(), '666') |
---|
| 172 | self.assertEqual(self.widget.ftol_newton.text(), '1e-06') # default |
---|
| 173 | self.assertEqual(self.widget.xtol_newton.text(), '0.01') |
---|
| 174 | |
---|
| 175 | if __name__ == "__main__": |
---|
| 176 | unittest.main() |
---|