source: sasview/src/sas/qtgui/Perspectives/Inversion/UnitTesting/InversionPerspectiveTest.py @ 0662f53

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

P(r) first layer of unit tests based off of final logic.

  • Property mode set to 100755
File size: 10.0 KB
Line 
1import sys
2import unittest
3import logging
4from unittest.mock import MagicMock
5
6from PyQt5 import QtGui, QtWidgets
7from PyQt5.QtTest import QTest
8from PyQt5 import QtCore
9
10import sas.qtgui.path_prepare
11from sas.qtgui.Perspectives.Inversion.InversionPerspective import InversionWindow
12from sas.qtgui.Perspectives.Inversion.InversionUtils import WIDGETS
13from sas.sascalc.dataloader.loader import Loader
14from sas.qtgui.Plotting.PlotterData import Data1D
15
16from sas.qtgui.MainWindow.DataManager import DataManager
17import sas.qtgui.Utilities.LocalConfig
18import sas.qtgui.Utilities.GuiUtils as GuiUtils
19
20#if not QtWidgets.QApplication.instance():
21app = QtWidgets.QApplication(sys.argv)
22
23
24class InversionTest(unittest.TestCase):
25    """ Test the Inversion Perspective GUI """
26
27    def setUp(self):
28        """ Create the InversionWindow """
29        self.widget = InversionWindow(None)
30        self.widget.performEstimate = MagicMock()
31        self.fakeData1 = GuiUtils.HashableStandardItem("A")
32        self.fakeData2 = GuiUtils.HashableStandardItem("B")
33        reference_data1 = Data1D(x=[0.1, 0.2], y=[0.0, 0.0], dy=[0.0, 0.0])
34        reference_data2 = Data1D(x=[0.1, 0.2], y=[0.0, 0.0], dy=[0.0, 0.0])
35        GuiUtils.updateModelItem(self.fakeData1, [reference_data1])
36        GuiUtils.updateModelItem(self.fakeData2, [reference_data2])
37
38    def tearDown(self):
39        """ Destroy the InversionWindow """
40        self.widget.setClosable(True)
41        self.widget.close()
42        self.widget = None
43
44    def removeAllData(self):
45        """ Cleanup method to restore widget to its base state """
46        while len(self.widget.dataList) > 0:
47            self.widget.removeData()
48
49    def baseGUIState(self):
50        """ Testing base state of Inversion """
51        # base class information
52        self.assertIsInstance(self.widget, QtWidgets.QWidget)
53        self.assertEqual(self.widget.windowTitle(), "P(r) Inversion Perspective")
54        self.assertFalse(self.widget.isClosable())
55        # mapper
56        self.assertIsInstance(self.widget.mapper, QtWidgets.QDataWidgetMapper)
57        self.assertNotEqual(self.widget.mapper.mappedSection(self.widget.dataList), -1)
58        # validators
59        self.assertIsInstance(self.widget.noOfTermsInput.validator(), QtGui.QIntValidator)
60        self.assertIsInstance(self.widget.regularizationConstantInput.validator(), QtGui.QDoubleValidator)
61        self.assertIsInstance(self.widget.maxDistanceInput.validator(), QtGui.QDoubleValidator)
62        self.assertIsInstance(self.widget.minQInput.validator(), QtGui.QDoubleValidator)
63        self.assertIsInstance(self.widget.maxQInput.validator(), QtGui.QDoubleValidator)
64        self.assertIsInstance(self.widget.slitHeightInput.validator(), QtGui.QDoubleValidator)
65        self.assertIsInstance(self.widget.slitWidthInput.validator(), QtGui.QDoubleValidator)
66        # model
67        self.assertEqual(self.widget.model.rowCount(), 22)
68        self.assertEqual(self.widget.model.columnCount(), 1)
69        self.assertEqual(self.widget.mapper.model(), self.widget.model)
70        # buttons
71        self.assertFalse(self.widget.calculateThisButton.isEnabled())
72        self.assertFalse(self.widget.removeButton.isEnabled())
73        self.assertFalse(self.widget.regConstantSuggestionButton.isEnabled())
74        self.assertFalse(self.widget.noOfTermsSuggestionButton.isEnabled())
75        self.assertFalse(self.widget.explorerButton.isEnabled())
76        self.assertTrue(self.widget.helpButton.isEnabled())
77        # extra windows and charts
78        self.assertIsNone(self.widget.dmaxWindow)
79        self.assertIsNone(self.widget.prPlot)
80        self.assertIsNone(self.widget.dataPlot)
81        # threads
82        self.assertIsNone(self.widget.calcThread)
83        self.assertIsNone(self.widget.estimationThread)
84        self.assertIsNone(self.widget.estimationThreadNT)
85
86    def baseBatchState(self):
87        """ Testing the base batch fitting state """
88        self.assertTrue(self.widget.allowBatch())
89        self.assertFalse(self.widget.isBatch)
90        self.assertIsNone(self.widget.batchResultsWindow)
91        self.assertFalse(self.widget.calculateAllButton.isEnabled())
92        self.assertEqual(len(self.widget.batchResults), 0)
93        self.assertEqual(len(self.widget.batchComplete), 0)
94        self.widget.closeBatchResults()
95        self.assertIsNone(self.widget.batchResultsWindow)
96
97    def zeroDataSetState(self):
98        """ Testing the base data state of the GUI """
99        # data variables
100        self.assertIsNone(self.widget._data)
101        self.assertEqual(len(self.widget._dataList), 0)
102        self.assertEqual(self.widget.nTermsSuggested, 10)
103        # inputs
104        self.assertEqual(len(self.widget.dataList), 0)
105        self.assertEqual(self.widget.backgroundInput.text(), "0.0")
106        self.assertEqual(self.widget.minQInput.text(), "")
107        self.assertEqual(self.widget.maxQInput.text(), self.widget.minQInput.text())
108        self.assertEqual(self.widget.regularizationConstantInput.text(), "0.0001")
109        self.assertEqual(self.widget.noOfTermsInput.text(), "10")
110        self.assertEqual(self.widget.maxDistanceInput.text(), "140.0")
111
112    def oneDataSetState(self):
113        """ Testing the base data state of the GUI """
114        # Test the globals after first sent
115        self.assertEqual(len(self.widget._dataList), 1)
116        self.assertEqual(self.widget.dataList.count(), 1)
117        # See that the buttons are now enabled properly
118        self.assertFalse(self.widget.calculateAllButton.isEnabled())
119        self.assertTrue(self.widget.calculateThisButton.isEnabled())
120        self.assertTrue(self.widget.removeButton.isEnabled())
121        self.assertTrue(self.widget.explorerButton.isEnabled())
122
123    def twoDataSetState(self):
124        """ Testing the base data state of the GUI """
125        # Test the globals after first sent
126        self.assertEqual(len(self.widget._dataList), 2)
127        self.assertEqual(self.widget.dataList.count(), 2)
128        # See that the buttons are now enabled properly
129        self.assertTrue(self.widget.calculateAllButton.isEnabled())
130        self.assertTrue(self.widget.calculateThisButton.isEnabled())
131        self.assertTrue(self.widget.removeButton.isEnabled())
132        self.assertTrue(self.widget.explorerButton.isEnabled())
133
134    def testDefaults(self):
135        """ Test the GUI in its default state """
136        self.baseGUIState()
137        self.zeroDataSetState()
138        self.baseBatchState()
139
140    def testAllowBatch(self):
141        """ Batch P(r) Tests """
142        self.baseBatchState()
143
144    def testSetData(self):
145        """ Check if sending data works as expected """
146        self.zeroDataSetState()
147        self.widget.setData([self.fakeData1])
148        self.oneDataSetState()
149        self.widget.setData([self.fakeData1])
150        self.oneDataSetState()
151        self.widget.setData([self.fakeData2])
152        self.twoDataSetState()
153        self.removeAllData()
154        self.zeroDataSetState()
155
156    def testRemoveData(self):
157        """ Test data removal from widget """
158        self.widget.setData([self.fakeData1, self.fakeData2])
159        self.twoDataSetState()
160        # Remove data 0
161        self.widget.removeData()
162        self.oneDataSetState()
163
164    def testClose(self):
165        """ Test methods related to closing the window """
166        self.assertFalse(self.widget.isClosable())
167        self.widget.close()
168        self.assertTrue(self.widget.isVisible())
169        self.widget.setClosable(False)
170        self.assertFalse(self.widget.isClosable())
171        self.widget.close()
172        self.assertTrue(self.widget.isVisible())
173        self.widget.setClosable(True)
174        self.assertTrue(self.widget.isClosable())
175        self.widget.setClosable()
176        self.assertTrue(self.widget.isClosable())
177        self.widget.close()
178        self.assertFalse(self.widget.isVisible())
179
180    def testGetNFunc(self):
181        """ test nfunc getter """
182        # Float
183        self.widget.noOfTermsInput.setText("10.0")
184        self.assertEqual(self.widget.getNFunc(), 10)
185        # Int
186        self.widget.noOfTermsInput.setText("980")
187        self.assertEqual(self.widget.getNFunc(), 980)
188        # Empty
189        with self.assertLogs(level='ERROR') as cm:
190            self.widget.noOfTermsInput.setText("")
191            n = self.widget.getNFunc()
192            self.assertEqual(cm.output, ['ERROR:root:Incorrect number of terms specified: '])
193        self.assertEqual(self.widget.getNFunc(), 10)
194        # string
195        with self.assertLogs(level='ERROR') as cm:
196            self.widget.noOfTermsInput.setText("Nordvest Pizza")
197            n = self.widget.getNFunc()
198            self.assertEqual(cm.output, ['ERROR:root:Incorrect number of terms specified: Nordvest Pizza'])
199        self.assertEqual(self.widget.getNFunc(), 10)
200
201    def testSetCurrentData(self):
202        """ test current data setter """
203        self.widget.setData([self.fakeData1, self.fakeData2])
204
205        # Check that the current data is reference2
206        self.assertEqual(self.widget._data, self.fakeData2)
207        # Set the ref to none
208        self.widget.setCurrentData(None)
209        self.assertEqual(self.widget._data, self.fakeData2)
210        # Set the ref to wrong type
211        with self.assertRaises(AttributeError):
212            self.widget.setCurrentData("Afandi Kebab")
213        # Set the reference to ref1
214        self.widget.setCurrentData(self.fakeData1)
215        self.assertEqual(self.widget._data, self.fakeData1)
216
217    def notestModelChanged(self):
218        """ test the model update """
219        self.assertTrue(self.widget._calculator.get_dmax(), 140.0)
220        self.widget.maxDistanceInput.setText("750.0")
221        self.assertTrue(self.widget._calculator.get_dmax(), 750.0)
222        self.assertTrue(self.widget._calculator.get_qmax(), 0.0)
223        self.widget.maxQInput.setText("100.0")
224        self.assertTrue(self.widget._calculator.get_dmax(), 100.0)
225
226    def notestHelp(self):
227        """ test help widget show """
228        # TODO: test help button(s)
229        pass
230
231    def notestOpenExplorerWindow(self):
232        """ open Dx window """
233        # TODO: test explorer functionality
234        self.widget.openExplorerWindow()
235        self.assertTrue(self.widget.dmaxWindow.isVisible())
236
237
238if __name__ == "__main__":
239    unittest.main()
Note: See TracBrowser for help on using the repository browser.