source: sasview/src/sas/qtgui/Perspectives/Fitting/UnitTesting/ComplexConstraintTest.py @ 460bdf4

ESS_GUIESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_opencl
Last change on this file since 460bdf4 was 2e5081b, checked in by Piotr Rozyczko <piotr.rozyczko@…>, 6 years ago

Redone the widget slightly, to fit with requirements in trac#1135.
Added comboboxes for all available fitpage names
Removed constraint of needing two selected fitpages
Removed the "Swap" button - it is redundant now.
Added context dependence on the "Select All" action.
Removed immediate constraint validation to allow for more complex
formulas

  • Property mode set to 100755
File size: 5.5 KB
Line 
1import sys
2import unittest
3import numpy as np
4import webbrowser
5
6from unittest.mock import MagicMock
7
8from PyQt5 import QtGui, QtWidgets
9
10# set up import paths
11import path_prepare
12
13from sas.qtgui.Utilities.GuiUtils import Communicate
14
15# Local
16from sas.qtgui.Perspectives.Fitting.FittingWidget import FittingWidget
17from sas.qtgui.Perspectives.Fitting.ComplexConstraint import ComplexConstraint
18
19if not QtWidgets.QApplication.instance():
20    app = QtWidgets.QApplication(sys.argv)
21
22class dummy_manager(object):
23    HELP_DIRECTORY_LOCATION = "html"
24    communicate = Communicate()
25
26class ComplexConstraintTest(unittest.TestCase):
27    '''Test the ComplexConstraint dialog'''
28    def setUp(self):
29        '''Create ComplexConstraint dialog'''
30        # mockup tabs
31        self.tab1 = FittingWidget(dummy_manager())
32        self.tab2 = FittingWidget(dummy_manager())
33        # set some models on tabs
34        category_index = self.tab1.cbCategory.findText("Shape Independent")
35        self.tab1.cbCategory.setCurrentIndex(category_index)
36        model_index = self.tab1.cbModel.findText("be_polyelectrolyte")
37        self.tab1.cbModel.setCurrentIndex(model_index)
38
39        category_index = self.tab2.cbCategory.findText("Cylinder")
40        self.tab2.cbCategory.setCurrentIndex(category_index)
41        model_index = self.tab2.cbModel.findText("barbell")
42        self.tab2.cbModel.setCurrentIndex(model_index)
43
44        tabs = [self.tab1, self.tab2]
45        self.widget = ComplexConstraint(parent=None, tabs=tabs)
46
47    def tearDown(self):
48        '''Destroy the GUI'''
49        self.widget.close()
50        self.widget = None
51
52    def testDefaults(self):
53        '''Test the GUI in its default state'''
54        self.assertIsInstance(self.widget, QtWidgets.QDialog)
55        # Default title
56        self.assertEqual(self.widget.windowTitle(), "Complex Constraint")
57
58        # Modal window
59        self.assertTrue(self.widget.isModal())
60
61        # initial tab names
62        self.assertEqual(self.widget.tab_names, ['M1','M1'])
63        self.assertIn('scale', self.widget.params[0])
64        self.assertIn('background', self.widget.params[1])
65
66    def testLabels(self):
67        ''' various labels on the widget '''
68        # params related setup
69        self.assertEqual(self.widget.txtConstraint.text(), 'M1.scale')
70        self.assertEqual(self.widget.txtOperator.text(), '=')
71        self.assertEqual(self.widget.cbModel1.currentText(), 'M1')
72        self.assertEqual(self.widget.cbModel2.currentText(), 'M1')
73
74    def testTooltip(self):
75        ''' test the tooltip'''
76        p1 = self.widget.tab_names[0] + ":" + self.widget.cbParam1.currentText()
77        p2 = self.widget.tab_names[1]+"."+self.widget.cbParam2.currentText()
78
79        tooltip = "E.g.\n%s = 2.0 * (%s)\n" %(p1, p2)
80        tooltip += "%s = sqrt(%s) + 5"%(p1, p2)
81        self.assertEqual(self.widget.txtConstraint.toolTip(), tooltip)
82
83    def notestValidateFormula(self):
84        ''' assure enablement and color for valid formula '''
85        # Invalid string
86        self.widget.validateConstraint = MagicMock(return_value=False)
87        self.widget.validateFormula()
88        style_sheet = "QLineEdit {background-color: red;}"
89        self.assertFalse(self.widget.cmdOK.isEnabled())
90        self.assertEqual(self.widget.txtConstraint.styleSheet(),style_sheet)
91
92        # Valid string
93        self.widget.validateConstraint = MagicMock(return_value=True)
94        self.widget.validateFormula()
95        style_sheet = "QLineEdit {background-color: white;}"
96        self.assertTrue(self.widget.cmdOK.isEnabled())
97        self.assertEqual(self.widget.txtConstraint.styleSheet(),style_sheet)
98
99    def testValidateConstraint(self):
100        ''' constraint validator test'''
101        #### BAD
102        # none
103        self.assertFalse(self.widget.validateConstraint(None))
104        # inf
105        self.assertFalse(self.widget.validateConstraint(np.inf))
106        # 0
107        self.assertFalse(self.widget.validateConstraint(0))
108        # ""
109        self.assertFalse(self.widget.validateConstraint(""))
110        # p2_
111        self.assertFalse(self.widget.validateConstraint("M2.scale_"))
112        # p1
113        self.assertFalse(self.widget.validateConstraint("M2.scale"))
114
115        ### GOOD
116        # p2
117        self.assertTrue(self.widget.validateConstraint("scale"))
118        # " p2    "
119        self.assertTrue(self.widget.validateConstraint(" scale    "))
120        # sqrt(p2)
121        self.assertTrue(self.widget.validateConstraint("sqrt(scale)"))
122        # -p2
123        self.assertTrue(self.widget.validateConstraint("-scale"))
124        # log10(p2) - sqrt(p2) + p2
125        self.assertTrue(self.widget.validateConstraint("log10(scale) - sqrt(scale) + scale"))
126        # log10(    p2    ) +  p2
127        self.assertTrue(self.widget.validateConstraint("log10(    scale    ) +  scale  "))
128
129    def testConstraint(self):
130        """
131        Test the return of specified constraint
132        """
133        # default data
134        c = self.widget.constraint()
135        self.assertEqual(c[0], 'M1')
136        self.assertEqual(c[1].func, 'M1.scale')
137
138        # Change parameter and operand
139        #self.widget.cbOperator.setCurrentIndex(3)
140        self.widget.cbParam2.setCurrentIndex(3)
141        c = self.widget.constraint()
142        self.assertEqual(c[0], 'M1')
143        self.assertEqual(c[1].func, 'M1.bjerrum_length')
144        #self.assertEqual(c[1].operator, '>=')
145
146    def testOnHelp(self):
147        """
148        Test the default help renderer
149        """
150        webbrowser.open = MagicMock()
151
152        # invoke the tested method
153        self.widget.onHelp()
154
155        # see that webbrowser open was attempted
156        webbrowser.open.assert_called_once()
Note: See TracBrowser for help on using the repository browser.