source: sasview/src/sas/qtgui/Utilities/UnitTesting/TabbedModelEditorTest.py @ 3b3b40b

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalc
Last change on this file since 3b3b40b was 3b3b40b, checked in by Piotr Rozyczko <rozyczko@…>, 4 years ago

Merging feature branches

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