Ignore:
Timestamp:
Jan 29, 2018 7:33:58 AM (6 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:
da9a0722
Parents:
9c0ce68
Message:

unit tests for constraints: FittingWidget?, FittingPerspective?

File:
1 edited

Legend:

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

    r676f137 r63319b0  
    1717from sas.qtgui.Utilities.GuiUtils import * 
    1818from sas.qtgui.Perspectives.Fitting.FittingWidget import * 
     19from sas.qtgui.Perspectives.Fitting.Constraints import Constraint 
     20 
    1921from sas.qtgui.UnitTesting.TestUtils import QtSignalSpy 
    2022 
     
    661663        test_data = Data1D(x=[1,2], y=[1,2]) 
    662664        item = QtGui.QStandardItem() 
    663         updateModelItem(item, [test_data], "test") 
     665        updateModelItem(item, test_data, "test") 
    664666        # Force same data into logic 
    665667        self.widget.data = item 
     
    687689        # Force same data into logic 
    688690        item = QtGui.QStandardItem() 
    689         updateModelItem(item, [test_data], "test") 
     691        updateModelItem(item, test_data, "test") 
    690692        # Force same data into logic 
    691693        self.widget.data = item 
     
    713715        test_data = Data1D(x=[1,2], y=[1,2]) 
    714716        item = QtGui.QStandardItem() 
    715         updateModelItem(item, [test_data], "test") 
     717        updateModelItem(item, test_data, "test") 
    716718        # Force same data into logic 
    717719        self.widget.data = item 
     
    757759        # Force same data into logic 
    758760        item = QtGui.QStandardItem() 
    759         updateModelItem(item, [test_data], "test") 
     761        updateModelItem(item, test_data, "test") 
    760762        # Force same data into logic 
    761763        self.widget.data = item 
     
    10251027        self.assertNotIn(new_value, self.widget.kernel_module.details[name_modified_param] ) 
    10261028 
     1029    def testModelContextMenu(self): 
     1030        """ 
     1031        Test the right click context menu in the parameter table 
     1032        """ 
     1033        # select model: cylinder / cylinder 
     1034        category_index = self.widget.cbCategory.findText("Cylinder") 
     1035        self.widget.cbCategory.setCurrentIndex(category_index) 
     1036 
     1037        model_index = self.widget.cbModel.findText("cylinder") 
     1038        self.widget.cbModel.setCurrentIndex(model_index) 
     1039 
     1040        # no rows selected 
     1041        menu = self.widget.modelContextMenu([]) 
     1042        self.assertEqual(len(menu.actions()), 0) 
     1043 
     1044        # 1 row selected 
     1045        menu = self.widget.modelContextMenu([1]) 
     1046        self.assertEqual(len(menu.actions()), 4) 
     1047 
     1048        # 2 rows selected 
     1049        menu = self.widget.modelContextMenu([1,3]) 
     1050        self.assertEqual(len(menu.actions()), 5) 
     1051 
     1052        # 3 rows selected 
     1053        menu = self.widget.modelContextMenu([1,2,3]) 
     1054        self.assertEqual(len(menu.actions()), 4) 
     1055 
     1056        # over 9000 
     1057        with self.assertRaises(AttributeError): 
     1058            menu = self.widget.modelContextMenu([i for i in range(9001)]) 
     1059        self.assertEqual(len(menu.actions()), 4) 
     1060 
     1061    def testShowModelContextMenu(self): 
     1062        # select model: cylinder / cylinder 
     1063        category_index = self.widget.cbCategory.findText("Cylinder") 
     1064        self.widget.cbCategory.setCurrentIndex(category_index) 
     1065 
     1066        model_index = self.widget.cbModel.findText("cylinder") 
     1067        self.widget.cbModel.setCurrentIndex(model_index) 
     1068 
     1069        # No selection 
     1070        logging.error = MagicMock() 
     1071        self.widget.showModelDescription = MagicMock() 
     1072        # Show the menu 
     1073        self.widget.showModelContextMenu(QtCore.QPoint(10,20)) 
     1074 
     1075        # Assure the description menu is shown 
     1076        self.assertTrue(self.widget.showModelDescription.called) 
     1077        self.assertFalse(logging.error.called) 
     1078 
     1079        # "select" two rows 
     1080        index1 = self.widget.lstParams.model().index(1, 0, QtCore.QModelIndex()) 
     1081        index2 = self.widget.lstParams.model().index(2, 0, QtCore.QModelIndex()) 
     1082        selection_model = self.widget.lstParams.selectionModel() 
     1083        selection_model.select(index1, selection_model.Select | selection_model.Rows) 
     1084        selection_model.select(index2, selection_model.Select | selection_model.Rows) 
     1085 
     1086        QtWidgets.QMenu.exec_ = MagicMock() 
     1087        logging.error = MagicMock() 
     1088        # Show the menu 
     1089        self.widget.showModelContextMenu(QtCore.QPoint(10,20)) 
     1090 
     1091        # Assure the menu is shown 
     1092        self.assertFalse(logging.error.called) 
     1093        self.assertTrue(QtWidgets.QMenu.exec_.called) 
     1094 
     1095    def testShowMultiConstraint(self): 
     1096        """ 
     1097        Test the widget update on new multi constraint 
     1098        """ 
     1099        # select model: cylinder / cylinder 
     1100        category_index = self.widget.cbCategory.findText("Cylinder") 
     1101        self.widget.cbCategory.setCurrentIndex(category_index) 
     1102 
     1103        model_index = self.widget.cbModel.findText("cylinder") 
     1104        self.widget.cbModel.setCurrentIndex(model_index) 
     1105 
     1106        # nothing selected 
     1107        with self.assertRaises(AssertionError): 
     1108            self.widget.showMultiConstraint() 
     1109 
     1110        # one row selected 
     1111        index = self.widget.lstParams.model().index(1, 0, QtCore.QModelIndex()) 
     1112        selection_model = self.widget.lstParams.selectionModel() 
     1113        selection_model.select(index, selection_model.Select | selection_model.Rows) 
     1114        with self.assertRaises(AssertionError): 
     1115            # should also throw 
     1116            self.widget.showMultiConstraint() 
     1117 
     1118        # two rows selected 
     1119        index1 = self.widget.lstParams.model().index(1, 0, QtCore.QModelIndex()) 
     1120        index2 = self.widget.lstParams.model().index(2, 0, QtCore.QModelIndex()) 
     1121        selection_model = self.widget.lstParams.selectionModel() 
     1122        selection_model.select(index1, selection_model.Select | selection_model.Rows) 
     1123        selection_model.select(index2, selection_model.Select | selection_model.Rows) 
     1124 
     1125        # return non-OK from dialog 
     1126        QtWidgets.QDialog.exec_ = MagicMock() 
     1127        self.widget.showMultiConstraint() 
     1128        # Check the dialog called 
     1129        self.assertTrue(QtWidgets.QDialog.exec_.called) 
     1130 
     1131        # return OK from dialog 
     1132        QtWidgets.QDialog.exec_ = MagicMock(return_value=QtWidgets.QDialog.Accepted) 
     1133        spy = QtSignalSpy(self.widget, self.widget.constraintAddedSignal) 
     1134 
     1135        self.widget.showMultiConstraint() 
     1136 
     1137        # Make sure the signal has been emitted 
     1138        self.assertEqual(spy.count(), 1) 
     1139 
     1140        # Check the argument value - should be row '1' 
     1141        self.assertEqual(spy.called()[0]['args'][0], [1]) 
     1142 
     1143    def testGetRowFromName(self): 
     1144        """ 
     1145        Helper function for parameter table 
     1146        """ 
     1147        # select model: cylinder / cylinder 
     1148        category_index = self.widget.cbCategory.findText("Cylinder") 
     1149        self.widget.cbCategory.setCurrentIndex(category_index) 
     1150 
     1151        model_index = self.widget.cbModel.findText("cylinder") 
     1152        self.widget.cbModel.setCurrentIndex(model_index) 
     1153 
     1154        # several random parameters 
     1155        self.assertEqual(self.widget.getRowFromName('scale'), 0) 
     1156        self.assertEqual(self.widget.getRowFromName('length'), 5) 
     1157 
     1158    def testGetParamNames(self): 
     1159        """ 
     1160        Helper function for parameter table 
     1161        """ 
     1162        # select model: cylinder / cylinder 
     1163        category_index = self.widget.cbCategory.findText("Cylinder") 
     1164        self.widget.cbCategory.setCurrentIndex(category_index) 
     1165 
     1166        model_index = self.widget.cbModel.findText("cylinder") 
     1167        self.widget.cbModel.setCurrentIndex(model_index) 
     1168 
     1169        cylinder_params = ['scale','background','sld','sld_solvent','radius','length'] 
     1170        # assure all parameters are returned 
     1171        self.assertEqual(self.widget.getParamNames(), cylinder_params) 
     1172 
     1173        # Switch to another model 
     1174        model_index = self.widget.cbModel.findText("pringle") 
     1175        self.widget.cbModel.setCurrentIndex(model_index) 
     1176 
     1177        # make sure the parameters are different than before 
     1178        self.assertFalse(self.widget.getParamNames() == cylinder_params) 
     1179 
     1180    def testAddConstraintToRow(self): 
     1181        """ 
     1182        Test the constraint row add operation 
     1183        """ 
     1184        # select model: cylinder / cylinder 
     1185        category_index = self.widget.cbCategory.findText("Cylinder") 
     1186        self.widget.cbCategory.setCurrentIndex(category_index) 
     1187 
     1188        model_index = self.widget.cbModel.findText("cylinder") 
     1189        self.widget.cbModel.setCurrentIndex(model_index) 
     1190 
     1191        # Create a constraint object 
     1192        const = Constraint(parent=None, value=7.0) 
     1193        row = 2 
     1194 
     1195        spy = QtSignalSpy(self.widget, self.widget.constraintAddedSignal) 
     1196 
     1197        # call the method tested 
     1198        self.widget.addConstraintToRow(constraint=const, row=row) 
     1199 
     1200        # Make sure the signal has been emitted 
     1201        self.assertEqual(spy.count(), 1) 
     1202 
     1203        # Check the argument value - should be row 'row' 
     1204        self.assertEqual(spy.called()[0]['args'][0], [row]) 
     1205 
     1206        # Assure the row has the constraint 
     1207        self.assertEqual(self.widget.getConstraintForRow(row), const) 
     1208        # but not complex constraint! 
     1209        self.assertFalse(self.widget.rowHasConstraint(row)) 
     1210 
     1211        # assign complex constraint now 
     1212        const = Constraint(parent=None, param='radius', func='5*sld') 
     1213        row = 4 
     1214        # call the method tested 
     1215        self.widget.addConstraintToRow(constraint=const, row=row) 
     1216 
     1217        # Make sure the signal has been emitted 
     1218        self.assertEqual(spy.count(), 2) 
     1219 
     1220        # Check the argument value - should be row 'row' 
     1221        self.assertEqual(spy.called()[1]['args'][0], [row]) 
     1222 
     1223        # Assure the row has the constraint 
     1224        self.assertEqual(self.widget.getConstraintForRow(row), const) 
     1225        # and it is a complex constraint 
     1226        self.assertTrue(self.widget.rowHasConstraint(row)) 
     1227 
     1228    def testAddSimpleConstraint(self): 
     1229        """ 
     1230        Test the constraint add operation 
     1231        """ 
     1232        # select model: cylinder / cylinder 
     1233        category_index = self.widget.cbCategory.findText("Cylinder") 
     1234        self.widget.cbCategory.setCurrentIndex(category_index) 
     1235 
     1236        model_index = self.widget.cbModel.findText("cylinder") 
     1237        self.widget.cbModel.setCurrentIndex(model_index) 
     1238 
     1239        # select two rows 
     1240        row1 = 1 
     1241        row2 = 4 
     1242        index1 = self.widget.lstParams.model().index(row1, 0, QtCore.QModelIndex()) 
     1243        index2 = self.widget.lstParams.model().index(row2, 0, QtCore.QModelIndex()) 
     1244        selection_model = self.widget.lstParams.selectionModel() 
     1245        selection_model.select(index1, selection_model.Select | selection_model.Rows) 
     1246        selection_model.select(index2, selection_model.Select | selection_model.Rows) 
     1247 
     1248        # define the signal spy 
     1249        spy = QtSignalSpy(self.widget, self.widget.constraintAddedSignal) 
     1250 
     1251        # call the method tested 
     1252        self.widget.addSimpleConstraint() 
     1253 
     1254        # Make sure the signal has been emitted 
     1255        self.assertEqual(spy.count(), 2) 
     1256 
     1257        # Check the argument value 
     1258        self.assertEqual(spy.called()[0]['args'][0], [row1]) 
     1259        self.assertEqual(spy.called()[1]['args'][0], [row2]) 
     1260 
     1261        # Other properties 
     1262        self.assertEqual(self.widget.getConstraintsForModel(), [('background', '0.001'), ('radius', '20')]) 
     1263 
     1264    def testDeleteConstraintOnParameter(self): 
     1265        """ 
     1266        Test the constraint deletion in model/view 
     1267        """ 
     1268        # select model: cylinder / cylinder 
     1269        category_index = self.widget.cbCategory.findText("Cylinder") 
     1270        self.widget.cbCategory.setCurrentIndex(category_index) 
     1271 
     1272        model_index = self.widget.cbModel.findText("cylinder") 
     1273        self.widget.cbModel.setCurrentIndex(model_index) 
     1274 
     1275        # select two rows 
     1276        row1 = 1 
     1277        row2 = 4 
     1278        index1 = self.widget.lstParams.model().index(row1, 0, QtCore.QModelIndex()) 
     1279        index2 = self.widget.lstParams.model().index(row2, 0, QtCore.QModelIndex()) 
     1280        selection_model = self.widget.lstParams.selectionModel() 
     1281        selection_model.select(index1, selection_model.Select | selection_model.Rows) 
     1282        selection_model.select(index2, selection_model.Select | selection_model.Rows) 
     1283 
     1284        # add constraints 
     1285        self.widget.addSimpleConstraint() 
     1286 
     1287        # deselect the model 
     1288        selection_model.clear() 
     1289 
     1290        # select a single row 
     1291        selection_model.select(index1, selection_model.Select | selection_model.Rows) 
     1292 
     1293        # delete one of the constraints 
     1294        self.widget.deleteConstraintOnParameter(param='background') 
     1295 
     1296        # see that the other constraint is still present 
     1297        self.assertEqual(self.widget.getConstraintsForModel(), [('radius', '20')]) 
     1298 
     1299        # kill the other constraint 
     1300        self.widget.deleteConstraint() 
     1301 
     1302        # see that the other constraint is still present 
     1303        self.assertEqual(self.widget.getConstraintsForModel(), []) 
     1304 
     1305    def testGetConstraintForRow(self): 
     1306        """ 
     1307        Helper function for parameter table 
     1308        """ 
     1309        # tested extensively elsewhere 
     1310        pass 
     1311 
     1312    def testRowHasConstraint(self): 
     1313        """ 
     1314        Helper function for parameter table 
     1315        """ 
     1316        # select model: cylinder / cylinder 
     1317        category_index = self.widget.cbCategory.findText("Cylinder") 
     1318        self.widget.cbCategory.setCurrentIndex(category_index) 
     1319 
     1320        model_index = self.widget.cbModel.findText("cylinder") 
     1321        self.widget.cbModel.setCurrentIndex(model_index) 
     1322 
     1323        # select two rows 
     1324        row1 = 1 
     1325        row2 = 4 
     1326        index1 = self.widget.lstParams.model().index(row1, 0, QtCore.QModelIndex()) 
     1327        index2 = self.widget.lstParams.model().index(row2, 0, QtCore.QModelIndex()) 
     1328        selection_model = self.widget.lstParams.selectionModel() 
     1329        selection_model.select(index1, selection_model.Select | selection_model.Rows) 
     1330        selection_model.select(index2, selection_model.Select | selection_model.Rows) 
     1331 
     1332        # add constraints 
     1333        self.widget.addSimpleConstraint() 
     1334 
     1335        con_list = [False, True, False, False, True, False] 
     1336        new_list = [] 
     1337        for row in range(self.widget._model_model.rowCount()): 
     1338            new_list.append(self.widget.rowHasConstraint(row)) 
     1339 
     1340        self.assertEqual(new_list, con_list) 
     1341 
     1342    def testRowHasActiveConstraint(self): 
     1343        """ 
     1344        Helper function for parameter table 
     1345        """ 
     1346        # select model: cylinder / cylinder 
     1347        category_index = self.widget.cbCategory.findText("Cylinder") 
     1348        self.widget.cbCategory.setCurrentIndex(category_index) 
     1349 
     1350        model_index = self.widget.cbModel.findText("cylinder") 
     1351        self.widget.cbModel.setCurrentIndex(model_index) 
     1352 
     1353        # select two rows 
     1354        row1 = 1 
     1355        row2 = 4 
     1356        index1 = self.widget.lstParams.model().index(row1, 0, QtCore.QModelIndex()) 
     1357        index2 = self.widget.lstParams.model().index(row2, 0, QtCore.QModelIndex()) 
     1358        selection_model = self.widget.lstParams.selectionModel() 
     1359        selection_model.select(index1, selection_model.Select | selection_model.Rows) 
     1360        selection_model.select(index2, selection_model.Select | selection_model.Rows) 
     1361 
     1362        # add constraints 
     1363        self.widget.addSimpleConstraint() 
     1364 
     1365        # deactivate the first constraint 
     1366        constraint_objects = self.widget.getConstraintObjectsForModel() 
     1367        constraint_objects[0].active = False 
     1368 
     1369        con_list = [False, False, False, False, True, False] 
     1370        new_list = [] 
     1371        for row in range(self.widget._model_model.rowCount()): 
     1372            new_list.append(self.widget.rowHasActiveConstraint(row)) 
     1373 
     1374        self.assertEqual(new_list, con_list) 
     1375 
     1376    def testGetConstraintsForModel(self): 
     1377        """ 
     1378        Test the constraint getter for constraint texts 
     1379        """ 
     1380        # select model: cylinder / cylinder 
     1381        category_index = self.widget.cbCategory.findText("Cylinder") 
     1382        self.widget.cbCategory.setCurrentIndex(category_index) 
     1383 
     1384        model_index = self.widget.cbModel.findText("cylinder") 
     1385        self.widget.cbModel.setCurrentIndex(model_index) 
     1386 
     1387        # no constraints 
     1388        self.assertEqual(self.widget.getConstraintsForModel(),[]) 
     1389 
     1390        # select two rows 
     1391        row1 = 1 
     1392        row2 = 4 
     1393        index1 = self.widget.lstParams.model().index(row1, 0, QtCore.QModelIndex()) 
     1394        index2 = self.widget.lstParams.model().index(row2, 0, QtCore.QModelIndex()) 
     1395        selection_model = self.widget.lstParams.selectionModel() 
     1396        selection_model.select(index1, selection_model.Select | selection_model.Rows) 
     1397        selection_model.select(index2, selection_model.Select | selection_model.Rows) 
     1398 
     1399        # add constraints 
     1400        self.widget.addSimpleConstraint() 
     1401 
     1402        # simple constraints 
     1403        self.assertEqual(self.widget.getConstraintsForModel(), [('background', '0.001'), ('radius', '20')]) 
     1404        objects = self.widget.getConstraintObjectsForModel() 
     1405        self.assertEqual(len(objects), 2) 
     1406        self.assertEqual(objects[1].value, '20') 
     1407        self.assertEqual(objects[0].param, 'background') 
     1408 
     1409 
     1410        # add complex constraint 
     1411        const = Constraint(parent=None, param='scale', func='5*sld') 
     1412        row = 0 
     1413        self.widget.addConstraintToRow(constraint=const, row=row) 
     1414        self.assertEqual(self.widget.getConstraintsForModel(),[('scale', '5*sld'), ('background', '0.001'), ('radius', '20')]) 
     1415        objects = self.widget.getConstraintObjectsForModel() 
     1416        self.assertEqual(len(objects), 3) 
     1417        self.assertEqual(objects[0].func, '5*sld') 
     1418 
     1419    def testReplaceConstraintName(self): 
     1420        """ 
     1421        Test the replacement of constraint moniker 
     1422        """ 
     1423        # select model: cylinder / cylinder 
     1424        category_index = self.widget.cbCategory.findText("Cylinder") 
     1425        self.widget.cbCategory.setCurrentIndex(category_index) 
     1426 
     1427        model_index = self.widget.cbModel.findText("cylinder") 
     1428        self.widget.cbModel.setCurrentIndex(model_index) 
     1429 
     1430        old_name = 'M5' 
     1431        new_name = 'poopy' 
     1432        # add complex constraint 
     1433        const = Constraint(parent=None, param='scale', func='%s.5*sld'%old_name) 
     1434        row = 0 
     1435        self.widget.addConstraintToRow(constraint=const, row=row) 
     1436 
     1437        # Replace 'm5' with 'poopy' 
     1438        self.widget.replaceConstraintName(old_name, new_name) 
     1439 
     1440        self.assertEqual(self.widget.getConstraintsForModel(),[('scale', 'poopy.5*sld')]) 
     1441 
    10271442 
    10281443if __name__ == "__main__": 
Note: See TracChangeset for help on using the changeset viewer.