Ignore:
File:
1 edited

Legend:

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

    r66d4370 r3fbd77b  
    1818from sas.qtgui.Perspectives.Fitting.FittingWidget import * 
    1919from sas.qtgui.Perspectives.Fitting.Constraint import Constraint 
    20 import sas.qtgui.Utilities.LocalConfig 
     20 
    2121from sas.qtgui.UnitTesting.TestUtils import QtSignalSpy 
    22 from sas.qtgui.Perspectives.Fitting.ModelThread import Calc1D 
    23 from sas.qtgui.Perspectives.Fitting.ModelThread import Calc2D 
    2422 
    2523from sas.qtgui.Plotting.PlotterData import Data1D 
     
    256254        self.widget.cbStructureFactor.setCurrentIndex(structure_index) 
    257255 
    258         # We have 4 more rows now 
     256        # We have 3 more param rows now (radius_effective is removed), and a new heading 
    259257        self.assertEqual(self.widget._model_model.rowCount(), rowcount+4) 
    260258 
     
    262260        self.widget.cbModel.setCurrentIndex(0) 
    263261 
    264         # Observe factor doesn't reset to None 
    265         self.assertEqual(self.widget.cbStructureFactor.currentText(), 'squarewell') 
     262        # Observe factor reset to None 
     263        self.assertEqual(self.widget.cbStructureFactor.currentText(), STRUCTURE_DEFAULT) 
    266264 
    267265        # Switch category to structure factor 
     
    276274        last_index = self.widget.cbStructureFactor.count() 
    277275        self.widget.cbStructureFactor.setCurrentIndex(last_index-1) 
    278         # Do we have all the rows? 
    279         self.assertEqual(self.widget._model_model.rowCount(), 4) 
     276        # Do we have all the rows (incl. radius_effective & heading row)? 
     277        self.assertEqual(self.widget._model_model.rowCount(), 5) 
    280278 
    281279        # Are the command buttons properly enabled? 
     
    321319        Check that the fitting 1D data object is ready 
    322320        """ 
    323  
    324         if LocalConfig.USING_TWISTED: 
    325             # Mock the thread creation 
    326             threads.deferToThread = MagicMock() 
    327             # Model for theory 
    328             self.widget.SASModelToQModel("cylinder") 
    329             # Call the tested method 
    330             self.widget.calculateQGridForModel() 
    331             time.sleep(1) 
    332             # Test the mock 
    333             self.assertTrue(threads.deferToThread.called) 
    334             self.assertEqual(threads.deferToThread.call_args_list[0][0][0].__name__, "compute") 
    335         else: 
    336             Calc2D.queue = MagicMock() 
    337             # Model for theory 
    338             self.widget.SASModelToQModel("cylinder") 
    339             # Call the tested method 
    340             self.widget.calculateQGridForModel() 
    341             time.sleep(1) 
    342             # Test the mock 
    343             self.assertTrue(Calc2D.queue.called) 
     321        # Mock the thread creation 
     322        threads.deferToThread = MagicMock() 
     323        # Model for theory 
     324        self.widget.SASModelToQModel("cylinder") 
     325        # Call the tested method 
     326        self.widget.calculateQGridForModel() 
     327        time.sleep(1) 
     328        # Test the mock 
     329        self.assertTrue(threads.deferToThread.called) 
     330        self.assertEqual(threads.deferToThread.call_args_list[0][0][0].__name__, "compute") 
    344331 
    345332    def testCalculateResiduals(self): 
     
    430417        index = self.widget._poly_model.index(0,0) 
    431418 
     419        #self.widget.show() 
     420        #QtWidgets.QApplication(sys.argv).exec_() 
     421 
    432422        # Set the checbox 
    433423        self.widget._poly_model.item(0,0).setCheckState(2) 
     
    445435        self.assertEqual(self.widget.kernel_module.details['radius_bell'][1], 1.0) 
    446436 
    447         #self.widget.show() 
    448         #QtWidgets.QApplication.exec_() 
    449  
    450437        # Change the number of points 
    451         self.assertEqual(self.widget.poly_params['radius_bell.npts'], 35) 
     438        self.assertEqual(self.widget.kernel_module.getParam('radius_bell.npts'), 35) 
    452439        self.widget._poly_model.item(0,4).setText("22") 
    453         self.assertEqual(self.widget.poly_params['radius_bell.npts'], 22) 
     440        self.assertEqual(self.widget.kernel_module.getParam('radius_bell.npts'), 22) 
    454441        # try something stupid 
    455442        self.widget._poly_model.item(0,4).setText("butt") 
    456443        # see that this didn't annoy the control at all 
    457         self.assertEqual(self.widget.poly_params['radius_bell.npts'], 22) 
     444        self.assertEqual(self.widget.kernel_module.getParam('radius_bell.npts'), 22) 
    458445 
    459446        # Change the number of sigmas 
    460         self.assertEqual(self.widget.poly_params['radius_bell.nsigmas'], 3) 
     447        self.assertEqual(self.widget.kernel_module.getParam('radius_bell.nsigmas'), 3) 
    461448        self.widget._poly_model.item(0,5).setText("222") 
    462         self.assertEqual(self.widget.poly_params['radius_bell.nsigmas'], 222) 
     449        self.assertEqual(self.widget.kernel_module.getParam('radius_bell.nsigmas'), 222) 
    463450        # try something stupid again 
    464451        self.widget._poly_model.item(0,4).setText("beer") 
    465452        # no efect 
    466         self.assertEqual(self.widget.poly_params['radius_bell.nsigmas'], 222) 
     453        self.assertEqual(self.widget.kernel_module.getParam('radius_bell.nsigmas'), 222) 
    467454 
    468455    def testOnPolyComboIndexChange(self): 
     
    485472        self.widget.onPolyComboIndexChange('rectangle', 0) 
    486473        # check values 
    487         self.assertEqual(self.widget.poly_params['radius_bell.npts'], 35) 
    488         self.assertAlmostEqual(self.widget.poly_params['radius_bell.nsigmas'], 1.73205, 5) 
     474        self.assertEqual(self.widget.kernel_module.getParam('radius_bell.npts'), 35) 
     475        self.assertAlmostEqual(self.widget.kernel_module.getParam('radius_bell.nsigmas'), 1.73205, 5) 
    489476        # Change the index 
    490477        self.widget.onPolyComboIndexChange('lognormal', 0) 
    491478        # check values 
    492         self.assertEqual(self.widget.poly_params['radius_bell.npts'], 80) 
    493         self.assertEqual(self.widget.poly_params['radius_bell.nsigmas'], 8) 
     479        self.assertEqual(self.widget.kernel_module.getParam('radius_bell.npts'), 80) 
     480        self.assertEqual(self.widget.kernel_module.getParam('radius_bell.nsigmas'), 8) 
    494481        # Change the index 
    495482        self.widget.onPolyComboIndexChange('schulz', 0) 
    496483        # check values 
    497         self.assertEqual(self.widget.poly_params['radius_bell.npts'], 80) 
    498         self.assertEqual(self.widget.poly_params['radius_bell.nsigmas'], 8) 
     484        self.assertEqual(self.widget.kernel_module.getParam('radius_bell.npts'), 80) 
     485        self.assertEqual(self.widget.kernel_module.getParam('radius_bell.nsigmas'), 8) 
    499486 
    500487        # mock up file load 
     
    509496        Test opening of the load file dialog for 'array' polydisp. function 
    510497        """ 
     498 
     499        # open a non-existent file 
    511500        filename = os.path.join("UnitTesting", "testdata_noexist.txt") 
     501        with self.assertRaises(OSError, msg="testdata_noexist.txt should be a non-existent file"): 
     502            os.stat(filename) 
    512503        QtWidgets.QFileDialog.getOpenFileName = MagicMock(return_value=(filename,'')) 
    513504        self.widget.show() 
     
    525516 
    526517        # good file 
     518        # TODO: this depends on the working directory being src/sas/qtgui, 
     519        # TODO: which isn't convenient if you want to run this test suite 
     520        # TODO: individually 
    527521        filename = os.path.join("UnitTesting", "testdata.txt") 
     522        try: 
     523            os.stat(filename) 
     524        except OSError: 
     525            self.assertTrue(False, "testdata.txt does not exist") 
    528526        QtWidgets.QFileDialog.getOpenFileName = MagicMock(return_value=(filename,'')) 
    529527 
     
    591589 
    592590        # Assure we have the combobox available 
    593         last_row = self.widget._last_model_row 
    594         func_index = self.widget._model_model.index(last_row-1, 1) 
     591        cbox_row = self.widget._n_shells_row 
     592        func_index = self.widget._model_model.index(cbox_row, 1) 
    595593        self.assertIsInstance(self.widget.lstParams.indexWidget(func_index), QtWidgets.QComboBox) 
     594 
     595        # get number of rows before changing shell count 
     596        last_row = self.widget._model_model.rowCount() 
    596597 
    597598        # Change the combo box index 
     
    650651        self.assertEqual(self.widget.cmdPlot.text(), 'Show Plot') 
    651652 
     653        self.widget.show() 
     654 
    652655        # Set data 
    653656        test_data = Data1D(x=[1,2], y=[1,2]) 
    654         item = QtGui.QStandardItem() 
    655         updateModelItem(item, test_data, "test") 
     657 
    656658        # Force same data into logic 
    657         self.widget.data = item 
     659        self.widget.logic.data = test_data 
     660        self.widget.data_is_loaded = True 
    658661 
    659662        # Change the category index so we have a model available 
     
    701704        self.widget.close() 
    702705 
    703     def testOnEmptyFit2(self): 
    704706        test_data = Data2D(image=[1.0, 2.0, 3.0], 
    705707                           err_image=[0.01, 0.02, 0.03], 
     
    712714        item = QtGui.QStandardItem() 
    713715        updateModelItem(item, test_data, "test") 
    714  
    715716        # Force same data into logic 
    716717        self.widget.data = item 
     
    730731        self.widget.close() 
    731732 
    732     def notestOnFit1D(self): 
     733 
     734    def testOnFit1D(self): 
    733735        """ 
    734736        Test the threaded fitting call 
     
    768770        self.widget.close() 
    769771 
    770     def notestOnFit2D(self): 
     772    def testOnFit2D(self): 
    771773        """ 
    772774        Test the threaded fitting call 
     
    863865        # Set data 
    864866        test_data = Data1D(x=[1,2], y=[1,2]) 
    865         item = QtGui.QStandardItem() 
    866         updateModelItem(item, test_data, "test") 
     867 
    867868        # Force same data into logic 
    868         self.widget.data = item 
    869  
    870         # Force same data into logic 
     869        self.widget.logic.data = test_data 
     870        self.widget.data_is_loaded = True 
    871871        category_index = self.widget.cbCategory.findText('Sphere') 
    872  
    873872        self.widget.cbCategory.setCurrentIndex(category_index) 
    874873        self.widget.main_params_to_fit = ['scale'] 
     
    952951        # Set data 
    953952        test_data = Data1D(x=[1,2], y=[1,2]) 
    954         item = QtGui.QStandardItem() 
    955         updateModelItem(item, test_data, "test") 
     953 
    956954        # Force same data into logic 
    957         self.widget.data = item 
     955        self.widget.logic.data = test_data 
     956        self.widget.data_is_loaded = True 
    958957        category_index = self.widget.cbCategory.findText("Sphere") 
    959958        self.widget.cbCategory.setCurrentIndex(category_index) 
     
    977976        # Set data 
    978977        test_data = Data1D(x=[1,2], y=[1,2]) 
    979         item = QtGui.QStandardItem() 
    980         updateModelItem(item, test_data, "test") 
     978 
    981979        # Force same data into logic 
    982         self.widget.data = item 
     980        self.widget.logic.data = test_data 
     981        self.widget.data_is_loaded = True 
    983982        category_index = self.widget.cbCategory.findText("Sphere") 
    984983 
     
    10271026 
    10281027         # Check the model 
    1029         self.assertEqual(self.widget._model_model.rowCount(), 6) 
     1028        self.assertEqual(self.widget._model_model.rowCount(), 7) 
    10301029        self.assertEqual(self.widget._model_model.columnCount(), 5) 
    10311030 
     
    11431142        # two rows selected 
    11441143        index1 = self.widget.lstParams.model().index(1, 0, QtCore.QModelIndex()) 
    1145         index2 = self.widget.lstParams.model().index(2, 0, QtCore.QModelIndex()) 
     1144        index2 = self.widget.lstParams.model().index(3, 0, QtCore.QModelIndex()) 
    11461145        selection_model = self.widget.lstParams.selectionModel() 
    11471146        selection_model.select(index1, selection_model.Select | selection_model.Rows) 
     
    11791178        # several random parameters 
    11801179        self.assertEqual(self.widget.getRowFromName('scale'), 0) 
    1181         self.assertEqual(self.widget.getRowFromName('length'), 5) 
     1180        self.assertEqual(self.widget.getRowFromName('length'), 6) 
    11821181 
    11831182    def testGetParamNames(self): 
     
    12161215        # Create a constraint object 
    12171216        const = Constraint(parent=None, value=7.0) 
    1218         row = 2 
     1217        row = 3 
    12191218 
    12201219        spy = QtSignalSpy(self.widget, self.widget.constraintAddedSignal) 
     
    12351234        # assign complex constraint now 
    12361235        const = Constraint(parent=None, param='radius', func='5*sld') 
    1237         row = 4 
     1236        row = 5 
    12381237        # call the method tested 
    12391238        self.widget.addConstraintToRow(constraint=const, row=row) 
     
    12941293        self.widget.cbModel.setCurrentIndex(model_index) 
    12951294 
     1295        row1 = 1 
     1296        row2 = 5 
     1297 
     1298        param1 = "background" 
     1299        param2 = "radius" 
     1300 
     1301        #default_value1 = "0.001" 
     1302        default_value2 = "20" 
     1303 
    12961304        # select two rows 
    1297         row1 = 1 
    1298         row2 = 4 
    12991305        index1 = self.widget.lstParams.model().index(row1, 0, QtCore.QModelIndex()) 
    13001306        index2 = self.widget.lstParams.model().index(row2, 0, QtCore.QModelIndex()) 
     
    13131319 
    13141320        # delete one of the constraints 
    1315         self.widget.deleteConstraintOnParameter(param='background') 
     1321        self.widget.deleteConstraintOnParameter(param=param1) 
    13161322 
    13171323        # see that the other constraint is still present 
    1318         cons = self.widget.getConstraintForRow(4) # 4 = radius 
    1319         self.assertEqual(cons.param, "radius") 
    1320         self.assertEqual(cons.value, "20") 
     1324        cons = self.widget.getConstraintForRow(row2) 
     1325        self.assertEqual(cons.param, param2) 
     1326        self.assertEqual(cons.value, default_value2) 
    13211327 
    13221328        # kill the other constraint 
     
    13241330 
    13251331        # see that the other constraint is still present 
    1326         self.assertEqual(self.widget.getConstraintsForModel(), [('radius', None)]) 
     1332        self.assertEqual(self.widget.getConstraintsForModel(), [(param2, None)]) 
    13271333 
    13281334    def testGetConstraintForRow(self): 
     
    13441350        self.widget.cbModel.setCurrentIndex(model_index) 
    13451351 
     1352        row1 = 1 
     1353        row2 = 5 
     1354 
    13461355        # select two rows 
    1347         row1 = 1 
    1348         row2 = 4 
    13491356        index1 = self.widget.lstParams.model().index(row1, 0, QtCore.QModelIndex()) 
    13501357        index2 = self.widget.lstParams.model().index(row2, 0, QtCore.QModelIndex()) 
     
    13561363        self.widget.addSimpleConstraint() 
    13571364 
    1358         con_list = [False, True, False, False, True, False] 
     1365        con_list = [False, True, False, False, False, True, False] 
    13591366        new_list = [] 
    13601367        for row in range(self.widget._model_model.rowCount()): 
     
    13741381        self.widget.cbModel.setCurrentIndex(model_index) 
    13751382 
     1383        row1 = 1 
     1384        row2 = 5 
     1385 
    13761386        # select two rows 
    1377         row1 = 1 
    1378         row2 = 4 
    13791387        index1 = self.widget.lstParams.model().index(row1, 0, QtCore.QModelIndex()) 
    13801388        index2 = self.widget.lstParams.model().index(row2, 0, QtCore.QModelIndex()) 
     
    13901398        constraint_objects[0].active = False 
    13911399 
    1392         con_list = [False, False, False, False, True, False] 
     1400        con_list = [False, False, False, False, False, True, False] 
    13931401        new_list = [] 
    13941402        for row in range(self.widget._model_model.rowCount()): 
     
    14111419        self.assertEqual(self.widget.getConstraintsForModel(),[]) 
    14121420 
     1421        row1 = 1 
     1422        row2 = 5 
     1423 
     1424        param1 = "background" 
     1425        param2 = "radius" 
     1426 
     1427        default_value1 = "0.001" 
     1428        default_value2 = "20" 
     1429 
    14131430        # select two rows 
    1414         row1 = 1 
    1415         row2 = 4 
    14161431        index1 = self.widget.lstParams.model().index(row1, 0, QtCore.QModelIndex()) 
    14171432        index2 = self.widget.lstParams.model().index(row2, 0, QtCore.QModelIndex()) 
     
    14251440        # simple constraints 
    14261441        # self.assertEqual(self.widget.getConstraintsForModel(), [('background', '0.001'), ('radius', '20')]) 
    1427         cons = self.widget.getConstraintForRow(1) # 1 - background 
    1428         self.assertEqual(cons.param, "background") 
    1429         self.assertEqual(cons.value, "0.001") 
    1430         cons = self.widget.getConstraintForRow(4) # 4 = radius 
    1431         self.assertEqual(cons.param, "radius") 
    1432         self.assertEqual(cons.value, "20") 
     1442        cons = self.widget.getConstraintForRow(row1) 
     1443        self.assertEqual(cons.param, param1) 
     1444        self.assertEqual(cons.value, default_value1) 
     1445        cons = self.widget.getConstraintForRow(row2) 
     1446        self.assertEqual(cons.param, param2) 
     1447        self.assertEqual(cons.value, default_value2) 
    14331448 
    14341449        objects = self.widget.getConstraintObjectsForModel() 
    14351450        self.assertEqual(len(objects), 2) 
    1436         self.assertEqual(objects[1].value, '20') 
    1437         self.assertEqual(objects[0].param, 'background') 
     1451        self.assertEqual(objects[1].value, default_value2) 
     1452        self.assertEqual(objects[0].param, param1) 
     1453 
     1454        row = 0 
     1455        param = "scale" 
     1456        func = "5*sld" 
    14381457 
    14391458        # add complex constraint 
    1440         const = Constraint(parent=None, param='scale', func='5*sld') 
    1441         row = 0 
     1459        const = Constraint(parent=None, param=param, func=func) 
    14421460        self.widget.addConstraintToRow(constraint=const, row=row) 
    14431461        #self.assertEqual(self.widget.getConstraintsForModel(),[('scale', '5*sld'), ('background', '0.001'), ('radius', None)]) 
    1444         cons = self.widget.getConstraintForRow(4) # 4 = radius 
    1445         self.assertEqual(cons.param, "radius") 
    1446         self.assertEqual(cons.value, "20") 
     1462        cons = self.widget.getConstraintForRow(row2) 
     1463        self.assertEqual(cons.param, param2) 
     1464        self.assertEqual(cons.value, default_value2) 
    14471465 
    14481466        objects = self.widget.getConstraintObjectsForModel() 
    14491467        self.assertEqual(len(objects), 3) 
    1450         self.assertEqual(objects[0].func, '5*sld') 
     1468        self.assertEqual(objects[0].func, func) 
    14511469 
    14521470    def testReplaceConstraintName(self): 
Note: See TracChangeset for help on using the changeset viewer.