source: sasview/src/sas/qtgui/Perspectives/Fitting/UnitTesting/ComplexConstraintTest.py @ 5b144c6

ESS_GUIESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalc
Last change on this file since 5b144c6 was 725d9c06, checked in by Piotr Rozyczko <rozyczko@…>, 7 years ago

More code review changes

  • Property mode set to 100755
File size: 5.2 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        category_index = self.tab2.cbCategory.findText("Cylinder")
37        self.tab2.cbCategory.setCurrentIndex(category_index)
38
39        tabs = [self.tab1, self.tab2]
40        self.widget = ComplexConstraint(parent=None, tabs=tabs)
41
42    def tearDown(self):
43        '''Destroy the GUI'''
44        self.widget.close()
45        self.widget = None
46
47    def testDefaults(self):
48        '''Test the GUI in its default state'''
49        self.assertIsInstance(self.widget, QtWidgets.QDialog)
50        # Default title
51        self.assertEqual(self.widget.windowTitle(), "Complex Constraint")
52
53        # Modal window
54        self.assertTrue(self.widget.isModal())
55
56        # initial tab names
57        self.assertEqual(self.widget.tab_names, ['M1','M1'])
58        self.assertIn('scale', self.widget.params[0])
59        self.assertIn('background', self.widget.params[1])
60
61    def testLabels(self):
62        ''' various labels on the widget '''
63        # params related setup
64        self.assertEqual(self.widget.txtConstraint.text(), 'M1.scale')
65        self.assertEqual(self.widget.txtOperator.text(), '=')
66        self.assertEqual(self.widget.txtName1.text(), 'M1')
67        self.assertEqual(self.widget.txtName2.text(), 'M1')
68
69    def testTooltip(self):
70        ''' test the tooltip'''
71        p1 = self.widget.tab_names[0] + ":" + self.widget.cbParam1.currentText()
72        p2 = self.widget.tab_names[1]+"."+self.widget.cbParam2.currentText()
73
74        tooltip = "E.g.\n%s = 2.0 * (%s)\n" %(p1, p2)
75        tooltip += "%s = sqrt(%s) + 5"%(p1, p2)
76        self.assertEqual(self.widget.txtConstraint.toolTip(), tooltip)
77
78    def testValidateFormula(self):
79        ''' assure enablement and color for valid formula '''
80        # Invalid string
81        self.widget.validateConstraint = MagicMock(return_value=False)
82        self.widget.validateFormula()
83        style_sheet = "QLineEdit {background-color: red;}"
84        self.assertFalse(self.widget.cmdOK.isEnabled())
85        self.assertEqual(self.widget.txtConstraint.styleSheet(),style_sheet)
86
87        # Valid string
88        self.widget.validateConstraint = MagicMock(return_value=True)
89        self.widget.validateFormula()
90        style_sheet = "QLineEdit {background-color: white;}"
91        self.assertTrue(self.widget.cmdOK.isEnabled())
92        self.assertEqual(self.widget.txtConstraint.styleSheet(),style_sheet)
93
94    def testValidateConstraint(self):
95        ''' constraint validator test'''
96        #### BAD
97        # none
98        self.assertFalse(self.widget.validateConstraint(None))
99        # inf
100        self.assertFalse(self.widget.validateConstraint(np.inf))
101        # 0
102        self.assertFalse(self.widget.validateConstraint(0))
103        # ""
104        self.assertFalse(self.widget.validateConstraint(""))
105        # p2_
106        self.assertFalse(self.widget.validateConstraint("M2.scale_"))
107        # p1
108        self.assertFalse(self.widget.validateConstraint("M2.scale"))
109
110        ### GOOD
111        # p2
112        self.assertTrue(self.widget.validateConstraint("scale"))
113        # " p2    "
114        self.assertTrue(self.widget.validateConstraint(" scale    "))
115        # sqrt(p2)
116        self.assertTrue(self.widget.validateConstraint("sqrt(scale)"))
117        # -p2
118        self.assertTrue(self.widget.validateConstraint("-scale"))
119        # log10(p2) - sqrt(p2) + p2
120        self.assertTrue(self.widget.validateConstraint("log10(scale) - sqrt(scale) + scale"))
121        # log10(    p2    ) +  p2
122        self.assertTrue(self.widget.validateConstraint("log10(    scale    ) +  scale  "))
123
124    def testConstraint(self):
125        """
126        Test the return of specified constraint
127        """
128        # default data
129        self.assertEqual(self.widget.constraint(), ('M1', 'scale', '=', 'M1.scale'))
130
131        # Change parameter and operand
132        self.widget.cbOperator.setCurrentIndex(3)
133        self.widget.cbParam1.setCurrentIndex(3)
134        self.assertEqual(self.widget.constraint(), ('M1', 'bjerrum_length', '>=', 'M1.scale'))
135
136    def testOnHelp(self):
137        """
138        Test the default help renderer
139        """
140        webbrowser.open = MagicMock()
141
142        # invoke the tested method
143        self.widget.onHelp()
144
145        # see that webbrowser open was attempted
146        webbrowser.open.assert_called_once()
Note: See TracBrowser for help on using the repository browser.