source: sasview/src/sas/qtgui/Perspectives/Fitting/UnitTesting/FittingPerspectiveTest.py @ 35e36fd

ESS_GUIESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalc
Last change on this file since 35e36fd was 144fe21, checked in by Piotr Rozyczko <rozyczko@…>, 6 years ago

new unit test runner script + test fixes. SASVIEW-970

  • Property mode set to 100644
File size: 5.7 KB
Line 
1import sys
2import unittest
3import webbrowser
4
5from PyQt5 import QtGui
6from PyQt5 import QtWidgets
7from PyQt5 import QtTest
8from PyQt5 import QtCore
9from unittest.mock import MagicMock
10
11# set up import paths
12import sas.qtgui.path_prepare
13
14# Local
15import sas.qtgui.Utilities.GuiUtils as GuiUtils
16from sas.qtgui.Plotting.PlotterData import Data1D
17from sas.qtgui.Perspectives.Fitting.FittingPerspective import FittingWindow
18
19if not QtWidgets.QApplication.instance():
20    app = QtWidgets.QApplication(sys.argv)
21
22
23class FittingPerspectiveTest(unittest.TestCase):
24    '''Test the Fitting Perspective'''
25    def setUp(self):
26        class dummy_manager(object):
27            def communicator(self):
28                return GuiUtils.Communicate()
29            communicate = GuiUtils.Communicate()
30
31        '''Create the perspective'''
32        self.widget = FittingWindow(dummy_manager())
33
34    def tearDown(self):
35        '''Destroy the perspective'''
36        self.widget.close()
37        self.widget = None
38
39    def testDefaults(self):
40        '''Test the GUI in its default state'''
41        self.assertIsInstance(self.widget, QtWidgets.QWidget)
42        self.assertIn("Fit panel", self.widget.windowTitle())
43        self.assertEqual(self.widget.optimizer, "Levenberg-Marquardt")
44        self.assertEqual(len(self.widget.tabs), 1)
45        self.assertEqual(self.widget.maxIndex, 2)
46        self.assertEqual(self.widget.getTabName(), "FitPage2")
47
48    def testAddTab(self):
49        '''Add a tab and test it'''
50
51        # Add an empty tab
52        self.widget.addFit(None)
53        self.assertEqual(len(self.widget.tabs), 2)
54        self.assertEqual(self.widget.getTabName(), "FitPage3")
55        self.assertEqual(self.widget.maxIndex, 3)
56        # Add an empty batch tab
57        self.widget.addFit(None, is_batch=True)
58        self.assertEqual(len(self.widget.tabs), 3)
59        self.assertEqual(self.widget.getTabName(2), "BatchPage4")
60        self.assertEqual(self.widget.maxIndex, 4)
61
62    def testAddCSTab(self):
63        ''' Add a constraint/simult tab'''
64        self.widget.addConstraintTab()
65        self.assertEqual(len(self.widget.tabs), 2)
66        self.assertEqual(self.widget.getCSTabName(), "Const. & Simul. Fit")
67
68    def testResetTab(self):
69        ''' Remove data from last tab'''
70        self.assertEqual(len(self.widget.tabs), 1)
71        self.assertEqual(self.widget.getTabName(), "FitPage2")
72        self.assertEqual(self.widget.maxIndex, 2)
73
74        # Attempt to remove the last tab
75        self.widget.resetTab(0)
76
77        # see that the tab didn't disappear, just changed the name/id
78        self.assertEqual(len(self.widget.tabs), 1)
79        self.assertEqual(self.widget.getTabName(), "FitPage3")
80        self.assertEqual(self.widget.maxIndex, 3)
81
82        # Now, add data
83        data = Data1D(x=[1,2], y=[1,2])
84        GuiUtils.dataFromItem = MagicMock(return_value=data)
85        item = QtGui.QStandardItem("test")
86        self.widget.setData([item])
87        # Assert data is on widget
88        self.assertEqual(len(self.widget.tabs[0].all_data), 1)
89        # Reset the tab
90        self.widget.resetTab(0)
91        # See that the tab contains data no more
92        self.assertEqual(len(self.widget.tabs[0].all_data), 0)
93
94    def testCloseTab(self):
95        '''Delete a tab and test'''
96        # Add an empty tab
97        self.widget.addFit(None)
98
99        # Remove the original tab
100        self.widget.tabCloses(1)
101        self.assertEqual(len(self.widget.tabs), 1)
102        self.assertEqual(self.widget.maxIndex, 3)
103        self.assertEqual(self.widget.getTabName(), "FitPage3")
104
105        # Attemtp to remove the last tab
106        self.widget.tabCloses(1)
107        # The tab should still be there
108        self.assertEqual(len(self.widget.tabs), 1)
109        self.assertEqual(self.widget.maxIndex, 4)
110        self.assertEqual(self.widget.getTabName(), "FitPage4")
111
112    def testAllowBatch(self):
113        '''Assure the perspective allows multiple datasets'''
114        self.assertTrue(self.widget.allowBatch())
115
116    def testSetData(self):
117        ''' Assure that setting data is correct'''
118        with self.assertRaises(AssertionError):
119            self.widget.setData(None)
120
121        with self.assertRaises(AttributeError):
122            self.widget.setData("BOOP")
123
124        # Mock the datafromitem() call from FittingWidget
125        data = Data1D(x=[1,2], y=[1,2])
126        GuiUtils.dataFromItem = MagicMock(return_value=data)
127
128        item = QtGui.QStandardItem("test")
129        self.widget.setData([item])
130
131        # First tab should accept data
132        self.assertEqual(len(self.widget.tabs), 1)
133
134        # Add another set of data
135        self.widget.setData([item])
136
137        # Now we should have two tabs
138        self.assertEqual(len(self.widget.tabs), 2)
139
140        # Add two more items in a list
141        self.widget.setData([item, item])
142
143        # Check for 4 tabs
144        self.assertEqual(len(self.widget.tabs), 4)
145
146    def testSetBatchData(self):
147        ''' Assure that setting batch data is correct'''
148
149        # Mock the datafromitem() call from FittingWidget
150        data1 = Data1D(x=[1,2], y=[1,2])
151        data2 = Data1D(x=[1,2], y=[1,2])
152        data_batch = [data1, data2]
153        GuiUtils.dataFromItem = MagicMock(return_value=data1)
154
155        item = QtGui.QStandardItem("test")
156        self.widget.setData([item, item], is_batch=True)
157
158        # First tab should not accept data
159        self.assertEqual(len(self.widget.tabs), 2)
160
161        # Add another set of data
162        self.widget.setData([item, item], is_batch=True)
163
164        # Now we should have two batch tabs
165        self.assertEqual(len(self.widget.tabs), 3)
166
167        # Check the names of the new tabs
168        self.assertEqual(self.widget.tabText(1), "BatchPage2")
169        self.assertEqual(self.widget.tabText(2), "BatchPage3")
170
171if __name__ == "__main__":
172    unittest.main()
Note: See TracBrowser for help on using the repository browser.