1 | # -*- coding: utf-8 -*- |
---|
2 | import sys |
---|
3 | import unittest |
---|
4 | import webbrowser |
---|
5 | |
---|
6 | from PyQt5 import QtCore |
---|
7 | from PyQt5 import QtGui, QtWidgets |
---|
8 | from unittest.mock import MagicMock |
---|
9 | |
---|
10 | # set up import paths |
---|
11 | import sas.qtgui.path_prepare |
---|
12 | |
---|
13 | # SV imports |
---|
14 | from sas.sascalc.dataloader.loader import Loader |
---|
15 | from sas.qtgui.MainWindow.DataManager import DataManager |
---|
16 | from sas.qtgui.Plotting.PlotterData import Data1D |
---|
17 | from sas.qtgui.Plotting.PlotterData import Data2D |
---|
18 | |
---|
19 | # Tested module |
---|
20 | from sas.qtgui.Utilities.GuiUtils import * |
---|
21 | |
---|
22 | if not QtWidgets.QApplication.instance(): |
---|
23 | app = QtWidgets.QApplication(sys.argv) |
---|
24 | |
---|
25 | class GuiUtilsTest(unittest.TestCase): |
---|
26 | '''Test the GUI Utilities methods''' |
---|
27 | def setUp(self): |
---|
28 | '''Empty''' |
---|
29 | pass |
---|
30 | |
---|
31 | def tearDown(self): |
---|
32 | '''Empty''' |
---|
33 | pass |
---|
34 | |
---|
35 | def testDefaults(self): |
---|
36 | """ |
---|
37 | Test all the global constants defined in the file. |
---|
38 | """ |
---|
39 | # Should probably test the constants in the file, |
---|
40 | # but this will done after trimming down GuiUtils |
---|
41 | # and retaining only necessary variables. |
---|
42 | pass |
---|
43 | |
---|
44 | def testGetAppDir(self): |
---|
45 | """ |
---|
46 | """ |
---|
47 | pass |
---|
48 | |
---|
49 | def testGetUserDirectory(self): |
---|
50 | """ |
---|
51 | Simple test of user directory getter |
---|
52 | """ |
---|
53 | home_dir = os.path.expanduser("~") |
---|
54 | self.assertIn(home_dir, get_user_directory()) |
---|
55 | |
---|
56 | def testCommunicate(self): |
---|
57 | """ |
---|
58 | Test the container class with signal definitions |
---|
59 | """ |
---|
60 | com = Communicate() |
---|
61 | |
---|
62 | # All defined signals |
---|
63 | list_of_signals = [ |
---|
64 | 'fileReadSignal', |
---|
65 | 'fileDataReceivedSignal', |
---|
66 | 'statusBarUpdateSignal', |
---|
67 | 'updatePerspectiveWithDataSignal', |
---|
68 | 'updateModelFromPerspectiveSignal', |
---|
69 | 'plotRequestedSignal', |
---|
70 | 'progressBarUpdateSignal', |
---|
71 | 'activeGraphName', |
---|
72 | 'sendDataToPanelSignal', |
---|
73 | 'updateModelFromDataOperationPanelSignal' |
---|
74 | ] |
---|
75 | |
---|
76 | # Assure all signals are defined. |
---|
77 | for signal in list_of_signals: |
---|
78 | self.assertIn(signal, dir(com)) |
---|
79 | |
---|
80 | def testupdateModelItem(self): |
---|
81 | """ |
---|
82 | Test the generic QModelItem update method |
---|
83 | """ |
---|
84 | test_item = QtGui.QStandardItem() |
---|
85 | test_list = ['aa', 4, True, ] |
---|
86 | name = "Black Sabbath" |
---|
87 | |
---|
88 | # update the item |
---|
89 | updateModelItem(test_item, test_list, name) |
---|
90 | |
---|
91 | # Make sure test_item got all data added |
---|
92 | self.assertEqual(test_item.child(0).text(), name) |
---|
93 | list_from_item = test_item.child(0).data() |
---|
94 | self.assertIsInstance(list_from_item, list) |
---|
95 | self.assertEqual(list_from_item[0], test_list[0]) |
---|
96 | self.assertEqual(list_from_item[1], test_list[1]) |
---|
97 | self.assertEqual(list_from_item[2], test_list[2]) |
---|
98 | |
---|
99 | def testupdateModelItemWithPlot(self): |
---|
100 | """ |
---|
101 | Test the QModelItem checkbox update method |
---|
102 | """ |
---|
103 | # test_item = QtGui.QStandardItem() |
---|
104 | # test_list = ['aa','11'] |
---|
105 | # update_data = test_list |
---|
106 | # name = "Black Sabbath" |
---|
107 | |
---|
108 | # # update the item |
---|
109 | # updateModelItemWithPlot(test_item, update_data, name) |
---|
110 | |
---|
111 | test_item = QtGui.QStandardItem() |
---|
112 | update_data = Data1D(x=[1.0, 2.0, 3.0], y=[10.0, 11.0, 12.0]) |
---|
113 | name = "Black Sabbath" |
---|
114 | update_data.id = '[0]data0' |
---|
115 | update_data.name = 'data0' |
---|
116 | # update the item |
---|
117 | updateModelItemWithPlot(test_item, update_data, name) |
---|
118 | |
---|
119 | # Make sure test_item got all data added |
---|
120 | self.assertEqual(test_item.child(0).text(), name) |
---|
121 | self.assertTrue(test_item.child(0).isCheckable()) |
---|
122 | data_from_item = test_item.child(0).child(0).data() |
---|
123 | self.assertIsInstance(data_from_item, Data1D) |
---|
124 | self.assertSequenceEqual(list(data_from_item.x), [1.0, 2.0, 3.0]) |
---|
125 | self.assertSequenceEqual(list(data_from_item.y), [10.0, 11.0, 12.0]) |
---|
126 | self.assertEqual(test_item.rowCount(), 1) |
---|
127 | |
---|
128 | # add another dataset (different from the first one) |
---|
129 | update_data1 = Data1D(x=[1.1, 2.1, 3.1], y=[10.1, 11.1, 12.1]) |
---|
130 | update_data1.id = '[0]data1' |
---|
131 | update_data1.name = 'data1' |
---|
132 | name1 = "Black Sabbath1" |
---|
133 | # update the item and check number of rows |
---|
134 | updateModelItemWithPlot(test_item, update_data1, name1) |
---|
135 | |
---|
136 | self.assertEqual(test_item.rowCount(), 2) |
---|
137 | |
---|
138 | # add another dataset (with the same name as the first one) |
---|
139 | # check that number of rows was not changed but data have been updated |
---|
140 | update_data2 = Data1D(x=[4.0, 5.0, 6.0], y=[13.0, 14.0, 15.0]) |
---|
141 | update_data2.id = '[1]data0' |
---|
142 | update_data2.name = 'data0' |
---|
143 | name2 = "Black Sabbath2" |
---|
144 | updateModelItemWithPlot(test_item, update_data2, name2) |
---|
145 | self.assertEqual(test_item.rowCount(), 2) |
---|
146 | |
---|
147 | data_from_item = test_item.child(0).child(0).data() |
---|
148 | self.assertSequenceEqual(list(data_from_item.x), [4.0, 5.0, 6.0]) |
---|
149 | self.assertSequenceEqual(list(data_from_item.y), [13.0, 14.0, 15.0]) |
---|
150 | |
---|
151 | |
---|
152 | def testPlotsFromCheckedItems(self): |
---|
153 | """ |
---|
154 | Test addition of a plottable to the model |
---|
155 | """ |
---|
156 | |
---|
157 | # Mockup data |
---|
158 | test_list0 = "FRIDAY" |
---|
159 | test_list1 = "SATURDAY" |
---|
160 | test_list2 = "MONDAY" |
---|
161 | |
---|
162 | # Main item ("file") |
---|
163 | checkbox_model = QtGui.QStandardItemModel() |
---|
164 | checkbox_item = QtGui.QStandardItem(True) |
---|
165 | checkbox_item.setCheckable(True) |
---|
166 | checkbox_item.setCheckState(QtCore.Qt.Checked) |
---|
167 | test_item0 = QtGui.QStandardItem() |
---|
168 | test_item0.setData(test_list0) |
---|
169 | |
---|
170 | # Checked item 1 |
---|
171 | test_item1 = QtGui.QStandardItem(True) |
---|
172 | test_item1.setCheckable(True) |
---|
173 | test_item1.setCheckState(QtCore.Qt.Checked) |
---|
174 | object_item = QtGui.QStandardItem() |
---|
175 | object_item.setData(test_list1) |
---|
176 | test_item1.setChild(0, object_item) |
---|
177 | |
---|
178 | checkbox_item.setChild(0, test_item0) |
---|
179 | checkbox_item.appendRow(test_item1) |
---|
180 | |
---|
181 | # Unchecked item 2 |
---|
182 | test_item2 = QtGui.QStandardItem(True) |
---|
183 | test_item2.setCheckable(True) |
---|
184 | test_item2.setCheckState(QtCore.Qt.Unchecked) |
---|
185 | object_item = QtGui.QStandardItem() |
---|
186 | object_item.setData(test_list2) |
---|
187 | test_item2.setChild(0, object_item) |
---|
188 | checkbox_item.appendRow(test_item2) |
---|
189 | |
---|
190 | checkbox_model.appendRow(checkbox_item) |
---|
191 | |
---|
192 | # Pull out the "plottable" documents |
---|
193 | plot_list = plotsFromCheckedItems(checkbox_model) |
---|
194 | |
---|
195 | # Make sure only the checked data is present |
---|
196 | # FRIDAY IN |
---|
197 | self.assertIn(test_list0, plot_list[1]) |
---|
198 | # SATURDAY IN |
---|
199 | self.assertIn(test_list1, plot_list[0]) |
---|
200 | # MONDAY NOT IN |
---|
201 | self.assertNotIn(test_list2, plot_list[0]) |
---|
202 | self.assertNotIn(test_list2, plot_list[1]) |
---|
203 | |
---|
204 | def testInfoFromData(self): |
---|
205 | """ |
---|
206 | Test Info element extraction from a plottable object |
---|
207 | """ |
---|
208 | loader = Loader() |
---|
209 | manager = DataManager() |
---|
210 | |
---|
211 | # get Data1D |
---|
212 | p_file="cyl_400_20.txt" |
---|
213 | output_object = loader.load(p_file) |
---|
214 | new_data = manager.create_gui_data(output_object[0], p_file) |
---|
215 | |
---|
216 | # Extract Info elements into a model item |
---|
217 | item = infoFromData(new_data) |
---|
218 | |
---|
219 | # Test the item and its children |
---|
220 | self.assertIsInstance(item, QtGui.QStandardItem) |
---|
221 | self.assertEqual(item.rowCount(), 5) |
---|
222 | self.assertEqual(item.text(), "Info") |
---|
223 | self.assertIn(p_file, item.child(0).text()) |
---|
224 | self.assertIn("Run", item.child(1).text()) |
---|
225 | self.assertIn("Data1D", item.child(2).text()) |
---|
226 | self.assertIn(p_file, item.child(3).text()) |
---|
227 | self.assertIn("Process",item.child(4).text()) |
---|
228 | |
---|
229 | def testOpenLink(self): |
---|
230 | """ |
---|
231 | Opening a link in the external browser |
---|
232 | """ |
---|
233 | good_url1 = r"http://test.test.com" |
---|
234 | good_url2 = r"mailto:test@mail.com" |
---|
235 | good_url3 = r"https://127.0.0.1" |
---|
236 | |
---|
237 | bad_url1 = "" |
---|
238 | bad_url2 = QtGui.QStandardItem() |
---|
239 | bad_url3 = r"poop;//**I.am.a.!bad@url" |
---|
240 | |
---|
241 | webbrowser.open = MagicMock() |
---|
242 | openLink(good_url1) |
---|
243 | openLink(good_url2) |
---|
244 | openLink(good_url3) |
---|
245 | self.assertEqual(webbrowser.open.call_count, 3) |
---|
246 | |
---|
247 | with self.assertRaises(AttributeError): |
---|
248 | openLink(bad_url1) |
---|
249 | with self.assertRaises(AttributeError): |
---|
250 | openLink(bad_url2) |
---|
251 | with self.assertRaises(AttributeError): |
---|
252 | openLink(bad_url3) |
---|
253 | |
---|
254 | def testRetrieveData1d(self): |
---|
255 | """ |
---|
256 | """ |
---|
257 | with self.assertRaises(AttributeError): |
---|
258 | retrieveData1d("BOOP") |
---|
259 | |
---|
260 | #data = Data1D() |
---|
261 | #with self.assertRaises(ValueError): |
---|
262 | # retrieveData1d(data) |
---|
263 | |
---|
264 | data = Data1D(x=[1.0, 2.0, 3.0], y=[10.0, 11.0, 12.0]) |
---|
265 | |
---|
266 | text = retrieveData1d(data) |
---|
267 | |
---|
268 | self.assertIn("Temperature:", text) |
---|
269 | self.assertIn("Beam_size:", text) |
---|
270 | self.assertIn("X_min = 1.0: X_max = 3.0", text) |
---|
271 | self.assertIn("3.0 \t12.0 \t0.0 \t0.0", text) |
---|
272 | |
---|
273 | def testRetrieveData2d(self): |
---|
274 | """ |
---|
275 | """ |
---|
276 | with self.assertRaises(AttributeError): |
---|
277 | retrieveData2d("BOOP") |
---|
278 | data = Data2D(image=[1.0, 2.0, 3.0], |
---|
279 | err_image=[0.01, 0.02, 0.03], |
---|
280 | qx_data=[0.1, 0.2, 0.3], |
---|
281 | qy_data=[0.1, 0.2, 0.3]) |
---|
282 | |
---|
283 | text = retrieveData2d(data) |
---|
284 | |
---|
285 | self.assertIn("Type: Data2D", text) |
---|
286 | self.assertIn("I_min = 1.0", text) |
---|
287 | self.assertIn("I_max = 3.0", text) |
---|
288 | self.assertIn("2 \t0.3 \t0.3 \t3.0 \t0.03 \t0.0 \t0.0", text) |
---|
289 | |
---|
290 | def testOnTXTSave(self): |
---|
291 | """ |
---|
292 | Test the file writer for saving 1d/2d data |
---|
293 | """ |
---|
294 | path = "test123" |
---|
295 | if os.path.isfile(path): |
---|
296 | os.remove(path) |
---|
297 | |
---|
298 | # Broken data |
---|
299 | data = Data1D(x=[1.0, 2.0, 3.0], y=[]) |
---|
300 | # Expect a raise |
---|
301 | with self.assertRaises(IndexError): |
---|
302 | onTXTSave(data, path) |
---|
303 | |
---|
304 | # Good data - no dX/dY |
---|
305 | data = Data1D(x=[1.0, 2.0, 3.0], y=[10.0, 11.0, 12.0]) |
---|
306 | onTXTSave(data, path) |
---|
307 | |
---|
308 | self.assertTrue(os.path.isfile(path)) |
---|
309 | with open(path,'r') as out: |
---|
310 | data_read = out.read() |
---|
311 | self.assertEqual("<X> <Y>\n1 10\n2 11\n3 12\n", data_read) |
---|
312 | |
---|
313 | if os.path.isfile(path): |
---|
314 | os.remove(path) |
---|
315 | |
---|
316 | # Good data - with dX/dY |
---|
317 | data = Data1D(x=[1.0, 2.0, 3.0], y=[10.0, 11.0, 12.0], |
---|
318 | dx=[0.1, 0.2, 0.3], dy=[0.1, 0.2, 0.3]) |
---|
319 | |
---|
320 | onTXTSave(data, path) |
---|
321 | with open(path,'r') as out: |
---|
322 | data_read = out.read() |
---|
323 | self.assertIn("<X> <Y> <dY> <dX>\n", data_read) |
---|
324 | self.assertIn("1 10 0.1 0.1\n", data_read) |
---|
325 | self.assertIn("2 11 0.2 0.2\n", data_read) |
---|
326 | self.assertIn("3 12 0.3 0.3\n", data_read) |
---|
327 | |
---|
328 | if os.path.isfile(path): |
---|
329 | os.remove(path) |
---|
330 | |
---|
331 | def testSaveData1D(self): |
---|
332 | """ |
---|
333 | Test the 1D file save method |
---|
334 | """ |
---|
335 | data = Data1D(x=[1.0, 2.0, 3.0], y=[10.0, 11.0, 12.0], |
---|
336 | dx=[0.1, 0.2, 0.3], dy=[0.1, 0.2, 0.3]) |
---|
337 | |
---|
338 | # Test the .txt format |
---|
339 | file_name = "test123_out.txt" |
---|
340 | QtWidgets.QFileDialog.getSaveFileName = MagicMock(return_value=(file_name,'')) |
---|
341 | data.filename = "test123.txt" |
---|
342 | saveData1D(data) |
---|
343 | self.assertTrue(os.path.isfile(file_name)) |
---|
344 | os.remove(file_name) |
---|
345 | |
---|
346 | # Test the .xml format |
---|
347 | file_name = "test123_out.xml" |
---|
348 | QtWidgets.QFileDialog.getSaveFileName = MagicMock(return_value=(file_name,'')) |
---|
349 | data.filename = "test123.xml" |
---|
350 | saveData1D(data) |
---|
351 | self.assertTrue(os.path.isfile(file_name)) |
---|
352 | os.remove(file_name) |
---|
353 | |
---|
354 | # Test the wrong format |
---|
355 | file_name = "test123_out.mp3" |
---|
356 | QtWidgets.QFileDialog.getSaveFileName = MagicMock(return_value=(file_name,'')) |
---|
357 | data.filename = "test123.mp3" |
---|
358 | saveData1D(data) |
---|
359 | self.assertFalse(os.path.isfile(file_name)) |
---|
360 | |
---|
361 | def testSaveData2D(self): |
---|
362 | """ |
---|
363 | Test the 1D file save method |
---|
364 | """ |
---|
365 | data = Data2D(image=[1.0, 2.0, 3.0], |
---|
366 | err_image=[0.01, 0.02, 0.03], |
---|
367 | qx_data=[0.1, 0.2, 0.3], |
---|
368 | qy_data=[0.1, 0.2, 0.3]) |
---|
369 | |
---|
370 | # Test the .txt format |
---|
371 | file_name = "test123_out.dat" |
---|
372 | QtWidgets.QFileDialog.getSaveFileName = MagicMock(return_value=(file_name,'')) |
---|
373 | data.filename = "test123.dat" |
---|
374 | saveData2D(data) |
---|
375 | self.assertTrue(os.path.isfile(file_name)) |
---|
376 | os.remove(file_name) |
---|
377 | |
---|
378 | # Test the wrong format |
---|
379 | file_name = "test123_out.mp3" |
---|
380 | QtWidgets.QFileDialog.getSaveFileName = MagicMock(return_value=(file_name,'')) |
---|
381 | data.filename = "test123.mp3" |
---|
382 | saveData2D(data) |
---|
383 | self.assertFalse(os.path.isfile(file_name)) |
---|
384 | |
---|
385 | def testXYTransform(self): |
---|
386 | """ Assure the unit/legend transformation is correct""" |
---|
387 | data = Data1D(x=[1.0, 2.0, 3.0], y=[10.0, 11.0, 12.0], |
---|
388 | dx=[0.1, 0.2, 0.3], dy=[0.1, 0.2, 0.3]) |
---|
389 | |
---|
390 | xLabel, yLabel, xscale, yscale = xyTransform(data, xLabel="x", yLabel="y") |
---|
391 | self.assertEqual(xLabel, "()") |
---|
392 | self.assertEqual(xscale, "linear") |
---|
393 | self.assertEqual(yscale, "linear") |
---|
394 | |
---|
395 | xLabel, yLabel, xscale, yscale = xyTransform(data, xLabel="x^(2)", yLabel="1/y") |
---|
396 | self.assertEqual(xLabel, "^{2}(()^{2})") |
---|
397 | self.assertEqual(yLabel, "1/(()^{-1})") |
---|
398 | self.assertEqual(xscale, "linear") |
---|
399 | self.assertEqual(yscale, "linear") |
---|
400 | |
---|
401 | xLabel, yLabel, xscale, yscale = xyTransform(data, xLabel="x^(4)", yLabel="ln(y)") |
---|
402 | self.assertEqual(xLabel, "^{4}(()^{4})") |
---|
403 | self.assertEqual(yLabel, "\\ln{()}()") |
---|
404 | self.assertEqual(xscale, "linear") |
---|
405 | self.assertEqual(yscale, "linear") |
---|
406 | |
---|
407 | xLabel, yLabel, xscale, yscale = xyTransform(data, xLabel="ln(x)", yLabel="y^(2)") |
---|
408 | self.assertEqual(xLabel, "\\ln{()}()") |
---|
409 | self.assertEqual(yLabel, "^{2}(()^{2})") |
---|
410 | self.assertEqual(xscale, "linear") |
---|
411 | self.assertEqual(yscale, "linear") |
---|
412 | |
---|
413 | xLabel, yLabel, xscale, yscale = xyTransform(data, xLabel="log10(x)", yLabel="y*x^(2)") |
---|
414 | self.assertEqual(xLabel, "()") |
---|
415 | self.assertEqual(yLabel, " \\ \\ ^{2}(()^{2})") |
---|
416 | self.assertEqual(xscale, "log") |
---|
417 | self.assertEqual(yscale, "linear") |
---|
418 | |
---|
419 | xLabel, yLabel, xscale, yscale = xyTransform(data, xLabel="log10(x^(4))", yLabel="y*x^(4)") |
---|
420 | self.assertEqual(xLabel, "^{4}(()^{4})") |
---|
421 | self.assertEqual(yLabel, " \\ \\ ^{4}(()^{16})") |
---|
422 | self.assertEqual(xscale, "log") |
---|
423 | self.assertEqual(yscale, "linear") |
---|
424 | |
---|
425 | xLabel, yLabel, xscale, yscale = xyTransform(data, xLabel="x", yLabel="1/sqrt(y)") |
---|
426 | self.assertEqual(yLabel, "1/\\sqrt{}(()^{-0.5})") |
---|
427 | self.assertEqual(yscale, "linear") |
---|
428 | |
---|
429 | xLabel, yLabel, xscale, yscale = xyTransform(data, xLabel="x", yLabel="log10(y)") |
---|
430 | self.assertEqual(yLabel, "()") |
---|
431 | self.assertEqual(yscale, "log") |
---|
432 | |
---|
433 | xLabel, yLabel, xscale, yscale = xyTransform(data, xLabel="x", yLabel="ln(y*x)") |
---|
434 | self.assertEqual(yLabel, "\\ln{( \\ \\ )}()") |
---|
435 | self.assertEqual(yscale, "linear") |
---|
436 | |
---|
437 | xLabel, yLabel, xscale, yscale = xyTransform(data, xLabel="x", yLabel="ln(y*x^(2))") |
---|
438 | self.assertEqual(yLabel, "\\ln ( \\ \\ ^{2})(()^{2})") |
---|
439 | self.assertEqual(yscale, "linear") |
---|
440 | |
---|
441 | xLabel, yLabel, xscale, yscale = xyTransform(data, xLabel="x", yLabel="ln(y*x^(4))") |
---|
442 | self.assertEqual(yLabel, "\\ln ( \\ \\ ^{4})(()^{4})") |
---|
443 | self.assertEqual(yscale, "linear") |
---|
444 | |
---|
445 | xLabel, yLabel, xscale, yscale = xyTransform(data, xLabel="x", yLabel="log10(y*x^(4))") |
---|
446 | self.assertEqual(yLabel, " \\ \\ ^{4}(()^{4})") |
---|
447 | self.assertEqual(yscale, "log") |
---|
448 | |
---|
449 | def testReplaceHTMLwithUTF8(self): |
---|
450 | ''' test single character replacement ''' |
---|
451 | s = None |
---|
452 | with self.assertRaises(AttributeError): |
---|
453 | result = replaceHTMLwithUTF8(s) |
---|
454 | |
---|
455 | s = "" |
---|
456 | self.assertEqual(replaceHTMLwithUTF8(s), s) |
---|
457 | |
---|
458 | s = "aaaa" |
---|
459 | self.assertEqual(replaceHTMLwithUTF8(s), s) |
---|
460 | |
---|
461 | s = "Å ∞ ±" |
---|
462 | self.assertEqual(replaceHTMLwithUTF8(s), "Ã
â ±") |
---|
463 | |
---|
464 | def testReplaceHTMLwithASCII(self): |
---|
465 | ''' test single character replacement''' |
---|
466 | s = None |
---|
467 | with self.assertRaises(AttributeError): |
---|
468 | result = replaceHTMLwithASCII(s) |
---|
469 | |
---|
470 | s = "" |
---|
471 | self.assertEqual(replaceHTMLwithASCII(s), s) |
---|
472 | |
---|
473 | s = "aaaa" |
---|
474 | self.assertEqual(replaceHTMLwithASCII(s), s) |
---|
475 | |
---|
476 | s = "Å ∞ ±" |
---|
477 | self.assertEqual(replaceHTMLwithASCII(s), "Ang inf +/-") |
---|
478 | |
---|
479 | def testConvertUnitToUTF8(self): |
---|
480 | ''' test unit string replacement''' |
---|
481 | s = None |
---|
482 | self.assertIsNone(convertUnitToUTF8(s)) |
---|
483 | |
---|
484 | s = "" |
---|
485 | self.assertEqual(convertUnitToUTF8(s), s) |
---|
486 | |
---|
487 | s = "aaaa" |
---|
488 | self.assertEqual(convertUnitToUTF8(s), s) |
---|
489 | |
---|
490 | s = "1/A" |
---|
491 | self.assertEqual(convertUnitToUTF8(s), "Ã
<sup>-1</sup>") |
---|
492 | |
---|
493 | s = "Ang" |
---|
494 | self.assertEqual(convertUnitToUTF8(s), "Ã
") |
---|
495 | |
---|
496 | s = "1e-6/Ang^2" |
---|
497 | self.assertEqual(convertUnitToUTF8(s), "10<sup>-6</sup>/Ã
<sup>2</sup>") |
---|
498 | |
---|
499 | s = "inf" |
---|
500 | self.assertEqual(convertUnitToUTF8(s), "â") |
---|
501 | |
---|
502 | s = "1/cm" |
---|
503 | self.assertEqual(convertUnitToUTF8(s), "cm<sup>-1</sup>") |
---|
504 | |
---|
505 | def testConvertUnitToHTML(self): |
---|
506 | ''' test unit string replacement''' |
---|
507 | s = None |
---|
508 | self.assertIsNone(convertUnitToHTML(s)) |
---|
509 | |
---|
510 | s = "" |
---|
511 | self.assertEqual(convertUnitToHTML(s), s) |
---|
512 | |
---|
513 | s = "aaaa" |
---|
514 | self.assertEqual(convertUnitToHTML(s), s) |
---|
515 | |
---|
516 | s = "1/A" |
---|
517 | self.assertEqual(convertUnitToHTML(s), "Å<sup>-1</sup>") |
---|
518 | |
---|
519 | s = "Ang" |
---|
520 | self.assertEqual(convertUnitToHTML(s), "Å") |
---|
521 | |
---|
522 | s = "1e-6/Ang^2" |
---|
523 | self.assertEqual(convertUnitToHTML(s), "10<sup>-6</sup>/Å<sup>2</sup>") |
---|
524 | |
---|
525 | s = "inf" |
---|
526 | self.assertEqual(convertUnitToHTML(s), "∞") |
---|
527 | s = "-inf" |
---|
528 | |
---|
529 | self.assertEqual(convertUnitToHTML(s), "-∞") |
---|
530 | |
---|
531 | s = "1/cm" |
---|
532 | self.assertEqual(convertUnitToHTML(s), "cm<sup>-1</sup>") |
---|
533 | |
---|
534 | def testParseName(self): |
---|
535 | '''test parse out a string from the beinning of a string''' |
---|
536 | # good input |
---|
537 | value = "_test" |
---|
538 | self.assertEqual(parseName(value, '_'), 'test') |
---|
539 | value = "____test____" |
---|
540 | self.assertEqual(parseName(value, '_'), '___test____') |
---|
541 | self.assertEqual(parseName(value, '___'), '_test____') |
---|
542 | self.assertEqual(parseName(value, 'test'), '____test____') |
---|
543 | # bad input |
---|
544 | with self.assertRaises(TypeError): |
---|
545 | parseName(value, None) |
---|
546 | with self.assertRaises(TypeError): |
---|
547 | parseName(None, '_') |
---|
548 | value = [] |
---|
549 | with self.assertRaises(TypeError): |
---|
550 | parseName(value, '_') |
---|
551 | value = 1.44 |
---|
552 | with self.assertRaises(TypeError): |
---|
553 | parseName(value, 'p') |
---|
554 | value = 100 |
---|
555 | with self.assertRaises(TypeError): |
---|
556 | parseName(value, 'p') |
---|
557 | |
---|
558 | def testToDouble(self): |
---|
559 | '''test homemade string-> double converter''' |
---|
560 | #good values |
---|
561 | value = "1" |
---|
562 | self.assertEqual(toDouble(value), 1.0) |
---|
563 | value = "1.2" |
---|
564 | # has to be AlmostEqual due to numerical rounding |
---|
565 | self.assertAlmostEqual(toDouble(value), 1.2, 6) |
---|
566 | value = "2,1" |
---|
567 | self.assertAlmostEqual(toDouble(value), 2.1, 6) |
---|
568 | |
---|
569 | # bad values |
---|
570 | value = None |
---|
571 | with self.assertRaises(TypeError): |
---|
572 | toDouble(value) |
---|
573 | value = "MyDouble" |
---|
574 | with self.assertRaises(TypeError): |
---|
575 | toDouble(value) |
---|
576 | value = [1,2.2] |
---|
577 | with self.assertRaises(TypeError): |
---|
578 | toDouble(value) |
---|
579 | |
---|
580 | |
---|
581 | class DoubleValidatorTest(unittest.TestCase): |
---|
582 | """ Test the validator for floats """ |
---|
583 | def setUp(self): |
---|
584 | '''Create the validator''' |
---|
585 | self.validator = DoubleValidator() |
---|
586 | |
---|
587 | def tearDown(self): |
---|
588 | '''Destroy the validator''' |
---|
589 | self.validator = None |
---|
590 | |
---|
591 | def testValidateGood(self): |
---|
592 | """Test a valid float """ |
---|
593 | QtCore.QLocale.setDefault(QtCore.QLocale('en_US')) |
---|
594 | float_good = "170" |
---|
595 | self.assertEqual(self.validator.validate(float_good, 1)[0], QtGui.QValidator.Acceptable) |
---|
596 | float_good = "170.11" |
---|
597 | ## investigate: a double returns Invalid here! |
---|
598 | ##self.assertEqual(self.validator.validate(float_good, 1)[0], QtGui.QValidator.Acceptable) |
---|
599 | float_good = "17e2" |
---|
600 | self.assertEqual(self.validator.validate(float_good, 1)[0], QtGui.QValidator.Acceptable) |
---|
601 | |
---|
602 | def testValidateBad(self): |
---|
603 | """Test a bad float """ |
---|
604 | float_bad = None |
---|
605 | self.assertEqual(self.validator.validate(float_bad, 1)[0], QtGui.QValidator.Intermediate) |
---|
606 | float_bad = [1] |
---|
607 | with self.assertRaises(TypeError): |
---|
608 | self.validator.validate(float_bad, 1) |
---|
609 | float_bad = "1,3" |
---|
610 | self.assertEqual(self.validator.validate(float_bad, 1)[0], QtGui.QValidator.Invalid) |
---|
611 | |
---|
612 | def notestFixup(self): |
---|
613 | """Fixup of a float""" |
---|
614 | float_to_fixup = "1,3" |
---|
615 | self.validator.fixup(float_to_fixup) |
---|
616 | self.assertEqual(float_to_fixup, "13") |
---|
617 | |
---|
618 | |
---|
619 | class FormulaValidatorTest(unittest.TestCase): |
---|
620 | """ Test the formula validator """ |
---|
621 | def setUp(self): |
---|
622 | '''Create the validator''' |
---|
623 | self.validator = FormulaValidator() |
---|
624 | |
---|
625 | def tearDown(self): |
---|
626 | '''Destroy the validator''' |
---|
627 | self.validator = None |
---|
628 | |
---|
629 | def testValidateGood(self): |
---|
630 | """Test a valid Formula """ |
---|
631 | formula_good = "H24O12C4C6N2Pu" |
---|
632 | self.assertEqual(self.validator.validate(formula_good, 1)[0], QtGui.QValidator.Acceptable) |
---|
633 | |
---|
634 | formula_good = "(H2O)0.5(D2O)0.5" |
---|
635 | self.assertEqual(self.validator.validate(formula_good, 1)[0], QtGui.QValidator.Acceptable) |
---|
636 | |
---|
637 | def testValidateBad(self): |
---|
638 | """Test an invalid Formula """ |
---|
639 | formula_bad = "H24 %%%O12C4C6N2Pu" |
---|
640 | self.assertRaises(self.validator.validate(formula_bad, 1)[0]) |
---|
641 | self.assertEqual(self.validator.validate(formula_bad, 1)[0], QtGui.QValidator.Intermediate) |
---|
642 | |
---|
643 | formula_bad = [1] |
---|
644 | self.assertEqual(self.validator.validate(formula_bad, 1)[0], QtGui.QValidator.Intermediate) |
---|
645 | |
---|
646 | class HashableStandardItemTest(unittest.TestCase): |
---|
647 | """ Test the reimplementation of QStandardItem """ |
---|
648 | def setUp(self): |
---|
649 | '''Create the validator''' |
---|
650 | self.item = HashableStandardItem() |
---|
651 | |
---|
652 | def tearDown(self): |
---|
653 | '''Destroy the validator''' |
---|
654 | self.item = None |
---|
655 | |
---|
656 | def testHash(self): |
---|
657 | '''assure the item returns hash''' |
---|
658 | self.assertEqual(self.item.__hash__(), 0) |
---|
659 | |
---|
660 | def testIndexing(self): |
---|
661 | '''test that we can use HashableSI as an index''' |
---|
662 | dictionary = {} |
---|
663 | dictionary[self.item] = "wow!" |
---|
664 | self.assertEqual(dictionary[self.item], "wow!") |
---|
665 | |
---|
666 | def testClone(self): |
---|
667 | '''let's see if we can clone the item''' |
---|
668 | item_clone = self.item.clone() |
---|
669 | self.assertEqual(item_clone.__hash__(), 0) |
---|
670 | |
---|
671 | if __name__ == "__main__": |
---|
672 | unittest.main() |
---|
673 | |
---|