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

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 e90988c was 50bfab0, checked in by Piotr Rozyczko <rozyczko@…>, 7 years ago

Initial unit tests for inversion - SASVIEW-609

  • Property mode set to 100755
File size: 6.9 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.sascalc.dataloader.loader import Loader
13from sas.qtgui.Plotting.PlotterData import Data1D
14
15from sas.qtgui.MainWindow.DataManager import DataManager
16import sas.qtgui.Utilities.LocalConfig
17import sas.qtgui.Utilities.GuiUtils as GuiUtils
18
19#if not QtWidgets.QApplication.instance():
20app = QtWidgets.QApplication(sys.argv)
21
22class InversionTest(unittest.TestCase):
23    '''Test the Inversion Interface'''
24    def setUp(self):
25        '''Create the InversionWindow'''
26        self.widget = InversionWindow(None)
27
28    def tearDown(self):
29        '''Destroy the InversionWindow'''
30        self.widget.close()
31        self.widget = None
32
33    def testDefaults(self):
34        '''Test the GUI in its default state'''
35        self.assertIsInstance(self.widget, QtWidgets.QWidget)
36        self.assertEqual(self.widget.windowTitle(), "P(r) Inversion Perspective")
37        self.assertEqual(self.widget.model.columnCount(), 1)
38        self.assertEqual(self.widget.model.rowCount(), 22)
39        self.assertFalse(self.widget.calculateAllButton.isEnabled())
40        self.assertFalse(self.widget.calculateThisButton.isEnabled())
41        self.assertIsInstance(self.widget.mapper, QtWidgets.QDataWidgetMapper)
42        # make sure the model is assigned and at least the data is mapped
43        self.assertEqual(self.widget.mapper.model(), self.widget.model)
44        self.assertNotEqual(self.widget.mapper.mappedSection(self.widget.dataList), -1)
45
46        # validators
47        self.assertIsInstance(self.widget.noOfTermsInput.validator(), QtGui.QIntValidator)
48        self.assertIsInstance(self.widget.regularizationConstantInput.validator(), QtGui.QDoubleValidator)
49        self.assertIsInstance(self.widget.maxDistanceInput.validator(), QtGui.QDoubleValidator)
50        self.assertIsInstance(self.widget.minQInput.validator(), QtGui.QDoubleValidator)
51        self.assertIsInstance(self.widget.maxQInput.validator(), QtGui.QDoubleValidator)
52        self.assertIsInstance(self.widget.slitHeightInput.validator(), QtGui.QDoubleValidator)
53        self.assertIsInstance(self.widget.slitWidthInput.validator(), QtGui.QDoubleValidator)
54
55        # model
56        self.assertEqual(self.widget.model.rowCount(), 22)
57
58    def testSetData(self):
59        ''' Check if sending data works as expected'''
60        # Create dummy data
61        item1 = GuiUtils.HashableStandardItem("A")
62        item2 = GuiUtils.HashableStandardItem("B")
63        reference_data = Data1D(x=[0.1, 0.2], y=[0.0, 0.0], dy=[0.0, 0.0])
64        GuiUtils.updateModelItem(item1, [reference_data])
65        GuiUtils.updateModelItem(item2, [reference_data])
66        self.widget.performEstimate = MagicMock()
67        self.widget.setData([item1, item2])
68
69        # Test the globals
70        self.assertEqual(len(self.widget._data_list), 2)
71        self.assertEqual(len(self.widget.data_plot_list), 2)
72        self.assertEqual(len(self.widget.pr_plot_list), 2)
73        self.assertEqual(self.widget.dataList.count(), 2)
74
75        # See that the buttons are now enabled
76        self.assertTrue(self.widget.calculateAllButton.isEnabled())
77        self.assertTrue(self.widget.calculateThisButton.isEnabled())
78        self.assertTrue(self.widget.removeButton.isEnabled())
79        self.assertTrue(self.widget.explorerButton.isEnabled())
80
81    def notestRemoveData(self):
82        ''' Test data removal from widget '''
83        # Create dummy data
84        item1 = GuiUtils.HashableStandardItem("A")
85        item2 = GuiUtils.HashableStandardItem("B")
86        reference_data1 = Data1D(x=[0.1, 0.2], y=[0.0, 0.0], dy=[0.0, 0.0])
87        reference_data2 = Data1D(x=[0.1, 0.2], y=[0.0, 0.0], dy=[0.0, 0.0])
88        GuiUtils.updateModelItem(item1, [reference_data1])
89        GuiUtils.updateModelItem(item2, [reference_data2])
90        self.widget.performEstimate = MagicMock()
91        self.widget.setData([item1, item2])
92
93        # Remove data 0
94        self.widget.removeData()
95
96        # Test the globals
97        self.assertEqual(len(self.widget._data_list), 1)
98        self.assertEqual(len(self.widget.data_plot_list), 1)
99        self.assertEqual(len(self.widget.pr_plot_list), 1)
100        self.assertEqual(self.widget.dataList.count(), 1)
101
102
103    def testAllowBatch(self):
104        ''' Batch is allowed for this perspective'''
105        self.assertTrue(self.widget.allowBatch())
106
107    def notestModelChanged(self):
108        ''' test the model update '''
109        # unfinished functionality
110        pass
111
112    def notestHelp(self):
113        ''' test help widget show '''
114        # unfinished functionality
115        pass
116
117    def testOpenExplorerWindow(self):
118        ''' open Dx window '''
119        self.widget.openExplorerWindow()
120        self.assertTrue(self.widget.dmaxWindow.isVisible())
121
122    def testGetNFunc(self):
123        ''' test nfunc getter '''
124        # Float
125        self.widget.noOfTermsInput.setText("10.0")
126        self.assertEqual(self.widget.getNFunc(), 10)
127        # Int
128        self.widget.noOfTermsInput.setText("980")
129        self.assertEqual(self.widget.getNFunc(), 980)
130        # Empty
131        with self.assertLogs(level='ERROR') as cm:
132            self.widget.noOfTermsInput.setText("")
133            n = self.widget.getNFunc()
134            self.assertEqual(cm.output, ['ERROR:root:Incorrect number of terms specified: '])
135        self.assertEqual(self.widget.getNFunc(), 10)
136        # string
137        with self.assertLogs(level='ERROR') as cm:
138            self.widget.noOfTermsInput.setText("Nordvest Pizza")
139            n = self.widget.getNFunc()
140            self.assertEqual(cm.output, ['ERROR:root:Incorrect number of terms specified: Nordvest Pizza'])
141        self.assertEqual(self.widget.getNFunc(), 10)
142
143    def testSetCurrentData(self):
144        ''' test current data setter '''
145        # Create dummy data
146        item1 = GuiUtils.HashableStandardItem("A")
147        item2 = GuiUtils.HashableStandardItem("B")
148        reference_data1 = Data1D(x=[0.1, 0.2], y=[0.0, 0.0], dy=[0.0, 0.0])
149        reference_data2 = Data1D(x=[0.1, 0.2], y=[0.0, 0.0], dy=[0.0, 0.0])
150        GuiUtils.updateModelItem(item1, [reference_data1])
151        GuiUtils.updateModelItem(item2, [reference_data2])
152        self.widget.performEstimate = MagicMock()
153        self.widget.setData([item1, item2])
154
155        # Check that the current data is reference2
156        self.assertEqual(self.widget._data, item2)
157
158        # Set the ref to none
159        self.widget.setCurrentData(None)
160        self.assertEqual(self.widget._data, item2)
161
162        # Set the ref to wrong type
163        with self.assertRaises(AttributeError):
164            self.widget.setCurrentData("Afandi Kebab")
165
166        # Set the reference to ref2
167        self.widget.setCurrentData(item1)
168        self.assertEqual(self.widget._data, item1)
169
170if __name__ == "__main__":
171    unittest.main()
Note: See TracBrowser for help on using the repository browser.