[da9a0722] | 1 | import sys |
---|
| 2 | import unittest |
---|
| 3 | import numpy as np |
---|
| 4 | import webbrowser |
---|
| 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 | from sas.qtgui.Utilities.GuiUtils import Communicate |
---|
| 14 | |
---|
| 15 | # Local |
---|
| 16 | from sas.qtgui.Perspectives.Fitting.FittingWidget import FittingWidget |
---|
| 17 | from sas.qtgui.Perspectives.Fitting.ComplexConstraint import ComplexConstraint |
---|
| 18 | |
---|
[a90c9c5] | 19 | if not QtWidgets.QApplication.instance(): |
---|
| 20 | app = QtWidgets.QApplication(sys.argv) |
---|
[da9a0722] | 21 | |
---|
| 22 | class dummy_manager(object): |
---|
| 23 | HELP_DIRECTORY_LOCATION = "html" |
---|
| 24 | communicate = Communicate() |
---|
| 25 | |
---|
| 26 | class 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) |
---|
[2eeda93] | 36 | model_index = self.tab1.cbModel.findText("be_polyelectrolyte") |
---|
| 37 | self.tab1.cbModel.setCurrentIndex(model_index) |
---|
| 38 | |
---|
[da9a0722] | 39 | category_index = self.tab2.cbCategory.findText("Cylinder") |
---|
| 40 | self.tab2.cbCategory.setCurrentIndex(category_index) |
---|
[2eeda93] | 41 | model_index = self.tab2.cbModel.findText("barbell") |
---|
| 42 | self.tab2.cbModel.setCurrentIndex(model_index) |
---|
[da9a0722] | 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(), '=') |
---|
[2e5081b] | 71 | self.assertEqual(self.widget.cbModel1.currentText(), 'M1') |
---|
| 72 | self.assertEqual(self.widget.cbModel2.currentText(), 'M1') |
---|
[da9a0722] | 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 | |
---|
[2e5081b] | 83 | def notestValidateFormula(self): |
---|
[da9a0722] | 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 |
---|
[ecc5d043] | 134 | c = self.widget.constraint() |
---|
| 135 | self.assertEqual(c[0], 'M1') |
---|
| 136 | self.assertEqual(c[1].func, 'M1.scale') |
---|
[da9a0722] | 137 | |
---|
| 138 | # Change parameter and operand |
---|
[d72ac57] | 139 | #self.widget.cbOperator.setCurrentIndex(3) |
---|
[ecc5d043] | 140 | self.widget.cbParam2.setCurrentIndex(3) |
---|
| 141 | c = self.widget.constraint() |
---|
| 142 | self.assertEqual(c[0], 'M1') |
---|
[2e5081b] | 143 | self.assertEqual(c[1].func, 'M1.bjerrum_length') |
---|
[d72ac57] | 144 | #self.assertEqual(c[1].operator, '>=') |
---|
[da9a0722] | 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() |
---|