source: sasview/src/sas/qtgui/Perspectives/Fitting/UnitTesting/MultiConstraintTest.py @ c02721c4

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

More code review changes

  • Property mode set to 100644
File size: 3.7 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
13# Local
14from sas.qtgui.Perspectives.Fitting.MultiConstraint import MultiConstraint
15
16if not QtWidgets.QApplication.instance():
17    app = QtWidgets.QApplication(sys.argv)
18
19class MultiConstraintTest(unittest.TestCase):
20    '''Test the MultiConstraint dialog'''
21    def setUp(self):
22        '''Create MultiConstraint dialog'''
23        params = ['p1', 'p2']
24        self.p1 = params[0]
25        self.p2 = params[1]
26        self.widget = MultiConstraint(parent=None, params=params)
27
28    def tearDown(self):
29        '''Destroy the GUI'''
30        self.widget.close()
31        self.widget = None
32
33    def testDefaults(self):
34        '''Test the GUI in its default state'''
35        self.assertIsInstance(self.widget, QtWidgets.QDialog)
36        # Default title
37        self.assertEqual(self.widget.windowTitle(), "2 parameter constraint")
38
39        # modal window
40        self.assertTrue(self.widget.isModal())
41
42    def testLabels(self):
43        ''' various labels on the widget '''
44        # params related setup
45        self.assertEqual(self.widget.txtParam1.text(), self.p1)
46        self.assertEqual(self.widget.txtParam1_2.text(), self.p1)
47        self.assertEqual(self.widget.txtParam2.text(), self.p2)
48
49    def testTooltip(self):
50        ''' test the tooltip'''
51        tooltip = "E.g.\n%s = 2.0 * (%s)\n" %(self.p1, self.p2)
52        tooltip += "%s = sqrt(%s) + 5"%(self.p1, self.p2)
53        self.assertEqual(self.widget.txtConstraint.toolTip(), tooltip)
54
55    def testValidateFormula(self):
56        ''' assure enablement and color for valid formula '''
57        # Invalid string
58        self.widget.validateConstraint = MagicMock(return_value=False)
59        self.widget.validateFormula()
60        style_sheet = "QLineEdit {background-color: red;}"
61        self.assertFalse(self.widget.cmdOK.isEnabled())
62        self.assertEqual(self.widget.txtConstraint.styleSheet(),style_sheet)
63
64        # Valid string
65        self.widget.validateConstraint = MagicMock(return_value=True)
66        self.widget.validateFormula()
67        style_sheet = "QLineEdit {background-color: white;}"
68        self.assertTrue(self.widget.cmdOK.isEnabled())
69        self.assertEqual(self.widget.txtConstraint.styleSheet(),style_sheet)
70
71    def testValidateConstraint(self):
72        ''' constraint validator test'''
73        #### BAD
74        # none
75        self.assertFalse(self.widget.validateConstraint(None))
76        # inf
77        self.assertFalse(self.widget.validateConstraint(np.inf))
78        # 0
79        self.assertFalse(self.widget.validateConstraint(0))
80        # ""
81        self.assertFalse(self.widget.validateConstraint(""))
82        # p2_
83        self.assertFalse(self.widget.validateConstraint("p2_"))
84        # p1
85        self.assertFalse(self.widget.validateConstraint("p1"))
86
87        ### GOOD
88        # p2
89        self.assertTrue(self.widget.validateConstraint("p2"))
90        # " p2    "
91        self.assertTrue(self.widget.validateConstraint(" p2    "))
92        # sqrt(p2)
93        self.assertTrue(self.widget.validateConstraint("sqrt(p2)"))
94        # -p2
95        self.assertTrue(self.widget.validateConstraint("-p2"))
96        # log10(p2) - sqrt(p2) + p2
97        self.assertTrue(self.widget.validateConstraint("log10(p2) - sqrt(p2) + p2"))
98        # log10(    p2    ) +  p2
99        self.assertTrue(self.widget.validateConstraint("log10(    p2    ) +  p2  "))
100
101    def testOnHelp(self):
102        """
103        Test the default help renderer
104        """
105        webbrowser.open = MagicMock()
106
107        # invoke the tested method
108        self.widget.onHelp()
109
110        # see that webbrowser open was attempted
111        webbrowser.open.assert_called_once()
Note: See TracBrowser for help on using the repository browser.