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

ESS_GUI
Last change on this file was 6edd344, checked in by Piotr Rozyczko <piotr.rozyczko@…>, 6 years ago

More unit test fixes.

  • Property mode set to 100644
File size: 5.1 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
15#from sas.qtgui.Calculators.SldPanel import SldResult
16from sas.qtgui.Calculators.SldPanel import SldPanel
17from sas.qtgui.Calculators.SldPanel import neutronSldAlgorithm, xraySldAlgorithm
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
25
26class SldAlgorithmTest(unittest.TestCase):
27    """ Test the periodictable wrapper """
28    def testSldAlgorithm1(self):
29        molecular_formula = "H2O"
30        mass_density = 1.0
31        wavelength = 6.0
32       
33        results = neutronSldAlgorithm( molecular_formula,
34                                mass_density,
35                                wavelength)
36        #self.assertIsInstance(results, SldResult)
37        self.assertAlmostEqual(results.neutron_length, 0.175463, 5)
38        self.assertAlmostEqual(results.neutron_inc_xs, 5.365857, 5)
39        self.assertAlmostEqual(results.neutron_abs_xs, 0.074224, 5)
40
41    def testSldAlgorithm2(self):
42        molecular_formula = "C29O[18]5+7NH[2]3"
43        mass_density = 3.0
44        wavelength = 666.0
45       
46        results = neutronSldAlgorithm( molecular_formula,
47                                mass_density,
48                                wavelength)
49        #self.assertIsInstance(results, SldResult)
50        self.assertAlmostEqual(results.neutron_length,   0.059402, 5)
51        self.assertAlmostEqual(results.neutron_inc_xs,   0.145427, 5)
52        self.assertAlmostEqual(results.neutron_abs_xs,  15.512215, 5)
53        self.assertAlmostEqual(results.neutron_sld_real, 1.3352833e-05, 5)
54        self.assertAlmostEqual(results.neutron_sld_imag, 1.1645807e-10, 5)
55
56
57class SLDCalculatorTest(unittest.TestCase):
58    '''Test the SLDCalculator'''
59    def setUp(self):
60        '''Create the SLDCalculator'''
61        self.widget = SldPanel(None)
62
63    def tearDown(self):
64        '''Destroy the DensityCalculator'''
65        self.widget.close()
66        self.widget = None
67
68    def testDefaults(self):
69        '''Test the GUI in its default state'''
70        self.assertIsInstance(self.widget, QtWidgets.QWidget)
71        # temporarily commented out until FormulaValidator fixed for Qt5
72        # self.assertEqual(self.widget.windowTitle(), "SLD Calculator")
73        # self.assertIsInstance(self.widget.ui.editMolecularFormula.validator(), FormulaValidator)
74        self.assertEqual(self.widget.ui.editMolecularFormula.styleSheet(), '')
75        self.assertEqual(self.widget.model.columnCount(), 1)
76        self.assertEqual(self.widget.model.rowCount(), 11)
77        self.assertEqual(self.widget.sizePolicy().Policy(), QtWidgets.QSizePolicy.Fixed)
78
79    def testSimpleEntry(self):
80        ''' Default compound calculations '''
81
82        self.widget.ui.editMassDensity.clear()
83        self.widget.ui.editMassDensity.insert("1.0")
84        # Send tab x3
85        key = QtCore.Qt.Key_Tab
86        QTest.keyEvent(QTest.Press, self.widget, key, QtCore.Qt.NoModifier)
87        QTest.keyEvent(QTest.Press, self.widget, key, QtCore.Qt.NoModifier)
88        QtWidgets.qApp.processEvents()
89        QTest.qWait(100)
90
91        # Assure the output fields are set
92        self.assertEqual(self.widget.ui.editNeutronIncXs.text(), '5.62')
93
94        # Change mass density
95        self.widget.ui.editNeutronWavelength.clear()
96        self.widget.ui.editNeutronWavelength.setText("666.0")
97
98        # Send shift-tab to update the molar volume field
99        QTest.keyEvent(QTest.Press, self.widget, key, QtCore.Qt.NoModifier)
100        QTest.keyEvent(QTest.Press, self.widget, key, QtCore.Qt.NoModifier)
101        QtWidgets.qApp.processEvents()
102        QTest.qWait(100)
103
104        # Assure the molar volume field got updated
105        self.assertEqual(self.widget.ui.editNeutronAbsXs.text(), '8.24')
106
107    def testComplexEntryAndReset(self):
108        ''' User entered compound calculations and subsequent reset'''
109
110        self.widget.ui.editMolecularFormula.clear()
111        self.widget.ui.editMolecularFormula.insert("CaCO[18]3+6H2O")
112        self.widget.ui.editMassDensity.insert("5.0")
113
114        self.widget.show()
115        # Send tab x2
116        key = QtCore.Qt.Key_Tab
117        QTest.keyEvent(QTest.Press, self.widget, key, QtCore.Qt.NoModifier)
118        QTest.keyEvent(QTest.Press, self.widget, key, QtCore.Qt.NoModifier)
119        QTest.qWait(100)
120        QTest.keyEvent(QTest.Press, self.widget, key, QtCore.Qt.NoModifier)
121
122        # Assure the mass density field is set
123        self.assertEqual(self.widget.ui.editNeutronIncXs.text(), '2.89')
124
125    def testHelp(self):
126        """ Assure help file is shown """
127        self.widget.manager = QtWidgets.QWidget()
128        self.widget.manager.showHelp = MagicMock()
129        self.widget.displayHelp()
130        self.assertTrue(self.widget.manager.showHelp.called_once())
131        args = self.widget.manager.showHelp.call_args
132        self.assertIn('sld_calculator_help.html', args[0][0])
133
134if __name__ == "__main__":
135    unittest.main()
Note: See TracBrowser for help on using the repository browser.