[f721030] | 1 | import sys |
---|
| 2 | import unittest |
---|
| 3 | |
---|
| 4 | from PyQt4.QtGui import * |
---|
| 5 | from PyQt4.QtTest import QTest |
---|
| 6 | from PyQt4.QtCore import * |
---|
| 7 | from mock import MagicMock |
---|
| 8 | |
---|
| 9 | # Local |
---|
| 10 | from sas.sasgui.guiframe.dataFitting import Data1D |
---|
| 11 | from DataExplorer import DataExplorerWindow |
---|
| 12 | from GuiManager import GuiManager |
---|
| 13 | from GuiUtils import * |
---|
| 14 | from UnitTesting.TestUtils import QtSignalSpy |
---|
| 15 | |
---|
| 16 | app = QApplication(sys.argv) |
---|
| 17 | |
---|
| 18 | class DataExplorerTest(unittest.TestCase): |
---|
| 19 | '''Test the Data Explorer GUI''' |
---|
| 20 | def setUp(self): |
---|
| 21 | '''Create the GUI''' |
---|
| 22 | class dummy_manager(object): |
---|
| 23 | def communicator(self): |
---|
| 24 | return Communicate() |
---|
| 25 | |
---|
| 26 | self.form = DataExplorerWindow(None, dummy_manager()) |
---|
| 27 | |
---|
| 28 | def tearDown(self): |
---|
| 29 | '''Destroy the GUI''' |
---|
| 30 | self.form.close() |
---|
| 31 | self.form = None |
---|
| 32 | |
---|
| 33 | def testDefaults(self): |
---|
| 34 | '''Test the GUI in its default state''' |
---|
| 35 | self.assertIsInstance(self.form, QTabWidget) |
---|
| 36 | self.assertIsInstance(self.form.treeView, QTreeView) |
---|
| 37 | self.assertIsInstance(self.form.listView, QListView) |
---|
| 38 | self.assertEqual(self.form.count(), 2) |
---|
| 39 | |
---|
| 40 | self.assertEqual(self.form.cmdLoad.text(), "Load") |
---|
| 41 | self.assertEqual(self.form.cmdDelete.text(), "Delete") |
---|
| 42 | self.assertEqual(self.form.cmdSendTo.text(), "Send to") |
---|
| 43 | self.assertEqual(self.form.chkBatch.text(), "Batch mode") |
---|
| 44 | self.assertFalse(self.form.chkBatch.isChecked()) |
---|
| 45 | |
---|
| 46 | self.assertEqual(self.form.cbSelect.count(), 6) |
---|
[488c49d] | 47 | self.assertEqual(self.form.cbSelect.currentIndex(), 0) |
---|
[f721030] | 48 | |
---|
| 49 | # Class is in the default state even without pressing OK |
---|
| 50 | self.assertEqual(self.form.treeView.model().rowCount(), 0) |
---|
| 51 | self.assertEqual(self.form.treeView.model().columnCount(), 0) |
---|
| 52 | self.assertEqual(self.form.model.rowCount(), 0) |
---|
| 53 | self.assertEqual(self.form.model.columnCount(), 0) |
---|
| 54 | |
---|
| 55 | def testLoadButton(self): |
---|
| 56 | loadButton = self.form.cmdLoad |
---|
| 57 | |
---|
| 58 | # Mock the system file open method |
---|
| 59 | QtGui.QFileDialog.getOpenFileName = MagicMock(return_value=None) |
---|
| 60 | |
---|
| 61 | # Click on the Load button |
---|
| 62 | QTest.mouseClick(loadButton, Qt.LeftButton) |
---|
| 63 | |
---|
| 64 | # Test the getOpenFileName() dialog called once |
---|
| 65 | self.assertTrue(QtGui.QFileDialog.getOpenFileName.called) |
---|
| 66 | QtGui.QFileDialog.getOpenFileName.assert_called_once() |
---|
| 67 | |
---|
| 68 | def testDeleteButton(self): |
---|
| 69 | |
---|
| 70 | deleteButton = self.form.cmdDelete |
---|
| 71 | |
---|
| 72 | # Mock the confirmation dialog with return=Yes |
---|
| 73 | |
---|
| 74 | # Populate the model |
---|
| 75 | |
---|
| 76 | # Assure the checkbox is on |
---|
| 77 | |
---|
| 78 | # Click on the delete button |
---|
| 79 | QTest.mouseClick(deleteButton, Qt.LeftButton) |
---|
| 80 | |
---|
| 81 | # Test the warning dialog called once |
---|
| 82 | # self.assertTrue(QtGui.QFileDialog.getOpenFileName.called) |
---|
| 83 | |
---|
| 84 | # 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 | |
---|
| 91 | # Populate the model |
---|
| 92 | |
---|
| 93 | # Assure the checkbox is on |
---|
| 94 | |
---|
| 95 | # Click on the Send To button |
---|
| 96 | QTest.mouseClick(sendToButton, Qt.LeftButton) |
---|
| 97 | |
---|
| 98 | # Test the set_data method called once |
---|
| 99 | # self.assertTrue(QtGui.QFileDialog.getOpenFileName.called) |
---|
| 100 | |
---|
| 101 | # Assure the model contains no items |
---|
| 102 | |
---|
[488c49d] | 103 | def testDataSelection(self): |
---|
| 104 | """ |
---|
| 105 | Tests the functionality of the Selection Option combobox |
---|
| 106 | """ |
---|
| 107 | # Populate the model with 1d and 2d data |
---|
| 108 | filename = ["cyl_400_20.txt", "Dec07031.ASC"] |
---|
| 109 | self.form.readData(filename) |
---|
| 110 | |
---|
| 111 | # Unselect all data |
---|
| 112 | self.form.cbSelect.setCurrentIndex(1) |
---|
| 113 | |
---|
| 114 | # Test the current selection |
---|
| 115 | item1D = self.form.model.item(0) |
---|
| 116 | item2D = self.form.model.item(1) |
---|
| 117 | self.assertTrue(item1D.checkState() == QtCore.Qt.Unchecked) |
---|
| 118 | self.assertTrue(item2D.checkState() == QtCore.Qt.Unchecked) |
---|
| 119 | |
---|
| 120 | # Select all data |
---|
| 121 | self.form.cbSelect.setCurrentIndex(0) |
---|
| 122 | |
---|
| 123 | # Test the current selection |
---|
| 124 | self.assertTrue(item1D.checkState() == QtCore.Qt.Checked) |
---|
| 125 | self.assertTrue(item2D.checkState() == QtCore.Qt.Checked) |
---|
| 126 | |
---|
| 127 | # select 1d data |
---|
| 128 | self.form.cbSelect.setCurrentIndex(2) |
---|
| 129 | |
---|
| 130 | # Test the current selection |
---|
| 131 | self.assertTrue(item1D.checkState() == QtCore.Qt.Checked) |
---|
| 132 | self.assertTrue(item2D.checkState() == QtCore.Qt.Unchecked) |
---|
| 133 | |
---|
| 134 | # unselect 1d data |
---|
| 135 | self.form.cbSelect.setCurrentIndex(3) |
---|
| 136 | |
---|
| 137 | # Test the current selection |
---|
| 138 | self.assertTrue(item1D.checkState() == QtCore.Qt.Unchecked) |
---|
| 139 | self.assertTrue(item2D.checkState() == QtCore.Qt.Unchecked) |
---|
| 140 | |
---|
| 141 | # select 2d data |
---|
| 142 | self.form.cbSelect.setCurrentIndex(4) |
---|
| 143 | |
---|
| 144 | # Test the current selection |
---|
| 145 | self.assertTrue(item1D.checkState() == QtCore.Qt.Unchecked) |
---|
| 146 | self.assertTrue(item2D.checkState() == QtCore.Qt.Checked) |
---|
| 147 | |
---|
| 148 | # unselect 2d data |
---|
| 149 | self.form.cbSelect.setCurrentIndex(5) |
---|
| 150 | |
---|
| 151 | # Test the current selection |
---|
| 152 | self.assertTrue(item1D.checkState() == QtCore.Qt.Unchecked) |
---|
| 153 | self.assertTrue(item2D.checkState() == QtCore.Qt.Unchecked) |
---|
| 154 | |
---|
| 155 | # choose impossible index and assure the code raises |
---|
| 156 | #with self.assertRaises(Exception): |
---|
| 157 | # self.form.cbSelect.setCurrentIndex(6) |
---|
| 158 | |
---|
[f721030] | 159 | def testReadData(self): |
---|
| 160 | """ |
---|
| 161 | Test the readData() method |
---|
| 162 | """ |
---|
| 163 | filename = ["cyl_400_20.txt"] |
---|
| 164 | self.form.manager.add_data = MagicMock() |
---|
| 165 | |
---|
| 166 | # Initialize signal spy instances |
---|
| 167 | spy_status_update = QtSignalSpy(self.form, self.form.communicate.statusBarUpdateSignal) |
---|
| 168 | spy_data_received = QtSignalSpy(self.form, self.form.communicate.fileDataReceivedSignal) |
---|
| 169 | |
---|
| 170 | # Read in the file |
---|
| 171 | self.form.readData(filename) |
---|
| 172 | |
---|
| 173 | # Expected two status bar updates |
---|
| 174 | self.assertEqual(spy_status_update.count(), 2) |
---|
| 175 | 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) |
---|
| 186 | |
---|
| 187 | # Check that the model contains the item |
---|
| 188 | self.assertEqual(self.form.model.rowCount(), 1) |
---|
| 189 | self.assertEqual(self.form.model.columnCount(), 1) |
---|
| 190 | |
---|
| 191 | # The 0th item header should be the name of the file |
---|
| 192 | model_item = self.form.model.index(0,0) |
---|
| 193 | model_name = str(self.form.model.data(model_item).toString()) |
---|
| 194 | self.assertEqual(model_name, filename[0]) |
---|
| 195 | |
---|
| 196 | # Assure add_data on data_manager was called (last call) |
---|
| 197 | self.assertTrue(self.form.manager.add_data.called) |
---|
| 198 | |
---|
| 199 | def testGetWList(self): |
---|
| 200 | """ |
---|
| 201 | """ |
---|
| 202 | list = self.form.getWlist() |
---|
| 203 | defaults = 'All (*.*);;canSAS files (*.xml);;SESANS files' +\ |
---|
| 204 | ' (*.ses);;ASCII files (*.txt);;IGOR 2D files (*.asc);;' +\ |
---|
| 205 | 'IGOR/DAT 2D Q_map files (*.dat);;IGOR 1D files (*.abs);;'+\ |
---|
| 206 | 'HFIR 1D files (*.d1d);;DANSE files (*.sans);;NXS files (*.nxs)' |
---|
| 207 | self.assertEqual(defaults, list) |
---|
| 208 | |
---|
| 209 | def testLoadComplete(self): |
---|
| 210 | """ |
---|
| 211 | """ |
---|
| 212 | # Initialize signal spy instances |
---|
| 213 | spy_status_update = QtSignalSpy(self.form, self.form.communicate.statusBarUpdateSignal) |
---|
| 214 | spy_data_received = QtSignalSpy(self.form, self.form.communicate.fileDataReceivedSignal) |
---|
| 215 | |
---|
| 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])) |
---|
| 225 | self.assertEqual(spy_data_received.count(), 1) |
---|
| 226 | |
---|
| 227 | # The data_manager update is going away, so don't bother testing |
---|
| 228 | |
---|
| 229 | if __name__ == "__main__": |
---|
| 230 | unittest.main() |
---|