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

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalc
Last change on this file since d3c0b95 was e90988c, checked in by Piotr Rozyczko <rozyczko@…>, 7 years ago

Show help pages in default browser. Fixed some help links and modified unit tests. SASVIEW-800

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