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 sas.sascalc.dataloader.loader import Loader |
---|
12 | from sas.sasgui.guiframe.data_manager import DataManager |
---|
13 | |
---|
14 | from DataExplorer import DataExplorerWindow |
---|
15 | from GuiManager import GuiManager |
---|
16 | from GuiUtils import * |
---|
17 | from UnitTesting.TestUtils import QtSignalSpy |
---|
18 | from Plotter import Plotter |
---|
19 | |
---|
20 | app = QApplication(sys.argv) |
---|
21 | |
---|
22 | class DataExplorerTest(unittest.TestCase): |
---|
23 | '''Test the Data Explorer GUI''' |
---|
24 | def setUp(self): |
---|
25 | '''Create the GUI''' |
---|
26 | class MyPerspective(object): |
---|
27 | def communicator(self): |
---|
28 | return Communicate() |
---|
29 | def allowBatch(self): |
---|
30 | return False |
---|
31 | def setData(self, data_item=None): |
---|
32 | return None |
---|
33 | def title(self): |
---|
34 | return "Dummy Perspective" |
---|
35 | |
---|
36 | class dummy_manager(object): |
---|
37 | def communicator(self): |
---|
38 | return Communicate() |
---|
39 | def perspective(self): |
---|
40 | return MyPerspective() |
---|
41 | |
---|
42 | self.form = DataExplorerWindow(None, dummy_manager()) |
---|
43 | |
---|
44 | def tearDown(self): |
---|
45 | '''Destroy the GUI''' |
---|
46 | self.form.close() |
---|
47 | self.form = None |
---|
48 | |
---|
49 | def testDefaults(self): |
---|
50 | '''Test the GUI in its default state''' |
---|
51 | self.assertIsInstance(self.form, QTabWidget) |
---|
52 | self.assertIsInstance(self.form.treeView, QTreeView) |
---|
53 | self.assertIsInstance(self.form.listView, QListView) |
---|
54 | self.assertEqual(self.form.count(), 2) |
---|
55 | |
---|
56 | self.assertEqual(self.form.cmdLoad.text(), "Load") |
---|
57 | self.assertEqual(self.form.cmdDelete.text(), "Delete") |
---|
58 | self.assertEqual(self.form.cmdSendTo.text(), "Send to") |
---|
59 | self.assertEqual(self.form.chkBatch.text(), "Batch mode") |
---|
60 | self.assertFalse(self.form.chkBatch.isChecked()) |
---|
61 | |
---|
62 | self.assertEqual(self.form.cbSelect.count(), 6) |
---|
63 | self.assertEqual(self.form.cbSelect.currentIndex(), 0) |
---|
64 | |
---|
65 | # Class is in the default state even without pressing OK |
---|
66 | self.assertEqual(self.form.treeView.model().rowCount(), 0) |
---|
67 | self.assertEqual(self.form.treeView.model().columnCount(), 0) |
---|
68 | self.assertEqual(self.form.model.rowCount(), 0) |
---|
69 | self.assertEqual(self.form.model.columnCount(), 0) |
---|
70 | |
---|
71 | def testLoadButton(self): |
---|
72 | loadButton = self.form.cmdLoad |
---|
73 | |
---|
74 | filename = "cyl_400_20.txt" |
---|
75 | # Initialize signal spy instances |
---|
76 | spy_file_read = QtSignalSpy(self.form, self.form.communicate.fileReadSignal) |
---|
77 | |
---|
78 | # Return no files. |
---|
79 | QtGui.QFileDialog.getOpenFileNames = MagicMock(return_value=None) |
---|
80 | |
---|
81 | # Click on the Load button |
---|
82 | QTest.mouseClick(loadButton, Qt.LeftButton) |
---|
83 | |
---|
84 | # Test the getOpenFileName() dialog called once |
---|
85 | self.assertTrue(QtGui.QFileDialog.getOpenFileNames.called) |
---|
86 | QtGui.QFileDialog.getOpenFileNames.assert_called_once() |
---|
87 | |
---|
88 | # Make sure the signal has not been emitted |
---|
89 | self.assertEqual(spy_file_read.count(), 0) |
---|
90 | |
---|
91 | # Now, return a single file |
---|
92 | QtGui.QFileDialog.getOpenFileNames = MagicMock(return_value=filename) |
---|
93 | |
---|
94 | # Click on the Load button |
---|
95 | QTest.mouseClick(loadButton, Qt.LeftButton) |
---|
96 | |
---|
97 | # Test the getOpenFileName() dialog called once |
---|
98 | self.assertTrue(QtGui.QFileDialog.getOpenFileNames.called) |
---|
99 | QtGui.QFileDialog.getOpenFileNames.assert_called_once() |
---|
100 | |
---|
101 | # Expected one spy instance |
---|
102 | self.assertEqual(spy_file_read.count(), 1) |
---|
103 | self.assertIn(filename, str(spy_file_read.called()[0]['args'][0])) |
---|
104 | |
---|
105 | def testDeleteButton(self): |
---|
106 | """ |
---|
107 | Functionality of the delete button |
---|
108 | """ |
---|
109 | |
---|
110 | deleteButton = self.form.cmdDelete |
---|
111 | |
---|
112 | # Mock the confirmation dialog with return=Yes |
---|
113 | QtGui.QMessageBox.question = MagicMock(return_value=QtGui.QMessageBox.No) |
---|
114 | |
---|
115 | # Populate the model |
---|
116 | filename = ["cyl_400_20.txt", "Dec07031.ASC", "cyl_400_20.txt"] |
---|
117 | self.form.readData(filename) |
---|
118 | |
---|
119 | # Assure the model contains three items |
---|
120 | self.assertEqual(self.form.model.rowCount(), 3) |
---|
121 | |
---|
122 | # Assure the checkboxes are on |
---|
123 | item1 = self.form.model.item(0) |
---|
124 | item2 = self.form.model.item(1) |
---|
125 | item3 = self.form.model.item(2) |
---|
126 | self.assertTrue(item1.checkState() == QtCore.Qt.Checked) |
---|
127 | self.assertTrue(item2.checkState() == QtCore.Qt.Checked) |
---|
128 | self.assertTrue(item3.checkState() == QtCore.Qt.Checked) |
---|
129 | |
---|
130 | # Click on the delete button |
---|
131 | QTest.mouseClick(deleteButton, Qt.LeftButton) |
---|
132 | |
---|
133 | # Test the warning dialog called once |
---|
134 | self.assertTrue(QtGui.QMessageBox.question.called) |
---|
135 | |
---|
136 | # Assure the model still contains the items |
---|
137 | self.assertEqual(self.form.model.rowCount(), 3) |
---|
138 | |
---|
139 | # Now, mock the confirmation dialog with return=Yes |
---|
140 | QtGui.QMessageBox.question = MagicMock(return_value=QtGui.QMessageBox.Yes) |
---|
141 | |
---|
142 | # Click on the delete button |
---|
143 | QTest.mouseClick(deleteButton, Qt.LeftButton) |
---|
144 | |
---|
145 | # Test the warning dialog called once |
---|
146 | self.assertTrue(QtGui.QMessageBox.question.called) |
---|
147 | |
---|
148 | # Assure the model contains no items |
---|
149 | self.assertEqual(self.form.model.rowCount(), 0) |
---|
150 | |
---|
151 | # Click delete once again to assure no nasty behaviour on empty model |
---|
152 | QTest.mouseClick(deleteButton, Qt.LeftButton) |
---|
153 | |
---|
154 | def testSendToButton(self): |
---|
155 | """ |
---|
156 | Test that clicking the Send To button sends checked data to a perspective |
---|
157 | """ |
---|
158 | |
---|
159 | # Populate the model |
---|
160 | filename = ["cyl_400_20.txt"] |
---|
161 | self.form.readData(filename) |
---|
162 | |
---|
163 | # setData is the method we want to see called |
---|
164 | mocked = self.form.parent.perspective().setData |
---|
165 | mocked = MagicMock(filename) |
---|
166 | |
---|
167 | # Assure the checkbox is on |
---|
168 | self.form.cbSelect.setCurrentIndex(0) |
---|
169 | |
---|
170 | # Click on the Send To button |
---|
171 | QTest.mouseClick(self.form.cmdSendTo, Qt.LeftButton) |
---|
172 | |
---|
173 | # Test the set_data method called once |
---|
174 | # self.assertTrue(mocked.called) |
---|
175 | |
---|
176 | # open another file |
---|
177 | filename = ["cyl_400_20.txt"] |
---|
178 | self.form.readData(filename) |
---|
179 | |
---|
180 | # Mock the warning message |
---|
181 | QtGui.QMessageBox = MagicMock() |
---|
182 | |
---|
183 | # Click on the button |
---|
184 | QTest.mouseClick(self.form.cmdSendTo, Qt.LeftButton) |
---|
185 | |
---|
186 | # Assure the message box popped up |
---|
187 | QtGui.QMessageBox.assert_called_once() |
---|
188 | |
---|
189 | def testDataSelection(self): |
---|
190 | """ |
---|
191 | Tests the functionality of the Selection Option combobox |
---|
192 | """ |
---|
193 | # Populate the model with 1d and 2d data |
---|
194 | filename = ["cyl_400_20.txt", "Dec07031.ASC"] |
---|
195 | self.form.readData(filename) |
---|
196 | |
---|
197 | # Unselect all data |
---|
198 | self.form.cbSelect.setCurrentIndex(1) |
---|
199 | |
---|
200 | # Test the current selection |
---|
201 | item1D = self.form.model.item(0) |
---|
202 | item2D = self.form.model.item(1) |
---|
203 | self.assertTrue(item1D.checkState() == QtCore.Qt.Unchecked) |
---|
204 | self.assertTrue(item2D.checkState() == QtCore.Qt.Unchecked) |
---|
205 | |
---|
206 | # Select all data |
---|
207 | self.form.cbSelect.setCurrentIndex(0) |
---|
208 | |
---|
209 | # Test the current selection |
---|
210 | self.assertTrue(item1D.checkState() == QtCore.Qt.Checked) |
---|
211 | self.assertTrue(item2D.checkState() == QtCore.Qt.Checked) |
---|
212 | |
---|
213 | # select 1d data |
---|
214 | self.form.cbSelect.setCurrentIndex(2) |
---|
215 | |
---|
216 | # Test the current selection |
---|
217 | self.assertTrue(item1D.checkState() == QtCore.Qt.Checked) |
---|
218 | self.assertTrue(item2D.checkState() == QtCore.Qt.Unchecked) |
---|
219 | |
---|
220 | # unselect 1d data |
---|
221 | self.form.cbSelect.setCurrentIndex(3) |
---|
222 | |
---|
223 | # Test the current selection |
---|
224 | self.assertTrue(item1D.checkState() == QtCore.Qt.Unchecked) |
---|
225 | self.assertTrue(item2D.checkState() == QtCore.Qt.Unchecked) |
---|
226 | |
---|
227 | # select 2d data |
---|
228 | self.form.cbSelect.setCurrentIndex(4) |
---|
229 | |
---|
230 | # Test the current selection |
---|
231 | self.assertTrue(item1D.checkState() == QtCore.Qt.Unchecked) |
---|
232 | self.assertTrue(item2D.checkState() == QtCore.Qt.Checked) |
---|
233 | |
---|
234 | # unselect 2d data |
---|
235 | self.form.cbSelect.setCurrentIndex(5) |
---|
236 | |
---|
237 | # Test the current selection |
---|
238 | self.assertTrue(item1D.checkState() == QtCore.Qt.Unchecked) |
---|
239 | self.assertTrue(item2D.checkState() == QtCore.Qt.Unchecked) |
---|
240 | |
---|
241 | # choose impossible index and assure the code raises |
---|
242 | #with self.assertRaises(Exception): |
---|
243 | # self.form.cbSelect.setCurrentIndex(6) |
---|
244 | |
---|
245 | def testReadData(self): |
---|
246 | """ |
---|
247 | Test the low level readData() method |
---|
248 | """ |
---|
249 | filename = ["cyl_400_20.txt"] |
---|
250 | self.form.manager.add_data = MagicMock() |
---|
251 | |
---|
252 | # Initialize signal spy instances |
---|
253 | spy_status_update = QtSignalSpy(self.form, self.form.communicate.statusBarUpdateSignal) |
---|
254 | spy_data_received = QtSignalSpy(self.form, self.form.communicate.fileDataReceivedSignal) |
---|
255 | |
---|
256 | # Read in the file |
---|
257 | self.form.readData(filename) |
---|
258 | |
---|
259 | # Expected two status bar updates |
---|
260 | self.assertEqual(spy_status_update.count(), 1) |
---|
261 | self.assertIn(filename[0], str(spy_status_update.called()[0]['args'][0])) |
---|
262 | |
---|
263 | |
---|
264 | # Check that the model contains the item |
---|
265 | self.assertEqual(self.form.model.rowCount(), 1) |
---|
266 | self.assertEqual(self.form.model.columnCount(), 1) |
---|
267 | |
---|
268 | # The 0th item header should be the name of the file |
---|
269 | model_item = self.form.model.index(0,0) |
---|
270 | model_name = str(self.form.model.data(model_item).toString()) |
---|
271 | self.assertEqual(model_name, filename[0]) |
---|
272 | |
---|
273 | def testLoadFile(self): |
---|
274 | """ |
---|
275 | Test the threaded call to readData() |
---|
276 | """ |
---|
277 | pass |
---|
278 | |
---|
279 | def testGetWList(self): |
---|
280 | """ |
---|
281 | """ |
---|
282 | list = self.form.getWlist() |
---|
283 | defaults = 'All (*.*);;canSAS files (*.xml);;SESANS files' +\ |
---|
284 | ' (*.ses);;ASCII files (*.txt);;IGOR 2D files (*.asc);;' +\ |
---|
285 | 'IGOR/DAT 2D Q_map files (*.dat);;IGOR 1D files (*.abs);;'+\ |
---|
286 | 'HFIR 1D files (*.d1d);;DANSE files (*.sans);;NXS files (*.nxs)' |
---|
287 | self.assertEqual(defaults, list) |
---|
288 | |
---|
289 | def testLoadComplete(self): |
---|
290 | """ |
---|
291 | Test the callback method updating the data object |
---|
292 | """ |
---|
293 | message="Loading Data Complete" |
---|
294 | data_dict = {"a1":Data1D()} |
---|
295 | output_data = (data_dict, message) |
---|
296 | |
---|
297 | self.form.manager.add_data = MagicMock() |
---|
298 | |
---|
299 | # Initialize signal spy instances |
---|
300 | spy_status_update = QtSignalSpy(self.form, self.form.communicate.statusBarUpdateSignal) |
---|
301 | spy_data_received = QtSignalSpy(self.form, self.form.communicate.fileDataReceivedSignal) |
---|
302 | |
---|
303 | # Read in the file |
---|
304 | self.form.loadComplete(output_data) |
---|
305 | |
---|
306 | # "Loading data complete" no longer sent in LoadFile but in callback |
---|
307 | self.assertIn("Loading Data Complete", str(spy_status_update.called()[0]['args'][0])) |
---|
308 | |
---|
309 | # Expect one Data Received signal |
---|
310 | self.assertEqual(spy_data_received.count(), 1) |
---|
311 | |
---|
312 | # Assure returned dictionary has correct data |
---|
313 | # We don't know the data ID, so need to iterate over dict |
---|
314 | data_dict = spy_data_received.called()[0]['args'][0] |
---|
315 | for data_key, data_value in data_dict.iteritems(): |
---|
316 | self.assertIsInstance(data_value, Data1D) |
---|
317 | |
---|
318 | # Assure add_data on data_manager was called (last call) |
---|
319 | self.assertTrue(self.form.manager.add_data.called) |
---|
320 | |
---|
321 | def testNewPlot(self): |
---|
322 | """ |
---|
323 | Creating new plots from Data1D/2D |
---|
324 | """ |
---|
325 | loader = Loader() |
---|
326 | manager = DataManager() |
---|
327 | |
---|
328 | # get Data1D |
---|
329 | p_file="cyl_400_20.txt" |
---|
330 | output_object = loader.load(p_file) |
---|
331 | new_data = [manager.create_gui_data(output_object, p_file)] |
---|
332 | |
---|
333 | # Mask the plot show call |
---|
334 | Plotter.show = MagicMock() |
---|
335 | |
---|
336 | # Mask retrieval of the data |
---|
337 | self.form.plotsFromCheckedItems = MagicMock(return_value=new_data) |
---|
338 | |
---|
339 | # Call the plotting method |
---|
340 | self.form.newPlot() |
---|
341 | |
---|
342 | # The plot was displayed |
---|
343 | self.assertTrue(Plotter.show.called) |
---|
344 | |
---|
345 | |
---|
346 | if __name__ == "__main__": |
---|
347 | unittest.main() |
---|