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

Last change on this file since 6b43c58 was 464cd07, checked in by Piotr Rozyczko <rozyczko@…>, 7 years ago

Use singleton QApplication in unit tests to avoid issues on Ubuntu. SASVIEW-485

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