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 | # SV imports |
---|
10 | from sas.sascalc.dataloader.loader import Loader |
---|
11 | from sas.sasgui.guiframe.data_manager import DataManager |
---|
12 | |
---|
13 | # Tested module |
---|
14 | from GuiUtils import * |
---|
15 | |
---|
16 | class FakeData(object): |
---|
17 | '''Data1D/2D object for testing''' |
---|
18 | def __init__(self): |
---|
19 | self.x = [] |
---|
20 | self.y = [] |
---|
21 | |
---|
22 | class GuiUtilsTest(unittest.TestCase): |
---|
23 | '''Test the GUI Utilities methods''' |
---|
24 | def setUp(self): |
---|
25 | '''Empty''' |
---|
26 | pass |
---|
27 | |
---|
28 | def tearDown(self): |
---|
29 | '''Empty''' |
---|
30 | pass |
---|
31 | |
---|
32 | def testDefaults(self): |
---|
33 | """ |
---|
34 | Test all the global constants defined in the file. |
---|
35 | """ |
---|
36 | # Should probably test the constants in the file, |
---|
37 | # but this will done after trimming down GuiUtils |
---|
38 | # and retaining only necessary variables. |
---|
39 | pass |
---|
40 | |
---|
41 | def testGetAppDir(self): |
---|
42 | """ |
---|
43 | """ |
---|
44 | pass |
---|
45 | |
---|
46 | def testGetUserDirectory(self): |
---|
47 | """ |
---|
48 | Simple test of user directory getter |
---|
49 | """ |
---|
50 | home_dir = os.path.expanduser("~") |
---|
51 | self.assertIn(home_dir, get_user_directory()) |
---|
52 | |
---|
53 | def testCommunicate(self): |
---|
54 | """ |
---|
55 | Test the container class with signal definitions |
---|
56 | """ |
---|
57 | com = Communicate() |
---|
58 | |
---|
59 | # All defined signals |
---|
60 | list_of_signals = [ |
---|
61 | 'fileReadSignal', |
---|
62 | 'fileDataReceivedSignal', |
---|
63 | 'statusBarUpdateSignal', |
---|
64 | 'updatePerspectiveWithDataSignal', |
---|
65 | 'updateModelFromPerspectiveSignal', |
---|
66 | 'plotRequestedSignal', |
---|
67 | 'progressBarUpdateSignal', |
---|
68 | ] |
---|
69 | |
---|
70 | # Assure all signals are defined. |
---|
71 | for signal in list_of_signals: |
---|
72 | self.assertIn(signal, dir(com)) |
---|
73 | |
---|
74 | |
---|
75 | def testUpdateModelItem(self): |
---|
76 | """ |
---|
77 | Test the QModelItem update method |
---|
78 | """ |
---|
79 | test_item = QtGui.QStandardItem() |
---|
80 | test_list = ['aa','11'] |
---|
81 | update_data = QtCore.QVariant(test_list) |
---|
82 | name = "Black Sabbath" |
---|
83 | |
---|
84 | # update the item |
---|
85 | updateModelItem(test_item, update_data, name) |
---|
86 | |
---|
87 | # Make sure test_item got all data added |
---|
88 | self.assertEqual(test_item.child(0).text(), name) |
---|
89 | self.assertTrue(test_item.child(0).isCheckable()) |
---|
90 | list_from_item = test_item.child(0).child(0).data().toPyObject() |
---|
91 | self.assertIsInstance(list_from_item, list) |
---|
92 | self.assertEqual(str(list_from_item[0]), test_list[0]) |
---|
93 | self.assertEqual(str(list_from_item[1]), test_list[1]) |
---|
94 | |
---|
95 | |
---|
96 | def testPlotsFromCheckedItems(self): |
---|
97 | """ |
---|
98 | Test addition of a plottable to the model |
---|
99 | """ |
---|
100 | |
---|
101 | # Mockup data |
---|
102 | test_list0 = "FRIDAY" |
---|
103 | test_list1 = "SATURDAY" |
---|
104 | test_list2 = "MONDAY" |
---|
105 | |
---|
106 | # Main item ("file") |
---|
107 | checkbox_model = QtGui.QStandardItemModel() |
---|
108 | checkbox_item = QtGui.QStandardItem(True) |
---|
109 | checkbox_item.setCheckable(True) |
---|
110 | checkbox_item.setCheckState(QtCore.Qt.Checked) |
---|
111 | test_item0 = QtGui.QStandardItem() |
---|
112 | test_item0.setData(QtCore.QVariant(test_list0)) |
---|
113 | |
---|
114 | # Checked item 1 |
---|
115 | test_item1 = QtGui.QStandardItem(True) |
---|
116 | test_item1.setCheckable(True) |
---|
117 | test_item1.setCheckState(QtCore.Qt.Checked) |
---|
118 | object_item = QtGui.QStandardItem() |
---|
119 | object_item.setData(QtCore.QVariant(test_list1)) |
---|
120 | test_item1.setChild(0, object_item) |
---|
121 | |
---|
122 | checkbox_item.setChild(0, test_item0) |
---|
123 | checkbox_item.appendRow(test_item1) |
---|
124 | |
---|
125 | # Unchecked item 2 |
---|
126 | test_item2 = QtGui.QStandardItem(True) |
---|
127 | test_item2.setCheckable(True) |
---|
128 | test_item2.setCheckState(QtCore.Qt.Unchecked) |
---|
129 | object_item = QtGui.QStandardItem() |
---|
130 | object_item.setData(QtCore.QVariant(test_list2)) |
---|
131 | test_item2.setChild(0, object_item) |
---|
132 | checkbox_item.appendRow(test_item2) |
---|
133 | |
---|
134 | checkbox_model.appendRow(checkbox_item) |
---|
135 | |
---|
136 | # Pull out the "plottable" documents |
---|
137 | plot_list = plotsFromCheckedItems(checkbox_model) |
---|
138 | |
---|
139 | # Make sure only the checked data is present |
---|
140 | # FRIDAY IN |
---|
141 | self.assertIn(test_list0, plot_list) |
---|
142 | # SATURDAY IN |
---|
143 | self.assertIn(test_list1, plot_list) |
---|
144 | # MONDAY NOT IN |
---|
145 | self.assertNotIn(test_list2, plot_list) |
---|
146 | |
---|
147 | def testInfoFromData(self): |
---|
148 | """ |
---|
149 | Test Info element extraction from a plottable object |
---|
150 | """ |
---|
151 | loader = Loader() |
---|
152 | manager = DataManager() |
---|
153 | |
---|
154 | # get Data1D |
---|
155 | p_file="cyl_400_20.txt" |
---|
156 | output_object = loader.load(p_file) |
---|
157 | new_data = manager.create_gui_data(output_object, p_file) |
---|
158 | |
---|
159 | # Extract Info elements into a model item |
---|
160 | item = infoFromData(new_data) |
---|
161 | |
---|
162 | # Test the item and its children |
---|
163 | self.assertIsInstance(item, QtGui.QStandardItem) |
---|
164 | self.assertEqual(item.rowCount(), 5) |
---|
165 | self.assertEqual(item.text(), "Info") |
---|
166 | self.assertIn(p_file, item.child(0).text()) |
---|
167 | self.assertIn("Run", item.child(1).text()) |
---|
168 | self.assertIn("Data1D", item.child(2).text()) |
---|
169 | self.assertIn(p_file, item.child(3).text()) |
---|
170 | self.assertIn("Process",item.child(4).text()) |
---|
171 | |
---|
172 | def testOpenLink(self): |
---|
173 | """ |
---|
174 | Opening a link in the external browser |
---|
175 | """ |
---|
176 | good_url1 = r"http://test.test.com" |
---|
177 | good_url2 = r"mailto:test@mail.com" |
---|
178 | good_url3 = r"https://127.0.0.1" |
---|
179 | |
---|
180 | bad_url1 = "" |
---|
181 | bad_url2 = QtGui.QStandardItem() |
---|
182 | bad_url3 = r"poop;//**I.am.a.!bad@url" |
---|
183 | |
---|
184 | webbrowser.open = MagicMock() |
---|
185 | openLink(good_url1) |
---|
186 | openLink(good_url2) |
---|
187 | openLink(good_url3) |
---|
188 | self.assertEqual(webbrowser.open.call_count, 3) |
---|
189 | |
---|
190 | with self.assertRaises(AttributeError): |
---|
191 | openLink(bad_url1) |
---|
192 | with self.assertRaises(AttributeError): |
---|
193 | openLink(bad_url2) |
---|
194 | with self.assertRaises(AttributeError): |
---|
195 | openLink(bad_url3) |
---|
196 | |
---|
197 | def testRetrieveData1d(self): |
---|
198 | """ |
---|
199 | """ |
---|
200 | self.assertRaises(retrieveData1d("BOOP")) |
---|
201 | |
---|
202 | # data = FakeData() |
---|
203 | pass |
---|
204 | |
---|
205 | def testRetrieveData2d(self): |
---|
206 | """ |
---|
207 | """ |
---|
208 | pass |
---|
209 | |
---|
210 | def testOnTXTSave(self): |
---|
211 | """ |
---|
212 | """ |
---|
213 | pass |
---|
214 | |
---|
215 | def testSaveData1D(self): |
---|
216 | """ |
---|
217 | """ |
---|
218 | pass |
---|
219 | |
---|
220 | def testSaveData2D(self): |
---|
221 | """ |
---|
222 | """ |
---|
223 | pass |
---|
224 | |
---|
225 | if __name__ == "__main__": |
---|
226 | unittest.main() |
---|
227 | |
---|