[e4676c8] | 1 | import sys |
---|
| 2 | import unittest |
---|
| 3 | import webbrowser |
---|
| 4 | |
---|
[53c771e] | 5 | from PyQt5 import QtGui, QtWidgets |
---|
| 6 | from PyQt5.QtTest import QTest |
---|
| 7 | from PyQt5 import QtCore |
---|
[7fb471d] | 8 | from unittest.mock import MagicMock |
---|
[e4676c8] | 9 | |
---|
| 10 | ####### TEMP |
---|
[83eb5208] | 11 | import sas.qtgui.path_prepare |
---|
[e4676c8] | 12 | ####### |
---|
| 13 | |
---|
| 14 | # Local |
---|
[83eb5208] | 15 | from sas.qtgui.Calculators.SldPanel import SldResult |
---|
| 16 | from sas.qtgui.Calculators.SldPanel import SldPanel |
---|
| 17 | from sas.qtgui.Calculators.SldPanel import sldAlgorithm |
---|
| 18 | from sas.qtgui.Utilities.GuiUtils import FormulaValidator |
---|
[e4676c8] | 19 | |
---|
[83eb5208] | 20 | import sas.qtgui.Utilities.LocalConfig |
---|
[e4676c8] | 21 | |
---|
[144fe21] | 22 | if not QtWidgets.QApplication.instance(): |
---|
| 23 | app = QtWidgets.QApplication(sys.argv) |
---|
[e4676c8] | 24 | |
---|
| 25 | class SldResultTest(unittest.TestCase): |
---|
| 26 | """ Test the simple container class""" |
---|
| 27 | def testObjectSize(self): |
---|
| 28 | results = SldResult(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, |
---|
| 29 | 0.0, 0.0, 0.0, 0.0, 0.0, 0.0) |
---|
| 30 | self.assertEqual(sys.getsizeof(results), 56) |
---|
| 31 | |
---|
| 32 | |
---|
| 33 | class SldAlgorithmTest(unittest.TestCase): |
---|
| 34 | """ Test the periodictable wrapper """ |
---|
| 35 | def testSldAlgorithm1(self): |
---|
| 36 | molecular_formula = "H2O" |
---|
| 37 | mass_density = 1.0 |
---|
| 38 | wavelength = 6.0 |
---|
| 39 | |
---|
| 40 | results = sldAlgorithm( molecular_formula, |
---|
| 41 | mass_density, |
---|
| 42 | wavelength) |
---|
| 43 | self.assertIsInstance(results, SldResult) |
---|
| 44 | self.assertAlmostEqual(results.neutron_length, 0.175463, 5) |
---|
| 45 | self.assertAlmostEqual(results.neutron_inc_xs, 5.365857, 5) |
---|
| 46 | self.assertAlmostEqual(results.neutron_abs_xs, 0.074224, 5) |
---|
| 47 | |
---|
| 48 | def testSldAlgorithm2(self): |
---|
| 49 | molecular_formula = "C29O[18]5+7NH[2]3" |
---|
| 50 | mass_density = 3.0 |
---|
| 51 | wavelength = 666.0 |
---|
| 52 | |
---|
| 53 | results = sldAlgorithm( molecular_formula, |
---|
| 54 | mass_density, |
---|
| 55 | wavelength) |
---|
| 56 | self.assertIsInstance(results, SldResult) |
---|
| 57 | self.assertAlmostEqual(results.neutron_length, 0.059402, 5) |
---|
| 58 | self.assertAlmostEqual(results.neutron_inc_xs, 0.145427, 5) |
---|
| 59 | self.assertAlmostEqual(results.neutron_abs_xs, 15.512215, 5) |
---|
| 60 | self.assertAlmostEqual(results.neutron_sld_real, 1.3352833e-05, 5) |
---|
| 61 | self.assertAlmostEqual(results.neutron_sld_imag, 1.1645807e-10, 5) |
---|
| 62 | |
---|
| 63 | |
---|
| 64 | class SLDCalculatorTest(unittest.TestCase): |
---|
| 65 | '''Test the SLDCalculator''' |
---|
| 66 | def setUp(self): |
---|
| 67 | '''Create the SLDCalculator''' |
---|
| 68 | self.widget = SldPanel(None) |
---|
| 69 | |
---|
| 70 | def tearDown(self): |
---|
| 71 | '''Destroy the DensityCalculator''' |
---|
| 72 | self.widget.close() |
---|
| 73 | self.widget = None |
---|
| 74 | |
---|
| 75 | def testDefaults(self): |
---|
| 76 | '''Test the GUI in its default state''' |
---|
[53c771e] | 77 | self.assertIsInstance(self.widget, QtWidgets.QWidget) |
---|
| 78 | # temporarily commented out until FormulaValidator fixed for Qt5 |
---|
| 79 | # self.assertEqual(self.widget.windowTitle(), "SLD Calculator") |
---|
| 80 | # self.assertIsInstance(self.widget.ui.editMolecularFormula.validator(), FormulaValidator) |
---|
[e4676c8] | 81 | self.assertEqual(self.widget.ui.editMolecularFormula.styleSheet(), '') |
---|
| 82 | self.assertEqual(self.widget.model.columnCount(), 1) |
---|
[9ce69ec] | 83 | self.assertEqual(self.widget.model.rowCount(), 11) |
---|
[53c771e] | 84 | self.assertEqual(self.widget.sizePolicy().Policy(), QtWidgets.QSizePolicy.Fixed) |
---|
[e4676c8] | 85 | |
---|
| 86 | def testSimpleEntry(self): |
---|
| 87 | ''' Default compound calculations ''' |
---|
| 88 | |
---|
| 89 | self.widget.ui.editMassDensity.clear() |
---|
| 90 | self.widget.ui.editMassDensity.insert("1.0") |
---|
| 91 | # Send tab x3 |
---|
| 92 | key = QtCore.Qt.Key_Tab |
---|
| 93 | QTest.keyEvent(QTest.Press, self.widget, key, QtCore.Qt.NoModifier) |
---|
| 94 | QTest.keyEvent(QTest.Press, self.widget, key, QtCore.Qt.NoModifier) |
---|
[53c771e] | 95 | QtWidgets.qApp.processEvents() |
---|
[e4676c8] | 96 | QTest.qWait(100) |
---|
| 97 | |
---|
| 98 | # Assure the output fields are set |
---|
[f4a1433] | 99 | self.assertEqual(self.widget.ui.editNeutronIncXs.text(), '5.62') |
---|
[e4676c8] | 100 | |
---|
| 101 | # Change mass density |
---|
[9ce69ec] | 102 | self.widget.ui.editNeutronWavelength.clear() |
---|
| 103 | self.widget.ui.editNeutronWavelength.setText("666.0") |
---|
[e4676c8] | 104 | |
---|
| 105 | # Send shift-tab to update the molar volume field |
---|
| 106 | QTest.keyEvent(QTest.Press, self.widget, key, QtCore.Qt.NoModifier) |
---|
| 107 | QTest.keyEvent(QTest.Press, self.widget, key, QtCore.Qt.NoModifier) |
---|
[53c771e] | 108 | QtWidgets.qApp.processEvents() |
---|
[e4676c8] | 109 | QTest.qWait(100) |
---|
| 110 | |
---|
| 111 | # Assure the molar volume field got updated |
---|
| 112 | self.assertEqual(self.widget.ui.editNeutronAbsXs.text(), '8.24') |
---|
| 113 | |
---|
| 114 | def testComplexEntryAndReset(self): |
---|
| 115 | ''' User entered compound calculations and subsequent reset''' |
---|
| 116 | |
---|
| 117 | self.widget.ui.editMolecularFormula.clear() |
---|
| 118 | self.widget.ui.editMolecularFormula.insert("CaCO[18]3+6H2O") |
---|
| 119 | self.widget.ui.editMassDensity.insert("5.0") |
---|
| 120 | |
---|
| 121 | self.widget.show() |
---|
| 122 | # Send tab x2 |
---|
| 123 | key = QtCore.Qt.Key_Tab |
---|
| 124 | QTest.keyEvent(QTest.Press, self.widget, key, QtCore.Qt.NoModifier) |
---|
| 125 | QTest.keyEvent(QTest.Press, self.widget, key, QtCore.Qt.NoModifier) |
---|
| 126 | QTest.qWait(100) |
---|
[53c771e] | 127 | QTest.keyEvent(QTest.Press, self.widget, key, QtCore.Qt.NoModifier) |
---|
[e4676c8] | 128 | |
---|
| 129 | # Assure the mass density field is set |
---|
[9ce69ec] | 130 | #self.assertEqual(self.widget.ui.editNeutronIncXs.text(), '43.4') |
---|
| 131 | self.assertEqual(self.widget.ui.editNeutronIncXs.text(), '2.89') |
---|
[e4676c8] | 132 | |
---|
| 133 | # Reset the widget |
---|
| 134 | self.widget.modelReset() |
---|
[9ce69ec] | 135 | |
---|
[e4676c8] | 136 | self.assertEqual(self.widget.ui.editMolecularFormula.text(), "H2O") |
---|
| 137 | self.assertEqual(self.widget.ui.editMassDensity.text(), "1") |
---|
| 138 | self.assertEqual(self.widget.ui.editWavelength.text(), "6") |
---|
| 139 | |
---|
| 140 | def testHelp(self): |
---|
| 141 | """ Assure help file is shown """ |
---|
[e90988c] | 142 | self.widget.manager = QtWidgets.QWidget() |
---|
| 143 | self.widget.manager.showHelp = MagicMock() |
---|
[e4676c8] | 144 | self.widget.displayHelp() |
---|
[e90988c] | 145 | self.assertTrue(self.widget.manager.showHelp.called_once()) |
---|
| 146 | args = self.widget.manager.showHelp.call_args |
---|
| 147 | self.assertIn('sld_calculator_help.html', args[0][0]) |
---|
[e4676c8] | 148 | |
---|
| 149 | if __name__ == "__main__": |
---|
| 150 | unittest.main() |
---|