1 | import sys |
---|
2 | import unittest |
---|
3 | import webbrowser |
---|
4 | |
---|
5 | from PyQt5 import QtGui, QtWidgets |
---|
6 | |
---|
7 | from unittest.mock import MagicMock |
---|
8 | |
---|
9 | # set up import paths |
---|
10 | import path_prepare |
---|
11 | |
---|
12 | from UnitTesting.TestUtils import QtSignalSpy |
---|
13 | |
---|
14 | # Local |
---|
15 | from sas.qtgui.Perspectives.Fitting.FittingOptions import \ |
---|
16 | FittingOptions, FittingMethod, FittingMethodParameter |
---|
17 | |
---|
18 | if not QtWidgets.QApplication.instance(): |
---|
19 | app = QtWidgets.QApplication(sys.argv) |
---|
20 | |
---|
21 | class FittingOptionsTest(unittest.TestCase): |
---|
22 | '''Test the FittingOptions dialog''' |
---|
23 | def setUp(self): |
---|
24 | '''Create FittingOptions dialog''' |
---|
25 | self.widget = FittingOptions() |
---|
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, QtWidgets.QDialog) |
---|
35 | # Default title |
---|
36 | self.assertEqual(self.widget.windowTitle(), "Fit Algorithms") |
---|
37 | |
---|
38 | # The combo box |
---|
39 | self.assertIsInstance(self.widget.cbAlgorithm, QtWidgets.QComboBox) |
---|
40 | self.assertTrue(self.widget.cbAlgorithm.count() >= 5) # at least the default bumps algos |
---|
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.widget.cbAlgorithm.setCurrentText("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.widget.cbAlgorithm.setCurrentText("Differential Evolution") |
---|
60 | self.assertIsInstance(self.widget.steps_de.validator(), QtGui.QIntValidator) |
---|
61 | self.assertIsInstance(self.widget.CR_de.validator(), QtGui.QDoubleValidator) |
---|
62 | self.assertIsInstance(self.widget.pop_de.validator(), QtGui.QDoubleValidator) |
---|
63 | self.assertIsInstance(self.widget.F_de.validator(), QtGui.QDoubleValidator) |
---|
64 | self.assertIsInstance(self.widget.ftol_de.validator(), QtGui.QDoubleValidator) |
---|
65 | self.assertIsInstance(self.widget.xtol_de.validator(), QtGui.QDoubleValidator) |
---|
66 | |
---|
67 | # bottom value for floats and ints |
---|
68 | self.assertEqual(self.widget.steps_de.validator().bottom(), 0) |
---|
69 | self.assertEqual(self.widget.CR_de.validator().bottom(), 0) |
---|
70 | |
---|
71 | # Behaviour on empty cell |
---|
72 | self.widget.onAlgorithmChange(3) |
---|
73 | self.widget.steps_de.setText("") |
---|
74 | # This should disable the OK button |
---|
75 | ## self.assertFalse(self.widget.buttonBox.button(QtGui.QDialogButtonBox.Ok).isEnabled()) |
---|
76 | # Let's put some valid value in lineedit |
---|
77 | self.widget.steps_de.setText("1") |
---|
78 | # This should enable the OK button |
---|
79 | self.assertTrue(self.widget.buttonBox.button(QtWidgets.QDialogButtonBox.Ok).isEnabled()) |
---|
80 | |
---|
81 | def testOnApply(self): |
---|
82 | '''Test bumps update''' |
---|
83 | # Spy on the update signal |
---|
84 | spy_apply = QtSignalSpy(self.widget, self.widget.fit_option_changed) |
---|
85 | |
---|
86 | # Set the DREAM optimizer |
---|
87 | self.widget.cbAlgorithm.setCurrentIndex(2) |
---|
88 | # Change some values |
---|
89 | self.widget.init_dream.setCurrentIndex(2) |
---|
90 | self.widget.steps_dream.setText("50") |
---|
91 | # Apply the new values |
---|
92 | self.widget.onApply() |
---|
93 | |
---|
94 | self.assertEqual(spy_apply.count(), 1) |
---|
95 | self.assertIn('DREAM', spy_apply.called()[0]['args'][0]) |
---|
96 | |
---|
97 | # Check the parameters |
---|
98 | self.assertEqual(self.widget.fittingMethods['DREAM'].params['steps'].value, 50.) |
---|
99 | self.assertEqual(self.widget.fittingMethods['DREAM'].params['init'].value, 'cov') |
---|
100 | |
---|
101 | # test disabled until pyQt5 works well |
---|
102 | def testOnHelp(self): |
---|
103 | ''' Test help display''' |
---|
104 | webbrowser.open = MagicMock() |
---|
105 | |
---|
106 | # Invoke the action on default tab |
---|
107 | self.widget.onHelp() |
---|
108 | # Check if show() got called |
---|
109 | self.assertTrue(webbrowser.open.called) |
---|
110 | # Assure the filename is correct |
---|
111 | self.assertIn("optimizer.html", webbrowser.open.call_args[0][0]) |
---|
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(webbrowser.open.call_count, 2) |
---|
118 | # Assure the filename is correct |
---|
119 | self.assertIn("fit-dream", webbrowser.open.call_args[0][0]) |
---|
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(webbrowser.open.call_count, 3) |
---|
126 | # Assure the filename is correct |
---|
127 | self.assertIn("fit-lm", webbrowser.open.call_args[0][0]) |
---|
128 | |
---|
129 | def testParamWidget(self): |
---|
130 | '''Test the helper function''' |
---|
131 | # test empty call |
---|
132 | self.assertIsNone(self.widget.paramWidget(None, None)) |
---|
133 | # test silly call |
---|
134 | self.assertIsNone(self.widget.paramWidget('poop', 'dsfsd')) |
---|
135 | self.assertIsNone(self.widget.paramWidget(QtWidgets.QMainWindow(), None)) |
---|
136 | |
---|
137 | # Switch to DREAM |
---|
138 | self.widget.cbAlgorithm.setCurrentText('DREAM') |
---|
139 | fm = self.widget.fittingMethods['DREAM'] |
---|
140 | # test smart call |
---|
141 | self.assertIsInstance(self.widget.paramWidget(fm, 'samples'), QtWidgets.QLineEdit) |
---|
142 | self.assertIsInstance(self.widget.paramWidget(fm, 'init'), QtWidgets.QComboBox) |
---|
143 | |
---|
144 | def testUpdateConfigFromWidget(self): |
---|
145 | '''Test the config update''' |
---|
146 | # test empty call |
---|
147 | self.assertIsNone(self.widget.updateConfigFromWidget(None)) |
---|
148 | # test silly call |
---|
149 | self.assertIsNone(self.widget.updateConfigFromWidget('poop')) |
---|
150 | self.assertIsNone(self.widget.updateConfigFromWidget(QtWidgets.QMainWindow())) |
---|
151 | |
---|
152 | # Switch to DREAM |
---|
153 | self.widget.cbAlgorithm.setCurrentText('DREAM') |
---|
154 | fm = FittingMethod("dream", "a long dream", [ |
---|
155 | FittingMethodParameter("samples", "# samples", int, 0), |
---|
156 | FittingMethodParameter("init", "init func", int, 0)]) |
---|
157 | # test smart call |
---|
158 | self.widget.updateConfigFromWidget(fm) |
---|
159 | self.assertEqual(fm.params['samples'].value, 10000) |
---|
160 | self.assertEqual(fm.params['init'].value, 'eps') |
---|
161 | |
---|
162 | def testUpdateWidgetFromConfig(self): |
---|
163 | '''Test the widget update''' |
---|
164 | self.widget.cbAlgorithm.setCurrentText("Quasi-Newton BFGS") |
---|
165 | # modify some value |
---|
166 | self.assertEqual(self.widget.steps_newton.text(), '3000') |
---|
167 | self.assertEqual(self.widget.starts_newton.text(), '1') |
---|
168 | self.assertEqual(self.widget.ftol_newton.text(), '1e-06') # default |
---|
169 | self.assertEqual(self.widget.xtol_newton.text(), '1e-12') |
---|
170 | fm = self.widget.fittingMethods["Quasi-Newton BFGS"] |
---|
171 | fm.params['steps'].value = 1234 |
---|
172 | fm.params['starts'].value = 666 |
---|
173 | fm.params['ftol'].value = 1e-6 |
---|
174 | fm.params['xtol'].value = 0.01 |
---|
175 | |
---|
176 | # Invoke the method for the changed |
---|
177 | self.widget.updateWidgetFromConfig() |
---|
178 | |
---|
179 | # See that the widget picked up the right values |
---|
180 | self.assertEqual(self.widget.steps_newton.text(), '1234') |
---|
181 | self.assertEqual(self.widget.starts_newton.text(), '666') |
---|
182 | self.assertEqual(self.widget.ftol_newton.text(), '1e-06') # default |
---|
183 | self.assertEqual(self.widget.xtol_newton.text(), '0.01') |
---|
184 | |
---|
185 | if __name__ == "__main__": |
---|
186 | unittest.main() |
---|