source: sasview/src/sas/qtgui/Perspectives/Fitting/UnitTesting/FittingWidgetTest.py @ 2eeda93

ESS_GUIESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalc
Last change on this file since 2eeda93 was 2eeda93, checked in by Piotr Rozyczko <piotr.rozyczko@…>, 11 months ago

Working version of Save/Load? Analysis. SASVIEW-983.
Changed the default behaviour of Category/Model? combos:
Selecting a category does not pre-select the first model now.

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