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