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

ESS_GUIESS_GUI_opencl
Last change on this file since c02721c4 was c02721c4, checked in by Piotr Rozyczko <piotr.rozyczko@…>, 7 months ago

Unit test update after latest round of commits.

  • Property mode set to 100644
File size: 5.3 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(), 11)
84        self.assertEqual(self.widget.sizePolicy().Policy(), QtWidgets.QSizePolicy.Fixed)
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)
95        QtWidgets.qApp.processEvents()
96        QTest.qWait(100)
97
98        # Assure the output fields are set
99        self.assertEqual(self.widget.ui.editNeutronIncXs.text(), '5.62')
100
101        # Change mass density
102        self.widget.ui.editNeutronWavelength.clear()
103        self.widget.ui.editNeutronWavelength.setText("666.0")
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)
108        QtWidgets.qApp.processEvents()
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)
127        QTest.keyEvent(QTest.Press, self.widget, key, QtCore.Qt.NoModifier)
128
129        # Assure the mass density field is set
130        self.assertEqual(self.widget.ui.editNeutronIncXs.text(), '2.89')
131
132    def testHelp(self):
133        """ Assure help file is shown """
134        self.widget.manager = QtWidgets.QWidget()
135        self.widget.manager.showHelp = MagicMock()
136        self.widget.displayHelp()
137        self.assertTrue(self.widget.manager.showHelp.called_once())
138        args = self.widget.manager.showHelp.call_args
139        self.assertIn('sld_calculator_help.html', args[0][0])
140
141if __name__ == "__main__":
142    unittest.main()
Note: See TracBrowser for help on using the repository browser.