[0595bb7] | 1 | import sys |
---|
| 2 | import unittest |
---|
| 3 | import numpy as np |
---|
[da9a0722] | 4 | import webbrowser |
---|
[0595bb7] | 5 | |
---|
| 6 | from unittest.mock import MagicMock |
---|
| 7 | |
---|
| 8 | from PyQt5 import QtGui, QtWidgets |
---|
| 9 | |
---|
| 10 | # set up import paths |
---|
| 11 | import path_prepare |
---|
| 12 | |
---|
| 13 | # Local |
---|
| 14 | from sas.qtgui.Perspectives.Fitting.MultiConstraint import MultiConstraint |
---|
| 15 | |
---|
| 16 | if not QtWidgets.QApplication.instance(): |
---|
| 17 | app = QtWidgets.QApplication(sys.argv) |
---|
| 18 | |
---|
| 19 | class 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 ")) |
---|
[da9a0722] | 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() |
---|