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

Last change on this file since c57ecca 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
RevLine 
[2162fa0]1import sys
2import unittest
[811bec1]3import webbrowser
[2162fa0]4
5from PyQt4 import QtGui
6from PyQt4 import QtTest
7from PyQt4 import QtCore
8from mock import MagicMock
9
[811bec1]10# set up import paths
11import sas.qtgui.path_prepare
12
[2162fa0]13# Local
[83eb5208]14import sas.qtgui.Utilities.GuiUtils as GuiUtils
[dc5ef15]15from sas.qtgui.Plotting.PlotterData import Data1D
[811bec1]16from sas.qtgui.Perspectives.Fitting.FittingPerspective import FittingWindow
[2162fa0]17
[464cd07]18if not QtGui.QApplication.instance():
19    app = QtGui.QApplication(sys.argv)
[2162fa0]20
21class FittingPerspectiveTest(unittest.TestCase):
[811bec1]22    '''Test the Fitting Perspective'''
[2162fa0]23    def setUp(self):
[811bec1]24        class dummy_manager(object):
25            def communicator(self):
26                return GuiUtils.Communicate()
27            def communicate(self):
28                return GuiUtils.Communicate()
[2162fa0]29
[811bec1]30        '''Create the perspective'''
31        self.widget = FittingWindow(dummy_manager())
[2162fa0]32
33    def tearDown(self):
[811bec1]34        '''Destroy the perspective'''
[b9c4c1e1]35        self.widget.close()
[2162fa0]36        self.widget = None
37
38    def testDefaults(self):
[811bec1]39        '''Test the GUI in its default state'''
[b9c4c1e1]40        self.assertIsInstance(self.widget, QtGui.QWidget)
[811bec1]41        self.assertIn("Fit panel", self.widget.windowTitle())
[1bc27f1]42        self.assertEqual(self.widget.optimizer, "Levenberg-Marquardt")
[811bec1]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)
[0e4d87c]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)
[811bec1]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)
[377ade1]102        self.assertEqual(self.widget.maxIndex, 3)
103        self.assertEqual(self.widget.tabName(), "FitPage3")
[811bec1]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
[2155824]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
[2162fa0]139
140if __name__ == "__main__":
141    unittest.main()
Note: See TracBrowser for help on using the repository browser.