[3b3b40b] | 1 | import os |
---|
| 2 | import sys |
---|
| 3 | import unittest |
---|
| 4 | import logging |
---|
| 5 | |
---|
| 6 | from unittest.mock import MagicMock |
---|
| 7 | |
---|
| 8 | from PyQt5.QtGui import * |
---|
| 9 | from PyQt5.QtCore import * |
---|
| 10 | from PyQt5.QtWidgets import * |
---|
| 11 | |
---|
| 12 | # set up import paths |
---|
| 13 | import sas.qtgui.path_prepare |
---|
| 14 | |
---|
| 15 | from UnitTesting.TestUtils import QtSignalSpy |
---|
| 16 | |
---|
| 17 | # Local |
---|
[80468f6] | 18 | import sas.qtgui.Utilities.GuiUtils as GuiUtils |
---|
[3b3b40b] | 19 | from sas.qtgui.Utilities.TabbedModelEditor import TabbedModelEditor |
---|
| 20 | from sas.qtgui.Utilities.PluginDefinition import PluginDefinition |
---|
| 21 | from sas.qtgui.Utilities.ModelEditor import ModelEditor |
---|
| 22 | |
---|
| 23 | |
---|
[80468f6] | 24 | if not QApplication.instance(): |
---|
| 25 | app = QApplication(sys.argv) |
---|
[3b3b40b] | 26 | |
---|
| 27 | class TabbedModelEditorTest(unittest.TestCase): |
---|
| 28 | def setUp(self): |
---|
| 29 | """ |
---|
| 30 | Prepare the editors |
---|
| 31 | """ |
---|
[80468f6] | 32 | class dummy_manager(object): |
---|
| 33 | _parent = QWidget() |
---|
| 34 | communicate = GuiUtils.Communicate() |
---|
| 35 | |
---|
| 36 | self.widget = TabbedModelEditor(parent=dummy_manager) |
---|
| 37 | self.widget_edit = TabbedModelEditor(parent=dummy_manager, edit_only=True) |
---|
[3b3b40b] | 38 | |
---|
| 39 | def tearDown(self): |
---|
| 40 | """Destroy the DataOperationUtility""" |
---|
| 41 | QMessageBox.exec = MagicMock(return_value=QMessageBox.Discard) |
---|
| 42 | self.widget.close() |
---|
| 43 | self.widget = None |
---|
| 44 | self.widget_edit.close() |
---|
| 45 | self.widget_edit = None |
---|
| 46 | |
---|
| 47 | def testDefaults(self): |
---|
| 48 | """Test the GUI in its default state""" |
---|
| 49 | self.assertEqual(self.widget.filename, "") |
---|
| 50 | self.assertEqual(self.widget.window_title, "Model Editor") |
---|
| 51 | self.assertFalse(self.widget.is_modified) |
---|
| 52 | self.assertFalse(self.widget.edit_only) |
---|
| 53 | self.assertTrue(self.widget_edit.edit_only) |
---|
| 54 | |
---|
| 55 | # Add model |
---|
| 56 | #self.assertFalse(self.widget.cmdLoad.isVisible()) |
---|
| 57 | #self.assertTrue(self.widget_edit.cmdLoad.isVisible()) |
---|
| 58 | self.assertIsInstance(self.widget.plugin_widget, PluginDefinition) |
---|
| 59 | self.assertIsInstance(self.widget.editor_widget, ModelEditor) |
---|
| 60 | # tabs |
---|
| 61 | self.assertEqual(self.widget.tabWidget.count(), 2) |
---|
| 62 | self.assertFalse(self.widget.editor_widget.isEnabled()) |
---|
| 63 | self.assertEqual(self.widget_edit.tabWidget.count(), 1) |
---|
| 64 | self.assertFalse(self.widget_edit.editor_widget.isEnabled()) |
---|
| 65 | |
---|
| 66 | #buttons |
---|
| 67 | self.assertFalse(self.widget.buttonBox.button(QDialogButtonBox.Apply).isEnabled()) |
---|
| 68 | self.assertEqual(self.widget.buttonBox.button(QDialogButtonBox.Apply).text(), "Apply") |
---|
| 69 | self.assertEqual(self.widget_edit.buttonBox.button(QDialogButtonBox.Apply).text(), "Save") |
---|
| 70 | |
---|
| 71 | def testSetPluginActive(self): |
---|
| 72 | """ Enables the plugin editor """ |
---|
| 73 | # by default it is on |
---|
| 74 | self.assertTrue(self.widget.plugin_widget.isEnabled()) |
---|
| 75 | # Let's disable it |
---|
| 76 | self.widget.setPluginActive(False) |
---|
| 77 | self.assertFalse(self.widget.plugin_widget.isEnabled()) |
---|
| 78 | # and back to enabled |
---|
| 79 | self.widget.setPluginActive(True) |
---|
| 80 | self.assertTrue(self.widget.plugin_widget.isEnabled()) |
---|
| 81 | |
---|
| 82 | def notestCloseEvent(self): |
---|
| 83 | """Test the close event wrt. saving info""" |
---|
| 84 | event = QObject() |
---|
| 85 | event.accept = MagicMock() |
---|
| 86 | |
---|
| 87 | # 1. no changes to document - straightforward exit |
---|
| 88 | self.widget.is_modified = False |
---|
| 89 | self.widget.closeEvent(event) |
---|
| 90 | self.assertTrue(event.accept.called_once()) |
---|
| 91 | |
---|
| 92 | # 2. document changed, cancelled |
---|
| 93 | self.widget.is_modified = True |
---|
| 94 | QMessageBox.exec = MagicMock(return_value=QMessageBox.Cancel) |
---|
| 95 | self.widget.closeEvent(event) |
---|
| 96 | self.assertTrue(QMessageBox.exec.called_once()) |
---|
| 97 | # no additional calls to event accept |
---|
| 98 | self.assertTrue(event.accept.called_once()) |
---|
| 99 | |
---|
| 100 | # 3. document changed, save |
---|
| 101 | QMessageBox.exec = MagicMock(return_value=QMessageBox.Save) |
---|
| 102 | self.widget.filename = "random string #8" |
---|
| 103 | self.widget.updateFromEditor = MagicMock() |
---|
| 104 | self.widget.closeEvent(event) |
---|
| 105 | self.assertTrue(QMessageBox.exec.called_once()) |
---|
| 106 | # no additional calls to event accept |
---|
| 107 | self.assertTrue(event.accept.called_once()) |
---|
| 108 | self.assertTrue(self.widget.updateFromEditor.called_once()) |
---|
| 109 | |
---|
| 110 | def testOnApply(self): |
---|
| 111 | """Test the Apply/Save event""" |
---|
| 112 | # name the plugin |
---|
| 113 | self.widget.plugin_widget.txtName.setText("Uncharacteristically eloquent filename") |
---|
| 114 | self.widget.plugin_widget.txtName.editingFinished.emit() |
---|
| 115 | |
---|
| 116 | # make sure the flag is set correctly |
---|
| 117 | self.assertTrue(self.widget.is_modified) |
---|
| 118 | |
---|
| 119 | # default tab |
---|
| 120 | self.widget.updateFromPlugin = MagicMock() |
---|
| 121 | self.widget.onApply() |
---|
| 122 | self.assertTrue(self.widget.updateFromPlugin.called_once()) |
---|
| 123 | |
---|
| 124 | # switch tabs |
---|
| 125 | self.widget.tabWidget.setCurrentIndex(1) |
---|
| 126 | self.widget.updateFromEditor = MagicMock() |
---|
| 127 | self.widget.onApply() |
---|
| 128 | self.assertTrue(self.widget.updateFromEditor.called_once()) |
---|
| 129 | |
---|
| 130 | def testEditorModelModified(self): |
---|
| 131 | """Test reaction to direct edit in the editor """ |
---|
| 132 | # Switch to the Editor tab |
---|
| 133 | self.widget.tabWidget.setCurrentIndex(1) |
---|
| 134 | self.assertFalse(self.widget.is_modified) |
---|
| 135 | |
---|
| 136 | # add some text. This invokes tested method |
---|
| 137 | self.widget.editor_widget.txtEditor.setPlainText("Plain Text") |
---|
| 138 | |
---|
| 139 | # assure relevant functionality is invoked |
---|
| 140 | self.assertTrue(self.widget.buttonBox.button(QDialogButtonBox.Apply).isEnabled()) |
---|
| 141 | self.assertTrue(self.widget.is_modified) |
---|
| 142 | self.assertIn("*", self.widget.windowTitle()) |
---|
| 143 | |
---|
| 144 | |
---|
[80468f6] | 145 | def testpluginTitleSet(self): |
---|
[3b3b40b] | 146 | """Test reaction to direct edit in plugin wizard""" |
---|
| 147 | self.assertFalse(self.widget.is_modified) |
---|
| 148 | |
---|
| 149 | # Call the tested method with no filename defined |
---|
[80468f6] | 150 | self.widget.pluginTitleSet() |
---|
[3b3b40b] | 151 | |
---|
| 152 | # Assure the apply button is disabled |
---|
| 153 | self.assertFalse(self.widget.buttonBox.button(QDialogButtonBox.Apply).isEnabled()) |
---|
| 154 | |
---|
| 155 | # Modify plugin name |
---|
| 156 | new_name = "NAME" |
---|
| 157 | self.widget.plugin_widget.txtName.setText(new_name) |
---|
| 158 | self.widget.plugin_widget.txtName.editingFinished.emit() |
---|
| 159 | |
---|
| 160 | # Assure relevant functionality is invoked |
---|
| 161 | self.assertIn("*", self.widget.windowTitle()) |
---|
| 162 | self.assertIn(new_name, self.widget.windowTitle()) |
---|
| 163 | self.assertTrue(self.widget.buttonBox.button(QDialogButtonBox.Apply).isEnabled()) |
---|
| 164 | self.assertTrue(self.widget.is_modified) |
---|
| 165 | |
---|
| 166 | def testSetTabEdited(self): |
---|
| 167 | """ |
---|
| 168 | Test the simple string update method |
---|
| 169 | """ |
---|
| 170 | title = "title" |
---|
| 171 | title_star = "title*" |
---|
| 172 | self.widget.setWindowTitle(title) |
---|
| 173 | |
---|
| 174 | # 1. -> edited |
---|
| 175 | self.widget.setTabEdited(True) |
---|
| 176 | self.assertIn("*", self.widget.windowTitle()) |
---|
| 177 | # make sure we don't get another star |
---|
| 178 | self.widget.setWindowTitle(title_star) |
---|
| 179 | self.widget.setTabEdited(True) |
---|
| 180 | self.assertEqual(title_star, self.widget.windowTitle()) |
---|
| 181 | |
---|
| 182 | # 2. -> not edited |
---|
| 183 | self.widget.setWindowTitle(title_star) |
---|
| 184 | self.widget.setTabEdited(False) |
---|
| 185 | self.assertEqual(title, self.widget.windowTitle()) |
---|
| 186 | # No changes when no star in title |
---|
| 187 | self.widget.setWindowTitle(title) |
---|
| 188 | self.widget.setTabEdited(False) |
---|
| 189 | self.assertEqual(title, self.widget.windowTitle()) |
---|
| 190 | |
---|
| 191 | def testUpdateFromEditor(self): |
---|
| 192 | """ |
---|
| 193 | Test the behaviour on editor window being updated |
---|
| 194 | """ |
---|
| 195 | # Assure the test rises when no filename present |
---|
| 196 | self.widget.filename = "" |
---|
| 197 | with self.assertRaises(Exception): |
---|
| 198 | self.widget.updateFromEditor() |
---|
| 199 | |
---|
| 200 | # change the filename |
---|
| 201 | self.widget.filename="testfile.py" |
---|
| 202 | self.widget.writeFile = MagicMock() |
---|
| 203 | boring_text = "so bored with unit tests" |
---|
| 204 | self.widget.editor_widget.txtEditor.toPlainText = MagicMock(return_value=boring_text) |
---|
| 205 | self.widget.writeFile = MagicMock() |
---|
[c02721c4] | 206 | self.widget.plugin_widget.is_python = MagicMock() |
---|
[3b3b40b] | 207 | #invoke the method |
---|
| 208 | self.widget.updateFromEditor() |
---|
| 209 | |
---|
| 210 | # Test the behaviour |
---|
| 211 | self.assertTrue(self.widget.writeFile.called_once()) |
---|
| 212 | self.assertTrue(self.widget.writeFile.called_with('testfile.py', boring_text)) |
---|
| 213 | |
---|
| 214 | def testCanWriteModel(self): |
---|
| 215 | """ |
---|
| 216 | Test if the model can be written to a file, given initial conditions |
---|
| 217 | """ |
---|
| 218 | test_model = {'overwrite':False, |
---|
| 219 | 'text':"return"} |
---|
| 220 | test_path = "test.py" |
---|
| 221 | |
---|
| 222 | with self.assertRaises(Exception): |
---|
| 223 | self.widget.canWriteModel() |
---|
| 224 | |
---|
| 225 | with self.assertRaises(Exception): |
---|
| 226 | self.widget.canWriteModel(model=test_model) |
---|
| 227 | |
---|
| 228 | with self.assertRaises(Exception): |
---|
| 229 | self.widget.canWriteModel(full_path=test_path) |
---|
| 230 | |
---|
| 231 | # 1. Overwrite box unchecked, file exists |
---|
| 232 | os.path.isfile = MagicMock(return_value=True) |
---|
| 233 | QMessageBox.critical = MagicMock() |
---|
| 234 | |
---|
| 235 | ret = self.widget.canWriteModel(model=test_model, full_path=test_path) |
---|
| 236 | self.assertFalse(ret) |
---|
| 237 | self.assertTrue(QMessageBox.critical.called_once()) |
---|
| 238 | self.assertIn('Plugin Error', QMessageBox.critical.call_args[0][1]) |
---|
| 239 | self.assertIn('Plugin with specified name already exists', QMessageBox.critical.call_args[0][2]) |
---|
| 240 | |
---|
| 241 | # 2. Overwrite box checked, file exists, empty model |
---|
| 242 | os.path.isfile = MagicMock(return_value=True) |
---|
| 243 | test_model['overwrite']=True |
---|
| 244 | test_model['text'] = "" |
---|
| 245 | QMessageBox.critical = MagicMock() |
---|
| 246 | |
---|
| 247 | ret = self.widget.canWriteModel(model=test_model, full_path=test_path) |
---|
| 248 | self.assertFalse(ret) |
---|
| 249 | self.assertTrue(QMessageBox.critical.called_once()) |
---|
| 250 | self.assertIn('Plugin Error', QMessageBox.critical.call_args[0][1]) |
---|
| 251 | self.assertIn('Error: Function is not defined', QMessageBox.critical.call_args[0][2]) |
---|
| 252 | |
---|
| 253 | # 3. Overwrite box unchecked, file doesn't exists, model with no 'return' |
---|
| 254 | os.path.isfile = MagicMock(return_value=False) |
---|
| 255 | test_model['overwrite']=False |
---|
| 256 | test_model['text'] = "I am a simple model" |
---|
| 257 | QMessageBox.critical = MagicMock() |
---|
| 258 | |
---|
| 259 | ret = self.widget.canWriteModel(model=test_model, full_path=test_path) |
---|
| 260 | self.assertFalse(ret) |
---|
| 261 | self.assertTrue(QMessageBox.critical.called_once()) |
---|
| 262 | self.assertIn('Plugin Error', QMessageBox.critical.call_args[0][1]) |
---|
| 263 | self.assertIn('Error: The func(x) must', QMessageBox.critical.call_args[0][2]) |
---|
| 264 | |
---|
| 265 | # 4. Overwrite box unchecked, file doesnt exist, good model |
---|
| 266 | os.path.isfile = MagicMock(return_value=False) |
---|
| 267 | test_model['text'] = "return" |
---|
| 268 | ret = self.widget.canWriteModel(model=test_model, full_path=test_path) |
---|
| 269 | self.assertTrue(ret) |
---|
| 270 | |
---|
| 271 | def testGenerateModel(self): |
---|
| 272 | """ |
---|
| 273 | Test the model text creation from the dictionary |
---|
| 274 | """ |
---|
| 275 | pass |
---|
| 276 | |
---|
| 277 | def notestCheckModel(self): |
---|
| 278 | """test the sasmodel model checker """ |
---|
| 279 | pass |
---|
| 280 | |
---|
| 281 | def testGetParamHelper(self): |
---|
| 282 | """ |
---|
| 283 | Test the convenience method for converting |
---|
| 284 | GUI parameter representation into sasmodel comprehensible string |
---|
| 285 | """ |
---|
| 286 | pass |
---|
| 287 | |
---|
| 288 | def testStrFromParamDict(self): |
---|
| 289 | """ |
---|
| 290 | Test Conversion from dict to param string |
---|
| 291 | """ |
---|
| 292 | test_dict = {0: ('a', '1'), |
---|
| 293 | 1: ('eee',''), |
---|
| 294 | 2: ('bob', None), |
---|
| 295 | 3: ('',-1), |
---|
| 296 | 4: (None, None)} |
---|
| 297 | |
---|
| 298 | #{0: ('variable','value'), |
---|
| 299 | # 1: ('variable','value'), |
---|
| 300 | |
---|
| 301 | test_str = self.widget.strFromParamDict(test_dict) |
---|
| 302 | self.assertIn("", test_str) |
---|
| 303 | |
---|