[811bec1] | 1 | import sys |
---|
| 2 | import unittest |
---|
[d48cc19] | 3 | import time |
---|
[811bec1] | 4 | |
---|
| 5 | from PyQt4 import QtGui |
---|
| 6 | from PyQt4 import QtTest |
---|
| 7 | from PyQt4 import QtCore |
---|
| 8 | from mock import MagicMock |
---|
[351b53e] | 9 | from twisted.internet import threads |
---|
[811bec1] | 10 | |
---|
| 11 | # set up import paths |
---|
| 12 | import sas.qtgui.path_prepare |
---|
| 13 | |
---|
| 14 | # Local |
---|
[83eb5208] | 15 | from sas.qtgui.Utilities.GuiUtils import * |
---|
[811bec1] | 16 | from sas.qtgui.Perspectives.Fitting.FittingWidget import * |
---|
[351b53e] | 17 | from sas.qtgui.UnitTesting.TestUtils import QtSignalSpy |
---|
| 18 | |
---|
[dc5ef15] | 19 | from sas.qtgui.Plotting.PlotterData import Data1D |
---|
| 20 | from sas.qtgui.Plotting.PlotterData import Data2D |
---|
[811bec1] | 21 | |
---|
| 22 | app = QtGui.QApplication(sys.argv) |
---|
| 23 | |
---|
| 24 | class dummy_manager(object): |
---|
[d48cc19] | 25 | communicate = Communicate() |
---|
[811bec1] | 26 | |
---|
| 27 | class FittingWidgetTest(unittest.TestCase): |
---|
| 28 | """Test the fitting widget GUI""" |
---|
| 29 | |
---|
| 30 | def setUp(self): |
---|
| 31 | """Create the GUI""" |
---|
| 32 | self.widget = FittingWidget(dummy_manager()) |
---|
| 33 | |
---|
| 34 | def tearDown(self): |
---|
| 35 | """Destroy the GUI""" |
---|
| 36 | self.widget.close() |
---|
| 37 | self.widget = None |
---|
| 38 | |
---|
| 39 | def testDefaults(self): |
---|
| 40 | """Test the GUI in its default state""" |
---|
| 41 | self.assertIsInstance(self.widget, QtGui.QWidget) |
---|
| 42 | self.assertEqual(self.widget.windowTitle(), "Fitting") |
---|
| 43 | self.assertEqual(self.widget.sizePolicy().Policy(), QtGui.QSizePolicy.Fixed) |
---|
| 44 | self.assertIsInstance(self.widget.lstParams.model(), QtGui.QStandardItemModel) |
---|
| 45 | self.assertIsInstance(self.widget.lstPoly.model(), QtGui.QStandardItemModel) |
---|
| 46 | self.assertIsInstance(self.widget.lstMagnetic.model(), QtGui.QStandardItemModel) |
---|
| 47 | self.assertFalse(self.widget.cbModel.isEnabled()) |
---|
| 48 | self.assertFalse(self.widget.cbStructureFactor.isEnabled()) |
---|
| 49 | self.assertFalse(self.widget.cmdFit.isEnabled()) |
---|
| 50 | self.assertTrue(self.widget.acceptsData()) |
---|
| 51 | self.assertFalse(self.widget.data_is_loaded) |
---|
| 52 | |
---|
[2add354] | 53 | def testSelectCategoryDefault(self): |
---|
[811bec1] | 54 | """ |
---|
| 55 | Test if model categories have been loaded properly |
---|
| 56 | """ |
---|
| 57 | fittingWindow = self.widget |
---|
| 58 | |
---|
| 59 | #Test loading from json categories |
---|
| 60 | category_list = fittingWindow.master_category_dict.keys() |
---|
| 61 | |
---|
| 62 | for category in category_list: |
---|
| 63 | self.assertNotEqual(fittingWindow.cbCategory.findText(category),-1) |
---|
| 64 | |
---|
| 65 | #Test what is current text in the combobox |
---|
| 66 | self.assertEqual(fittingWindow.cbCategory.currentText(), CATEGORY_DEFAULT) |
---|
| 67 | |
---|
| 68 | def testWidgetWithData(self): |
---|
| 69 | """ |
---|
| 70 | Test the instantiation of the widget with initial data |
---|
| 71 | """ |
---|
| 72 | data = Data1D(x=[1,2], y=[1,2]) |
---|
| 73 | GuiUtils.dataFromItem = MagicMock(return_value=data) |
---|
| 74 | item = QtGui.QStandardItem("test") |
---|
| 75 | |
---|
[1bc27f1] | 76 | widget_with_data = FittingWidget(dummy_manager(), data=item, tab_id=3) |
---|
[811bec1] | 77 | |
---|
| 78 | self.assertEqual(widget_with_data.data, data) |
---|
| 79 | self.assertTrue(widget_with_data.data_is_loaded) |
---|
[7248d75d] | 80 | # self.assertTrue(widget_with_data.cmdFit.isEnabled()) |
---|
[811bec1] | 81 | self.assertFalse(widget_with_data.acceptsData()) |
---|
| 82 | |
---|
| 83 | def testSelectPolydispersity(self): |
---|
| 84 | """ |
---|
| 85 | Test if models have been loaded properly |
---|
| 86 | :return: |
---|
| 87 | """ |
---|
| 88 | fittingWindow = self.widget |
---|
| 89 | |
---|
| 90 | #Test loading from json categories |
---|
[7248d75d] | 91 | fittingWindow.SASModelToQModel("cylinder") |
---|
[811bec1] | 92 | pd_index = fittingWindow.lstPoly.model().index(0,0) |
---|
| 93 | self.assertEqual(str(pd_index.data().toString()), "Distribution of radius") |
---|
| 94 | pd_index = fittingWindow.lstPoly.model().index(1,0) |
---|
| 95 | self.assertEqual(str(pd_index.data().toString()), "Distribution of length") |
---|
| 96 | |
---|
| 97 | def testSelectStructureFactor(self): |
---|
| 98 | """ |
---|
| 99 | Test if structure factors have been loaded properly |
---|
| 100 | :return: |
---|
| 101 | """ |
---|
| 102 | fittingWindow = self.widget |
---|
| 103 | |
---|
| 104 | #Test for existence in combobox |
---|
| 105 | self.assertNotEqual(fittingWindow.cbStructureFactor.findText("stickyhardsphere"),-1) |
---|
| 106 | self.assertNotEqual(fittingWindow.cbStructureFactor.findText("hayter_msa"),-1) |
---|
| 107 | self.assertNotEqual(fittingWindow.cbStructureFactor.findText("squarewell"),-1) |
---|
| 108 | self.assertNotEqual(fittingWindow.cbStructureFactor.findText("hardsphere"),-1) |
---|
| 109 | |
---|
| 110 | #Test what is current text in the combobox |
---|
| 111 | self.assertTrue(fittingWindow.cbCategory.currentText(), "None") |
---|
| 112 | |
---|
| 113 | def testSignals(self): |
---|
| 114 | """ |
---|
[351b53e] | 115 | Test the widget emitted signals |
---|
[811bec1] | 116 | """ |
---|
| 117 | pass |
---|
| 118 | |
---|
[2add354] | 119 | def testSelectCategory(self): |
---|
[811bec1] | 120 | """ |
---|
| 121 | Assure proper behaviour on changing category |
---|
| 122 | """ |
---|
[351b53e] | 123 | self.widget.show() |
---|
| 124 | self.assertEqual(self.widget._previous_category_index, 0) |
---|
| 125 | # confirm the model combo contains no models |
---|
| 126 | self.assertEqual(self.widget.cbModel.count(), 0) |
---|
| 127 | |
---|
| 128 | # invoke the method by changing the index |
---|
[9934e48] | 129 | category_index = self.widget.cbCategory.findText("Shape Independent") |
---|
[b1e36a3] | 130 | self.widget.cbCategory.setCurrentIndex(category_index) |
---|
[351b53e] | 131 | |
---|
| 132 | # test the model combo content |
---|
| 133 | self.assertEqual(self.widget.cbModel.count(), 29) |
---|
| 134 | |
---|
| 135 | # Try to change back to default |
---|
| 136 | self.widget.cbCategory.setCurrentIndex(0) |
---|
| 137 | |
---|
| 138 | # Observe no such luck |
---|
[9934e48] | 139 | self.assertEqual(self.widget.cbCategory.currentIndex(), 6) |
---|
[351b53e] | 140 | self.assertEqual(self.widget.cbModel.count(), 29) |
---|
| 141 | |
---|
| 142 | # Set the structure factor |
---|
| 143 | structure_index=self.widget.cbCategory.findText(CATEGORY_STRUCTURE) |
---|
| 144 | self.widget.cbCategory.setCurrentIndex(structure_index) |
---|
| 145 | # check the enablement of controls |
---|
| 146 | self.assertFalse(self.widget.cbModel.isEnabled()) |
---|
| 147 | self.assertTrue(self.widget.cbStructureFactor.isEnabled()) |
---|
[811bec1] | 148 | |
---|
| 149 | def testSelectModel(self): |
---|
| 150 | """ |
---|
| 151 | Assure proper behaviour on changing model |
---|
| 152 | """ |
---|
[351b53e] | 153 | self.widget.show() |
---|
| 154 | # Change the category index so we have some models |
---|
[9934e48] | 155 | category_index = self.widget.cbCategory.findText("Shape Independent") |
---|
[b1e36a3] | 156 | self.widget.cbCategory.setCurrentIndex(category_index) |
---|
[351b53e] | 157 | |
---|
| 158 | # check the enablement of controls |
---|
| 159 | self.assertTrue(self.widget.cbModel.isEnabled()) |
---|
| 160 | self.assertFalse(self.widget.cbStructureFactor.isEnabled()) |
---|
| 161 | |
---|
| 162 | # set up the model update spy |
---|
| 163 | # spy = QtSignalSpy(self.widget._model_model, self.widget._model_model.itemChanged) |
---|
| 164 | |
---|
| 165 | # mock the tested methods |
---|
| 166 | self.widget.SASModelToQModel = MagicMock() |
---|
| 167 | self.widget.createDefaultDataset = MagicMock() |
---|
[b1e36a3] | 168 | self.widget.calculateQGridForModel = MagicMock() |
---|
[351b53e] | 169 | # |
---|
| 170 | # Now change the model |
---|
| 171 | self.widget.cbModel.setCurrentIndex(3) |
---|
[9934e48] | 172 | self.assertEqual(self.widget.cbModel.currentText(),'dab') |
---|
[351b53e] | 173 | |
---|
| 174 | # No data sent -> no index set, only createDefaultDataset called |
---|
| 175 | self.assertTrue(self.widget.createDefaultDataset.called) |
---|
| 176 | self.assertTrue(self.widget.SASModelToQModel.called) |
---|
[b1e36a3] | 177 | self.assertFalse(self.widget.calculateQGridForModel.called) |
---|
[351b53e] | 178 | |
---|
[6fd4e36] | 179 | # Let's tell the widget that data has been loaded |
---|
| 180 | self.widget.data_is_loaded = True |
---|
[351b53e] | 181 | # Reset the sasmodel index |
---|
| 182 | self.widget.cbModel.setCurrentIndex(1) |
---|
[9934e48] | 183 | self.assertEqual(self.widget.cbModel.currentText(),'broad_peak') |
---|
[351b53e] | 184 | |
---|
[b1e36a3] | 185 | # Observe calculateQGridForModel called |
---|
| 186 | self.assertTrue(self.widget.calculateQGridForModel.called) |
---|
[811bec1] | 187 | |
---|
| 188 | def testSelectFactor(self): |
---|
| 189 | """ |
---|
| 190 | Assure proper behaviour on changing structure factor |
---|
| 191 | """ |
---|
[351b53e] | 192 | self.widget.show() |
---|
| 193 | # Change the category index so we have some models |
---|
[9934e48] | 194 | category_index = self.widget.cbCategory.findText("Shape Independent") |
---|
[b1e36a3] | 195 | self.widget.cbCategory.setCurrentIndex(category_index) |
---|
[351b53e] | 196 | # Change the model to one that supports structure factors |
---|
| 197 | model_index = self.widget.cbModel.findText('fractal_core_shell') |
---|
| 198 | self.widget.cbModel.setCurrentIndex(model_index) |
---|
| 199 | |
---|
| 200 | # Check that the factor combo is active and the default is chosen |
---|
| 201 | self.assertTrue(self.widget.cbStructureFactor.isEnabled()) |
---|
| 202 | self.assertEqual(self.widget.cbStructureFactor.currentText(), STRUCTURE_DEFAULT) |
---|
| 203 | |
---|
| 204 | # We have this many rows in the model |
---|
| 205 | rowcount = self.widget._model_model.rowCount() |
---|
| 206 | #self.assertEqual(self.widget._model_model.rowCount(), 8) |
---|
| 207 | |
---|
| 208 | # Change structure factor to something more exciting |
---|
| 209 | structure_index = self.widget.cbStructureFactor.findText('squarewell') |
---|
| 210 | self.widget.cbStructureFactor.setCurrentIndex(structure_index) |
---|
| 211 | |
---|
| 212 | # We have 4 more rows now |
---|
| 213 | self.assertEqual(self.widget._model_model.rowCount(), rowcount+4) |
---|
| 214 | |
---|
| 215 | # Switch models |
---|
| 216 | self.widget.cbModel.setCurrentIndex(0) |
---|
| 217 | |
---|
| 218 | # Observe factor reset to None |
---|
| 219 | self.assertEqual(self.widget.cbStructureFactor.currentText(), STRUCTURE_DEFAULT) |
---|
| 220 | |
---|
[fd1ae6d1] | 221 | # Switch category to structure factor |
---|
| 222 | structure_index=self.widget.cbCategory.findText(CATEGORY_STRUCTURE) |
---|
| 223 | self.widget.cbCategory.setCurrentIndex(structure_index) |
---|
| 224 | # Observe the correct enablement |
---|
| 225 | self.assertTrue(self.widget.cbStructureFactor.isEnabled()) |
---|
| 226 | self.assertFalse(self.widget.cbModel.isEnabled()) |
---|
| 227 | self.assertEqual(self.widget._model_model.rowCount(), 0) |
---|
| 228 | |
---|
| 229 | # Choose the last factor |
---|
| 230 | last_index = self.widget.cbStructureFactor.count() |
---|
| 231 | self.widget.cbStructureFactor.setCurrentIndex(last_index-1) |
---|
| 232 | # Do we have all the rows? |
---|
| 233 | self.assertEqual(self.widget._model_model.rowCount(), 4) |
---|
[351b53e] | 234 | |
---|
[fd1ae6d1] | 235 | # Are the command buttons properly enabled? |
---|
| 236 | self.assertTrue(self.widget.cmdPlot.isEnabled()) |
---|
| 237 | self.assertFalse(self.widget.cmdFit.isEnabled()) |
---|
[811bec1] | 238 | |
---|
| 239 | def testReadCategoryInfo(self): |
---|
| 240 | """ |
---|
| 241 | Check the category file reader |
---|
| 242 | """ |
---|
[351b53e] | 243 | # Tested in default checks |
---|
[811bec1] | 244 | pass |
---|
| 245 | |
---|
| 246 | def testUpdateParamsFromModel(self): |
---|
| 247 | """ |
---|
| 248 | Checks the sasmodel parameter update from QModel items |
---|
| 249 | """ |
---|
[351b53e] | 250 | # Tested in default checks |
---|
[811bec1] | 251 | pass |
---|
| 252 | |
---|
[d48cc19] | 253 | def testCreateTheoryIndex(self): |
---|
[811bec1] | 254 | """ |
---|
[351b53e] | 255 | Test the data->QIndex conversion |
---|
[811bec1] | 256 | """ |
---|
[351b53e] | 257 | # set up the model update spy |
---|
| 258 | spy = QtSignalSpy(self.widget._model_model, self.widget.communicate.updateTheoryFromPerspectiveSignal) |
---|
[811bec1] | 259 | |
---|
[351b53e] | 260 | self.widget.show() |
---|
| 261 | # Change the category index so we have some models |
---|
| 262 | self.widget.cbCategory.setCurrentIndex(1) |
---|
[811bec1] | 263 | |
---|
[351b53e] | 264 | # Create the index |
---|
[d48cc19] | 265 | self.widget.createTheoryIndex(Data1D(x=[1,2], y=[1,2])) |
---|
[811bec1] | 266 | |
---|
[d48cc19] | 267 | # Make sure the signal has been emitted |
---|
| 268 | self.assertEqual(spy.count(), 1) |
---|
[811bec1] | 269 | |
---|
[d48cc19] | 270 | # Check the argument type |
---|
| 271 | self.assertIsInstance(spy.called()[0]['args'][0], QtGui.QStandardItem) |
---|
[811bec1] | 272 | |
---|
[b1e36a3] | 273 | def testCalculateQGridForModel(self): |
---|
[811bec1] | 274 | """ |
---|
[351b53e] | 275 | Check that the fitting 1D data object is ready |
---|
[811bec1] | 276 | """ |
---|
[351b53e] | 277 | # Mock the thread creation |
---|
| 278 | threads.deferToThread = MagicMock() |
---|
[d48cc19] | 279 | # Model for theory |
---|
| 280 | self.widget.SASModelToQModel("cylinder") |
---|
[351b53e] | 281 | # Call the tested method |
---|
[b1e36a3] | 282 | self.widget.calculateQGridForModel() |
---|
[d48cc19] | 283 | time.sleep(1) |
---|
[351b53e] | 284 | # Test the mock |
---|
| 285 | self.assertTrue(threads.deferToThread.called) |
---|
| 286 | self.assertEqual(threads.deferToThread.call_args_list[0][0][0].__name__, "compute") |
---|
[811bec1] | 287 | |
---|
[d48cc19] | 288 | def testCalculateResiduals(self): |
---|
[811bec1] | 289 | """ |
---|
[d48cc19] | 290 | Check that the residuals are calculated and plots updated |
---|
[811bec1] | 291 | """ |
---|
[d48cc19] | 292 | test_data = Data1D(x=[1,2], y=[1,2]) |
---|
[811bec1] | 293 | |
---|
[d48cc19] | 294 | # Model for theory |
---|
| 295 | self.widget.SASModelToQModel("cylinder") |
---|
| 296 | # Invoke the tested method |
---|
| 297 | self.widget.calculateResiduals(test_data) |
---|
| 298 | # Check the Chi2 value - should be undetermined |
---|
| 299 | self.assertEqual(self.widget.lblChi2Value.text(), '---') |
---|
| 300 | |
---|
| 301 | # Force same data into logic |
---|
| 302 | self.widget.logic.data = test_data |
---|
| 303 | self.widget.calculateResiduals(test_data) |
---|
| 304 | # Now, the difference is 0, as data is the same |
---|
| 305 | self.assertEqual(self.widget.lblChi2Value.text(), '0') |
---|
| 306 | |
---|
| 307 | # Change data |
---|
| 308 | test_data_2 = Data1D(x=[1,2], y=[2.1,3.49]) |
---|
| 309 | self.widget.logic.data = test_data_2 |
---|
| 310 | self.widget.calculateResiduals(test_data) |
---|
| 311 | # Now, the difference is non-zero |
---|
[672b8ab] | 312 | self.assertEqual(float(self.widget.lblChi2Value.text()), 1.7151) |
---|
[811bec1] | 313 | |
---|
| 314 | def testSetPolyModel(self): |
---|
| 315 | """ |
---|
| 316 | Test the polydispersity model setup |
---|
| 317 | """ |
---|
[351b53e] | 318 | self.widget.show() |
---|
| 319 | # Change the category index so we have a model with no poly |
---|
[9934e48] | 320 | category_index = self.widget.cbCategory.findText("Shape Independent") |
---|
[b1e36a3] | 321 | self.widget.cbCategory.setCurrentIndex(category_index) |
---|
[351b53e] | 322 | # Check the poly model |
---|
| 323 | self.assertEqual(self.widget._poly_model.rowCount(), 0) |
---|
| 324 | self.assertEqual(self.widget._poly_model.columnCount(), 0) |
---|
| 325 | |
---|
| 326 | # Change the category index so we have a model available |
---|
| 327 | self.widget.cbCategory.setCurrentIndex(2) |
---|
| 328 | |
---|
| 329 | # Check the poly model |
---|
[9934e48] | 330 | self.assertEqual(self.widget._poly_model.rowCount(), 4) |
---|
[351b53e] | 331 | self.assertEqual(self.widget._poly_model.columnCount(), 7) |
---|
| 332 | |
---|
| 333 | # Test the header |
---|
| 334 | self.assertEqual(self.widget.lstPoly.horizontalHeader().count(), 7) |
---|
| 335 | self.assertFalse(self.widget.lstPoly.horizontalHeader().stretchLastSection()) |
---|
| 336 | |
---|
| 337 | # Test presence of comboboxes in last column |
---|
| 338 | for row in xrange(self.widget._poly_model.rowCount()): |
---|
| 339 | func_index = self.widget._poly_model.index(row, 6) |
---|
[6011788] | 340 | #self.assertTrue(isinstance(self.widget.lstPoly.indexWidget(func_index), QtGui.QComboBox)) |
---|
[351b53e] | 341 | self.assertIn('Distribution of', self.widget._poly_model.item(row, 0).text()) |
---|
[811bec1] | 342 | |
---|
| 343 | def testSetMagneticModel(self): |
---|
| 344 | """ |
---|
| 345 | Test the magnetic model setup |
---|
| 346 | """ |
---|
[351b53e] | 347 | self.widget.show() |
---|
| 348 | # Change the category index so we have a model available |
---|
[9934e48] | 349 | category_index = self.widget.cbCategory.findText("Sphere") |
---|
[b1e36a3] | 350 | self.widget.cbCategory.setCurrentIndex(category_index) |
---|
[351b53e] | 351 | |
---|
| 352 | # Check the magnetic model |
---|
| 353 | self.assertEqual(self.widget._magnet_model.rowCount(), 9) |
---|
| 354 | self.assertEqual(self.widget._magnet_model.columnCount(), 5) |
---|
| 355 | |
---|
| 356 | # Test the header |
---|
| 357 | self.assertEqual(self.widget.lstMagnetic.horizontalHeader().count(), 5) |
---|
| 358 | self.assertFalse(self.widget.lstMagnetic.horizontalHeader().stretchLastSection()) |
---|
| 359 | |
---|
| 360 | # Test rows |
---|
| 361 | for row in xrange(self.widget._magnet_model.rowCount()): |
---|
| 362 | func_index = self.widget._magnet_model.index(row, 0) |
---|
| 363 | self.assertIn(':', self.widget._magnet_model.item(row, 0).text()) |
---|
| 364 | |
---|
[811bec1] | 365 | |
---|
| 366 | def testAddExtraShells(self): |
---|
| 367 | """ |
---|
| 368 | Test how the extra shells are presented |
---|
| 369 | """ |
---|
| 370 | pass |
---|
| 371 | |
---|
| 372 | def testModifyShellsInList(self): |
---|
| 373 | """ |
---|
| 374 | Test the additional rows added by modifying the shells combobox |
---|
| 375 | """ |
---|
[351b53e] | 376 | self.widget.show() |
---|
| 377 | # Change the model to multi shell |
---|
[9934e48] | 378 | category_index = self.widget.cbCategory.findText("Sphere") |
---|
[b1e36a3] | 379 | self.widget.cbCategory.setCurrentIndex(category_index) |
---|
| 380 | model_index = self.widget.cbModel.findText("core_multi_shell") |
---|
| 381 | self.widget.cbModel.setCurrentIndex(model_index) |
---|
[351b53e] | 382 | |
---|
| 383 | # Assure we have the combobox available |
---|
| 384 | last_row = self.widget._last_model_row |
---|
| 385 | func_index = self.widget._model_model.index(last_row-1, 1) |
---|
| 386 | self.assertIsInstance(self.widget.lstParams.indexWidget(func_index), QtGui.QComboBox) |
---|
| 387 | |
---|
| 388 | # Change the combo box index |
---|
| 389 | self.widget.lstParams.indexWidget(func_index).setCurrentIndex(3) |
---|
| 390 | |
---|
| 391 | # Check that the number of rows increased |
---|
| 392 | more_rows = self.widget._model_model.rowCount() - last_row |
---|
| 393 | self.assertEqual(more_rows, 6) # 6 new rows: 2 params per index |
---|
| 394 | |
---|
| 395 | # Back to 0 |
---|
| 396 | self.widget.lstParams.indexWidget(func_index).setCurrentIndex(0) |
---|
| 397 | self.assertEqual(self.widget._model_model.rowCount(), last_row) |
---|
[811bec1] | 398 | |
---|
[d48cc19] | 399 | def testPlotTheory(self): |
---|
| 400 | """ |
---|
| 401 | See that theory item can produce a chart |
---|
| 402 | """ |
---|
| 403 | # By default, the compute/plot button is disabled |
---|
| 404 | self.assertFalse(self.widget.cmdPlot.isEnabled()) |
---|
| 405 | self.assertEqual(self.widget.cmdPlot.text(), 'Show Plot') |
---|
| 406 | |
---|
| 407 | # Assign a model |
---|
| 408 | self.widget.show() |
---|
| 409 | # Change the category index so we have a model available |
---|
| 410 | category_index = self.widget.cbCategory.findText("Sphere") |
---|
| 411 | self.widget.cbCategory.setCurrentIndex(category_index) |
---|
| 412 | |
---|
| 413 | # Check the enablement/text |
---|
| 414 | self.assertTrue(self.widget.cmdPlot.isEnabled()) |
---|
| 415 | self.assertEqual(self.widget.cmdPlot.text(), 'Calculate') |
---|
| 416 | |
---|
| 417 | # Spying on plot update signal |
---|
| 418 | spy = QtSignalSpy(self.widget, self.widget.communicate.plotRequestedSignal) |
---|
| 419 | |
---|
| 420 | # Press Calculate |
---|
| 421 | QtTest.QTest.mouseClick(self.widget.cmdPlot, QtCore.Qt.LeftButton) |
---|
| 422 | |
---|
| 423 | # Observe cmdPlot caption change |
---|
| 424 | self.assertEqual(self.widget.cmdPlot.text(), 'Show Plot') |
---|
| 425 | |
---|
| 426 | # Make sure the signal has NOT been emitted |
---|
| 427 | self.assertEqual(spy.count(), 0) |
---|
| 428 | |
---|
| 429 | # Click again |
---|
| 430 | QtTest.QTest.mouseClick(self.widget.cmdPlot, QtCore.Qt.LeftButton) |
---|
| 431 | |
---|
| 432 | # This time, we got the update signal |
---|
| 433 | self.assertEqual(spy.count(), 0) |
---|
| 434 | |
---|
| 435 | def testPlotData(self): |
---|
| 436 | """ |
---|
| 437 | See that data item can produce a chart |
---|
| 438 | """ |
---|
| 439 | # By default, the compute/plot button is disabled |
---|
| 440 | self.assertFalse(self.widget.cmdPlot.isEnabled()) |
---|
| 441 | self.assertEqual(self.widget.cmdPlot.text(), 'Show Plot') |
---|
| 442 | |
---|
| 443 | self.widget.show() |
---|
| 444 | |
---|
| 445 | # Set data |
---|
| 446 | test_data = Data1D(x=[1,2], y=[1,2]) |
---|
| 447 | |
---|
| 448 | # Force same data into logic |
---|
| 449 | self.widget.logic.data = test_data |
---|
| 450 | self.widget.data_is_loaded = True |
---|
| 451 | |
---|
| 452 | # Change the category index so we have a model available |
---|
| 453 | category_index = self.widget.cbCategory.findText("Sphere") |
---|
| 454 | self.widget.cbCategory.setCurrentIndex(category_index) |
---|
| 455 | |
---|
| 456 | # Check the enablement/text |
---|
| 457 | self.assertTrue(self.widget.cmdPlot.isEnabled()) |
---|
| 458 | self.assertEqual(self.widget.cmdPlot.text(), 'Show Plot') |
---|
| 459 | |
---|
| 460 | # Spying on plot update signal |
---|
| 461 | spy = QtSignalSpy(self.widget, self.widget.communicate.plotRequestedSignal) |
---|
| 462 | |
---|
| 463 | # Press Calculate |
---|
| 464 | QtTest.QTest.mouseClick(self.widget.cmdPlot, QtCore.Qt.LeftButton) |
---|
| 465 | |
---|
| 466 | # Observe cmdPlot caption did not change |
---|
| 467 | self.assertEqual(self.widget.cmdPlot.text(), 'Show Plot') |
---|
| 468 | |
---|
| 469 | # Make sure the signal has been emitted == new plot |
---|
| 470 | self.assertEqual(spy.count(), 1) |
---|
| 471 | |
---|
[2add354] | 472 | def testOnFit1D(self): |
---|
[02ddfb4] | 473 | """ |
---|
| 474 | Test the threaded fitting call |
---|
| 475 | """ |
---|
| 476 | # Set data |
---|
| 477 | test_data = Data1D(x=[1,2], y=[1,2]) |
---|
| 478 | |
---|
| 479 | # Force same data into logic |
---|
| 480 | self.widget.logic.data = test_data |
---|
| 481 | self.widget.data_is_loaded = True |
---|
| 482 | category_index = self.widget.cbCategory.findText("Sphere") |
---|
| 483 | self.widget.cbCategory.setCurrentIndex(category_index) |
---|
| 484 | |
---|
| 485 | self.widget.show() |
---|
| 486 | |
---|
| 487 | # Test no fitting params |
---|
| 488 | self.widget.parameters_to_fit = [] |
---|
[2add354] | 489 | |
---|
| 490 | with self.assertRaises(ValueError) as error: |
---|
| 491 | self.widget.onFit() |
---|
| 492 | self.assertEqual(str(error.exception), 'no fitting parameters') |
---|
| 493 | |
---|
| 494 | # Assing fitting params |
---|
| 495 | self.widget.parameters_to_fit = ['scale'] |
---|
| 496 | |
---|
| 497 | # Spying on status update signal |
---|
| 498 | update_spy = QtSignalSpy(self.widget, self.widget.communicate.statusBarUpdateSignal) |
---|
| 499 | |
---|
| 500 | with threads.deferToThread as MagicMock: |
---|
| 501 | self.widget.onFit() |
---|
| 502 | # thread called |
---|
| 503 | self.assertTrue(threads.deferToThread.called) |
---|
| 504 | # thread method is 'compute' |
---|
| 505 | self.assertEqual(threads.deferToThread.call_args_list[0][0][0].__name__, 'compute') |
---|
| 506 | |
---|
| 507 | # the fit button changed caption and got disabled |
---|
| 508 | self.assertEqual(self.widget.cmdFit.text(), 'Calculating...') |
---|
| 509 | self.assertFalse(self.widget.cmdFit.isEnabled()) |
---|
| 510 | |
---|
| 511 | # Signal pushed up |
---|
| 512 | self.assertEqual(update_spy.count(), 1) |
---|
| 513 | |
---|
| 514 | def testOnFit2D(self): |
---|
| 515 | """ |
---|
| 516 | Test the threaded fitting call |
---|
| 517 | """ |
---|
| 518 | # Set data |
---|
| 519 | test_data = Data2D(image=[1.0, 2.0, 3.0], |
---|
| 520 | err_image=[0.01, 0.02, 0.03], |
---|
| 521 | qx_data=[0.1, 0.2, 0.3], |
---|
| 522 | qy_data=[0.1, 0.2, 0.3], |
---|
| 523 | xmin=0.1, xmax=0.3, ymin=0.1, ymax=0.3, |
---|
| 524 | mask=[True, True, True]) |
---|
| 525 | |
---|
| 526 | # Force same data into logic |
---|
| 527 | self.widget.logic.data = test_data |
---|
| 528 | self.widget.data_is_loaded = True |
---|
| 529 | category_index = self.widget.cbCategory.findText("Sphere") |
---|
| 530 | self.widget.cbCategory.setCurrentIndex(category_index) |
---|
| 531 | |
---|
| 532 | self.widget.show() |
---|
| 533 | |
---|
| 534 | # Test no fitting params |
---|
| 535 | self.widget.parameters_to_fit = [] |
---|
[02ddfb4] | 536 | |
---|
| 537 | with self.assertRaises(ValueError) as error: |
---|
| 538 | self.widget.onFit() |
---|
| 539 | self.assertEqual(str(error.exception), 'no fitting parameters') |
---|
| 540 | |
---|
| 541 | # Assing fitting params |
---|
| 542 | self.widget.parameters_to_fit = ['scale'] |
---|
| 543 | |
---|
| 544 | # Spying on status update signal |
---|
| 545 | update_spy = QtSignalSpy(self.widget, self.widget.communicate.statusBarUpdateSignal) |
---|
| 546 | |
---|
| 547 | with threads.deferToThread as MagicMock: |
---|
| 548 | self.widget.onFit() |
---|
| 549 | # thread called |
---|
| 550 | self.assertTrue(threads.deferToThread.called) |
---|
| 551 | # thread method is 'compute' |
---|
| 552 | self.assertEqual(threads.deferToThread.call_args_list[0][0][0].__name__, 'compute') |
---|
| 553 | |
---|
| 554 | # the fit button changed caption and got disabled |
---|
| 555 | self.assertEqual(self.widget.cmdFit.text(), 'Calculating...') |
---|
| 556 | self.assertFalse(self.widget.cmdFit.isEnabled()) |
---|
| 557 | |
---|
| 558 | # Signal pushed up |
---|
| 559 | self.assertEqual(update_spy.count(), 1) |
---|
| 560 | |
---|
[672b8ab] | 561 | def testReadFitPage(self): |
---|
| 562 | """ |
---|
| 563 | Read in the fitpage object and restore state |
---|
| 564 | """ |
---|
| 565 | # Set data |
---|
| 566 | test_data = Data1D(x=[1,2], y=[1,2]) |
---|
| 567 | |
---|
| 568 | # Force same data into logic |
---|
| 569 | self.widget.logic.data = test_data |
---|
| 570 | self.widget.data_is_loaded = True |
---|
| 571 | category_index = self.widget.cbCategory.findText('Sphere') |
---|
| 572 | self.widget.cbCategory.setCurrentIndex(category_index) |
---|
| 573 | self.widget.parameters_to_fit = ['scale'] |
---|
| 574 | # Invoke the tested method |
---|
| 575 | fp = self.widget.currentState() |
---|
| 576 | |
---|
| 577 | # Prepare modified fit page |
---|
| 578 | fp.current_model = 'onion' |
---|
| 579 | fp.is_polydisperse = True |
---|
| 580 | |
---|
| 581 | # Read in modified state |
---|
| 582 | self.widget.readFitPage(fp) |
---|
| 583 | |
---|
| 584 | # Check if the widget got updated accordingly |
---|
| 585 | self.assertEqual(self.widget.cbModel.currentText(), 'onion') |
---|
| 586 | self.assertTrue(self.widget.chkPolydispersity.isChecked()) |
---|
| 587 | |
---|
| 588 | def testCurrentState(self): |
---|
| 589 | """ |
---|
| 590 | Set up the fitpage with current state |
---|
| 591 | """ |
---|
| 592 | # Set data |
---|
| 593 | test_data = Data1D(x=[1,2], y=[1,2]) |
---|
| 594 | |
---|
| 595 | # Force same data into logic |
---|
| 596 | self.widget.logic.data = test_data |
---|
| 597 | self.widget.data_is_loaded = True |
---|
| 598 | category_index = self.widget.cbCategory.findText("Sphere") |
---|
| 599 | self.widget.cbCategory.setCurrentIndex(category_index) |
---|
| 600 | self.widget.parameters_to_fit = ['scale'] |
---|
| 601 | |
---|
| 602 | # Invoke the tested method |
---|
| 603 | fp = self.widget.currentState() |
---|
| 604 | |
---|
| 605 | # Test some entries. (Full testing of fp is done in FitPageTest) |
---|
| 606 | self.assertIsInstance(fp.data, Data1D) |
---|
| 607 | self.assertListEqual(list(fp.data.x), [1,2]) |
---|
| 608 | self.assertTrue(fp.data_is_loaded) |
---|
| 609 | self.assertEqual(fp.current_category, "Sphere") |
---|
| 610 | self.assertEqual(fp.current_model, "adsorbed_layer") |
---|
| 611 | self.assertListEqual(fp.parameters_to_fit, ['scale']) |
---|
| 612 | |
---|
| 613 | def testPushFitPage(self): |
---|
| 614 | """ |
---|
| 615 | Push current state of fitpage onto stack |
---|
| 616 | """ |
---|
[2241130] | 617 | # Set data |
---|
| 618 | test_data = Data1D(x=[1,2], y=[1,2]) |
---|
| 619 | |
---|
| 620 | # Force same data into logic |
---|
| 621 | self.widget.logic.data = test_data |
---|
| 622 | self.widget.data_is_loaded = True |
---|
| 623 | category_index = self.widget.cbCategory.findText("Sphere") |
---|
| 624 | |
---|
| 625 | # Asses the initial state of stack |
---|
| 626 | self.assertEqual(self.widget.page_stack, []) |
---|
| 627 | |
---|
| 628 | # Set the undo flag |
---|
| 629 | self.widget.undo_supported = True |
---|
| 630 | self.widget.cbCategory.setCurrentIndex(category_index) |
---|
| 631 | self.widget.parameters_to_fit = ['scale'] |
---|
| 632 | |
---|
| 633 | # Check that the stack is updated |
---|
| 634 | self.assertEqual(len(self.widget.page_stack), 1) |
---|
| 635 | |
---|
| 636 | # Change another parameter |
---|
| 637 | self.widget._model_model.item(2, 1).setText("3.0") |
---|
| 638 | # Check that the stack is updated |
---|
| 639 | self.assertEqual(len(self.widget.page_stack), 2) |
---|
[672b8ab] | 640 | |
---|
| 641 | def testPopFitPage(self): |
---|
| 642 | """ |
---|
| 643 | Pop current state of fitpage from stack |
---|
| 644 | """ |
---|
[2241130] | 645 | # TODO: to be added when implementing UNDO/REDO |
---|
[672b8ab] | 646 | pass |
---|
[811bec1] | 647 | |
---|
| 648 | if __name__ == "__main__": |
---|
| 649 | unittest.main() |
---|