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