source: sasview/src/sas/qtgui/Calculators/UnitTesting/DensityCalculatorTest.py @ 60a4e71

Last change on this file since 60a4e71 was bc3debf8, checked in by Piotr Rozyczko <rozyczko@…>, 7 years ago

MOdified unit test after changing the printout detail level

  • Property mode set to 100644
File size: 4.8 KB
RevLine 
[9968d6a]1import sys
2import unittest
3import webbrowser
4
[53c771e]5from PyQt5 import QtGui, QtWidgets
6from PyQt5.QtTest import QTest
7from PyQt5 import QtCore
[7fb471d]8from unittest.mock import MagicMock
[9968d6a]9
10####### TEMP
[83eb5208]11import sas.qtgui.path_prepare
[9968d6a]12#######
13
14# Local
[83eb5208]15from sas.qtgui.Calculators.DensityPanel import DensityPanel
16from sas.qtgui.Calculators.DensityPanel import toMolarMass
17from sas.qtgui.Utilities.GuiUtils import FormulaValidator
[9968d6a]18
[83eb5208]19import sas.qtgui.Utilities.LocalConfig
[9968d6a]20
[53c771e]21if not QtWidgets.QApplication.instance():
22    app = QtWidgets.QApplication(sys.argv)
[9968d6a]23
24class ToMolarMassTest(unittest.TestCase):
25    """ Test the auxiliary conversion method"""
26    def testGoodEasy(self):
27        self.assertEqual(toMolarMass("H2"), "2.01588")
28
29    def testGoodComplex(self):
30        self.assertEqual(toMolarMass("H24O12C4C6N2Pu"), "608.304")
31
32    def testGoodComplex2(self):
33        self.assertEqual(toMolarMass("(H2O)0.5(D2O)0.5"), "19.0214")
34
35    def testBadInputInt(self):
36        self.assertEqual(toMolarMass(1), "")
37
38    def testBadInputStr(self):
39        self.assertEqual(toMolarMass("Im a bad string"), "")
40
41class DensityCalculatorTest(unittest.TestCase):
42    '''Test the DensityCalculator'''
43    def setUp(self):
44        '''Create the DensityCalculator'''
45        self.widget = DensityPanel(None)
46
[53c771e]47        # temporarily set the text here
48        self.widget.ui.editMolecularFormula.setText("H2O")
49
[9968d6a]50    def tearDown(self):
51        '''Destroy the DensityCalculator'''
52        self.widget.close()
53        self.widget = None
54
55    def testDefaults(self):
56        '''Test the GUI in its default state'''
[53c771e]57        self.assertIsInstance(self.widget, QtWidgets.QWidget)
[9968d6a]58        self.assertEqual(self.widget.windowTitle(), "Density/Volume Calculator")
[53c771e]59        # temporarily commented out until FormulaValidator fixed for Qt5
60        #self.assertIsInstance(self.widget.ui.editMolecularFormula.validator(), FormulaValidator)
[9968d6a]61        self.assertEqual(self.widget.ui.editMolecularFormula.styleSheet(), '')
62        self.assertEqual(self.widget.model.columnCount(), 1)
63        self.assertEqual(self.widget.model.rowCount(), 4)
[53c771e]64        self.assertEqual(self.widget.sizePolicy().Policy(), QtWidgets.QSizePolicy.Fixed)
[9968d6a]65
66    def testSimpleEntry(self):
67        ''' Default compound calculations '''
68        self.widget.ui.editMolarVolume.insert("1.0")
69
70        self.widget.show()
71        # Send tab x3
72        key = QtCore.Qt.Key_Tab
73        QTest.keyEvent(QTest.Press, self.widget, key, QtCore.Qt.NoModifier)
74        QTest.keyEvent(QTest.Press, self.widget, key, QtCore.Qt.NoModifier)
75        QTest.keyEvent(QTest.Press, self.widget, key, QtCore.Qt.NoModifier)
76        QTest.qWait(100)
[53c771e]77        QTest.keyEvent(QTest.Press, self.widget, key, QtCore.Qt.NoModifier)
[9968d6a]78
79        # Assure the mass density field is set
[bc3debf8]80        self.assertEqual(self.widget.ui.editMassDensity.text(), '18.015')
[9968d6a]81
82        # Change mass density
83        self.widget.ui.editMassDensity.insert("16.0")
84        # Send shift-tab to update the molar volume field
85        key =  QtCore.Qt.Key_Tab
86        QTest.keyEvent(QTest.Press, self.widget, key, QtCore.Qt.ShiftModifier)
87        QTest.qWait(100)
88
89        # Assure the molar volume field got updated
[bc3debf8]90        self.assertEqual(self.widget.ui.editMolarVolume.text(), '1.126')
[9968d6a]91
92    def testComplexEntryAndReset(self):
93        ''' User entered compound calculations and subsequent reset'''
94
95        self.widget.ui.editMolecularFormula.clear()
96        self.widget.ui.editMolecularFormula.insert("KMnO4")
97        self.widget.ui.editMolarVolume.insert("2.0")
98
99        self.widget.show()
100        # Send tab x3
101        key = QtCore.Qt.Key_Tab
102        QTest.keyEvent(QTest.Press, self.widget, key, QtCore.Qt.NoModifier)
103        QTest.keyEvent(QTest.Press, self.widget, key, QtCore.Qt.NoModifier)
104        QTest.keyEvent(QTest.Press, self.widget, key, QtCore.Qt.NoModifier)
105        QTest.qWait(100)
[53c771e]106        QTest.keyEvent(QTest.Press, self.widget, key, QtCore.Qt.NoModifier)
[9968d6a]107
108        # Assure the mass density field is set
109        self.assertEqual(self.widget.ui.editMassDensity.text(), '79.017')
110
111        # Reset the widget
112        self.widget.modelReset()
113
114        self.assertEqual(self.widget.ui.editMolecularFormula.text(), "H2O")
[bc3debf8]115        self.assertEqual(self.widget.ui.editMolarMass.text(), "18.015")
[9968d6a]116        self.assertEqual(self.widget.ui.editMolarVolume.text(), "")
117        self.assertEqual(self.widget.ui.editMassDensity.text(), "")
118
119        #self.widget.exec_()
120
121    def testHelp(self):
122        """ Assure help file is shown """
[e90988c]123        self.widget.manager = QtWidgets.QWidget()
124        self.widget.manager.showHelp = MagicMock()
[9968d6a]125        self.widget.displayHelp()
[e90988c]126        self.assertTrue(self.widget.manager.showHelp.called_once())
127        args = self.widget.manager.showHelp.call_args
128        self.assertIn('density_calculator_help.html', args[0][0])
[9968d6a]129
130if __name__ == "__main__":
131    unittest.main()
Note: See TracBrowser for help on using the repository browser.