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

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 01ef3f7 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
RevLine 
[2162fa0]1import sys
2import unittest
[811bec1]3import webbrowser
[2162fa0]4
[53c771e]5from PyQt5 import QtGui
6from PyQt5 import QtWidgets
7from PyQt5 import QtTest
8from PyQt5 import QtCore
[7fb471d]9from unittest.mock import MagicMock
[2162fa0]10
[811bec1]11# set up import paths
12import sas.qtgui.path_prepare
13
[2162fa0]14# Local
[83eb5208]15import sas.qtgui.Utilities.GuiUtils as GuiUtils
[dc5ef15]16from sas.qtgui.Plotting.PlotterData import Data1D
[811bec1]17from sas.qtgui.Perspectives.Fitting.FittingPerspective import FittingWindow
[2162fa0]18
[53c771e]19if not QtWidgets.QApplication.instance():
20    app = QtWidgets.QApplication(sys.argv)
[2162fa0]21
[63319b0]22
[2162fa0]23class FittingPerspectiveTest(unittest.TestCase):
[811bec1]24    '''Test the Fitting Perspective'''
[2162fa0]25    def setUp(self):
[811bec1]26        class dummy_manager(object):
27            def communicator(self):
28                return GuiUtils.Communicate()
29            def communicate(self):
30                return GuiUtils.Communicate()
[2162fa0]31
[811bec1]32        '''Create the perspective'''
33        self.widget = FittingWindow(dummy_manager())
[2162fa0]34
35    def tearDown(self):
[811bec1]36        '''Destroy the perspective'''
[b9c4c1e1]37        self.widget.close()
[2162fa0]38        self.widget = None
39
40    def testDefaults(self):
[811bec1]41        '''Test the GUI in its default state'''
[53c771e]42        self.assertIsInstance(self.widget, QtWidgets.QWidget)
[811bec1]43        self.assertIn("Fit panel", self.widget.windowTitle())
[1bc27f1]44        self.assertEqual(self.widget.optimizer, "Levenberg-Marquardt")
[811bec1]45        self.assertEqual(len(self.widget.tabs), 1)
46        self.assertEqual(self.widget.maxIndex, 1)
[676f137]47        self.assertEqual(self.widget.maxCSIndex, 0)
48        self.assertEqual(self.widget.getTabName(), "FitPage1")
[811bec1]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)
[676f137]56        self.assertEqual(self.widget.getTabName(), "FitPage2")
[811bec1]57        self.assertEqual(self.widget.maxIndex, 2)
[0e4d87c]58        # Add an empty batch tab
59        self.widget.addFit(None, is_batch=True)
60        self.assertEqual(len(self.widget.tabs), 3)
[676f137]61        self.assertEqual(self.widget.getTabName(2), "BatchPage3")
[0e4d87c]62        self.assertEqual(self.widget.maxIndex, 3)
63
[676f137]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
[0e4d87c]71    def testResetTab(self):
72        ''' Remove data from last tab'''
73        self.assertEqual(len(self.widget.tabs), 1)
[676f137]74        self.assertEqual(self.widget.getTabName(), "FitPage1")
[0e4d87c]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)
[676f137]82        self.assertEqual(self.widget.getTabName(), "FitPage2")
[0e4d87c]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)
[811bec1]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)
[676f137]106        self.assertEqual(self.widget.getTabName(), "FitPage2")
[811bec1]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)
[377ade1]112        self.assertEqual(self.widget.maxIndex, 3)
[676f137]113        self.assertEqual(self.widget.getTabName(), "FitPage3")
[811bec1]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
[2155824]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
[63319b0]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")
[2162fa0]173
174if __name__ == "__main__":
175    unittest.main()
Note: See TracBrowser for help on using the repository browser.