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