source: sasview/src/sas/qtgui/UnitTesting/DensityCalculatorTest.py @ 9968d6a

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 9968d6a was 9968d6a, checked in by Piotr Rozyczko <rozyczko@…>, 8 years ago

Added unit tests for the density calculator SAVIEW-247

  • Property mode set to 100755
File size: 5.0 KB
Line 
1import sys
2import unittest
3import webbrowser
4
5from PyQt4 import QtGui
6from PyQt4.QtTest import QTest
7from PyQt4 import QtCore
8from mock import MagicMock
9
10####### TEMP
11import LocalSetup
12#######
13
14# Local
15from DensityPanel import DensityPanel
16from DensityPanel import toMolarMass
17from DensityPanel import FormulaValidator
18from UnitTesting.TestUtils import mousePress
19
20import LocalConfig
21
22app = QtGui.QApplication(sys.argv)
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 FormulaValidatorTest(unittest.TestCase):
42    """ Test the formula validator """
43    def setUp(self):
44        '''Create the validator'''
45        self.validator = FormulaValidator()
46
47    def tearDown(self):
48        '''Destroy the validator'''
49        self.validator = None
50
51    def testValidateGood(self):
52        """Test a valid Formula """
53        formula_good = "H24O12C4C6N2Pu"
54        self.assertEqual(self.validator.validate(formula_good, 1)[0], QtGui.QValidator.Acceptable)
55
56        formula_good = "(H2O)0.5(D2O)0.5"
57        self.assertEqual(self.validator.validate(formula_good, 1)[0], QtGui.QValidator.Acceptable)
58
59    def testValidateBad(self):
60        """Test a valid Formula """
61        formula_bad = "H24 %%%O12C4C6N2Pu"
62        self.assertRaises(self.validator.validate(formula_bad, 1)[0])
63        self.assertEqual(self.validator.validate(formula_bad, 1)[0], QtGui.QValidator.Intermediate)
64
65        formula_bad = [1]
66        self.assertEqual(self.validator.validate(formula_bad, 1)[0], QtGui.QValidator.Intermediate)
67
68class DensityCalculatorTest(unittest.TestCase):
69    '''Test the DensityCalculator'''
70    def setUp(self):
71        '''Create the DensityCalculator'''
72        self.widget = DensityPanel(None)
73
74    def tearDown(self):
75        '''Destroy the DensityCalculator'''
76        self.widget.close()
77        self.widget = None
78
79    def testDefaults(self):
80        '''Test the GUI in its default state'''
81        self.assertIsInstance(self.widget, QtGui.QWidget)
82        self.assertEqual(self.widget.windowTitle(), "Density/Volume Calculator")
83        self.assertIsInstance(self.widget.ui.editMolecularFormula.validator(), FormulaValidator)
84        self.assertEqual(self.widget.ui.editMolecularFormula.styleSheet(), '')
85        self.assertEqual(self.widget.model.columnCount(), 1)
86        self.assertEqual(self.widget.model.rowCount(), 4)
87
88    def testSimpleEntry(self):
89        ''' Default compound calculations '''
90        self.widget.ui.editMolarVolume.insert("1.0")
91
92        self.widget.show()
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        QTest.keyEvent(QTest.Press, self.widget, key, QtCore.Qt.NoModifier)
98        QTest.qWait(100)
99
100        # Assure the mass density field is set
101        self.assertEqual(self.widget.ui.editMassDensity.text(), '18.0153')
102
103        # Change mass density
104        self.widget.ui.editMassDensity.insert("16.0")
105        # Send shift-tab to update the molar volume field
106        key =  QtCore.Qt.Key_Tab
107        QTest.keyEvent(QTest.Press, self.widget, key, QtCore.Qt.ShiftModifier)
108        QTest.qWait(100)
109
110        # Assure the molar volume field got updated
111        self.assertEqual(self.widget.ui.editMolarVolume.text(), '1.12595625')
112
113    def testComplexEntryAndReset(self):
114        ''' User entered compound calculations and subsequent reset'''
115
116        self.widget.ui.editMolecularFormula.clear()
117        self.widget.ui.editMolecularFormula.insert("KMnO4")
118        self.widget.ui.editMolarVolume.insert("2.0")
119
120        self.widget.show()
121        # Send tab x3
122        key = QtCore.Qt.Key_Tab
123        QTest.keyEvent(QTest.Press, self.widget, key, QtCore.Qt.NoModifier)
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
128        # Assure the mass density field is set
129        self.assertEqual(self.widget.ui.editMassDensity.text(), '79.017')
130
131        # Reset the widget
132        self.widget.modelReset()
133
134        self.assertEqual(self.widget.ui.editMolecularFormula.text(), "H2O")
135        self.assertEqual(self.widget.ui.editMolarMass.text(), "18.0153")
136        self.assertEqual(self.widget.ui.editMolarVolume.text(), "")
137        self.assertEqual(self.widget.ui.editMassDensity.text(), "")
138
139        #self.widget.exec_()
140
141    def testHelp(self):
142        """ Assure help file is shown """
143
144        # this should not rise
145        self.widget.displayHelp()
146
147if __name__ == "__main__":
148    unittest.main()
Note: See TracBrowser for help on using the repository browser.