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

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 c3eb858 was 63319b0, checked in by Piotr Rozyczko <rozyczko@…>, 7 years ago

unit tests for constraints: FittingWidget?, FittingPerspective?

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