[811bec1] | 1 | import sys |
---|
| 2 | import unittest |
---|
[d48cc19] | 3 | import time |
---|
[53c771e] | 4 | import logging |
---|
[811bec1] | 5 | |
---|
[53c771e] | 6 | from PyQt5 import QtGui |
---|
| 7 | from PyQt5 import QtWidgets |
---|
| 8 | from PyQt5 import QtTest |
---|
| 9 | from PyQt5 import QtCore |
---|
[7fb471d] | 10 | from unittest.mock import MagicMock |
---|
[351b53e] | 11 | from twisted.internet import threads |
---|
[811bec1] | 12 | |
---|
| 13 | # set up import paths |
---|
| 14 | import sas.qtgui.path_prepare |
---|
| 15 | |
---|
| 16 | # Local |
---|
[83eb5208] | 17 | from sas.qtgui.Utilities.GuiUtils import * |
---|
[811bec1] | 18 | from sas.qtgui.Perspectives.Fitting.FittingWidget import * |
---|
[14ec91c5] | 19 | from sas.qtgui.Perspectives.Fitting.Constraint import Constraint |
---|
[63319b0] | 20 | |
---|
[351b53e] | 21 | from sas.qtgui.UnitTesting.TestUtils import QtSignalSpy |
---|
| 22 | |
---|
[dc5ef15] | 23 | from sas.qtgui.Plotting.PlotterData import Data1D |
---|
| 24 | from sas.qtgui.Plotting.PlotterData import Data2D |
---|
[811bec1] | 25 | |
---|
[53c771e] | 26 | if not QtWidgets.QApplication.instance(): |
---|
| 27 | app = QtWidgets.QApplication(sys.argv) |
---|
[811bec1] | 28 | |
---|
| 29 | class dummy_manager(object): |
---|
[70080a0] | 30 | HELP_DIRECTORY_LOCATION = "html" |
---|
[d48cc19] | 31 | communicate = Communicate() |
---|
[811bec1] | 32 | |
---|
| 33 | class FittingWidgetTest(unittest.TestCase): |
---|
| 34 | """Test the fitting widget GUI""" |
---|
| 35 | |
---|
| 36 | def setUp(self): |
---|
| 37 | """Create the GUI""" |
---|
| 38 | self.widget = FittingWidget(dummy_manager()) |
---|
| 39 | |
---|
| 40 | def tearDown(self): |
---|
| 41 | """Destroy the GUI""" |
---|
| 42 | self.widget.close() |
---|
[06b0138] | 43 | del self.widget |
---|
[811bec1] | 44 | |
---|
| 45 | def testDefaults(self): |
---|
| 46 | """Test the GUI in its default state""" |
---|
[53c771e] | 47 | self.assertIsInstance(self.widget, QtWidgets.QWidget) |
---|
[811bec1] | 48 | self.assertEqual(self.widget.windowTitle(), "Fitting") |
---|
[53c771e] | 49 | self.assertEqual(self.widget.sizePolicy().Policy(), QtWidgets.QSizePolicy.Fixed) |
---|
[811bec1] | 50 | self.assertIsInstance(self.widget.lstParams.model(), QtGui.QStandardItemModel) |
---|
| 51 | self.assertIsInstance(self.widget.lstPoly.model(), QtGui.QStandardItemModel) |
---|
| 52 | self.assertIsInstance(self.widget.lstMagnetic.model(), QtGui.QStandardItemModel) |
---|
| 53 | self.assertFalse(self.widget.cbModel.isEnabled()) |
---|
| 54 | self.assertFalse(self.widget.cbStructureFactor.isEnabled()) |
---|
| 55 | self.assertFalse(self.widget.cmdFit.isEnabled()) |
---|
| 56 | self.assertTrue(self.widget.acceptsData()) |
---|
| 57 | self.assertFalse(self.widget.data_is_loaded) |
---|
| 58 | |
---|
[2add354] | 59 | def testSelectCategoryDefault(self): |
---|
[811bec1] | 60 | """ |
---|
| 61 | Test if model categories have been loaded properly |
---|
| 62 | """ |
---|
| 63 | fittingWindow = self.widget |
---|
| 64 | |
---|
| 65 | #Test loading from json categories |
---|
[7fb471d] | 66 | category_list = list(fittingWindow.master_category_dict.keys()) |
---|
[811bec1] | 67 | |
---|
| 68 | for category in category_list: |
---|
| 69 | self.assertNotEqual(fittingWindow.cbCategory.findText(category),-1) |
---|
| 70 | |
---|
| 71 | #Test what is current text in the combobox |
---|
| 72 | self.assertEqual(fittingWindow.cbCategory.currentText(), CATEGORY_DEFAULT) |
---|
| 73 | |
---|
| 74 | def testWidgetWithData(self): |
---|
| 75 | """ |
---|
| 76 | Test the instantiation of the widget with initial data |
---|
| 77 | """ |
---|
| 78 | data = Data1D(x=[1,2], y=[1,2]) |
---|
| 79 | GuiUtils.dataFromItem = MagicMock(return_value=data) |
---|
| 80 | item = QtGui.QStandardItem("test") |
---|
| 81 | |
---|
[1bc27f1] | 82 | widget_with_data = FittingWidget(dummy_manager(), data=item, tab_id=3) |
---|
[811bec1] | 83 | |
---|
| 84 | self.assertEqual(widget_with_data.data, data) |
---|
| 85 | self.assertTrue(widget_with_data.data_is_loaded) |
---|
[7248d75d] | 86 | # self.assertTrue(widget_with_data.cmdFit.isEnabled()) |
---|
[811bec1] | 87 | self.assertFalse(widget_with_data.acceptsData()) |
---|
| 88 | |
---|
| 89 | def testSelectPolydispersity(self): |
---|
| 90 | """ |
---|
| 91 | Test if models have been loaded properly |
---|
| 92 | """ |
---|
| 93 | fittingWindow = self.widget |
---|
| 94 | |
---|
[53c771e] | 95 | self.assertIsInstance(fittingWindow.lstPoly.itemDelegate(), QtWidgets.QStyledItemDelegate) |
---|
[811bec1] | 96 | #Test loading from json categories |
---|
[7248d75d] | 97 | fittingWindow.SASModelToQModel("cylinder") |
---|
[811bec1] | 98 | pd_index = fittingWindow.lstPoly.model().index(0,0) |
---|
[7fb471d] | 99 | self.assertEqual(str(pd_index.data()), "Distribution of radius") |
---|
[811bec1] | 100 | pd_index = fittingWindow.lstPoly.model().index(1,0) |
---|
[7fb471d] | 101 | self.assertEqual(str(pd_index.data()), "Distribution of length") |
---|
[811bec1] | 102 | |
---|
[06b0138] | 103 | # test the delegate a bit |
---|
| 104 | delegate = fittingWindow.lstPoly.itemDelegate() |
---|
[8222f171] | 105 | self.assertEqual(len(delegate.POLYDISPERSE_FUNCTIONS), 5) |
---|
[9ea43c82] | 106 | self.assertEqual(delegate.editableParameters(), [1, 2, 3, 4, 5]) |
---|
[8eaa101] | 107 | self.assertEqual(delegate.poly_function, 6) |
---|
[06b0138] | 108 | self.assertIsInstance(delegate.combo_updated, QtCore.pyqtBoundSignal) |
---|
| 109 | |
---|
[e00b76e] | 110 | def testSelectMagnetism(self): |
---|
| 111 | """ |
---|
| 112 | Test if models have been loaded properly |
---|
| 113 | """ |
---|
| 114 | fittingWindow = self.widget |
---|
| 115 | |
---|
[676f137] | 116 | self.assertIsInstance(fittingWindow.lstMagnetic.itemDelegate(), QtWidgets.QStyledItemDelegate) |
---|
[e00b76e] | 117 | #Test loading from json categories |
---|
| 118 | fittingWindow.SASModelToQModel("cylinder") |
---|
| 119 | mag_index = fittingWindow.lstMagnetic.model().index(0,0) |
---|
[676f137] | 120 | self.assertEqual(mag_index.data(), "up:frac_i") |
---|
[e00b76e] | 121 | mag_index = fittingWindow.lstMagnetic.model().index(1,0) |
---|
[676f137] | 122 | self.assertEqual(mag_index.data(), "up:frac_f") |
---|
[e00b76e] | 123 | mag_index = fittingWindow.lstMagnetic.model().index(2,0) |
---|
[676f137] | 124 | self.assertEqual(mag_index.data(), "up:angle") |
---|
[e00b76e] | 125 | mag_index = fittingWindow.lstMagnetic.model().index(3,0) |
---|
[676f137] | 126 | self.assertEqual(mag_index.data(), "M0:sld") |
---|
[e00b76e] | 127 | mag_index = fittingWindow.lstMagnetic.model().index(4,0) |
---|
[676f137] | 128 | self.assertEqual(mag_index.data(), "mtheta:sld") |
---|
[e00b76e] | 129 | mag_index = fittingWindow.lstMagnetic.model().index(5,0) |
---|
[676f137] | 130 | self.assertEqual(mag_index.data(), "mphi:sld") |
---|
[e00b76e] | 131 | mag_index = fittingWindow.lstMagnetic.model().index(6,0) |
---|
[676f137] | 132 | self.assertEqual(mag_index.data(), "M0:sld_solvent") |
---|
[e00b76e] | 133 | mag_index = fittingWindow.lstMagnetic.model().index(7,0) |
---|
[676f137] | 134 | self.assertEqual(mag_index.data(), "mtheta:sld_solvent") |
---|
[e00b76e] | 135 | mag_index = fittingWindow.lstMagnetic.model().index(8,0) |
---|
[676f137] | 136 | self.assertEqual(mag_index.data(), "mphi:sld_solvent") |
---|
[e00b76e] | 137 | |
---|
| 138 | # test the delegate a bit |
---|
| 139 | delegate = fittingWindow.lstMagnetic.itemDelegate() |
---|
| 140 | self.assertEqual(delegate.editableParameters(), [1, 2, 3]) |
---|
| 141 | |
---|
[811bec1] | 142 | def testSelectStructureFactor(self): |
---|
| 143 | """ |
---|
| 144 | Test if structure factors have been loaded properly |
---|
| 145 | """ |
---|
| 146 | fittingWindow = self.widget |
---|
| 147 | |
---|
| 148 | #Test for existence in combobox |
---|
| 149 | self.assertNotEqual(fittingWindow.cbStructureFactor.findText("stickyhardsphere"),-1) |
---|
| 150 | self.assertNotEqual(fittingWindow.cbStructureFactor.findText("hayter_msa"),-1) |
---|
| 151 | self.assertNotEqual(fittingWindow.cbStructureFactor.findText("squarewell"),-1) |
---|
| 152 | self.assertNotEqual(fittingWindow.cbStructureFactor.findText("hardsphere"),-1) |
---|
| 153 | |
---|
| 154 | #Test what is current text in the combobox |
---|
| 155 | self.assertTrue(fittingWindow.cbCategory.currentText(), "None") |
---|
| 156 | |
---|
| 157 | def testSignals(self): |
---|
| 158 | """ |
---|
[351b53e] | 159 | Test the widget emitted signals |
---|
[811bec1] | 160 | """ |
---|
| 161 | pass |
---|
| 162 | |
---|
[2add354] | 163 | def testSelectCategory(self): |
---|
[811bec1] | 164 | """ |
---|
| 165 | Assure proper behaviour on changing category |
---|
| 166 | """ |
---|
[351b53e] | 167 | self.widget.show() |
---|
| 168 | self.assertEqual(self.widget._previous_category_index, 0) |
---|
| 169 | # confirm the model combo contains no models |
---|
| 170 | self.assertEqual(self.widget.cbModel.count(), 0) |
---|
| 171 | |
---|
| 172 | # invoke the method by changing the index |
---|
[9934e48] | 173 | category_index = self.widget.cbCategory.findText("Shape Independent") |
---|
[b1e36a3] | 174 | self.widget.cbCategory.setCurrentIndex(category_index) |
---|
[351b53e] | 175 | |
---|
| 176 | # test the model combo content |
---|
| 177 | self.assertEqual(self.widget.cbModel.count(), 29) |
---|
| 178 | |
---|
| 179 | # Try to change back to default |
---|
| 180 | self.widget.cbCategory.setCurrentIndex(0) |
---|
| 181 | |
---|
| 182 | # Observe no such luck |
---|
[9934e48] | 183 | self.assertEqual(self.widget.cbCategory.currentIndex(), 6) |
---|
[351b53e] | 184 | self.assertEqual(self.widget.cbModel.count(), 29) |
---|
| 185 | |
---|
| 186 | # Set the structure factor |
---|
| 187 | structure_index=self.widget.cbCategory.findText(CATEGORY_STRUCTURE) |
---|
| 188 | self.widget.cbCategory.setCurrentIndex(structure_index) |
---|
| 189 | # check the enablement of controls |
---|
| 190 | self.assertFalse(self.widget.cbModel.isEnabled()) |
---|
| 191 | self.assertTrue(self.widget.cbStructureFactor.isEnabled()) |
---|
[811bec1] | 192 | |
---|
| 193 | def testSelectModel(self): |
---|
| 194 | """ |
---|
| 195 | Assure proper behaviour on changing model |
---|
| 196 | """ |
---|
[351b53e] | 197 | self.widget.show() |
---|
| 198 | # Change the category index so we have some models |
---|
[9934e48] | 199 | category_index = self.widget.cbCategory.findText("Shape Independent") |
---|
[b1e36a3] | 200 | self.widget.cbCategory.setCurrentIndex(category_index) |
---|
[351b53e] | 201 | |
---|
| 202 | # check the enablement of controls |
---|
| 203 | self.assertTrue(self.widget.cbModel.isEnabled()) |
---|
| 204 | self.assertFalse(self.widget.cbStructureFactor.isEnabled()) |
---|
| 205 | |
---|
| 206 | # set up the model update spy |
---|
| 207 | # spy = QtSignalSpy(self.widget._model_model, self.widget._model_model.itemChanged) |
---|
| 208 | |
---|
| 209 | # mock the tested methods |
---|
| 210 | self.widget.SASModelToQModel = MagicMock() |
---|
| 211 | self.widget.createDefaultDataset = MagicMock() |
---|
[b1e36a3] | 212 | self.widget.calculateQGridForModel = MagicMock() |
---|
[351b53e] | 213 | # |
---|
| 214 | # Now change the model |
---|
| 215 | self.widget.cbModel.setCurrentIndex(3) |
---|
[9934e48] | 216 | self.assertEqual(self.widget.cbModel.currentText(),'dab') |
---|
[351b53e] | 217 | |
---|
| 218 | # No data sent -> no index set, only createDefaultDataset called |
---|
| 219 | self.assertTrue(self.widget.createDefaultDataset.called) |
---|
| 220 | self.assertTrue(self.widget.SASModelToQModel.called) |
---|
[b1e36a3] | 221 | self.assertFalse(self.widget.calculateQGridForModel.called) |
---|
[351b53e] | 222 | |
---|
[6fd4e36] | 223 | # Let's tell the widget that data has been loaded |
---|
| 224 | self.widget.data_is_loaded = True |
---|
[351b53e] | 225 | # Reset the sasmodel index |
---|
| 226 | self.widget.cbModel.setCurrentIndex(1) |
---|
[9934e48] | 227 | self.assertEqual(self.widget.cbModel.currentText(),'broad_peak') |
---|
[351b53e] | 228 | |
---|
[b1e36a3] | 229 | # Observe calculateQGridForModel called |
---|
| 230 | self.assertTrue(self.widget.calculateQGridForModel.called) |
---|
[811bec1] | 231 | |
---|
| 232 | def testSelectFactor(self): |
---|
| 233 | """ |
---|
| 234 | Assure proper behaviour on changing structure factor |
---|
| 235 | """ |
---|
[351b53e] | 236 | self.widget.show() |
---|
| 237 | # Change the category index so we have some models |
---|
[9934e48] | 238 | category_index = self.widget.cbCategory.findText("Shape Independent") |
---|
[b1e36a3] | 239 | self.widget.cbCategory.setCurrentIndex(category_index) |
---|
[351b53e] | 240 | # Change the model to one that supports structure factors |
---|
| 241 | model_index = self.widget.cbModel.findText('fractal_core_shell') |
---|
| 242 | self.widget.cbModel.setCurrentIndex(model_index) |
---|
| 243 | |
---|
| 244 | # Check that the factor combo is active and the default is chosen |
---|
| 245 | self.assertTrue(self.widget.cbStructureFactor.isEnabled()) |
---|
| 246 | self.assertEqual(self.widget.cbStructureFactor.currentText(), STRUCTURE_DEFAULT) |
---|
| 247 | |
---|
| 248 | # We have this many rows in the model |
---|
| 249 | rowcount = self.widget._model_model.rowCount() |
---|
| 250 | #self.assertEqual(self.widget._model_model.rowCount(), 8) |
---|
| 251 | |
---|
| 252 | # Change structure factor to something more exciting |
---|
| 253 | structure_index = self.widget.cbStructureFactor.findText('squarewell') |
---|
| 254 | self.widget.cbStructureFactor.setCurrentIndex(structure_index) |
---|
| 255 | |
---|
| 256 | # We have 4 more rows now |
---|
| 257 | self.assertEqual(self.widget._model_model.rowCount(), rowcount+4) |
---|
| 258 | |
---|
| 259 | # Switch models |
---|
| 260 | self.widget.cbModel.setCurrentIndex(0) |
---|
| 261 | |
---|
| 262 | # Observe factor reset to None |
---|
| 263 | self.assertEqual(self.widget.cbStructureFactor.currentText(), STRUCTURE_DEFAULT) |
---|
| 264 | |
---|
[fd1ae6d1] | 265 | # Switch category to structure factor |
---|
| 266 | structure_index=self.widget.cbCategory.findText(CATEGORY_STRUCTURE) |
---|
| 267 | self.widget.cbCategory.setCurrentIndex(structure_index) |
---|
| 268 | # Observe the correct enablement |
---|
| 269 | self.assertTrue(self.widget.cbStructureFactor.isEnabled()) |
---|
| 270 | self.assertFalse(self.widget.cbModel.isEnabled()) |
---|
| 271 | self.assertEqual(self.widget._model_model.rowCount(), 0) |
---|
| 272 | |
---|
| 273 | # Choose the last factor |
---|
| 274 | last_index = self.widget.cbStructureFactor.count() |
---|
| 275 | self.widget.cbStructureFactor.setCurrentIndex(last_index-1) |
---|
| 276 | # Do we have all the rows? |
---|
| 277 | self.assertEqual(self.widget._model_model.rowCount(), 4) |
---|
[351b53e] | 278 | |
---|
[fd1ae6d1] | 279 | # Are the command buttons properly enabled? |
---|
| 280 | self.assertTrue(self.widget.cmdPlot.isEnabled()) |
---|
| 281 | self.assertFalse(self.widget.cmdFit.isEnabled()) |
---|
[811bec1] | 282 | |
---|
| 283 | def testReadCategoryInfo(self): |
---|
| 284 | """ |
---|
| 285 | Check the category file reader |
---|
| 286 | """ |
---|
[351b53e] | 287 | # Tested in default checks |
---|
[811bec1] | 288 | pass |
---|
| 289 | |
---|
| 290 | def testUpdateParamsFromModel(self): |
---|
| 291 | """ |
---|
| 292 | Checks the sasmodel parameter update from QModel items |
---|
| 293 | """ |
---|
[351b53e] | 294 | # Tested in default checks |
---|
[811bec1] | 295 | pass |
---|
| 296 | |
---|
[d48cc19] | 297 | def testCreateTheoryIndex(self): |
---|
[811bec1] | 298 | """ |
---|
[351b53e] | 299 | Test the data->QIndex conversion |
---|
[811bec1] | 300 | """ |
---|
[351b53e] | 301 | # set up the model update spy |
---|
| 302 | spy = QtSignalSpy(self.widget._model_model, self.widget.communicate.updateTheoryFromPerspectiveSignal) |
---|
[811bec1] | 303 | |
---|
[351b53e] | 304 | self.widget.show() |
---|
| 305 | # Change the category index so we have some models |
---|
| 306 | self.widget.cbCategory.setCurrentIndex(1) |
---|
[811bec1] | 307 | |
---|
[351b53e] | 308 | # Create the index |
---|
[d48cc19] | 309 | self.widget.createTheoryIndex(Data1D(x=[1,2], y=[1,2])) |
---|
[811bec1] | 310 | |
---|
[d48cc19] | 311 | # Make sure the signal has been emitted |
---|
| 312 | self.assertEqual(spy.count(), 1) |
---|
[811bec1] | 313 | |
---|
[d48cc19] | 314 | # Check the argument type |
---|
| 315 | self.assertIsInstance(spy.called()[0]['args'][0], QtGui.QStandardItem) |
---|
[811bec1] | 316 | |
---|
[b1e36a3] | 317 | def testCalculateQGridForModel(self): |
---|
[811bec1] | 318 | """ |
---|
[351b53e] | 319 | Check that the fitting 1D data object is ready |
---|
[811bec1] | 320 | """ |
---|
[351b53e] | 321 | # Mock the thread creation |
---|
| 322 | threads.deferToThread = MagicMock() |
---|
[d48cc19] | 323 | # Model for theory |
---|
| 324 | self.widget.SASModelToQModel("cylinder") |
---|
[351b53e] | 325 | # Call the tested method |
---|
[b1e36a3] | 326 | self.widget.calculateQGridForModel() |
---|
[d48cc19] | 327 | time.sleep(1) |
---|
[351b53e] | 328 | # Test the mock |
---|
| 329 | self.assertTrue(threads.deferToThread.called) |
---|
| 330 | self.assertEqual(threads.deferToThread.call_args_list[0][0][0].__name__, "compute") |
---|
[811bec1] | 331 | |
---|
[d48cc19] | 332 | def testCalculateResiduals(self): |
---|
[811bec1] | 333 | """ |
---|
[d48cc19] | 334 | Check that the residuals are calculated and plots updated |
---|
[811bec1] | 335 | """ |
---|
[d48cc19] | 336 | test_data = Data1D(x=[1,2], y=[1,2]) |
---|
[811bec1] | 337 | |
---|
[d48cc19] | 338 | # Model for theory |
---|
| 339 | self.widget.SASModelToQModel("cylinder") |
---|
| 340 | # Invoke the tested method |
---|
| 341 | self.widget.calculateResiduals(test_data) |
---|
| 342 | # Check the Chi2 value - should be undetermined |
---|
| 343 | self.assertEqual(self.widget.lblChi2Value.text(), '---') |
---|
| 344 | |
---|
| 345 | # Force same data into logic |
---|
| 346 | self.widget.logic.data = test_data |
---|
| 347 | self.widget.calculateResiduals(test_data) |
---|
| 348 | # Now, the difference is 0, as data is the same |
---|
| 349 | self.assertEqual(self.widget.lblChi2Value.text(), '0') |
---|
| 350 | |
---|
| 351 | # Change data |
---|
| 352 | test_data_2 = Data1D(x=[1,2], y=[2.1,3.49]) |
---|
| 353 | self.widget.logic.data = test_data_2 |
---|
| 354 | self.widget.calculateResiduals(test_data) |
---|
| 355 | # Now, the difference is non-zero |
---|
[672b8ab] | 356 | self.assertEqual(float(self.widget.lblChi2Value.text()), 1.7151) |
---|
[811bec1] | 357 | |
---|
| 358 | def testSetPolyModel(self): |
---|
| 359 | """ |
---|
| 360 | Test the polydispersity model setup |
---|
| 361 | """ |
---|
[351b53e] | 362 | self.widget.show() |
---|
| 363 | # Change the category index so we have a model with no poly |
---|
[9934e48] | 364 | category_index = self.widget.cbCategory.findText("Shape Independent") |
---|
[b1e36a3] | 365 | self.widget.cbCategory.setCurrentIndex(category_index) |
---|
[351b53e] | 366 | # Check the poly model |
---|
| 367 | self.assertEqual(self.widget._poly_model.rowCount(), 0) |
---|
| 368 | self.assertEqual(self.widget._poly_model.columnCount(), 0) |
---|
| 369 | |
---|
| 370 | # Change the category index so we have a model available |
---|
| 371 | self.widget.cbCategory.setCurrentIndex(2) |
---|
| 372 | |
---|
| 373 | # Check the poly model |
---|
[9934e48] | 374 | self.assertEqual(self.widget._poly_model.rowCount(), 4) |
---|
[8222f171] | 375 | self.assertEqual(self.widget._poly_model.columnCount(), 8) |
---|
[351b53e] | 376 | |
---|
| 377 | # Test the header |
---|
[8222f171] | 378 | self.assertEqual(self.widget.lstPoly.horizontalHeader().count(), 8) |
---|
[351b53e] | 379 | self.assertFalse(self.widget.lstPoly.horizontalHeader().stretchLastSection()) |
---|
| 380 | |
---|
[c7358b2] | 381 | # Test tooltips |
---|
| 382 | self.assertEqual(len(self.widget._poly_model.header_tooltips), 8) |
---|
| 383 | |
---|
| 384 | header_tooltips = ['Select parameter for fitting', |
---|
| 385 | 'Enter polydispersity ratio (STD/mean). ' |
---|
| 386 | 'STD: standard deviation from the mean value', |
---|
| 387 | 'Enter minimum value for parameter', |
---|
| 388 | 'Enter maximum value for parameter', |
---|
| 389 | 'Enter number of points for parameter', |
---|
| 390 | 'Enter number of sigmas parameter', |
---|
| 391 | 'Select distribution function', |
---|
| 392 | 'Select filename with user-definable distribution'] |
---|
| 393 | for column, tooltip in enumerate(header_tooltips): |
---|
| 394 | self.assertEqual(self.widget._poly_model.headerData( column, |
---|
| 395 | QtCore.Qt.Horizontal, QtCore.Qt.ToolTipRole), |
---|
| 396 | header_tooltips[column]) |
---|
| 397 | |
---|
[351b53e] | 398 | # Test presence of comboboxes in last column |
---|
[7fb471d] | 399 | for row in range(self.widget._poly_model.rowCount()): |
---|
[351b53e] | 400 | func_index = self.widget._poly_model.index(row, 6) |
---|
[53c771e] | 401 | self.assertTrue(isinstance(self.widget.lstPoly.indexWidget(func_index), QtWidgets.QComboBox)) |
---|
[351b53e] | 402 | self.assertIn('Distribution of', self.widget._poly_model.item(row, 0).text()) |
---|
[06b0138] | 403 | #self.widget.close() |
---|
| 404 | |
---|
| 405 | def testPolyModelChange(self): |
---|
| 406 | """ |
---|
| 407 | Polydispersity model changed - test all possible scenarios |
---|
| 408 | """ |
---|
| 409 | self.widget.show() |
---|
| 410 | # Change the category index so we have a model with polydisp |
---|
| 411 | category_index = self.widget.cbCategory.findText("Cylinder") |
---|
| 412 | self.widget.cbCategory.setCurrentIndex(category_index) |
---|
| 413 | |
---|
| 414 | # click on a poly parameter checkbox |
---|
| 415 | index = self.widget._poly_model.index(0,0) |
---|
| 416 | # Set the checbox |
---|
| 417 | self.widget._poly_model.item(0,0).setCheckState(2) |
---|
| 418 | # Assure the parameter is added |
---|
| 419 | self.assertEqual(self.widget.parameters_to_fit, ['radius_bell.width']) |
---|
| 420 | |
---|
| 421 | # Add another parameter |
---|
| 422 | self.widget._poly_model.item(2,0).setCheckState(2) |
---|
| 423 | # Assure the parameters are added |
---|
| 424 | self.assertEqual(self.widget.parameters_to_fit, ['radius_bell.width', 'length.width']) |
---|
| 425 | |
---|
| 426 | # Change the min/max values |
---|
| 427 | self.assertEqual(self.widget.kernel_module.details['radius_bell'][1], 0.0) |
---|
| 428 | self.widget._poly_model.item(0,2).setText("1.0") |
---|
| 429 | self.assertEqual(self.widget.kernel_module.details['radius_bell'][1], 1.0) |
---|
| 430 | |
---|
| 431 | # Change the number of points |
---|
| 432 | self.assertEqual(self.widget.kernel_module.getParam('radius_bell.npts'), 35) |
---|
| 433 | self.widget._poly_model.item(0,4).setText("22") |
---|
| 434 | self.assertEqual(self.widget.kernel_module.getParam('radius_bell.npts'), 22) |
---|
| 435 | # try something stupid |
---|
| 436 | self.widget._poly_model.item(0,4).setText("butt") |
---|
| 437 | # see that this didn't annoy the control at all |
---|
| 438 | self.assertEqual(self.widget.kernel_module.getParam('radius_bell.npts'), 22) |
---|
| 439 | |
---|
| 440 | # Change the number of sigmas |
---|
| 441 | self.assertEqual(self.widget.kernel_module.getParam('radius_bell.nsigmas'), 3) |
---|
| 442 | self.widget._poly_model.item(0,5).setText("222") |
---|
| 443 | self.assertEqual(self.widget.kernel_module.getParam('radius_bell.nsigmas'), 222) |
---|
| 444 | # try something stupid again |
---|
| 445 | self.widget._poly_model.item(0,4).setText("beer") |
---|
| 446 | # no efect |
---|
| 447 | self.assertEqual(self.widget.kernel_module.getParam('radius_bell.nsigmas'), 222) |
---|
| 448 | |
---|
| 449 | def testOnPolyComboIndexChange(self): |
---|
| 450 | """ |
---|
| 451 | Test the slot method for polydisp. combo box index change |
---|
| 452 | """ |
---|
| 453 | self.widget.show() |
---|
| 454 | # Change the category index so we have a model with polydisp |
---|
| 455 | category_index = self.widget.cbCategory.findText("Cylinder") |
---|
| 456 | self.widget.cbCategory.setCurrentIndex(category_index) |
---|
| 457 | |
---|
| 458 | # call method with default settings |
---|
| 459 | self.widget.onPolyComboIndexChange('gaussian', 0) |
---|
| 460 | # check values |
---|
| 461 | self.assertEqual(self.widget.kernel_module.getParam('radius_bell.npts'), 35) |
---|
| 462 | self.assertEqual(self.widget.kernel_module.getParam('radius_bell.nsigmas'), 3) |
---|
| 463 | # Change the index |
---|
| 464 | self.widget.onPolyComboIndexChange('rectangle', 0) |
---|
| 465 | # check values |
---|
| 466 | self.assertEqual(self.widget.kernel_module.getParam('radius_bell.npts'), 35) |
---|
[53c771e] | 467 | self.assertAlmostEqual(self.widget.kernel_module.getParam('radius_bell.nsigmas'), 1.70325, 5) |
---|
[06b0138] | 468 | # Change the index |
---|
| 469 | self.widget.onPolyComboIndexChange('lognormal', 0) |
---|
| 470 | # check values |
---|
| 471 | self.assertEqual(self.widget.kernel_module.getParam('radius_bell.npts'), 80) |
---|
| 472 | self.assertEqual(self.widget.kernel_module.getParam('radius_bell.nsigmas'), 8) |
---|
| 473 | # Change the index |
---|
| 474 | self.widget.onPolyComboIndexChange('schulz', 0) |
---|
| 475 | # check values |
---|
| 476 | self.assertEqual(self.widget.kernel_module.getParam('radius_bell.npts'), 80) |
---|
| 477 | self.assertEqual(self.widget.kernel_module.getParam('radius_bell.nsigmas'), 8) |
---|
| 478 | |
---|
| 479 | # mock up file load |
---|
| 480 | self.widget.loadPolydispArray = MagicMock() |
---|
| 481 | # Change to 'array' |
---|
| 482 | self.widget.onPolyComboIndexChange('array', 0) |
---|
| 483 | # See the mock fire |
---|
| 484 | self.assertTrue(self.widget.loadPolydispArray.called) |
---|
| 485 | |
---|
| 486 | def testLoadPolydispArray(self): |
---|
| 487 | """ |
---|
| 488 | Test opening of the load file dialog for 'array' polydisp. function |
---|
| 489 | """ |
---|
| 490 | filename = os.path.join("UnitTesting", "testdata_noexist.txt") |
---|
[53c771e] | 491 | QtWidgets.QFileDialog.getOpenFileName = MagicMock(return_value=(filename,'')) |
---|
[06b0138] | 492 | self.widget.show() |
---|
| 493 | # Change the category index so we have a model with polydisp |
---|
| 494 | category_index = self.widget.cbCategory.findText("Cylinder") |
---|
| 495 | self.widget.cbCategory.setCurrentIndex(category_index) |
---|
| 496 | |
---|
| 497 | self.widget.onPolyComboIndexChange('array', 0) |
---|
| 498 | # check values - unchanged since the file doesn't exist |
---|
| 499 | self.assertTrue(self.widget._poly_model.item(0, 1).isEnabled()) |
---|
| 500 | with self.assertRaises(AttributeError): |
---|
| 501 | self.widget.disp_model() |
---|
| 502 | |
---|
| 503 | # good file |
---|
| 504 | filename = os.path.join("UnitTesting", "testdata.txt") |
---|
[53c771e] | 505 | QtWidgets.QFileDialog.getOpenFileName = MagicMock(return_value=(filename,'')) |
---|
[06b0138] | 506 | |
---|
| 507 | self.widget.onPolyComboIndexChange('array', 0) |
---|
| 508 | # check values - disabled control, present weights |
---|
| 509 | self.assertFalse(self.widget._poly_model.item(0, 1).isEnabled()) |
---|
| 510 | self.assertEqual(self.widget.disp_model.weights[0], 2.83954) |
---|
| 511 | self.assertEqual(len(self.widget.disp_model.weights), 19) |
---|
| 512 | self.assertEqual(len(self.widget.disp_model.values), 19) |
---|
| 513 | self.assertEqual(self.widget.disp_model.values[0], 0.0) |
---|
| 514 | self.assertEqual(self.widget.disp_model.values[18], 3.67347) |
---|
[811bec1] | 515 | |
---|
| 516 | def testSetMagneticModel(self): |
---|
| 517 | """ |
---|
| 518 | Test the magnetic model setup |
---|
| 519 | """ |
---|
[351b53e] | 520 | self.widget.show() |
---|
| 521 | # Change the category index so we have a model available |
---|
[9934e48] | 522 | category_index = self.widget.cbCategory.findText("Sphere") |
---|
[b1e36a3] | 523 | self.widget.cbCategory.setCurrentIndex(category_index) |
---|
[351b53e] | 524 | |
---|
| 525 | # Check the magnetic model |
---|
| 526 | self.assertEqual(self.widget._magnet_model.rowCount(), 9) |
---|
| 527 | self.assertEqual(self.widget._magnet_model.columnCount(), 5) |
---|
| 528 | |
---|
| 529 | # Test the header |
---|
| 530 | self.assertEqual(self.widget.lstMagnetic.horizontalHeader().count(), 5) |
---|
| 531 | self.assertFalse(self.widget.lstMagnetic.horizontalHeader().stretchLastSection()) |
---|
| 532 | |
---|
[c7358b2] | 533 | #Test tooltips |
---|
| 534 | self.assertEqual(len(self.widget._magnet_model.header_tooltips), 5) |
---|
| 535 | |
---|
| 536 | header_tooltips = ['Select parameter for fitting', |
---|
| 537 | 'Enter parameter value', |
---|
| 538 | 'Enter minimum value for parameter', |
---|
| 539 | 'Enter maximum value for parameter', |
---|
| 540 | 'Unit of the parameter'] |
---|
| 541 | for column, tooltip in enumerate(header_tooltips): |
---|
| 542 | self.assertEqual(self.widget._magnet_model.headerData(column, |
---|
| 543 | QtCore.Qt.Horizontal, QtCore.Qt.ToolTipRole), |
---|
| 544 | header_tooltips[column]) |
---|
| 545 | |
---|
[351b53e] | 546 | # Test rows |
---|
[7fb471d] | 547 | for row in range(self.widget._magnet_model.rowCount()): |
---|
[351b53e] | 548 | func_index = self.widget._magnet_model.index(row, 0) |
---|
| 549 | self.assertIn(':', self.widget._magnet_model.item(row, 0).text()) |
---|
| 550 | |
---|
[811bec1] | 551 | |
---|
| 552 | def testAddExtraShells(self): |
---|
| 553 | """ |
---|
| 554 | Test how the extra shells are presented |
---|
| 555 | """ |
---|
| 556 | pass |
---|
| 557 | |
---|
| 558 | def testModifyShellsInList(self): |
---|
| 559 | """ |
---|
| 560 | Test the additional rows added by modifying the shells combobox |
---|
| 561 | """ |
---|
[351b53e] | 562 | self.widget.show() |
---|
| 563 | # Change the model to multi shell |
---|
[9934e48] | 564 | category_index = self.widget.cbCategory.findText("Sphere") |
---|
[b1e36a3] | 565 | self.widget.cbCategory.setCurrentIndex(category_index) |
---|
| 566 | model_index = self.widget.cbModel.findText("core_multi_shell") |
---|
| 567 | self.widget.cbModel.setCurrentIndex(model_index) |
---|
[351b53e] | 568 | |
---|
| 569 | # Assure we have the combobox available |
---|
| 570 | last_row = self.widget._last_model_row |
---|
| 571 | func_index = self.widget._model_model.index(last_row-1, 1) |
---|
[53c771e] | 572 | self.assertIsInstance(self.widget.lstParams.indexWidget(func_index), QtWidgets.QComboBox) |
---|
[351b53e] | 573 | |
---|
| 574 | # Change the combo box index |
---|
| 575 | self.widget.lstParams.indexWidget(func_index).setCurrentIndex(3) |
---|
| 576 | |
---|
| 577 | # Check that the number of rows increased |
---|
| 578 | more_rows = self.widget._model_model.rowCount() - last_row |
---|
| 579 | self.assertEqual(more_rows, 6) # 6 new rows: 2 params per index |
---|
| 580 | |
---|
| 581 | # Back to 0 |
---|
| 582 | self.widget.lstParams.indexWidget(func_index).setCurrentIndex(0) |
---|
| 583 | self.assertEqual(self.widget._model_model.rowCount(), last_row) |
---|
[811bec1] | 584 | |
---|
[d48cc19] | 585 | def testPlotTheory(self): |
---|
| 586 | """ |
---|
| 587 | See that theory item can produce a chart |
---|
| 588 | """ |
---|
| 589 | # By default, the compute/plot button is disabled |
---|
| 590 | self.assertFalse(self.widget.cmdPlot.isEnabled()) |
---|
| 591 | self.assertEqual(self.widget.cmdPlot.text(), 'Show Plot') |
---|
| 592 | |
---|
| 593 | # Assign a model |
---|
| 594 | self.widget.show() |
---|
| 595 | # Change the category index so we have a model available |
---|
| 596 | category_index = self.widget.cbCategory.findText("Sphere") |
---|
| 597 | self.widget.cbCategory.setCurrentIndex(category_index) |
---|
| 598 | |
---|
| 599 | # Check the enablement/text |
---|
| 600 | self.assertTrue(self.widget.cmdPlot.isEnabled()) |
---|
| 601 | self.assertEqual(self.widget.cmdPlot.text(), 'Calculate') |
---|
| 602 | |
---|
| 603 | # Spying on plot update signal |
---|
| 604 | spy = QtSignalSpy(self.widget, self.widget.communicate.plotRequestedSignal) |
---|
| 605 | |
---|
| 606 | # Press Calculate |
---|
| 607 | QtTest.QTest.mouseClick(self.widget.cmdPlot, QtCore.Qt.LeftButton) |
---|
| 608 | |
---|
| 609 | # Observe cmdPlot caption change |
---|
| 610 | self.assertEqual(self.widget.cmdPlot.text(), 'Show Plot') |
---|
| 611 | |
---|
| 612 | # Make sure the signal has NOT been emitted |
---|
| 613 | self.assertEqual(spy.count(), 0) |
---|
| 614 | |
---|
| 615 | # Click again |
---|
| 616 | QtTest.QTest.mouseClick(self.widget.cmdPlot, QtCore.Qt.LeftButton) |
---|
| 617 | |
---|
| 618 | # This time, we got the update signal |
---|
| 619 | self.assertEqual(spy.count(), 0) |
---|
| 620 | |
---|
| 621 | def testPlotData(self): |
---|
| 622 | """ |
---|
| 623 | See that data item can produce a chart |
---|
| 624 | """ |
---|
| 625 | # By default, the compute/plot button is disabled |
---|
| 626 | self.assertFalse(self.widget.cmdPlot.isEnabled()) |
---|
| 627 | self.assertEqual(self.widget.cmdPlot.text(), 'Show Plot') |
---|
| 628 | |
---|
| 629 | self.widget.show() |
---|
| 630 | |
---|
| 631 | # Set data |
---|
| 632 | test_data = Data1D(x=[1,2], y=[1,2]) |
---|
| 633 | |
---|
| 634 | # Force same data into logic |
---|
| 635 | self.widget.logic.data = test_data |
---|
| 636 | self.widget.data_is_loaded = True |
---|
| 637 | |
---|
| 638 | # Change the category index so we have a model available |
---|
| 639 | category_index = self.widget.cbCategory.findText("Sphere") |
---|
| 640 | self.widget.cbCategory.setCurrentIndex(category_index) |
---|
| 641 | |
---|
| 642 | # Check the enablement/text |
---|
| 643 | self.assertTrue(self.widget.cmdPlot.isEnabled()) |
---|
| 644 | self.assertEqual(self.widget.cmdPlot.text(), 'Show Plot') |
---|
| 645 | |
---|
| 646 | # Spying on plot update signal |
---|
| 647 | spy = QtSignalSpy(self.widget, self.widget.communicate.plotRequestedSignal) |
---|
| 648 | |
---|
| 649 | # Press Calculate |
---|
| 650 | QtTest.QTest.mouseClick(self.widget.cmdPlot, QtCore.Qt.LeftButton) |
---|
| 651 | |
---|
| 652 | # Observe cmdPlot caption did not change |
---|
| 653 | self.assertEqual(self.widget.cmdPlot.text(), 'Show Plot') |
---|
| 654 | |
---|
| 655 | # Make sure the signal has been emitted == new plot |
---|
| 656 | self.assertEqual(spy.count(), 1) |
---|
| 657 | |
---|
[53c771e] | 658 | def testOnEmptyFit(self): |
---|
[02ddfb4] | 659 | """ |
---|
[53c771e] | 660 | Test a 1D/2D fit with no parameters |
---|
[02ddfb4] | 661 | """ |
---|
| 662 | # Set data |
---|
| 663 | test_data = Data1D(x=[1,2], y=[1,2]) |
---|
[377ade1] | 664 | item = QtGui.QStandardItem() |
---|
[63319b0] | 665 | updateModelItem(item, test_data, "test") |
---|
[02ddfb4] | 666 | # Force same data into logic |
---|
[f7d14a1] | 667 | self.widget.data = item |
---|
[02ddfb4] | 668 | category_index = self.widget.cbCategory.findText("Sphere") |
---|
| 669 | self.widget.cbCategory.setCurrentIndex(category_index) |
---|
| 670 | |
---|
| 671 | self.widget.show() |
---|
| 672 | |
---|
| 673 | # Test no fitting params |
---|
| 674 | self.widget.parameters_to_fit = [] |
---|
[2add354] | 675 | |
---|
[53c771e] | 676 | logging.error = MagicMock() |
---|
| 677 | |
---|
| 678 | self.widget.onFit() |
---|
| 679 | self.assertTrue(logging.error.called_with('no fitting parameters')) |
---|
| 680 | self.widget.close() |
---|
| 681 | |
---|
| 682 | test_data = Data2D(image=[1.0, 2.0, 3.0], |
---|
| 683 | err_image=[0.01, 0.02, 0.03], |
---|
| 684 | qx_data=[0.1, 0.2, 0.3], |
---|
| 685 | qy_data=[0.1, 0.2, 0.3], |
---|
| 686 | xmin=0.1, xmax=0.3, ymin=0.1, ymax=0.3, |
---|
| 687 | mask=[True, True, True]) |
---|
| 688 | |
---|
| 689 | # Force same data into logic |
---|
| 690 | item = QtGui.QStandardItem() |
---|
[63319b0] | 691 | updateModelItem(item, test_data, "test") |
---|
[53c771e] | 692 | # Force same data into logic |
---|
| 693 | self.widget.data = item |
---|
| 694 | category_index = self.widget.cbCategory.findText("Sphere") |
---|
| 695 | self.widget.cbCategory.setCurrentIndex(category_index) |
---|
| 696 | |
---|
| 697 | self.widget.show() |
---|
| 698 | |
---|
| 699 | # Test no fitting params |
---|
| 700 | self.widget.parameters_to_fit = [] |
---|
| 701 | |
---|
| 702 | logging.error = MagicMock() |
---|
| 703 | |
---|
| 704 | self.widget.onFit() |
---|
| 705 | self.assertTrue(logging.error.called_once()) |
---|
| 706 | self.assertTrue(logging.error.called_with('no fitting parameters')) |
---|
| 707 | self.widget.close() |
---|
| 708 | |
---|
| 709 | |
---|
| 710 | def testOnFit1D(self): |
---|
| 711 | """ |
---|
| 712 | Test the threaded fitting call |
---|
| 713 | """ |
---|
| 714 | # Set data |
---|
| 715 | test_data = Data1D(x=[1,2], y=[1,2]) |
---|
| 716 | item = QtGui.QStandardItem() |
---|
[63319b0] | 717 | updateModelItem(item, test_data, "test") |
---|
[53c771e] | 718 | # Force same data into logic |
---|
| 719 | self.widget.data = item |
---|
| 720 | category_index = self.widget.cbCategory.findText("Sphere") |
---|
| 721 | self.widget.cbCategory.setCurrentIndex(category_index) |
---|
| 722 | |
---|
| 723 | self.widget.show() |
---|
[2add354] | 724 | |
---|
| 725 | # Assing fitting params |
---|
| 726 | self.widget.parameters_to_fit = ['scale'] |
---|
| 727 | |
---|
| 728 | # Spying on status update signal |
---|
| 729 | update_spy = QtSignalSpy(self.widget, self.widget.communicate.statusBarUpdateSignal) |
---|
| 730 | |
---|
| 731 | with threads.deferToThread as MagicMock: |
---|
| 732 | self.widget.onFit() |
---|
| 733 | # thread called |
---|
| 734 | self.assertTrue(threads.deferToThread.called) |
---|
| 735 | # thread method is 'compute' |
---|
| 736 | self.assertEqual(threads.deferToThread.call_args_list[0][0][0].__name__, 'compute') |
---|
| 737 | |
---|
| 738 | # the fit button changed caption and got disabled |
---|
[377ade1] | 739 | self.assertEqual(self.widget.cmdFit.text(), 'Running...') |
---|
[2add354] | 740 | self.assertFalse(self.widget.cmdFit.isEnabled()) |
---|
| 741 | |
---|
| 742 | # Signal pushed up |
---|
| 743 | self.assertEqual(update_spy.count(), 1) |
---|
| 744 | |
---|
[53c771e] | 745 | self.widget.close() |
---|
| 746 | |
---|
[2add354] | 747 | def testOnFit2D(self): |
---|
| 748 | """ |
---|
| 749 | Test the threaded fitting call |
---|
| 750 | """ |
---|
| 751 | # Set data |
---|
| 752 | test_data = Data2D(image=[1.0, 2.0, 3.0], |
---|
| 753 | err_image=[0.01, 0.02, 0.03], |
---|
| 754 | qx_data=[0.1, 0.2, 0.3], |
---|
| 755 | qy_data=[0.1, 0.2, 0.3], |
---|
| 756 | xmin=0.1, xmax=0.3, ymin=0.1, ymax=0.3, |
---|
| 757 | mask=[True, True, True]) |
---|
| 758 | |
---|
| 759 | # Force same data into logic |
---|
[377ade1] | 760 | item = QtGui.QStandardItem() |
---|
[63319b0] | 761 | updateModelItem(item, test_data, "test") |
---|
[377ade1] | 762 | # Force same data into logic |
---|
[f7d14a1] | 763 | self.widget.data = item |
---|
[2add354] | 764 | category_index = self.widget.cbCategory.findText("Sphere") |
---|
| 765 | self.widget.cbCategory.setCurrentIndex(category_index) |
---|
| 766 | |
---|
| 767 | self.widget.show() |
---|
| 768 | |
---|
[02ddfb4] | 769 | # Assing fitting params |
---|
| 770 | self.widget.parameters_to_fit = ['scale'] |
---|
| 771 | |
---|
| 772 | # Spying on status update signal |
---|
| 773 | update_spy = QtSignalSpy(self.widget, self.widget.communicate.statusBarUpdateSignal) |
---|
| 774 | |
---|
| 775 | with threads.deferToThread as MagicMock: |
---|
| 776 | self.widget.onFit() |
---|
| 777 | # thread called |
---|
| 778 | self.assertTrue(threads.deferToThread.called) |
---|
| 779 | # thread method is 'compute' |
---|
| 780 | self.assertEqual(threads.deferToThread.call_args_list[0][0][0].__name__, 'compute') |
---|
| 781 | |
---|
| 782 | # the fit button changed caption and got disabled |
---|
[377ade1] | 783 | self.assertEqual(self.widget.cmdFit.text(), 'Running...') |
---|
[02ddfb4] | 784 | self.assertFalse(self.widget.cmdFit.isEnabled()) |
---|
| 785 | |
---|
| 786 | # Signal pushed up |
---|
| 787 | self.assertEqual(update_spy.count(), 1) |
---|
| 788 | |
---|
[14ec91c5] | 789 | def testOnHelp(self): |
---|
[70080a0] | 790 | """ |
---|
| 791 | Test various help pages shown in this widget |
---|
| 792 | """ |
---|
[14ec91c5] | 793 | #Mock the webbrowser.open method |
---|
| 794 | self.widget.parent.showHelp = MagicMock() |
---|
| 795 | #webbrowser.open = MagicMock() |
---|
[70080a0] | 796 | |
---|
| 797 | # Invoke the action on default tab |
---|
| 798 | self.widget.onHelp() |
---|
| 799 | # Check if show() got called |
---|
[14ec91c5] | 800 | self.assertTrue(self.widget.parent.showHelp.called) |
---|
[70080a0] | 801 | # Assure the filename is correct |
---|
[14ec91c5] | 802 | self.assertIn("fitting_help.html", self.widget.parent.showHelp.call_args[0][0]) |
---|
[70080a0] | 803 | |
---|
| 804 | # Change the tab to options |
---|
| 805 | self.widget.tabFitting.setCurrentIndex(1) |
---|
| 806 | self.widget.onHelp() |
---|
| 807 | # Check if show() got called |
---|
[14ec91c5] | 808 | self.assertEqual(self.widget.parent.showHelp.call_count, 2) |
---|
[70080a0] | 809 | # Assure the filename is correct |
---|
[14ec91c5] | 810 | self.assertIn("residuals_help.html", self.widget.parent.showHelp.call_args[0][0]) |
---|
[70080a0] | 811 | |
---|
| 812 | # Change the tab to smearing |
---|
| 813 | self.widget.tabFitting.setCurrentIndex(2) |
---|
| 814 | self.widget.onHelp() |
---|
| 815 | # Check if show() got called |
---|
[14ec91c5] | 816 | self.assertEqual(self.widget.parent.showHelp.call_count, 3) |
---|
[70080a0] | 817 | # Assure the filename is correct |
---|
[14ec91c5] | 818 | self.assertIn("resolution.html", self.widget.parent.showHelp.call_args[0][0]) |
---|
[70080a0] | 819 | |
---|
| 820 | # Change the tab to poly |
---|
| 821 | self.widget.tabFitting.setCurrentIndex(3) |
---|
| 822 | self.widget.onHelp() |
---|
| 823 | # Check if show() got called |
---|
[14ec91c5] | 824 | self.assertEqual(self.widget.parent.showHelp.call_count, 4) |
---|
[70080a0] | 825 | # Assure the filename is correct |
---|
[14ec91c5] | 826 | self.assertIn("polydispersity.html", self.widget.parent.showHelp.call_args[0][0]) |
---|
[70080a0] | 827 | |
---|
| 828 | # Change the tab to magnetism |
---|
| 829 | self.widget.tabFitting.setCurrentIndex(4) |
---|
| 830 | self.widget.onHelp() |
---|
| 831 | # Check if show() got called |
---|
[14ec91c5] | 832 | self.assertEqual(self.widget.parent.showHelp.call_count, 5) |
---|
[70080a0] | 833 | # Assure the filename is correct |
---|
[14ec91c5] | 834 | self.assertIn("magnetism.html", self.widget.parent.showHelp.call_args[0][0]) |
---|
[70080a0] | 835 | |
---|
[672b8ab] | 836 | def testReadFitPage(self): |
---|
| 837 | """ |
---|
| 838 | Read in the fitpage object and restore state |
---|
| 839 | """ |
---|
| 840 | # Set data |
---|
| 841 | test_data = Data1D(x=[1,2], y=[1,2]) |
---|
| 842 | |
---|
| 843 | # Force same data into logic |
---|
| 844 | self.widget.logic.data = test_data |
---|
| 845 | self.widget.data_is_loaded = True |
---|
| 846 | category_index = self.widget.cbCategory.findText('Sphere') |
---|
| 847 | self.widget.cbCategory.setCurrentIndex(category_index) |
---|
| 848 | self.widget.parameters_to_fit = ['scale'] |
---|
| 849 | # Invoke the tested method |
---|
| 850 | fp = self.widget.currentState() |
---|
| 851 | |
---|
| 852 | # Prepare modified fit page |
---|
| 853 | fp.current_model = 'onion' |
---|
| 854 | fp.is_polydisperse = True |
---|
| 855 | |
---|
| 856 | # Read in modified state |
---|
| 857 | self.widget.readFitPage(fp) |
---|
| 858 | |
---|
| 859 | # Check if the widget got updated accordingly |
---|
| 860 | self.assertEqual(self.widget.cbModel.currentText(), 'onion') |
---|
| 861 | self.assertTrue(self.widget.chkPolydispersity.isChecked()) |
---|
[4f9226c] | 862 | #Check if polidispersity tab is available |
---|
| 863 | self.assertTrue(self.widget.tabFitting.isTabEnabled(3)) |
---|
| 864 | |
---|
| 865 | #Check if magnetism box and tab are disabled when 1D data is loaded |
---|
| 866 | self.assertFalse(self.widget.chkMagnetism.isEnabled()) |
---|
| 867 | self.assertFalse(self.widget.tabFitting.isTabEnabled(4)) |
---|
| 868 | |
---|
| 869 | def testReadFitPage2D(self): |
---|
| 870 | """ |
---|
| 871 | Read in the fitpage object and restore state |
---|
| 872 | """ |
---|
| 873 | # Set data |
---|
| 874 | |
---|
| 875 | test_data = Data2D(image=[1.0, 2.0, 3.0], |
---|
| 876 | err_image=[0.01, 0.02, 0.03], |
---|
| 877 | qx_data=[0.1, 0.2, 0.3], |
---|
| 878 | qy_data=[0.1, 0.2, 0.3], |
---|
| 879 | xmin=0.1, xmax=0.3, ymin=0.1, ymax=0.3, |
---|
| 880 | mask=[True, True, True]) |
---|
| 881 | |
---|
| 882 | # Force same data into logic |
---|
| 883 | self.widget.logic.data = test_data |
---|
| 884 | self.widget.data_is_loaded = True |
---|
| 885 | |
---|
| 886 | #item = QtGui.QStandardItem() |
---|
| 887 | #updateModelItem(item, [test_data], "test") |
---|
| 888 | # Force same data into logic |
---|
| 889 | #self.widget.logic.data = item |
---|
| 890 | #self.widget.data_is_loaded = True |
---|
| 891 | |
---|
| 892 | category_index = self.widget.cbCategory.findText("Cylinder") |
---|
| 893 | self.widget.cbCategory.setCurrentIndex(category_index) |
---|
| 894 | |
---|
| 895 | # Test no fitting params |
---|
| 896 | self.widget.parameters_to_fit = ['scale'] |
---|
| 897 | |
---|
| 898 | # Invoke the tested method |
---|
| 899 | fp = self.widget.currentState() |
---|
| 900 | |
---|
| 901 | # Prepare modified fit page |
---|
| 902 | fp.current_model = 'cylinder' |
---|
| 903 | fp.is_polydisperse = True |
---|
| 904 | fp.is_magnetic = True |
---|
| 905 | fp.is2D = True |
---|
| 906 | |
---|
| 907 | # Read in modified state |
---|
| 908 | self.widget.readFitPage(fp) |
---|
| 909 | |
---|
| 910 | # Check if the widget got updated accordingly |
---|
| 911 | self.assertEqual(self.widget.cbModel.currentText(), 'cylinder') |
---|
| 912 | self.assertTrue(self.widget.chkPolydispersity.isChecked()) |
---|
| 913 | self.assertTrue(self.widget.chkPolydispersity.isEnabled()) |
---|
| 914 | #Check if polidispersity tab is available |
---|
| 915 | self.assertTrue(self.widget.tabFitting.isTabEnabled(3)) |
---|
| 916 | |
---|
| 917 | #Check if magnetism box and tab are disabled when 1D data is loaded |
---|
| 918 | self.assertTrue(self.widget.chkMagnetism.isChecked()) |
---|
| 919 | self.assertTrue(self.widget.chkMagnetism.isEnabled()) |
---|
| 920 | self.assertTrue(self.widget.tabFitting.isTabEnabled(4)) |
---|
[672b8ab] | 921 | |
---|
| 922 | def testCurrentState(self): |
---|
| 923 | """ |
---|
| 924 | Set up the fitpage with current state |
---|
| 925 | """ |
---|
| 926 | # Set data |
---|
| 927 | test_data = Data1D(x=[1,2], y=[1,2]) |
---|
| 928 | |
---|
| 929 | # Force same data into logic |
---|
| 930 | self.widget.logic.data = test_data |
---|
| 931 | self.widget.data_is_loaded = True |
---|
| 932 | category_index = self.widget.cbCategory.findText("Sphere") |
---|
| 933 | self.widget.cbCategory.setCurrentIndex(category_index) |
---|
| 934 | self.widget.parameters_to_fit = ['scale'] |
---|
| 935 | |
---|
| 936 | # Invoke the tested method |
---|
| 937 | fp = self.widget.currentState() |
---|
| 938 | |
---|
| 939 | # Test some entries. (Full testing of fp is done in FitPageTest) |
---|
| 940 | self.assertIsInstance(fp.data, Data1D) |
---|
| 941 | self.assertListEqual(list(fp.data.x), [1,2]) |
---|
| 942 | self.assertTrue(fp.data_is_loaded) |
---|
| 943 | self.assertEqual(fp.current_category, "Sphere") |
---|
| 944 | self.assertEqual(fp.current_model, "adsorbed_layer") |
---|
| 945 | self.assertListEqual(fp.parameters_to_fit, ['scale']) |
---|
| 946 | |
---|
| 947 | def testPushFitPage(self): |
---|
| 948 | """ |
---|
| 949 | Push current state of fitpage onto stack |
---|
| 950 | """ |
---|
[2241130] | 951 | # Set data |
---|
| 952 | test_data = Data1D(x=[1,2], y=[1,2]) |
---|
| 953 | |
---|
| 954 | # Force same data into logic |
---|
| 955 | self.widget.logic.data = test_data |
---|
| 956 | self.widget.data_is_loaded = True |
---|
| 957 | category_index = self.widget.cbCategory.findText("Sphere") |
---|
| 958 | |
---|
| 959 | # Asses the initial state of stack |
---|
| 960 | self.assertEqual(self.widget.page_stack, []) |
---|
| 961 | |
---|
| 962 | # Set the undo flag |
---|
| 963 | self.widget.undo_supported = True |
---|
| 964 | self.widget.cbCategory.setCurrentIndex(category_index) |
---|
| 965 | self.widget.parameters_to_fit = ['scale'] |
---|
| 966 | |
---|
| 967 | # Check that the stack is updated |
---|
| 968 | self.assertEqual(len(self.widget.page_stack), 1) |
---|
| 969 | |
---|
| 970 | # Change another parameter |
---|
| 971 | self.widget._model_model.item(2, 1).setText("3.0") |
---|
| 972 | # Check that the stack is updated |
---|
| 973 | self.assertEqual(len(self.widget.page_stack), 2) |
---|
[672b8ab] | 974 | |
---|
| 975 | def testPopFitPage(self): |
---|
| 976 | """ |
---|
| 977 | Pop current state of fitpage from stack |
---|
| 978 | """ |
---|
[2241130] | 979 | # TODO: to be added when implementing UNDO/REDO |
---|
[672b8ab] | 980 | pass |
---|
[811bec1] | 981 | |
---|
[a14a2b0] | 982 | def testOnMainPageChange(self): |
---|
| 983 | """ |
---|
| 984 | Test update values of modified parameters in models |
---|
| 985 | """ |
---|
| 986 | # select model: cylinder / cylinder |
---|
| 987 | category_index = self.widget.cbCategory.findText("Cylinder") |
---|
| 988 | self.widget.cbCategory.setCurrentIndex(category_index) |
---|
| 989 | |
---|
| 990 | model_index = self.widget.cbModel.findText("cylinder") |
---|
| 991 | self.widget.cbModel.setCurrentIndex(model_index) |
---|
| 992 | |
---|
| 993 | # modify the initial value of length (different from default) |
---|
| 994 | # print self.widget.kernel_module.details['length'] |
---|
| 995 | |
---|
| 996 | new_value = "333.0" |
---|
| 997 | self.widget._model_model.item(5, 1).setText(new_value) |
---|
| 998 | |
---|
| 999 | # name of modified parameter |
---|
| 1000 | name_modified_param = str(self.widget._model_model.item(5, 0).text()) |
---|
| 1001 | |
---|
[c7358b2] | 1002 | # Check the model |
---|
| 1003 | self.assertEqual(self.widget._model_model.rowCount(), 6) |
---|
| 1004 | self.assertEqual(self.widget._model_model.columnCount(), 5) |
---|
| 1005 | |
---|
| 1006 | # Test the header |
---|
| 1007 | #self.assertEqual(self.widget.lstParams.horizontalHeader().count(), 5) |
---|
| 1008 | #self.assertFalse(self.widget.lstParams.horizontalHeader().stretchLastSection()) |
---|
| 1009 | |
---|
| 1010 | self.assertEqual(len(self.widget._model_model.header_tooltips), 5) |
---|
| 1011 | header_tooltips = ['Select parameter for fitting', |
---|
| 1012 | 'Enter parameter value', |
---|
| 1013 | 'Enter minimum value for parameter', |
---|
| 1014 | 'Enter maximum value for parameter', |
---|
| 1015 | 'Unit of the parameter'] |
---|
| 1016 | for column, tooltip in enumerate(header_tooltips): |
---|
| 1017 | self.assertEqual(self.widget._model_model.headerData(column, |
---|
| 1018 | QtCore.Qt.Horizontal, QtCore.Qt.ToolTipRole), |
---|
| 1019 | header_tooltips[column]) |
---|
| 1020 | |
---|
[a14a2b0] | 1021 | # check that the value has been modified in kernel_module |
---|
| 1022 | self.assertEqual(new_value, |
---|
| 1023 | str(self.widget.kernel_module.params[name_modified_param])) |
---|
| 1024 | |
---|
| 1025 | # check that range of variation for this parameter has NOT been changed |
---|
| 1026 | self.assertNotIn(new_value, self.widget.kernel_module.details[name_modified_param] ) |
---|
| 1027 | |
---|
[63319b0] | 1028 | def testModelContextMenu(self): |
---|
| 1029 | """ |
---|
| 1030 | Test the right click context menu in the parameter table |
---|
| 1031 | """ |
---|
| 1032 | # select model: cylinder / cylinder |
---|
| 1033 | category_index = self.widget.cbCategory.findText("Cylinder") |
---|
| 1034 | self.widget.cbCategory.setCurrentIndex(category_index) |
---|
| 1035 | |
---|
| 1036 | model_index = self.widget.cbModel.findText("cylinder") |
---|
| 1037 | self.widget.cbModel.setCurrentIndex(model_index) |
---|
| 1038 | |
---|
| 1039 | # no rows selected |
---|
| 1040 | menu = self.widget.modelContextMenu([]) |
---|
| 1041 | self.assertEqual(len(menu.actions()), 0) |
---|
| 1042 | |
---|
| 1043 | # 1 row selected |
---|
| 1044 | menu = self.widget.modelContextMenu([1]) |
---|
| 1045 | self.assertEqual(len(menu.actions()), 4) |
---|
| 1046 | |
---|
| 1047 | # 2 rows selected |
---|
| 1048 | menu = self.widget.modelContextMenu([1,3]) |
---|
| 1049 | self.assertEqual(len(menu.actions()), 5) |
---|
| 1050 | |
---|
| 1051 | # 3 rows selected |
---|
| 1052 | menu = self.widget.modelContextMenu([1,2,3]) |
---|
| 1053 | self.assertEqual(len(menu.actions()), 4) |
---|
| 1054 | |
---|
| 1055 | # over 9000 |
---|
| 1056 | with self.assertRaises(AttributeError): |
---|
| 1057 | menu = self.widget.modelContextMenu([i for i in range(9001)]) |
---|
| 1058 | self.assertEqual(len(menu.actions()), 4) |
---|
| 1059 | |
---|
| 1060 | def testShowModelContextMenu(self): |
---|
| 1061 | # select model: cylinder / cylinder |
---|
| 1062 | category_index = self.widget.cbCategory.findText("Cylinder") |
---|
| 1063 | self.widget.cbCategory.setCurrentIndex(category_index) |
---|
| 1064 | |
---|
| 1065 | model_index = self.widget.cbModel.findText("cylinder") |
---|
| 1066 | self.widget.cbModel.setCurrentIndex(model_index) |
---|
| 1067 | |
---|
| 1068 | # No selection |
---|
| 1069 | logging.error = MagicMock() |
---|
| 1070 | self.widget.showModelDescription = MagicMock() |
---|
| 1071 | # Show the menu |
---|
| 1072 | self.widget.showModelContextMenu(QtCore.QPoint(10,20)) |
---|
| 1073 | |
---|
| 1074 | # Assure the description menu is shown |
---|
| 1075 | self.assertTrue(self.widget.showModelDescription.called) |
---|
| 1076 | self.assertFalse(logging.error.called) |
---|
| 1077 | |
---|
| 1078 | # "select" two rows |
---|
| 1079 | index1 = self.widget.lstParams.model().index(1, 0, QtCore.QModelIndex()) |
---|
| 1080 | index2 = self.widget.lstParams.model().index(2, 0, QtCore.QModelIndex()) |
---|
| 1081 | selection_model = self.widget.lstParams.selectionModel() |
---|
| 1082 | selection_model.select(index1, selection_model.Select | selection_model.Rows) |
---|
| 1083 | selection_model.select(index2, selection_model.Select | selection_model.Rows) |
---|
| 1084 | |
---|
| 1085 | QtWidgets.QMenu.exec_ = MagicMock() |
---|
| 1086 | logging.error = MagicMock() |
---|
| 1087 | # Show the menu |
---|
| 1088 | self.widget.showModelContextMenu(QtCore.QPoint(10,20)) |
---|
| 1089 | |
---|
| 1090 | # Assure the menu is shown |
---|
| 1091 | self.assertFalse(logging.error.called) |
---|
| 1092 | self.assertTrue(QtWidgets.QMenu.exec_.called) |
---|
| 1093 | |
---|
| 1094 | def testShowMultiConstraint(self): |
---|
| 1095 | """ |
---|
| 1096 | Test the widget update on new multi constraint |
---|
| 1097 | """ |
---|
| 1098 | # select model: cylinder / cylinder |
---|
| 1099 | category_index = self.widget.cbCategory.findText("Cylinder") |
---|
| 1100 | self.widget.cbCategory.setCurrentIndex(category_index) |
---|
| 1101 | |
---|
| 1102 | model_index = self.widget.cbModel.findText("cylinder") |
---|
| 1103 | self.widget.cbModel.setCurrentIndex(model_index) |
---|
| 1104 | |
---|
| 1105 | # nothing selected |
---|
| 1106 | with self.assertRaises(AssertionError): |
---|
| 1107 | self.widget.showMultiConstraint() |
---|
| 1108 | |
---|
| 1109 | # one row selected |
---|
| 1110 | index = self.widget.lstParams.model().index(1, 0, QtCore.QModelIndex()) |
---|
| 1111 | selection_model = self.widget.lstParams.selectionModel() |
---|
| 1112 | selection_model.select(index, selection_model.Select | selection_model.Rows) |
---|
| 1113 | with self.assertRaises(AssertionError): |
---|
| 1114 | # should also throw |
---|
| 1115 | self.widget.showMultiConstraint() |
---|
| 1116 | |
---|
| 1117 | # two rows selected |
---|
| 1118 | index1 = self.widget.lstParams.model().index(1, 0, QtCore.QModelIndex()) |
---|
| 1119 | index2 = self.widget.lstParams.model().index(2, 0, QtCore.QModelIndex()) |
---|
| 1120 | selection_model = self.widget.lstParams.selectionModel() |
---|
| 1121 | selection_model.select(index1, selection_model.Select | selection_model.Rows) |
---|
| 1122 | selection_model.select(index2, selection_model.Select | selection_model.Rows) |
---|
| 1123 | |
---|
| 1124 | # return non-OK from dialog |
---|
| 1125 | QtWidgets.QDialog.exec_ = MagicMock() |
---|
| 1126 | self.widget.showMultiConstraint() |
---|
| 1127 | # Check the dialog called |
---|
| 1128 | self.assertTrue(QtWidgets.QDialog.exec_.called) |
---|
| 1129 | |
---|
| 1130 | # return OK from dialog |
---|
| 1131 | QtWidgets.QDialog.exec_ = MagicMock(return_value=QtWidgets.QDialog.Accepted) |
---|
| 1132 | spy = QtSignalSpy(self.widget, self.widget.constraintAddedSignal) |
---|
| 1133 | |
---|
| 1134 | self.widget.showMultiConstraint() |
---|
| 1135 | |
---|
| 1136 | # Make sure the signal has been emitted |
---|
| 1137 | self.assertEqual(spy.count(), 1) |
---|
| 1138 | |
---|
| 1139 | # Check the argument value - should be row '1' |
---|
| 1140 | self.assertEqual(spy.called()[0]['args'][0], [1]) |
---|
| 1141 | |
---|
| 1142 | def testGetRowFromName(self): |
---|
| 1143 | """ |
---|
| 1144 | Helper function for parameter table |
---|
| 1145 | """ |
---|
| 1146 | # select model: cylinder / cylinder |
---|
| 1147 | category_index = self.widget.cbCategory.findText("Cylinder") |
---|
| 1148 | self.widget.cbCategory.setCurrentIndex(category_index) |
---|
| 1149 | |
---|
| 1150 | model_index = self.widget.cbModel.findText("cylinder") |
---|
| 1151 | self.widget.cbModel.setCurrentIndex(model_index) |
---|
| 1152 | |
---|
| 1153 | # several random parameters |
---|
| 1154 | self.assertEqual(self.widget.getRowFromName('scale'), 0) |
---|
| 1155 | self.assertEqual(self.widget.getRowFromName('length'), 5) |
---|
| 1156 | |
---|
| 1157 | def testGetParamNames(self): |
---|
| 1158 | """ |
---|
| 1159 | Helper function for parameter table |
---|
| 1160 | """ |
---|
| 1161 | # select model: cylinder / cylinder |
---|
| 1162 | category_index = self.widget.cbCategory.findText("Cylinder") |
---|
| 1163 | self.widget.cbCategory.setCurrentIndex(category_index) |
---|
| 1164 | |
---|
| 1165 | model_index = self.widget.cbModel.findText("cylinder") |
---|
| 1166 | self.widget.cbModel.setCurrentIndex(model_index) |
---|
| 1167 | |
---|
| 1168 | cylinder_params = ['scale','background','sld','sld_solvent','radius','length'] |
---|
| 1169 | # assure all parameters are returned |
---|
| 1170 | self.assertEqual(self.widget.getParamNames(), cylinder_params) |
---|
| 1171 | |
---|
| 1172 | # Switch to another model |
---|
| 1173 | model_index = self.widget.cbModel.findText("pringle") |
---|
| 1174 | self.widget.cbModel.setCurrentIndex(model_index) |
---|
| 1175 | |
---|
| 1176 | # make sure the parameters are different than before |
---|
| 1177 | self.assertFalse(self.widget.getParamNames() == cylinder_params) |
---|
| 1178 | |
---|
| 1179 | def testAddConstraintToRow(self): |
---|
| 1180 | """ |
---|
| 1181 | Test the constraint row add operation |
---|
| 1182 | """ |
---|
| 1183 | # select model: cylinder / cylinder |
---|
| 1184 | category_index = self.widget.cbCategory.findText("Cylinder") |
---|
| 1185 | self.widget.cbCategory.setCurrentIndex(category_index) |
---|
| 1186 | |
---|
| 1187 | model_index = self.widget.cbModel.findText("cylinder") |
---|
| 1188 | self.widget.cbModel.setCurrentIndex(model_index) |
---|
| 1189 | |
---|
| 1190 | # Create a constraint object |
---|
| 1191 | const = Constraint(parent=None, value=7.0) |
---|
| 1192 | row = 2 |
---|
| 1193 | |
---|
| 1194 | spy = QtSignalSpy(self.widget, self.widget.constraintAddedSignal) |
---|
| 1195 | |
---|
| 1196 | # call the method tested |
---|
| 1197 | self.widget.addConstraintToRow(constraint=const, row=row) |
---|
| 1198 | |
---|
| 1199 | # Make sure the signal has been emitted |
---|
| 1200 | self.assertEqual(spy.count(), 1) |
---|
| 1201 | |
---|
| 1202 | # Check the argument value - should be row 'row' |
---|
| 1203 | self.assertEqual(spy.called()[0]['args'][0], [row]) |
---|
| 1204 | |
---|
| 1205 | # Assure the row has the constraint |
---|
| 1206 | self.assertEqual(self.widget.getConstraintForRow(row), const) |
---|
| 1207 | # but not complex constraint! |
---|
| 1208 | self.assertFalse(self.widget.rowHasConstraint(row)) |
---|
| 1209 | |
---|
| 1210 | # assign complex constraint now |
---|
| 1211 | const = Constraint(parent=None, param='radius', func='5*sld') |
---|
| 1212 | row = 4 |
---|
| 1213 | # call the method tested |
---|
| 1214 | self.widget.addConstraintToRow(constraint=const, row=row) |
---|
| 1215 | |
---|
| 1216 | # Make sure the signal has been emitted |
---|
| 1217 | self.assertEqual(spy.count(), 2) |
---|
| 1218 | |
---|
| 1219 | # Check the argument value - should be row 'row' |
---|
| 1220 | self.assertEqual(spy.called()[1]['args'][0], [row]) |
---|
| 1221 | |
---|
| 1222 | # Assure the row has the constraint |
---|
| 1223 | self.assertEqual(self.widget.getConstraintForRow(row), const) |
---|
| 1224 | # and it is a complex constraint |
---|
| 1225 | self.assertTrue(self.widget.rowHasConstraint(row)) |
---|
| 1226 | |
---|
| 1227 | def testAddSimpleConstraint(self): |
---|
| 1228 | """ |
---|
| 1229 | Test the constraint add operation |
---|
| 1230 | """ |
---|
| 1231 | # select model: cylinder / cylinder |
---|
| 1232 | category_index = self.widget.cbCategory.findText("Cylinder") |
---|
| 1233 | self.widget.cbCategory.setCurrentIndex(category_index) |
---|
| 1234 | |
---|
| 1235 | model_index = self.widget.cbModel.findText("cylinder") |
---|
| 1236 | self.widget.cbModel.setCurrentIndex(model_index) |
---|
| 1237 | |
---|
| 1238 | # select two rows |
---|
| 1239 | row1 = 1 |
---|
| 1240 | row2 = 4 |
---|
| 1241 | index1 = self.widget.lstParams.model().index(row1, 0, QtCore.QModelIndex()) |
---|
| 1242 | index2 = self.widget.lstParams.model().index(row2, 0, QtCore.QModelIndex()) |
---|
| 1243 | selection_model = self.widget.lstParams.selectionModel() |
---|
| 1244 | selection_model.select(index1, selection_model.Select | selection_model.Rows) |
---|
| 1245 | selection_model.select(index2, selection_model.Select | selection_model.Rows) |
---|
| 1246 | |
---|
| 1247 | # define the signal spy |
---|
| 1248 | spy = QtSignalSpy(self.widget, self.widget.constraintAddedSignal) |
---|
| 1249 | |
---|
| 1250 | # call the method tested |
---|
| 1251 | self.widget.addSimpleConstraint() |
---|
| 1252 | |
---|
| 1253 | # Make sure the signal has been emitted |
---|
| 1254 | self.assertEqual(spy.count(), 2) |
---|
| 1255 | |
---|
| 1256 | # Check the argument value |
---|
| 1257 | self.assertEqual(spy.called()[0]['args'][0], [row1]) |
---|
| 1258 | self.assertEqual(spy.called()[1]['args'][0], [row2]) |
---|
| 1259 | |
---|
| 1260 | # Other properties |
---|
| 1261 | self.assertEqual(self.widget.getConstraintsForModel(), [('background', '0.001'), ('radius', '20')]) |
---|
| 1262 | |
---|
| 1263 | def testDeleteConstraintOnParameter(self): |
---|
| 1264 | """ |
---|
| 1265 | Test the constraint deletion in model/view |
---|
| 1266 | """ |
---|
| 1267 | # select model: cylinder / cylinder |
---|
| 1268 | category_index = self.widget.cbCategory.findText("Cylinder") |
---|
| 1269 | self.widget.cbCategory.setCurrentIndex(category_index) |
---|
| 1270 | |
---|
| 1271 | model_index = self.widget.cbModel.findText("cylinder") |
---|
| 1272 | self.widget.cbModel.setCurrentIndex(model_index) |
---|
| 1273 | |
---|
| 1274 | # select two rows |
---|
| 1275 | row1 = 1 |
---|
| 1276 | row2 = 4 |
---|
| 1277 | index1 = self.widget.lstParams.model().index(row1, 0, QtCore.QModelIndex()) |
---|
| 1278 | index2 = self.widget.lstParams.model().index(row2, 0, QtCore.QModelIndex()) |
---|
| 1279 | selection_model = self.widget.lstParams.selectionModel() |
---|
| 1280 | selection_model.select(index1, selection_model.Select | selection_model.Rows) |
---|
| 1281 | selection_model.select(index2, selection_model.Select | selection_model.Rows) |
---|
| 1282 | |
---|
| 1283 | # add constraints |
---|
| 1284 | self.widget.addSimpleConstraint() |
---|
| 1285 | |
---|
| 1286 | # deselect the model |
---|
| 1287 | selection_model.clear() |
---|
| 1288 | |
---|
| 1289 | # select a single row |
---|
| 1290 | selection_model.select(index1, selection_model.Select | selection_model.Rows) |
---|
| 1291 | |
---|
| 1292 | # delete one of the constraints |
---|
| 1293 | self.widget.deleteConstraintOnParameter(param='background') |
---|
| 1294 | |
---|
| 1295 | # see that the other constraint is still present |
---|
| 1296 | self.assertEqual(self.widget.getConstraintsForModel(), [('radius', '20')]) |
---|
| 1297 | |
---|
| 1298 | # kill the other constraint |
---|
| 1299 | self.widget.deleteConstraint() |
---|
| 1300 | |
---|
| 1301 | # see that the other constraint is still present |
---|
| 1302 | self.assertEqual(self.widget.getConstraintsForModel(), []) |
---|
| 1303 | |
---|
| 1304 | def testGetConstraintForRow(self): |
---|
| 1305 | """ |
---|
| 1306 | Helper function for parameter table |
---|
| 1307 | """ |
---|
| 1308 | # tested extensively elsewhere |
---|
| 1309 | pass |
---|
| 1310 | |
---|
| 1311 | def testRowHasConstraint(self): |
---|
| 1312 | """ |
---|
| 1313 | Helper function for parameter table |
---|
| 1314 | """ |
---|
| 1315 | # select model: cylinder / cylinder |
---|
| 1316 | category_index = self.widget.cbCategory.findText("Cylinder") |
---|
| 1317 | self.widget.cbCategory.setCurrentIndex(category_index) |
---|
| 1318 | |
---|
| 1319 | model_index = self.widget.cbModel.findText("cylinder") |
---|
| 1320 | self.widget.cbModel.setCurrentIndex(model_index) |
---|
| 1321 | |
---|
| 1322 | # select two rows |
---|
| 1323 | row1 = 1 |
---|
| 1324 | row2 = 4 |
---|
| 1325 | index1 = self.widget.lstParams.model().index(row1, 0, QtCore.QModelIndex()) |
---|
| 1326 | index2 = self.widget.lstParams.model().index(row2, 0, QtCore.QModelIndex()) |
---|
| 1327 | selection_model = self.widget.lstParams.selectionModel() |
---|
| 1328 | selection_model.select(index1, selection_model.Select | selection_model.Rows) |
---|
| 1329 | selection_model.select(index2, selection_model.Select | selection_model.Rows) |
---|
| 1330 | |
---|
| 1331 | # add constraints |
---|
| 1332 | self.widget.addSimpleConstraint() |
---|
| 1333 | |
---|
| 1334 | con_list = [False, True, False, False, True, False] |
---|
| 1335 | new_list = [] |
---|
| 1336 | for row in range(self.widget._model_model.rowCount()): |
---|
| 1337 | new_list.append(self.widget.rowHasConstraint(row)) |
---|
| 1338 | |
---|
| 1339 | self.assertEqual(new_list, con_list) |
---|
| 1340 | |
---|
| 1341 | def testRowHasActiveConstraint(self): |
---|
| 1342 | """ |
---|
| 1343 | Helper function for parameter table |
---|
| 1344 | """ |
---|
| 1345 | # select model: cylinder / cylinder |
---|
| 1346 | category_index = self.widget.cbCategory.findText("Cylinder") |
---|
| 1347 | self.widget.cbCategory.setCurrentIndex(category_index) |
---|
| 1348 | |
---|
| 1349 | model_index = self.widget.cbModel.findText("cylinder") |
---|
| 1350 | self.widget.cbModel.setCurrentIndex(model_index) |
---|
| 1351 | |
---|
| 1352 | # select two rows |
---|
| 1353 | row1 = 1 |
---|
| 1354 | row2 = 4 |
---|
| 1355 | index1 = self.widget.lstParams.model().index(row1, 0, QtCore.QModelIndex()) |
---|
| 1356 | index2 = self.widget.lstParams.model().index(row2, 0, QtCore.QModelIndex()) |
---|
| 1357 | selection_model = self.widget.lstParams.selectionModel() |
---|
| 1358 | selection_model.select(index1, selection_model.Select | selection_model.Rows) |
---|
| 1359 | selection_model.select(index2, selection_model.Select | selection_model.Rows) |
---|
| 1360 | |
---|
| 1361 | # add constraints |
---|
| 1362 | self.widget.addSimpleConstraint() |
---|
| 1363 | |
---|
| 1364 | # deactivate the first constraint |
---|
| 1365 | constraint_objects = self.widget.getConstraintObjectsForModel() |
---|
| 1366 | constraint_objects[0].active = False |
---|
| 1367 | |
---|
| 1368 | con_list = [False, False, False, False, True, False] |
---|
| 1369 | new_list = [] |
---|
| 1370 | for row in range(self.widget._model_model.rowCount()): |
---|
| 1371 | new_list.append(self.widget.rowHasActiveConstraint(row)) |
---|
| 1372 | |
---|
| 1373 | self.assertEqual(new_list, con_list) |
---|
| 1374 | |
---|
| 1375 | def testGetConstraintsForModel(self): |
---|
| 1376 | """ |
---|
| 1377 | Test the constraint getter for constraint texts |
---|
| 1378 | """ |
---|
| 1379 | # select model: cylinder / cylinder |
---|
| 1380 | category_index = self.widget.cbCategory.findText("Cylinder") |
---|
| 1381 | self.widget.cbCategory.setCurrentIndex(category_index) |
---|
| 1382 | |
---|
| 1383 | model_index = self.widget.cbModel.findText("cylinder") |
---|
| 1384 | self.widget.cbModel.setCurrentIndex(model_index) |
---|
| 1385 | |
---|
| 1386 | # no constraints |
---|
| 1387 | self.assertEqual(self.widget.getConstraintsForModel(),[]) |
---|
| 1388 | |
---|
| 1389 | # select two rows |
---|
| 1390 | row1 = 1 |
---|
| 1391 | row2 = 4 |
---|
| 1392 | index1 = self.widget.lstParams.model().index(row1, 0, QtCore.QModelIndex()) |
---|
| 1393 | index2 = self.widget.lstParams.model().index(row2, 0, QtCore.QModelIndex()) |
---|
| 1394 | selection_model = self.widget.lstParams.selectionModel() |
---|
| 1395 | selection_model.select(index1, selection_model.Select | selection_model.Rows) |
---|
| 1396 | selection_model.select(index2, selection_model.Select | selection_model.Rows) |
---|
| 1397 | |
---|
| 1398 | # add constraints |
---|
| 1399 | self.widget.addSimpleConstraint() |
---|
| 1400 | |
---|
| 1401 | # simple constraints |
---|
| 1402 | self.assertEqual(self.widget.getConstraintsForModel(), [('background', '0.001'), ('radius', '20')]) |
---|
| 1403 | objects = self.widget.getConstraintObjectsForModel() |
---|
| 1404 | self.assertEqual(len(objects), 2) |
---|
| 1405 | self.assertEqual(objects[1].value, '20') |
---|
| 1406 | self.assertEqual(objects[0].param, 'background') |
---|
| 1407 | |
---|
| 1408 | |
---|
| 1409 | # add complex constraint |
---|
| 1410 | const = Constraint(parent=None, param='scale', func='5*sld') |
---|
| 1411 | row = 0 |
---|
| 1412 | self.widget.addConstraintToRow(constraint=const, row=row) |
---|
| 1413 | self.assertEqual(self.widget.getConstraintsForModel(),[('scale', '5*sld'), ('background', '0.001'), ('radius', '20')]) |
---|
| 1414 | objects = self.widget.getConstraintObjectsForModel() |
---|
| 1415 | self.assertEqual(len(objects), 3) |
---|
| 1416 | self.assertEqual(objects[0].func, '5*sld') |
---|
| 1417 | |
---|
| 1418 | def testReplaceConstraintName(self): |
---|
| 1419 | """ |
---|
| 1420 | Test the replacement of constraint moniker |
---|
| 1421 | """ |
---|
| 1422 | # select model: cylinder / cylinder |
---|
| 1423 | category_index = self.widget.cbCategory.findText("Cylinder") |
---|
| 1424 | self.widget.cbCategory.setCurrentIndex(category_index) |
---|
| 1425 | |
---|
| 1426 | model_index = self.widget.cbModel.findText("cylinder") |
---|
| 1427 | self.widget.cbModel.setCurrentIndex(model_index) |
---|
| 1428 | |
---|
| 1429 | old_name = 'M5' |
---|
| 1430 | new_name = 'poopy' |
---|
| 1431 | # add complex constraint |
---|
| 1432 | const = Constraint(parent=None, param='scale', func='%s.5*sld'%old_name) |
---|
| 1433 | row = 0 |
---|
| 1434 | self.widget.addConstraintToRow(constraint=const, row=row) |
---|
| 1435 | |
---|
| 1436 | # Replace 'm5' with 'poopy' |
---|
| 1437 | self.widget.replaceConstraintName(old_name, new_name) |
---|
| 1438 | |
---|
| 1439 | self.assertEqual(self.widget.getConstraintsForModel(),[('scale', 'poopy.5*sld')]) |
---|
| 1440 | |
---|
[a14a2b0] | 1441 | |
---|
[811bec1] | 1442 | if __name__ == "__main__": |
---|
| 1443 | unittest.main() |
---|