Changeset 5032ea68 in sasview for src/sas/qtgui/UnitTesting
- Timestamp:
- Jun 14, 2016 4:51:18 AM (8 years ago)
- 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:
- a281ab8
- Parents:
- 488c49d
- Location:
- src/sas/qtgui/UnitTesting
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
src/sas/qtgui/UnitTesting/DataExplorerTest.py
r488c49d r5032ea68 1 1 import sys 2 2 import unittest 3 #from twisted.trial import unittest 4 #from twisted.internet import reactor, defer, interfaces, threads, protocol, error 3 5 4 6 from PyQt4.QtGui import * … … 20 22 def setUp(self): 21 23 '''Create the GUI''' 24 class MyPerspective(object): 25 def communicator(self): 26 return Communicate() 27 def allowBatch(self): 28 return False 29 def setData(self, data_list=None): 30 return None 31 def title(self): 32 return "Dummy Perspective" 33 22 34 class dummy_manager(object): 23 35 def communicator(self): 24 36 return Communicate() 37 def perspective(self): 38 return MyPerspective() 25 39 26 40 self.form = DataExplorerWindow(None, dummy_manager()) … … 67 81 68 82 def testDeleteButton(self): 83 """ 84 Functionality of the delete button 85 """ 69 86 70 87 deleteButton = self.form.cmdDelete 71 88 72 89 # Mock the confirmation dialog with return=Yes 90 QtGui.QMessageBox.question = MagicMock(return_value=QtGui.QMessageBox.No) 73 91 74 92 # Populate the model 75 76 # Assure the checkbox is on 93 filename = ["cyl_400_20.txt", "Dec07031.ASC", "cyl_400_20.txt"] 94 self.form.readData(filename) 95 96 # Assure the model contains three items 97 self.assertEqual(self.form.model.rowCount(), 3) 98 99 # Assure the checkboxes are on 100 item1 = self.form.model.item(0) 101 item2 = self.form.model.item(1) 102 item3 = self.form.model.item(2) 103 self.assertTrue(item1.checkState() == QtCore.Qt.Checked) 104 self.assertTrue(item2.checkState() == QtCore.Qt.Checked) 105 self.assertTrue(item3.checkState() == QtCore.Qt.Checked) 77 106 78 107 # Click on the delete button … … 80 109 81 110 # Test the warning dialog called once 82 # self.assertTrue(QtGui.QFileDialog.getOpenFileName.called) 111 self.assertTrue(QtGui.QMessageBox.question.called) 112 113 # Assure the model still contains the items 114 self.assertEqual(self.form.model.rowCount(), 3) 115 116 # Now, mock the confirmation dialog with return=Yes 117 QtGui.QMessageBox.question = MagicMock(return_value=QtGui.QMessageBox.Yes) 118 119 # Click on the delete button 120 QTest.mouseClick(deleteButton, Qt.LeftButton) 121 122 # Test the warning dialog called once 123 self.assertTrue(QtGui.QMessageBox.question.called) 83 124 84 125 # Assure the model contains no items 85 86 def testSendToButton(self): 87 sendToButton = self.form.cmdSendTo 88 89 # Mock the current perspective set_data method 90 126 self.assertEqual(self.form.model.rowCount(), 0) 127 128 # Click delete once again to assure no nasty behaviour on empty model 129 QTest.mouseClick(deleteButton, Qt.LeftButton) 130 131 132 def testSendToButton(self): 133 """ 134 Test that clicking the Send To button sends checked data to a perspective 135 """ 136 91 137 # Populate the model 138 filename = ["cyl_400_20.txt"] 139 self.form.readData(filename) 140 141 # setData is the method we want to see called 142 mocked = self.form.parent.perspective().setData 143 mocked = MagicMock(filename) 92 144 93 145 # Assure the checkbox is on 146 self.form.cbSelect.setCurrentIndex(0) 94 147 95 148 # Click on the Send To button 96 QTest.mouseClick(se ndToButton, Qt.LeftButton)149 QTest.mouseClick(self.form.cmdSendTo, Qt.LeftButton) 97 150 98 151 # Test the set_data method called once 99 # self.assertTrue(QtGui.QFileDialog.getOpenFileName.called) 100 101 # Assure the model contains no items 152 # self.assertTrue(mocked.called) 153 154 # open another file 155 filename = ["cyl_400_20.txt"] 156 self.form.readData(filename) 157 158 # Mock the warning message 159 QtGui.QMessageBox = MagicMock() 160 161 # Click on the button 162 QTest.mouseClick(self.form.cmdSendTo, Qt.LeftButton) 163 164 # Assure the message box popped up 165 QtGui.QMessageBox.assert_called_once() 166 102 167 103 168 def testDataSelection(self): … … 159 224 def testReadData(self): 160 225 """ 161 Test the readData() method226 Test the low level readData() method 162 227 """ 163 228 filename = ["cyl_400_20.txt"] … … 172 237 173 238 # Expected two status bar updates 174 self.assertEqual(spy_status_update.count(), 2)239 self.assertEqual(spy_status_update.count(), 1) 175 240 self.assertIn(filename[0], str(spy_status_update.called()[0]['args'][0])) 176 self.assertIn("Loading Data Complete", str(spy_status_update.called()[1]['args'][0])) 177 178 # Expect one Data Received signal 179 self.assertEqual(spy_data_received.count(), 1) 180 181 # Assure returned dictionary has correct data 182 # We don't know the data ID, so need to iterate over dict 183 data_dict = spy_data_received.called()[0]['args'][0] 184 for data_key, data_value in data_dict.iteritems(): 185 self.assertIsInstance(data_value, Data1D) 241 186 242 187 243 # Check that the model contains the item … … 194 250 self.assertEqual(model_name, filename[0]) 195 251 196 # Assure add_data on data_manager was called (last call) 197 self.assertTrue(self.form.manager.add_data.called) 252 def testLoadFile(self): 253 """ 254 Test the threaded call to readData() 255 """ 256 pass 198 257 199 258 def testGetWList(self): … … 206 265 'HFIR 1D files (*.d1d);;DANSE files (*.sans);;NXS files (*.nxs)' 207 266 self.assertEqual(defaults, list) 208 267 209 268 def testLoadComplete(self): 210 269 """ 211 """ 212 # Initialize signal spy instances 270 Test the callback method updating the data object 271 """ 272 273 data_dict = {"a1":Data1D()} 274 275 self.form.manager.add_data = MagicMock() 276 277 # Initialize signal spy instances 213 278 spy_status_update = QtSignalSpy(self.form, self.form.communicate.statusBarUpdateSignal) 214 279 spy_data_received = QtSignalSpy(self.form, self.form.communicate.fileDataReceivedSignal) 215 280 216 # Need an empty Data1D object 217 mockData = Data1D() 218 219 # Call the tested method 220 self.form.loadComplete({"a":mockData}, message="test message") 221 222 # test the signals 223 self.assertEqual(spy_status_update.count(), 1) 224 self.assertIn("message", str(spy_status_update.called()[0]['args'][0])) 281 # Read in the file 282 self.form.loadComplete(data_dict, message="Loading Data Complete") 283 284 # "Loading data complete" no longer sent in LoadFile but in callback 285 self.assertIn("Loading Data Complete", str(spy_status_update.called()[0]['args'][0])) 286 287 # Expect one Data Received signal 225 288 self.assertEqual(spy_data_received.count(), 1) 226 289 227 # The data_manager update is going away, so don't bother testing 228 290 # Assure returned dictionary has correct data 291 # We don't know the data ID, so need to iterate over dict 292 data_dict = spy_data_received.called()[0]['args'][0] 293 for data_key, data_value in data_dict.iteritems(): 294 self.assertIsInstance(data_value, Data1D) 295 296 # Assure add_data on data_manager was called (last call) 297 self.assertTrue(self.form.manager.add_data.called) 298 299 229 300 if __name__ == "__main__": 230 301 unittest.main() -
src/sas/qtgui/UnitTesting/GuiManagerTest.py
r488c49d r5032ea68 9 9 # Local 10 10 from GuiManager import GuiManager 11 from UI.MainWindowUI import MainWindow 11 12 12 #app = QApplication(sys.argv)13 app = QApplication(sys.argv) 13 14 14 15 class GuiManagerTest(unittest.TestCase): 15 '''Test the WelcomePanel'''16 '''Test the Main Window functionality''' 16 17 def setUp(self): 17 18 '''Create the tested object''' 19 class MainSasViewWindow(MainWindow): 20 # Main window of the application 21 def __init__(self, reactor, parent=None): 22 super(MainSasViewWindow, self).__init__(parent) 23 24 # define workspace for dialogs. 25 self.workspace = QWorkspace(self) 26 self.setCentralWidget(self.workspace) 18 27 19 self.manager = GuiManager( None)28 self.manager = GuiManager(MainSasViewWindow(None), None) 20 29 21 30 def tearDown(self): … … 52 61 pass 53 62 63 def testActionLoadData(self): 64 """ 65 Menu File/Load Data File(s) 66 """ 67 # Mock the system file open method 68 QFileDialog.getOpenFileName = MagicMock(return_value=None) 69 70 # invoke the action 71 72 # Test the getOpenFileName() dialog called once 73 #self.assertTrue(QtGui.QFileDialog.getOpenFileName.called) 74 #QtGui.QFileDialog.getOpenFileName.assert_called_once() 75 76 54 77 # test each action separately 55 78 -
src/sas/qtgui/UnitTesting/TestUtilsTest.py
r488c49d r5032ea68 18 18 def testQtSignalSpy(self): 19 19 '''Create the Spy the correct way''' 20 test_string = 'my precious' 20 21 22 def signalReceived(signal): 23 # Test the signal callback 24 self.assertEqual(signal, test_string) 25 26 communicator = Communicate() 27 communicator.statusBarUpdateSignal.connect(signalReceived) 28 29 # Define the signal spy for testing 21 30 widget = QWidget() 22 signal = Communicate.statusBarUpdateSignal 23 self.spy = QtSignalSpy(widget, signal) 31 spy = QtSignalSpy(widget, communicator.statusBarUpdateSignal) 24 32 25 33 # Emit a signal 26 signal.emit('aa')34 communicator.statusBarUpdateSignal.emit(test_string) 27 35 28 # Test the spy object 36 # Was the signal caught by the signal spy? 37 self.assertEqual(spy.count(), 1) 29 38 30 39 if __name__ == "__main__":
Note: See TracChangeset
for help on using the changeset viewer.