source: sasview/src/sas/qtgui/Utilities/UnitTesting/TabbedModelEditorTest.py @ 59b925c1

ESS_GUIESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_openclESS_GUI_sync_sascalc
Last change on this file since 59b925c1 was 80468f6, checked in by Piotr Rozyczko <rozyczko@…>, 7 years ago

Fixed a few unit test files

  • Property mode set to 100644
File size: 11.0 KB
Line 
1import os
2import sys
3import unittest
4import logging
5
6from unittest.mock import MagicMock
7
8from PyQt5.QtGui import *
9from PyQt5.QtCore import *
10from PyQt5.QtWidgets import *
11
12# set up import paths
13import sas.qtgui.path_prepare
14
15from UnitTesting.TestUtils import QtSignalSpy
16
17# Local
18import sas.qtgui.Utilities.GuiUtils as GuiUtils
19from sas.qtgui.Utilities.TabbedModelEditor import TabbedModelEditor
20from sas.qtgui.Utilities.PluginDefinition import PluginDefinition
21from sas.qtgui.Utilities.ModelEditor import ModelEditor
22
23
24if not QApplication.instance():
25    app = QApplication(sys.argv)
26
27class TabbedModelEditorTest(unittest.TestCase):
28    def setUp(self):
29        """
30        Prepare the editors
31        """
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)
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
145    def testpluginTitleSet(self):
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
150        self.widget.pluginTitleSet()
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()
206        #invoke the method
207        self.widget.updateFromEditor()
208
209        # Test the behaviour
210        self.assertTrue(self.widget.writeFile.called_once())
211        self.assertTrue(self.widget.writeFile.called_with('testfile.py', boring_text))
212
213    def testCanWriteModel(self):
214        """
215        Test if the model can be written to a file, given initial conditions
216        """
217        test_model = {'overwrite':False,
218                      'text':"return"}
219        test_path = "test.py"
220
221        with self.assertRaises(Exception):
222            self.widget.canWriteModel()
223
224        with self.assertRaises(Exception):
225            self.widget.canWriteModel(model=test_model)
226
227        with self.assertRaises(Exception):
228            self.widget.canWriteModel(full_path=test_path)
229
230        # 1. Overwrite box unchecked, file exists
231        os.path.isfile = MagicMock(return_value=True)
232        QMessageBox.critical = MagicMock()
233
234        ret = self.widget.canWriteModel(model=test_model, full_path=test_path)
235        self.assertFalse(ret)
236        self.assertTrue(QMessageBox.critical.called_once())
237        self.assertIn('Plugin Error', QMessageBox.critical.call_args[0][1])
238        self.assertIn('Plugin with specified name already exists', QMessageBox.critical.call_args[0][2])
239
240        # 2. Overwrite box checked, file exists, empty model
241        os.path.isfile = MagicMock(return_value=True)
242        test_model['overwrite']=True
243        test_model['text'] = ""
244        QMessageBox.critical = MagicMock()
245
246        ret = self.widget.canWriteModel(model=test_model, full_path=test_path)
247        self.assertFalse(ret)
248        self.assertTrue(QMessageBox.critical.called_once())
249        self.assertIn('Plugin Error', QMessageBox.critical.call_args[0][1])
250        self.assertIn('Error: Function is not defined', QMessageBox.critical.call_args[0][2])
251
252        # 3. Overwrite box unchecked, file doesn't exists, model with no 'return'
253        os.path.isfile = MagicMock(return_value=False)
254        test_model['overwrite']=False
255        test_model['text'] = "I am a simple model"
256        QMessageBox.critical = MagicMock()
257
258        ret = self.widget.canWriteModel(model=test_model, full_path=test_path)
259        self.assertFalse(ret)
260        self.assertTrue(QMessageBox.critical.called_once())
261        self.assertIn('Plugin Error', QMessageBox.critical.call_args[0][1])
262        self.assertIn('Error: The func(x) must', QMessageBox.critical.call_args[0][2])
263
264        # 4. Overwrite box unchecked, file doesnt exist, good model
265        os.path.isfile = MagicMock(return_value=False)
266        test_model['text'] = "return"
267        ret = self.widget.canWriteModel(model=test_model, full_path=test_path)
268        self.assertTrue(ret)
269
270    def testGenerateModel(self):
271        """
272        Test the model text creation from the dictionary
273        """
274        pass
275
276    def notestCheckModel(self):
277        """test the sasmodel model checker """
278        pass
279
280    def testGetParamHelper(self):
281        """
282        Test the convenience method for converting
283        GUI parameter representation into sasmodel comprehensible string
284        """
285        pass
286
287    def testStrFromParamDict(self):
288        """
289        Test Conversion from dict to param string
290        """
291        test_dict = {0: ('a', '1'),
292                     1: ('eee',''),
293                     2: ('bob', None),
294                     3: ('',-1),
295                     4: (None, None)}
296
297        #{0: ('variable','value'),
298        # 1: ('variable','value'),
299
300        test_str = self.widget.strFromParamDict(test_dict)
301        self.assertIn("", test_str)
302
Note: See TracBrowser for help on using the repository browser.