1 | import sys |
---|
2 | import unittest |
---|
3 | import webbrowser |
---|
4 | |
---|
5 | from PyQt4 import QtCore |
---|
6 | from PyQt4 import QtGui |
---|
7 | from mock import MagicMock |
---|
8 | |
---|
9 | # set up import paths |
---|
10 | import sas.qtgui.path_prepare |
---|
11 | |
---|
12 | # SV imports |
---|
13 | from sas.sascalc.dataloader.loader import Loader |
---|
14 | from sas.qtgui.MainWindow.DataManager import DataManager |
---|
15 | from sas.qtgui.Plotting.PlotterData import Data1D |
---|
16 | from sas.qtgui.Plotting.PlotterData import Data2D |
---|
17 | |
---|
18 | # Tested module |
---|
19 | from sas.qtgui.Utilities.GuiUtils import * |
---|
20 | |
---|
21 | if not QtGui.QApplication.instance(): |
---|
22 | app = QtGui.QApplication(sys.argv) |
---|
23 | |
---|
24 | class GuiUtilsTest(unittest.TestCase): |
---|
25 | '''Test the GUI Utilities methods''' |
---|
26 | def setUp(self): |
---|
27 | '''Empty''' |
---|
28 | pass |
---|
29 | |
---|
30 | def tearDown(self): |
---|
31 | '''Empty''' |
---|
32 | pass |
---|
33 | |
---|
34 | def testDefaults(self): |
---|
35 | """ |
---|
36 | Test all the global constants defined in the file. |
---|
37 | """ |
---|
38 | # Should probably test the constants in the file, |
---|
39 | # but this will done after trimming down GuiUtils |
---|
40 | # and retaining only necessary variables. |
---|
41 | pass |
---|
42 | |
---|
43 | def testGetAppDir(self): |
---|
44 | """ |
---|
45 | """ |
---|
46 | pass |
---|
47 | |
---|
48 | def testGetUserDirectory(self): |
---|
49 | """ |
---|
50 | Simple test of user directory getter |
---|
51 | """ |
---|
52 | home_dir = os.path.expanduser("~") |
---|
53 | self.assertIn(home_dir, get_user_directory()) |
---|
54 | |
---|
55 | def testCommunicate(self): |
---|
56 | """ |
---|
57 | Test the container class with signal definitions |
---|
58 | """ |
---|
59 | com = Communicate() |
---|
60 | |
---|
61 | # All defined signals |
---|
62 | list_of_signals = [ |
---|
63 | 'fileReadSignal', |
---|
64 | 'fileDataReceivedSignal', |
---|
65 | 'statusBarUpdateSignal', |
---|
66 | 'updatePerspectiveWithDataSignal', |
---|
67 | 'updateModelFromPerspectiveSignal', |
---|
68 | 'plotRequestedSignal', |
---|
69 | 'progressBarUpdateSignal', |
---|
70 | 'activeGraphName', |
---|
71 | 'sendDataToPanelSignal', |
---|
72 | 'updateModelFromDataOperationPanelSignal' |
---|
73 | ] |
---|
74 | |
---|
75 | # Assure all signals are defined. |
---|
76 | for signal in list_of_signals: |
---|
77 | self.assertIn(signal, dir(com)) |
---|
78 | |
---|
79 | def testupdateModelItem(self): |
---|
80 | """ |
---|
81 | Test the generic QModelItem update method |
---|
82 | """ |
---|
83 | test_item = QtGui.QStandardItem() |
---|
84 | test_list = ['aa', 4, True, ] |
---|
85 | name = "Black Sabbath" |
---|
86 | |
---|
87 | # update the item |
---|
88 | updateModelItem(test_item, test_list, name) |
---|
89 | |
---|
90 | # Make sure test_item got all data added |
---|
91 | self.assertEqual(test_item.child(0).text(), name) |
---|
92 | list_from_item = test_item.child(0).data().toList() |
---|
93 | self.assertIsInstance(list_from_item, list) |
---|
94 | self.assertEqual(list_from_item[0].toPyObject(), test_list[0]) |
---|
95 | self.assertEqual(list_from_item[1].toPyObject(), test_list[1]) |
---|
96 | self.assertEqual(list_from_item[2].toPyObject(), test_list[2]) |
---|
97 | |
---|
98 | def testupdateModelItemWithPlot(self): |
---|
99 | """ |
---|
100 | Test the QModelItem checkbox update method |
---|
101 | """ |
---|
102 | test_item = QtGui.QStandardItem() |
---|
103 | test_list = ['aa','11'] |
---|
104 | update_data = QtCore.QVariant(test_list) |
---|
105 | name = "Black Sabbath" |
---|
106 | |
---|
107 | # update the item |
---|
108 | updateModelItemWithPlot(test_item, update_data, name) |
---|
109 | |
---|
110 | # Make sure test_item got all data added |
---|
111 | self.assertEqual(test_item.child(0).text(), name) |
---|
112 | self.assertTrue(test_item.child(0).isCheckable()) |
---|
113 | list_from_item = test_item.child(0).child(0).data().toPyObject() |
---|
114 | self.assertIsInstance(list_from_item, list) |
---|
115 | self.assertEqual(str(list_from_item[0]), test_list[0]) |
---|
116 | self.assertEqual(str(list_from_item[1]), test_list[1]) |
---|
117 | |
---|
118 | |
---|
119 | def testPlotsFromCheckedItems(self): |
---|
120 | """ |
---|
121 | Test addition of a plottable to the model |
---|
122 | """ |
---|
123 | |
---|
124 | # Mockup data |
---|
125 | test_list0 = "FRIDAY" |
---|
126 | test_list1 = "SATURDAY" |
---|
127 | test_list2 = "MONDAY" |
---|
128 | |
---|
129 | # Main item ("file") |
---|
130 | checkbox_model = QtGui.QStandardItemModel() |
---|
131 | checkbox_item = QtGui.QStandardItem(True) |
---|
132 | checkbox_item.setCheckable(True) |
---|
133 | checkbox_item.setCheckState(QtCore.Qt.Checked) |
---|
134 | test_item0 = QtGui.QStandardItem() |
---|
135 | test_item0.setData(QtCore.QVariant(test_list0)) |
---|
136 | |
---|
137 | # Checked item 1 |
---|
138 | test_item1 = QtGui.QStandardItem(True) |
---|
139 | test_item1.setCheckable(True) |
---|
140 | test_item1.setCheckState(QtCore.Qt.Checked) |
---|
141 | object_item = QtGui.QStandardItem() |
---|
142 | object_item.setData(QtCore.QVariant(test_list1)) |
---|
143 | test_item1.setChild(0, object_item) |
---|
144 | |
---|
145 | checkbox_item.setChild(0, test_item0) |
---|
146 | checkbox_item.appendRow(test_item1) |
---|
147 | |
---|
148 | # Unchecked item 2 |
---|
149 | test_item2 = QtGui.QStandardItem(True) |
---|
150 | test_item2.setCheckable(True) |
---|
151 | test_item2.setCheckState(QtCore.Qt.Unchecked) |
---|
152 | object_item = QtGui.QStandardItem() |
---|
153 | object_item.setData(QtCore.QVariant(test_list2)) |
---|
154 | test_item2.setChild(0, object_item) |
---|
155 | checkbox_item.appendRow(test_item2) |
---|
156 | |
---|
157 | checkbox_model.appendRow(checkbox_item) |
---|
158 | |
---|
159 | # Pull out the "plottable" documents |
---|
160 | plot_list = plotsFromCheckedItems(checkbox_model) |
---|
161 | |
---|
162 | # Make sure only the checked data is present |
---|
163 | # FRIDAY IN |
---|
164 | self.assertIn(test_list0, plot_list[0]) |
---|
165 | # SATURDAY IN |
---|
166 | self.assertIn(test_list1, plot_list[1]) |
---|
167 | # MONDAY NOT IN |
---|
168 | self.assertNotIn(test_list2, plot_list[0]) |
---|
169 | self.assertNotIn(test_list2, plot_list[1]) |
---|
170 | |
---|
171 | def testInfoFromData(self): |
---|
172 | """ |
---|
173 | Test Info element extraction from a plottable object |
---|
174 | """ |
---|
175 | loader = Loader() |
---|
176 | manager = DataManager() |
---|
177 | |
---|
178 | # get Data1D |
---|
179 | p_file="cyl_400_20.txt" |
---|
180 | output_object = loader.load(p_file) |
---|
181 | new_data = manager.create_gui_data(output_object[0], p_file) |
---|
182 | |
---|
183 | # Extract Info elements into a model item |
---|
184 | item = infoFromData(new_data) |
---|
185 | |
---|
186 | # Test the item and its children |
---|
187 | self.assertIsInstance(item, QtGui.QStandardItem) |
---|
188 | self.assertEqual(item.rowCount(), 5) |
---|
189 | self.assertEqual(item.text(), "Info") |
---|
190 | self.assertIn(p_file, item.child(0).text()) |
---|
191 | self.assertIn("Run", item.child(1).text()) |
---|
192 | self.assertIn("Data1D", item.child(2).text()) |
---|
193 | self.assertIn(p_file, item.child(3).text()) |
---|
194 | self.assertIn("Process",item.child(4).text()) |
---|
195 | |
---|
196 | def testOpenLink(self): |
---|
197 | """ |
---|
198 | Opening a link in the external browser |
---|
199 | """ |
---|
200 | good_url1 = r"http://test.test.com" |
---|
201 | good_url2 = r"mailto:test@mail.com" |
---|
202 | good_url3 = r"https://127.0.0.1" |
---|
203 | |
---|
204 | bad_url1 = "" |
---|
205 | bad_url2 = QtGui.QStandardItem() |
---|
206 | bad_url3 = r"poop;//**I.am.a.!bad@url" |
---|
207 | |
---|
208 | webbrowser.open = MagicMock() |
---|
209 | openLink(good_url1) |
---|
210 | openLink(good_url2) |
---|
211 | openLink(good_url3) |
---|
212 | self.assertEqual(webbrowser.open.call_count, 3) |
---|
213 | |
---|
214 | with self.assertRaises(AttributeError): |
---|
215 | openLink(bad_url1) |
---|
216 | with self.assertRaises(AttributeError): |
---|
217 | openLink(bad_url2) |
---|
218 | with self.assertRaises(AttributeError): |
---|
219 | openLink(bad_url3) |
---|
220 | |
---|
221 | def testRetrieveData1d(self): |
---|
222 | """ |
---|
223 | """ |
---|
224 | with self.assertRaises(AttributeError): |
---|
225 | retrieveData1d("BOOP") |
---|
226 | |
---|
227 | #data = Data1D() |
---|
228 | #with self.assertRaises(ValueError): |
---|
229 | # retrieveData1d(data) |
---|
230 | |
---|
231 | data = Data1D(x=[1.0, 2.0, 3.0], y=[10.0, 11.0, 12.0]) |
---|
232 | |
---|
233 | text = retrieveData1d(data) |
---|
234 | |
---|
235 | self.assertIn("Temperature:", text) |
---|
236 | self.assertIn("Beam_size:", text) |
---|
237 | self.assertIn("X_min = 1.0: X_max = 3.0", text) |
---|
238 | self.assertIn("3.0 \t12.0 \t0.0 \t0.0", text) |
---|
239 | |
---|
240 | def testRetrieveData2d(self): |
---|
241 | """ |
---|
242 | """ |
---|
243 | with self.assertRaises(AttributeError): |
---|
244 | retrieveData2d("BOOP") |
---|
245 | data = Data2D(image=[1.0, 2.0, 3.0], |
---|
246 | err_image=[0.01, 0.02, 0.03], |
---|
247 | qx_data=[0.1, 0.2, 0.3], |
---|
248 | qy_data=[0.1, 0.2, 0.3]) |
---|
249 | |
---|
250 | text = retrieveData2d(data) |
---|
251 | |
---|
252 | self.assertIn("Type: Data2D", text) |
---|
253 | self.assertIn("I_min = 1.0", text) |
---|
254 | self.assertIn("I_max = 3.0", text) |
---|
255 | self.assertIn("2 \t0.3 \t0.3 \t3.0 \t0.03 \t0.0 \t0.0", text) |
---|
256 | |
---|
257 | def testOnTXTSave(self): |
---|
258 | """ |
---|
259 | Test the file writer for saving 1d/2d data |
---|
260 | """ |
---|
261 | path = "test123" |
---|
262 | if os.path.isfile(path): |
---|
263 | os.remove(path) |
---|
264 | |
---|
265 | # Broken data |
---|
266 | data = Data1D(x=[1.0, 2.0, 3.0], y=[]) |
---|
267 | # Expect a raise |
---|
268 | with self.assertRaises(IndexError): |
---|
269 | onTXTSave(data, path) |
---|
270 | |
---|
271 | # Good data - no dX/dY |
---|
272 | data = Data1D(x=[1.0, 2.0, 3.0], y=[10.0, 11.0, 12.0]) |
---|
273 | onTXTSave(data, path) |
---|
274 | |
---|
275 | self.assertTrue(os.path.isfile(path)) |
---|
276 | with open(path,'r') as out: |
---|
277 | data_read = out.read() |
---|
278 | self.assertEqual("<X> <Y>\n1 10\n2 11\n3 12\n", data_read) |
---|
279 | |
---|
280 | if os.path.isfile(path): |
---|
281 | os.remove(path) |
---|
282 | |
---|
283 | # Good data - with dX/dY |
---|
284 | data = Data1D(x=[1.0, 2.0, 3.0], y=[10.0, 11.0, 12.0], |
---|
285 | dx=[0.1, 0.2, 0.3], dy=[0.1, 0.2, 0.3]) |
---|
286 | |
---|
287 | onTXTSave(data, path) |
---|
288 | with open(path,'r') as out: |
---|
289 | data_read = out.read() |
---|
290 | self.assertIn("<X> <Y> <dY> <dX>\n", data_read) |
---|
291 | self.assertIn("1 10 0.1 0.1\n", data_read) |
---|
292 | self.assertIn("2 11 0.2 0.2\n", data_read) |
---|
293 | self.assertIn("3 12 0.3 0.3\n", data_read) |
---|
294 | |
---|
295 | if os.path.isfile(path): |
---|
296 | os.remove(path) |
---|
297 | |
---|
298 | def testSaveData1D(self): |
---|
299 | """ |
---|
300 | Test the 1D file save method |
---|
301 | """ |
---|
302 | data = Data1D(x=[1.0, 2.0, 3.0], y=[10.0, 11.0, 12.0], |
---|
303 | dx=[0.1, 0.2, 0.3], dy=[0.1, 0.2, 0.3]) |
---|
304 | |
---|
305 | # Test the .txt format |
---|
306 | file_name = "test123_out.txt" |
---|
307 | QtGui.QFileDialog.getSaveFileName = MagicMock(return_value=file_name) |
---|
308 | data.filename = "test123.txt" |
---|
309 | saveData1D(data) |
---|
310 | self.assertTrue(os.path.isfile(file_name)) |
---|
311 | os.remove(file_name) |
---|
312 | |
---|
313 | # Test the .xml format |
---|
314 | file_name = "test123_out.xml" |
---|
315 | QtGui.QFileDialog.getSaveFileName = MagicMock(return_value=file_name) |
---|
316 | data.filename = "test123.xml" |
---|
317 | saveData1D(data) |
---|
318 | self.assertTrue(os.path.isfile(file_name)) |
---|
319 | os.remove(file_name) |
---|
320 | |
---|
321 | # Test the wrong format |
---|
322 | file_name = "test123_out.mp3" |
---|
323 | QtGui.QFileDialog.getSaveFileName = MagicMock(return_value=file_name) |
---|
324 | data.filename = "test123.mp3" |
---|
325 | saveData1D(data) |
---|
326 | self.assertFalse(os.path.isfile(file_name)) |
---|
327 | |
---|
328 | def testSaveData2D(self): |
---|
329 | """ |
---|
330 | Test the 1D file save method |
---|
331 | """ |
---|
332 | data = Data2D(image=[1.0, 2.0, 3.0], |
---|
333 | err_image=[0.01, 0.02, 0.03], |
---|
334 | qx_data=[0.1, 0.2, 0.3], |
---|
335 | qy_data=[0.1, 0.2, 0.3]) |
---|
336 | |
---|
337 | # Test the .txt format |
---|
338 | file_name = "test123_out.dat" |
---|
339 | QtGui.QFileDialog.getSaveFileName = MagicMock(return_value=file_name) |
---|
340 | data.filename = "test123.dat" |
---|
341 | saveData2D(data) |
---|
342 | self.assertTrue(os.path.isfile(file_name)) |
---|
343 | os.remove(file_name) |
---|
344 | |
---|
345 | # Test the wrong format |
---|
346 | file_name = "test123_out.mp3" |
---|
347 | QtGui.QFileDialog.getSaveFileName = MagicMock(return_value=file_name) |
---|
348 | data.filename = "test123.mp3" |
---|
349 | saveData2D(data) |
---|
350 | self.assertFalse(os.path.isfile(file_name)) |
---|
351 | |
---|
352 | def testXYTransform(self): |
---|
353 | """ Assure the unit/legend transformation is correct""" |
---|
354 | data = Data1D(x=[1.0, 2.0, 3.0], y=[10.0, 11.0, 12.0], |
---|
355 | dx=[0.1, 0.2, 0.3], dy=[0.1, 0.2, 0.3]) |
---|
356 | |
---|
357 | xLabel, yLabel, xscale, yscale = xyTransform(data, xLabel="x", yLabel="y") |
---|
358 | self.assertEqual(xLabel, "()") |
---|
359 | self.assertEqual(xscale, "linear") |
---|
360 | self.assertEqual(yscale, "linear") |
---|
361 | |
---|
362 | xLabel, yLabel, xscale, yscale = xyTransform(data, xLabel="x^(2)", yLabel="1/y") |
---|
363 | self.assertEqual(xLabel, "^{2}(()^{2})") |
---|
364 | self.assertEqual(yLabel, "1/(()^{-1})") |
---|
365 | self.assertEqual(xscale, "linear") |
---|
366 | self.assertEqual(yscale, "linear") |
---|
367 | |
---|
368 | xLabel, yLabel, xscale, yscale = xyTransform(data, xLabel="x^(4)", yLabel="ln(y)") |
---|
369 | self.assertEqual(xLabel, "^{4}(()^{4})") |
---|
370 | self.assertEqual(yLabel, "\\ln{()}()") |
---|
371 | self.assertEqual(xscale, "linear") |
---|
372 | self.assertEqual(yscale, "linear") |
---|
373 | |
---|
374 | xLabel, yLabel, xscale, yscale = xyTransform(data, xLabel="ln(x)", yLabel="y^(2)") |
---|
375 | self.assertEqual(xLabel, "\\ln{()}()") |
---|
376 | self.assertEqual(yLabel, "^{2}(()^{2})") |
---|
377 | self.assertEqual(xscale, "linear") |
---|
378 | self.assertEqual(yscale, "linear") |
---|
379 | |
---|
380 | xLabel, yLabel, xscale, yscale = xyTransform(data, xLabel="log10(x)", yLabel="y*x^(2)") |
---|
381 | self.assertEqual(xLabel, "()") |
---|
382 | self.assertEqual(yLabel, " \\ \\ ^{2}(()^{2})") |
---|
383 | self.assertEqual(xscale, "log") |
---|
384 | self.assertEqual(yscale, "linear") |
---|
385 | |
---|
386 | xLabel, yLabel, xscale, yscale = xyTransform(data, xLabel="log10(x^(4))", yLabel="y*x^(4)") |
---|
387 | self.assertEqual(xLabel, "^{4}(()^{4})") |
---|
388 | self.assertEqual(yLabel, " \\ \\ ^{4}(()^{16})") |
---|
389 | self.assertEqual(xscale, "log") |
---|
390 | self.assertEqual(yscale, "linear") |
---|
391 | |
---|
392 | xLabel, yLabel, xscale, yscale = xyTransform(data, xLabel="x", yLabel="1/sqrt(y)") |
---|
393 | self.assertEqual(yLabel, "1/\\sqrt{}(()^{-0.5})") |
---|
394 | self.assertEqual(yscale, "linear") |
---|
395 | |
---|
396 | xLabel, yLabel, xscale, yscale = xyTransform(data, xLabel="x", yLabel="log10(y)") |
---|
397 | self.assertEqual(yLabel, "()") |
---|
398 | self.assertEqual(yscale, "log") |
---|
399 | |
---|
400 | xLabel, yLabel, xscale, yscale = xyTransform(data, xLabel="x", yLabel="ln(y*x)") |
---|
401 | self.assertEqual(yLabel, "\\ln{( \\ \\ )}()") |
---|
402 | self.assertEqual(yscale, "linear") |
---|
403 | |
---|
404 | xLabel, yLabel, xscale, yscale = xyTransform(data, xLabel="x", yLabel="ln(y*x^(2))") |
---|
405 | self.assertEqual(yLabel, "\\ln ( \\ \\ ^{2})(()^{2})") |
---|
406 | self.assertEqual(yscale, "linear") |
---|
407 | |
---|
408 | xLabel, yLabel, xscale, yscale = xyTransform(data, xLabel="x", yLabel="ln(y*x^(4))") |
---|
409 | self.assertEqual(yLabel, "\\ln ( \\ \\ ^{4})(()^{4})") |
---|
410 | self.assertEqual(yscale, "linear") |
---|
411 | |
---|
412 | xLabel, yLabel, xscale, yscale = xyTransform(data, xLabel="x", yLabel="log10(y*x^(4))") |
---|
413 | self.assertEqual(yLabel, " \\ \\ ^{4}(()^{4})") |
---|
414 | self.assertEqual(yscale, "log") |
---|
415 | |
---|
416 | |
---|
417 | class FormulaValidatorTest(unittest.TestCase): |
---|
418 | """ Test the formula validator """ |
---|
419 | def setUp(self): |
---|
420 | '''Create the validator''' |
---|
421 | self.validator = FormulaValidator() |
---|
422 | |
---|
423 | def tearDown(self): |
---|
424 | '''Destroy the validator''' |
---|
425 | self.validator = None |
---|
426 | |
---|
427 | def testValidateGood(self): |
---|
428 | """Test a valid Formula """ |
---|
429 | formula_good = "H24O12C4C6N2Pu" |
---|
430 | self.assertEqual(self.validator.validate(formula_good, 1)[0], QtGui.QValidator.Acceptable) |
---|
431 | |
---|
432 | formula_good = "(H2O)0.5(D2O)0.5" |
---|
433 | self.assertEqual(self.validator.validate(formula_good, 1)[0], QtGui.QValidator.Acceptable) |
---|
434 | |
---|
435 | def testValidateBad(self): |
---|
436 | """Test a valid Formula """ |
---|
437 | formula_bad = "H24 %%%O12C4C6N2Pu" |
---|
438 | self.assertRaises(self.validator.validate(formula_bad, 1)[0]) |
---|
439 | self.assertEqual(self.validator.validate(formula_bad, 1)[0], QtGui.QValidator.Intermediate) |
---|
440 | |
---|
441 | formula_bad = [1] |
---|
442 | self.assertEqual(self.validator.validate(formula_bad, 1)[0], QtGui.QValidator.Intermediate) |
---|
443 | |
---|
444 | |
---|
445 | if __name__ == "__main__": |
---|
446 | unittest.main() |
---|
447 | |
---|