source: sasview/src/sas/qtgui/Perspectives/Inversion/UnitTesting/InversionPerspectiveTest.py @ 6bd0d81

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 6bd0d81 was 6bd0d81, checked in by krzywon, 4 years ago

Unit tests for dmax window.

  • Property mode set to 100755
File size: 13.0 KB
Line 
1import time
2import unittest
3from unittest.mock import MagicMock
4
5from PyQt5 import QtGui, QtWidgets
6
7from sas.qtgui.Utilities.GuiUtils import *
8from sas.qtgui.Perspectives.Inversion.InversionPerspective import InversionWindow
9from sas.qtgui.Perspectives.Inversion.InversionUtils import WIDGETS
10from sas.qtgui.Plotting.PlotterData import Data1D
11
12import sas.qtgui.Utilities.GuiUtils as GuiUtils
13
14#if not QtWidgets.QApplication.instance():
15app = QtWidgets.QApplication(sys.argv)
16
17
18class dummy_manager(object):
19    HELP_DIRECTORY_LOCATION = "html"
20    communicate = Communicate()
21
22    def communicator(self):
23        return self.communicate
24
25
26class InversionTest(unittest.TestCase):
27    """ Test the Inversion Perspective GUI """
28
29    def setUp(self):
30        """ Create the InversionWindow """
31        self.widget = InversionWindow(dummy_manager())
32        self.widget.show()
33        self.fakeData1 = GuiUtils.HashableStandardItem("A")
34        self.fakeData2 = GuiUtils.HashableStandardItem("B")
35        reference_data1 = Data1D(x=[0.1, 0.2], y=[0.0, 0.0], dy=[0.0, 0.0])
36        reference_data1.filename = "Test A"
37        reference_data2 = Data1D(x=[0.1, 0.2], y=[0.0, 0.0], dy=[0.0, 0.0])
38        reference_data2.filename = "Test B"
39        GuiUtils.updateModelItem(self.fakeData1, reference_data1)
40        GuiUtils.updateModelItem(self.fakeData2, reference_data2)
41
42    def tearDown(self):
43        """ Destroy the InversionWindow """
44        self.widget.setClosable(False)
45        self.widget.close()
46        self.widget = None
47
48    def removeAllData(self):
49        """ Cleanup method to restore widget to its base state """
50        if len(self.widget.dataList) > 0:
51            remove_me = list(self.widget._dataList.keys())
52            self.widget.removeData(remove_me)
53
54    def baseGUIState(self):
55        """ Testing base state of Inversion """
56        # base class information
57        self.assertIsInstance(self.widget, QtWidgets.QWidget)
58        self.assertEqual(self.widget.windowTitle(), "P(r) Inversion Perspective")
59        self.assertFalse(self.widget.isClosable())
60        # mapper
61        self.assertIsInstance(self.widget.mapper, QtWidgets.QDataWidgetMapper)
62        self.assertNotEqual(self.widget.mapper.mappedSection(self.widget.dataList), -1)
63        # validators
64        self.assertIsInstance(self.widget.noOfTermsInput.validator(), QtGui.QIntValidator)
65        self.assertIsInstance(self.widget.regularizationConstantInput.validator(), QtGui.QDoubleValidator)
66        self.assertIsInstance(self.widget.maxDistanceInput.validator(), QtGui.QDoubleValidator)
67        self.assertIsInstance(self.widget.minQInput.validator(), QtGui.QDoubleValidator)
68        self.assertIsInstance(self.widget.maxQInput.validator(), QtGui.QDoubleValidator)
69        self.assertIsInstance(self.widget.slitHeightInput.validator(), QtGui.QDoubleValidator)
70        self.assertIsInstance(self.widget.slitWidthInput.validator(), QtGui.QDoubleValidator)
71        # model
72        self.assertEqual(self.widget.model.rowCount(), 22)
73        self.assertEqual(self.widget.model.columnCount(), 1)
74        self.assertEqual(self.widget.mapper.model(), self.widget.model)
75        # buttons
76        self.assertFalse(self.widget.calculateThisButton.isEnabled())
77        self.assertFalse(self.widget.removeButton.isEnabled())
78        self.assertFalse(self.widget.regConstantSuggestionButton.isEnabled())
79        self.assertFalse(self.widget.noOfTermsSuggestionButton.isEnabled())
80        self.assertFalse(self.widget.explorerButton.isEnabled())
81        self.assertTrue(self.widget.helpButton.isEnabled())
82        # extra windows and charts
83        self.assertIsNone(self.widget.dmaxWindow)
84        self.assertIsNone(self.widget.prPlot)
85        self.assertIsNone(self.widget.dataPlot)
86        # threads
87        self.assertIsNone(self.widget.calcThread)
88        self.assertIsNone(self.widget.estimationThread)
89        self.assertIsNone(self.widget.estimationThreadNT)
90
91    def baseBatchState(self):
92        """ Testing the base batch fitting state """
93        self.assertTrue(self.widget.allowBatch())
94        self.assertFalse(self.widget.isBatch)
95        self.assertIsNone(self.widget.batchResultsWindow)
96        self.assertFalse(self.widget.calculateAllButton.isEnabled())
97        self.assertEqual(len(self.widget.batchResults), 0)
98        self.assertEqual(len(self.widget.batchComplete), 0)
99        self.widget.closeBatchResults()
100        self.assertIsNone(self.widget.batchResultsWindow)
101
102    def zeroDataSetState(self):
103        """ Testing the base data state of the GUI """
104        # data variables
105        self.assertIsNone(self.widget._data)
106        self.assertEqual(len(self.widget._dataList), 0)
107        self.assertEqual(self.widget.nTermsSuggested, 10)
108        # inputs
109        self.assertEqual(len(self.widget.dataList), 0)
110        self.assertEqual(self.widget.backgroundInput.text(), "0.0")
111        self.assertEqual(self.widget.minQInput.text(), "")
112        self.assertEqual(self.widget.maxQInput.text(), "")
113        self.assertEqual(self.widget.regularizationConstantInput.text(), "0.0001")
114        self.assertEqual(self.widget.noOfTermsInput.text(), "10")
115        self.assertEqual(self.widget.maxDistanceInput.text(), "140.0")
116
117    def oneDataSetState(self):
118        """ Testing the base data state of the GUI """
119        # Test the globals after first sent
120        self.assertEqual(len(self.widget._dataList), 1)
121        self.assertEqual(self.widget.dataList.count(), 1)
122        # See that the buttons are now enabled properly
123        self.widget.enableButtons()
124        self.assertFalse(self.widget.calculateAllButton.isEnabled())
125        self.assertTrue(self.widget.calculateThisButton.isEnabled())
126        self.assertTrue(self.widget.removeButton.isEnabled())
127        self.assertTrue(self.widget.explorerButton.isEnabled())
128
129    def twoDataSetState(self):
130        """ Testing the base data state of the GUI """
131        # Test the globals after first sent
132        self.assertEqual(len(self.widget._dataList), 2)
133        self.assertEqual(self.widget.dataList.count(), 2)
134        # See that the buttons are now enabled properly
135        self.widget.enableButtons()
136        self.assertTrue(self.widget.calculateThisButton.isEnabled())
137        self.assertTrue(self.widget.calculateAllButton.isEnabled())
138        self.assertTrue(self.widget.removeButton.isEnabled())
139        self.assertTrue(self.widget.explorerButton.isEnabled())
140
141    def testDefaults(self):
142        """ Test the GUI in its default state """
143        self.baseGUIState()
144        self.zeroDataSetState()
145        self.baseBatchState()
146        self.removeAllData()
147
148    def testAllowBatch(self):
149        """ Batch P(r) Tests """
150        self.baseBatchState()
151        self.widget.setData([self.fakeData1])
152        self.oneDataSetState()
153        self.widget.setData([self.fakeData2])
154        self.twoDataSetState()
155        self.widget.calculateAllButton.click()
156        self.assertIsNotNone(self.widget.batchResultsWindow)
157        self.assertTrue(self.widget.batchResultsWindow.cmdHelp.isEnabled())
158        self.assertEqual(self.widget.batchResultsWindow.tblParams.columnCount(), 9)
159        self.assertEqual(self.widget.batchResultsWindow.tblParams.rowCount(), 2)
160        if self.widget.isBatch:
161            self.widget.isBatch = False
162        self.widget.batchResultsWindow.close()
163        self.assertIsNone(self.widget.batchResultsWindow)
164        # Last test
165        self.widget.removeData()
166        self.removeAllData()
167        self.baseBatchState()
168
169    def testSetData(self):
170        """ Check if sending data works as expected """
171        self.zeroDataSetState()
172        self.widget.setData([self.fakeData1])
173        self.oneDataSetState()
174        self.widget.setData([self.fakeData1])
175        self.oneDataSetState()
176        self.widget.setData([self.fakeData2])
177        self.twoDataSetState()
178        self.removeAllData()
179        self.zeroDataSetState()
180        self.removeAllData()
181
182    def testRemoveData(self):
183        """ Test data removal from widget """
184        self.widget.setData([self.fakeData1, self.fakeData2])
185        self.twoDataSetState()
186        # Remove data 0
187        self.widget.removeData()
188        self.oneDataSetState()
189        self.removeAllData()
190
191    def testClose(self):
192        """ Test methods related to closing the window """
193        self.assertFalse(self.widget.isClosable())
194        self.widget.close()
195        self.assertTrue(self.widget.isMinimized())
196        self.assertIsNone(self.widget.dmaxWindow)
197        self.assertIsNone(self.widget.batchResultsWindow)
198        self.widget.setClosable(False)
199        self.assertFalse(self.widget.isClosable())
200        self.widget.close()
201        self.assertTrue(self.widget.isMinimized())
202        self.widget.setClosable(True)
203        self.assertTrue(self.widget.isClosable())
204        self.widget.setClosable()
205        self.assertTrue(self.widget.isClosable())
206        self.removeAllData()
207
208    def testGetNFunc(self):
209        """ test nfunc getter """
210        # Float
211        self.widget.noOfTermsInput.setText("10.0")
212        self.assertEqual(self.widget.getNFunc(), 10)
213        # Int
214        self.widget.noOfTermsInput.setText("980")
215        self.assertEqual(self.widget.getNFunc(), 980)
216        # Empty
217        with self.assertLogs(level='ERROR') as cm:
218            self.widget.noOfTermsInput.setText("")
219            n = self.widget.getNFunc()
220            self.assertEqual(cm.output, ['ERROR:root:Incorrect number of terms specified: '])
221        self.assertEqual(self.widget.getNFunc(), 10)
222        # string
223        with self.assertLogs(level='ERROR') as cm:
224            self.widget.noOfTermsInput.setText("Nordvest Pizza")
225            n = self.widget.getNFunc()
226            self.assertEqual(cm.output, ['ERROR:root:Incorrect number of terms specified: Nordvest Pizza'])
227        self.assertEqual(self.widget.getNFunc(), 10)
228        self.removeAllData()
229
230    def testSetCurrentData(self):
231        """ test current data setter """
232        self.widget.setData([self.fakeData1, self.fakeData2])
233
234        # Check that the current data is reference2
235        self.assertEqual(self.widget._data, self.fakeData2)
236        # Set the ref to none
237        self.widget.setCurrentData(None)
238        self.assertEqual(self.widget._data, self.fakeData2)
239        # Set the ref to wrong type
240        with self.assertRaises(AttributeError):
241            self.widget.setCurrentData("Afandi Kebab")
242        # Set the reference to ref1
243        self.widget.setCurrentData(self.fakeData1)
244        self.assertEqual(self.widget._data, self.fakeData1)
245        self.removeAllData()
246
247    def testModelChanged(self):
248        """ Test setting the input and the model and vice-versa """
249        # Initial values
250        self.assertEqual(self.widget._calculator.get_dmax(), 140.0)
251        self.assertEqual(self.widget._calculator.get_qmax(), -1.0)
252        self.assertEqual(self.widget._calculator.get_qmin(), -1.0)
253        self.assertEqual(self.widget._calculator.slit_height, 0.0)
254        self.assertEqual(self.widget._calculator.slit_width, 0.0)
255        self.assertEqual(self.widget._calculator.alpha, 0.0001)
256        # Set new values
257        self.widget.maxDistanceInput.setText("1.0")
258        self.widget.maxQInput.setText("3.0")
259        self.widget.minQInput.setText("5.0")
260        self.widget.slitHeightInput.setText("7.0")
261        self.widget.slitWidthInput.setText("9.0")
262        self.widget.regularizationConstantInput.setText("11.0")
263        # Check new values
264        self.assertEqual(self.widget._calculator.get_dmax(), 1.0)
265        self.assertEqual(self.widget._calculator.get_qmax(), 3.0)
266        self.assertEqual(self.widget._calculator.get_qmin(), 5.0)
267        self.assertEqual(self.widget._calculator.slit_height, 7.0)
268        self.assertEqual(self.widget._calculator.slit_width, 9.0)
269        self.assertEqual(self.widget._calculator.alpha, 11.0)
270        # Change model directly
271        self.widget.model.setItem(WIDGETS.W_MAX_DIST, QtGui.QStandardItem("2.0"))
272        self.widget.model.setItem(WIDGETS.W_QMIN, QtGui.QStandardItem("4.0"))
273        self.widget.model.setItem(WIDGETS.W_QMAX, QtGui.QStandardItem("6.0"))
274        self.widget.model.setItem(WIDGETS.W_SLIT_HEIGHT, QtGui.QStandardItem("8.0"))
275        self.widget.model.setItem(WIDGETS.W_SLIT_WIDTH, QtGui.QStandardItem("10.0"))
276        self.widget.model.setItem(WIDGETS.W_REGULARIZATION, QtGui.QStandardItem("12.0"))
277        # Check values
278        self.assertEqual(self.widget._calculator.get_dmax(), 2.0)
279        self.assertEqual(self.widget._calculator.get_qmin(), 4.0)
280        self.assertEqual(self.widget._calculator.get_qmax(), 6.0)
281        self.assertEqual(self.widget._calculator.slit_height, 8.0)
282        self.assertEqual(self.widget._calculator.slit_width, 10.0)
283        self.assertEqual(self.widget._calculator.alpha, 12.0)
284        self.removeAllData()
285
286    def testOpenExplorerWindow(self):
287        """ open Dx window """
288        self.assertIsNone(self.widget.dmaxWindow)
289        self.assertFalse(self.widget.explorerButton.isEnabled())
290        self.widget.openExplorerWindow()
291        self.assertIsNotNone(self.widget.dmaxWindow)
292        self.assertTrue(self.widget.dmaxWindow.isVisible())
293        self.assertTrue(self.widget.dmaxWindow.windowTitle() == "Dₐₓ Explorer")
294
295
296if __name__ == "__main__":
297    unittest.main()
Note: See TracBrowser for help on using the repository browser.