Changeset 351b53e in sasview


Ignore:
Timestamp:
Mar 22, 2017 4:23:49 AM (7 years ago)
Author:
Piotr Rozyczko <rozyczko@…>
Branches:
ESS_GUI, ESS_GUI_Docs, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_iss959, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc
Children:
0979dfb
Parents:
7248d75d
Message:

More unit tests for fitting SASVIEW-499

Location:
src/sas/qtgui/Perspectives/Fitting
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • src/sas/qtgui/Perspectives/Fitting/FittingWidget.py

    r4d457df r351b53e  
    3131CATEGORY_DEFAULT = "Choose category..." 
    3232CATEGORY_STRUCTURE = "Structure Factor" 
     33STRUCTURE_DEFAULT = "None" 
    3334QMIN_DEFAULT = 0.0005 
    3435QMAX_DEFAULT = 0.5 
     
    231232        """ 
    232233        structure_factor_list = self.master_category_dict.pop(CATEGORY_STRUCTURE) 
    233         structure_factors = ["None"] 
     234        factors = [factor[0] for factor in structure_factor_list] 
     235        factors.insert(0, STRUCTURE_DEFAULT) 
    234236        self.cbStructureFactor.clear() 
    235         structure_factors = [factor[0] for factor in structure_factor_list] 
    236         self.cbStructureFactor.addItems(sorted(structure_factors)) 
     237        self.cbStructureFactor.addItems(sorted(factors)) 
    237238 
    238239    def onSelectCategory(self): 
     
    246247            # Otherwise, just return 
    247248            if self._previous_category_index != 0: 
     249                # We need to block signals, or else state changes on perceived unchanged conditions 
     250                self.cbCategory.blockSignals(True) 
    248251                self.cbCategory.setCurrentIndex(self._previous_category_index) 
     252                self.cbCategory.blockSignals(False) 
    249253            return 
    250254 
     
    288292        model = str(self.cbModel.currentText()) 
    289293 
     294        # Reset structure factor 
     295        self.cbStructureFactor.setCurrentIndex(0) 
     296 
    290297        # SasModel -> QModel 
    291298        self.SASModelToQModel(model) 
     
    357364        Setting model parameters into table based on selected category 
    358365        """ 
     366        # TODO - modify for structure factor-only choice 
     367 
    359368        # Crete/overwrite model items 
    360369        self._model_model.clear() 
     
    415424            parameter_name = parameter_name[16:] 
    416425        property_name = str(self._poly_model.headerData(model_column, 1).toPyObject()) # Value, min, max, etc. 
    417         print "%s(%s) => %d" % (parameter_name, property_name, value) 
     426        # print "%s(%s) => %d" % (parameter_name, property_name, value) 
    418427 
    419428        # Update the sasmodel 
     
    454463        property_name = str(self._model_model.headerData(1, model_column).toPyObject()) # Value, min, max, etc. 
    455464 
    456         print "%s(%s) => %d" % (parameter_name, property_name, value) 
     465        # print "%s(%s) => %d" % (parameter_name, property_name, value) 
    457466        self.kernel_module.params[parameter_name] = value 
    458467 
     
    691700        self._last_model_row = self._model_model.rowCount() 
    692701 
    693  
    694702    def modifyShellsInList(self, index): 
    695703        """ 
  • src/sas/qtgui/Perspectives/Fitting/UnitTesting/FittingLogicTest.py

    r7248d75d r351b53e  
    125125        q_0 = numpy.sqrt(qx_0 * qx_0 + qy_0 * qy_0) 
    126126 
    127         data = Data2D(image=x_0, err_image=dx_0, qx_data=qx_0,  
    128                       qy_data=qy_0, q_data=q_0, mask=mask_0,  
     127        data = Data2D(image=x_0, err_image=dx_0, qx_data=qx_0, 
     128                      qy_data=qy_0, q_data=q_0, mask=mask_0, 
    129129                      dqx_data=dqx_0, dqy_data=dqy_0) 
    130130 
  • src/sas/qtgui/Perspectives/Fitting/UnitTesting/FittingWidgetTest.py

    r7248d75d r351b53e  
    66from PyQt4 import QtCore 
    77from mock import MagicMock 
     8from twisted.internet import threads 
    89 
    910# set up import paths 
     
    1314from sas.qtgui.GuiUtils import * 
    1415from sas.qtgui.Perspectives.Fitting.FittingWidget import * 
    15 #from sas.qtgui.Perspectives.Fitting.FittingWidget import FittingWidget 
     16from sas.qtgui.UnitTesting.TestUtils import QtSignalSpy 
     17 
    1618from sas.sasgui.guiframe.dataFitting import Data1D 
    1719 
     
    149151    def testSignals(self): 
    150152        """ 
    151         Test the widget exmitted signals 
     153        Test the widget emitted signals 
    152154        """ 
    153155        pass 
     
    157159        Assure proper behaviour on changing category 
    158160        """ 
    159         pass 
     161        self.widget.show() 
     162        self.assertEqual(self.widget._previous_category_index, 0) 
     163        # confirm the model combo contains no models 
     164        self.assertEqual(self.widget.cbModel.count(), 0) 
     165 
     166        # invoke the method by changing the index 
     167        self.widget.cbCategory.setCurrentIndex(1) 
     168 
     169        # test the model combo content 
     170        self.assertEqual(self.widget.cbModel.count(), 29) 
     171 
     172        # Try to change back to default 
     173        self.widget.cbCategory.setCurrentIndex(0) 
     174 
     175        # Observe no such luck 
     176        self.assertEqual(self.widget.cbCategory.currentIndex(), 1) 
     177        self.assertEqual(self.widget.cbModel.count(), 29) 
     178 
     179        # Set the structure factor 
     180        structure_index=self.widget.cbCategory.findText(CATEGORY_STRUCTURE) 
     181        self.widget.cbCategory.setCurrentIndex(structure_index) 
     182        # check the enablement of controls 
     183        self.assertFalse(self.widget.cbModel.isEnabled()) 
     184        self.assertTrue(self.widget.cbStructureFactor.isEnabled()) 
    160185 
    161186    def testSelectModel(self): 
     
    163188        Assure proper behaviour on changing model 
    164189        """ 
    165         pass 
     190        self.widget.show() 
     191        # Change the category index so we have some models 
     192        self.widget.cbCategory.setCurrentIndex(1) 
     193 
     194        # check the enablement of controls 
     195        self.assertTrue(self.widget.cbModel.isEnabled()) 
     196        self.assertFalse(self.widget.cbStructureFactor.isEnabled()) 
     197 
     198        # set up the model update spy 
     199        # spy = QtSignalSpy(self.widget._model_model, self.widget._model_model.itemChanged) 
     200 
     201        # mock the tested methods 
     202        self.widget.SASModelToQModel = MagicMock() 
     203        self.widget.createDefaultDataset = MagicMock() 
     204        self.widget.calculateDataForModel = MagicMock() 
     205        #  
     206        # Now change the model 
     207        self.widget.cbModel.setCurrentIndex(3) 
     208        self.assertEqual(self.widget.cbModel.currentText(),'correlation_length') 
     209 
     210        # No data sent -> no index set, only createDefaultDataset called 
     211        self.assertTrue(self.widget.createDefaultDataset.called) 
     212        self.assertTrue(self.widget.SASModelToQModel.called) 
     213        self.assertFalse(self.widget.calculateDataForModel.called) 
     214 
     215        # Let's set a dummy index on widget 
     216        self.widget._index = QtGui.QStandardItem() 
     217        # Reset the sasmodel index 
     218        self.widget.cbModel.setCurrentIndex(1) 
     219        self.assertEqual(self.widget.cbModel.currentText(),'be_polyelectrolyte') 
     220 
     221        # Observe calculateDataForModel called 
     222        self.assertTrue(self.widget.calculateDataForModel.called) 
    166223 
    167224    def testSelectFactor(self): 
     
    169226        Assure proper behaviour on changing structure factor 
    170227        """ 
    171         pass 
     228        self.widget.show() 
     229        # Change the category index so we have some models 
     230        self.widget.cbCategory.setCurrentIndex(1) 
     231        # Change the model to one that supports structure factors 
     232        model_index = self.widget.cbModel.findText('fractal_core_shell') 
     233        self.widget.cbModel.setCurrentIndex(model_index) 
     234 
     235        # Check that the factor combo is active and the default is chosen 
     236        self.assertTrue(self.widget.cbStructureFactor.isEnabled()) 
     237        self.assertEqual(self.widget.cbStructureFactor.currentText(), STRUCTURE_DEFAULT) 
     238 
     239        # We have this many rows in the model 
     240        rowcount = self.widget._model_model.rowCount() 
     241        #self.assertEqual(self.widget._model_model.rowCount(), 8) 
     242 
     243        # Change structure factor to something more exciting 
     244        structure_index = self.widget.cbStructureFactor.findText('squarewell') 
     245        self.widget.cbStructureFactor.setCurrentIndex(structure_index) 
     246 
     247        # We have 4 more rows now 
     248        self.assertEqual(self.widget._model_model.rowCount(), rowcount+4) 
     249 
     250        # Switch models 
     251        self.widget.cbModel.setCurrentIndex(0) 
     252 
     253        # Observe factor reset to None 
     254        self.assertEqual(self.widget.cbStructureFactor.currentText(), STRUCTURE_DEFAULT) 
     255 
     256 
     257        # TODO once functionality fixed 
     258        ## Switch category to structure factor 
     259        #structure_index=self.widget.cbCategory.findText(CATEGORY_STRUCTURE) 
     260        #self.widget.cbCategory.setCurrentIndex(structure_index) 
     261        ## Choose the last factor 
     262        #last_index = self.widget.cbStructureFactor.count() 
     263        #self.widget.cbStructureFactor.setCurrentIndex(last_index-1) 
    172264 
    173265    def testReadCategoryInfo(self): 
     
    175267        Check the category file reader 
    176268        """ 
     269        # Tested in default checks 
    177270        pass 
    178271 
     
    181274        Checks the sasmodel parameter update from QModel items 
    182275        """ 
    183         pass 
    184  
    185     def testComputeDataRange(self): 
    186         """ 
    187         Tests the data range calculator on Data1D/Data2D 
    188         """ 
    189         pass 
    190  
    191     def testCreateDefault1dData(self): 
    192         """ 
    193         Tests the default 1D set 
    194         """ 
    195         pass 
    196  
    197     def testCreateDefault2dData(self): 
    198         """ 
    199         Tests the default 2D set 
    200         """ 
    201         pass 
    202  
    203     def testCreateTheoryIndex(self): 
     276        # Tested in default checks 
     277        pass 
     278 
     279    def notestCreateTheoryIndex(self): 
    204280        """ 
    205281        Test the data->QIndex conversion 
    206282        """ 
    207         pass 
    208  
    209     def testCalculate1DForModel(self): 
     283        # set up the model update spy 
     284        spy = QtSignalSpy(self.widget._model_model, self.widget.communicate.updateTheoryFromPerspectiveSignal) 
     285 
     286        self.widget.show() 
     287        # Change the category index so we have some models 
     288        self.widget.cbCategory.setCurrentIndex(1) 
     289 
     290        # Create the index 
     291        self.widget.createTheoryIndex() 
     292 
     293        # Check the signal sent 
     294        print spy 
     295 
     296        # Check the index 
     297 
     298    def testCalculateDataForModel(self): 
    210299        """ 
    211300        Check that the fitting 1D data object is ready 
    212301        """ 
    213         pass 
    214  
    215     def testCalculate2DForModel(self): 
    216         """ 
    217         Check that the fitting 2D data object is ready 
    218         """ 
    219         pass 
     302        # Mock the thread creation 
     303        threads.deferToThread = MagicMock() 
     304        # Call the tested method 
     305        self.widget.calculateDataForModel() 
     306        # Test the mock 
     307        self.assertTrue(threads.deferToThread.called) 
     308        self.assertEqual(threads.deferToThread.call_args_list[0][0][0].__name__, "compute") 
    220309 
    221310    def testComplete1D(self): 
     
    223312        Check that a new 1D plot is generated 
    224313        """ 
     314        # TODO when chisqr method coded 
    225315        pass 
    226316 
     
    229319        Check that a new 2D plot is generated 
    230320        """ 
     321        # TODO when chisqr method coded 
    231322        pass 
    232323 
     
    235326        Test the polydispersity model setup 
    236327        """ 
    237         pass 
     328        self.widget.show() 
     329        # Change the category index so we have a model with no poly 
     330        self.widget.cbCategory.setCurrentIndex(1) 
     331        # Check the poly model 
     332        self.assertEqual(self.widget._poly_model.rowCount(), 0) 
     333        self.assertEqual(self.widget._poly_model.columnCount(), 0) 
     334 
     335        # Change the category index so we have a model available 
     336        self.widget.cbCategory.setCurrentIndex(2) 
     337 
     338        # Check the poly model 
     339        self.assertEqual(self.widget._poly_model.rowCount(), 3) 
     340        self.assertEqual(self.widget._poly_model.columnCount(), 7) 
     341 
     342        # Test the header 
     343        self.assertEqual(self.widget.lstPoly.horizontalHeader().count(), 7) 
     344        self.assertFalse(self.widget.lstPoly.horizontalHeader().stretchLastSection()) 
     345 
     346        # Test presence of comboboxes in last column 
     347        for row in xrange(self.widget._poly_model.rowCount()): 
     348            func_index = self.widget._poly_model.index(row, 6) 
     349            self.assertTrue(isinstance(self.widget.lstPoly.indexWidget(func_index), QtGui.QComboBox)) 
     350            self.assertIn('Distribution of', self.widget._poly_model.item(row, 0).text()) 
    238351 
    239352    def testSetMagneticModel(self): 
     
    241354        Test the magnetic model setup 
    242355        """ 
    243         pass 
     356        self.widget.show() 
     357        # Change the category index so we have a model available 
     358        self.widget.cbCategory.setCurrentIndex(2) 
     359 
     360        # Check the magnetic model 
     361        self.assertEqual(self.widget._magnet_model.rowCount(), 9) 
     362        self.assertEqual(self.widget._magnet_model.columnCount(), 5) 
     363 
     364        # Test the header 
     365        self.assertEqual(self.widget.lstMagnetic.horizontalHeader().count(), 5) 
     366        self.assertFalse(self.widget.lstMagnetic.horizontalHeader().stretchLastSection()) 
     367 
     368        # Test rows 
     369        for row in xrange(self.widget._magnet_model.rowCount()): 
     370            func_index = self.widget._magnet_model.index(row, 0) 
     371            self.assertIn(':', self.widget._magnet_model.item(row, 0).text()) 
     372 
    244373 
    245374    def testAddExtraShells(self): 
     
    253382        Test the additional rows added by modifying the shells combobox 
    254383        """ 
    255         pass 
     384        self.widget.show() 
     385        # Change the model to multi shell 
     386        self.widget.cbCategory.setCurrentIndex(2) 
     387        self.widget.cbModel.setCurrentIndex(4) 
     388 
     389        # Assure we have the combobox available 
     390        last_row = self.widget._last_model_row 
     391        func_index = self.widget._model_model.index(last_row-1, 1) 
     392        self.assertIsInstance(self.widget.lstParams.indexWidget(func_index), QtGui.QComboBox) 
     393 
     394        # Change the combo box index 
     395        self.widget.lstParams.indexWidget(func_index).setCurrentIndex(3) 
     396 
     397        # Check that the number of rows increased 
     398        more_rows = self.widget._model_model.rowCount() - last_row 
     399        self.assertEqual(more_rows, 6) # 6 new rows: 2 params per index 
     400 
     401        # Back to 0 
     402        self.widget.lstParams.indexWidget(func_index).setCurrentIndex(0) 
     403        self.assertEqual(self.widget._model_model.rowCount(), last_row) 
    256404 
    257405 
Note: See TracChangeset for help on using the changeset viewer.