source: sasview/src/sas/qtgui/Calculators/UnitTesting/SLDCalculatorTest.py @ 47471ec

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 47471ec was 144fe21, checked in by Piotr Rozyczko <rozyczko@…>, 6 years ago

new unit test runner script + test fixes. SASVIEW-970

  • Property mode set to 100644
File size: 5.6 KB
Line 
1import sys
2import unittest
3import webbrowser
4
5from PyQt5 import QtGui, QtWidgets
6from PyQt5.QtTest import QTest
7from PyQt5 import QtCore
8from unittest.mock import MagicMock
9
10####### TEMP
11import sas.qtgui.path_prepare
12#######
13
14# Local
15from sas.qtgui.Calculators.SldPanel import SldResult
16from sas.qtgui.Calculators.SldPanel import SldPanel
17from sas.qtgui.Calculators.SldPanel import sldAlgorithm
18from sas.qtgui.Utilities.GuiUtils import FormulaValidator
19
20import sas.qtgui.Utilities.LocalConfig
21
22if not QtWidgets.QApplication.instance():
23    app = QtWidgets.QApplication(sys.argv)
24
25class 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
33class 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
64class 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'''
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)
81        self.assertEqual(self.widget.ui.editMolecularFormula.styleSheet(), '')
82        self.assertEqual(self.widget.model.columnCount(), 1)
83        self.assertEqual(self.widget.model.rowCount(), 12)
84        self.assertEqual(self.widget.sizePolicy().Policy(), QtWidgets.QSizePolicy.Fixed)
85
86    def testSimpleEntry(self):
87        ''' Default compound calculations '''
88
89        self.widget.show()
90
91        self.widget.ui.editMassDensity.clear()
92        self.widget.ui.editMassDensity.insert("1.0")
93        # Send tab x3
94        key = QtCore.Qt.Key_Tab
95        QTest.keyEvent(QTest.Press, self.widget, key, QtCore.Qt.NoModifier)
96        QTest.keyEvent(QTest.Press, self.widget, key, QtCore.Qt.NoModifier)
97        QtWidgets.qApp.processEvents()
98        QTest.qWait(100)
99
100        # Assure the output fields are set
101        self.assertEqual(self.widget.ui.editNeutronIncXs.text(), '5.62')
102
103        # Change mass density
104        self.widget.ui.editWavelength.clear()
105        self.widget.ui.editWavelength.setText("666.0")
106
107        # Send shift-tab to update the molar volume field
108        QTest.keyEvent(QTest.Press, self.widget, key, QtCore.Qt.NoModifier)
109        QTest.keyEvent(QTest.Press, self.widget, key, QtCore.Qt.NoModifier)
110        QtWidgets.qApp.processEvents()
111        QTest.qWait(100)
112
113        # Assure the molar volume field got updated
114        self.assertEqual(self.widget.ui.editNeutronAbsXs.text(), '8.24')
115
116    def testComplexEntryAndReset(self):
117        ''' User entered compound calculations and subsequent reset'''
118
119        self.widget.ui.editMolecularFormula.clear()
120        self.widget.ui.editMolecularFormula.insert("CaCO[18]3+6H2O")
121        self.widget.ui.editMassDensity.insert("5.0")
122
123        self.widget.show()
124        # Send tab x2
125        key = QtCore.Qt.Key_Tab
126        QTest.keyEvent(QTest.Press, self.widget, key, QtCore.Qt.NoModifier)
127        QTest.keyEvent(QTest.Press, self.widget, key, QtCore.Qt.NoModifier)
128        QTest.qWait(100)
129        QTest.keyEvent(QTest.Press, self.widget, key, QtCore.Qt.NoModifier)
130
131        # Assure the mass density field is set
132        self.assertEqual(self.widget.ui.editNeutronIncXs.text(), '43.4')
133
134        # Reset the widget
135        self.widget.modelReset()
136
137        self.assertEqual(self.widget.ui.editMolecularFormula.text(), "H2O")
138        self.assertEqual(self.widget.ui.editMassDensity.text(), "1")
139        self.assertEqual(self.widget.ui.editWavelength.text(), "6")
140
141    def testHelp(self):
142        """ Assure help file is shown """
143        self.widget.manager = QtWidgets.QWidget()
144        self.widget.manager.showHelp = MagicMock()
145        self.widget.displayHelp()
146        self.assertTrue(self.widget.manager.showHelp.called_once())
147        args = self.widget.manager.showHelp.call_args
148        self.assertIn('sld_calculator_help.html', args[0][0])
149
150if __name__ == "__main__":
151    unittest.main()
Note: See TracBrowser for help on using the repository browser.