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

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

Initial implementation of the mask editor.
Refactored plotter base to QDialog and reimplemented as local widgets.
Fixed some unit tests.

  • Property mode set to 100644
File size: 4.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 path_prepare
12#######
13
14# Local
15from DensityPanel import DensityPanel
16from DensityPanel import toMolarMass
17from sas.qtgui.GuiUtils import FormulaValidator
18
19import sas.qtgui.LocalConfig
20
21app = QtGui.QApplication(sys.argv)
22
23class ToMolarMassTest(unittest.TestCase):
24    """ Test the auxiliary conversion method"""
25    def testGoodEasy(self):
26        self.assertEqual(toMolarMass("H2"), "2.01588")
27
28    def testGoodComplex(self):
29        self.assertEqual(toMolarMass("H24O12C4C6N2Pu"), "608.304")
30
31    def testGoodComplex2(self):
32        self.assertEqual(toMolarMass("(H2O)0.5(D2O)0.5"), "19.0214")
33
34    def testBadInputInt(self):
35        self.assertEqual(toMolarMass(1), "")
36
37    def testBadInputStr(self):
38        self.assertEqual(toMolarMass("Im a bad string"), "")
39
40class DensityCalculatorTest(unittest.TestCase):
41    '''Test the DensityCalculator'''
42    def setUp(self):
43        '''Create the DensityCalculator'''
44        self.widget = DensityPanel(None)
45
46    def tearDown(self):
47        '''Destroy the DensityCalculator'''
48        self.widget.close()
49        self.widget = None
50
51    def testDefaults(self):
52        '''Test the GUI in its default state'''
53        self.assertIsInstance(self.widget, QtGui.QWidget)
54        self.assertEqual(self.widget.windowTitle(), "Density/Volume Calculator")
55        self.assertIsInstance(self.widget.ui.editMolecularFormula.validator(), FormulaValidator)
56        self.assertEqual(self.widget.ui.editMolecularFormula.styleSheet(), '')
57        self.assertEqual(self.widget.model.columnCount(), 1)
58        self.assertEqual(self.widget.model.rowCount(), 4)
59        self.assertEqual(self.widget.sizePolicy().Policy(), QtGui.QSizePolicy.Fixed)
60
61    def testSimpleEntry(self):
62        ''' Default compound calculations '''
63        self.widget.ui.editMolarVolume.insert("1.0")
64
65        self.widget.show()
66        # Send tab x3
67        key = QtCore.Qt.Key_Tab
68        QTest.keyEvent(QTest.Press, self.widget, key, QtCore.Qt.NoModifier)
69        QTest.keyEvent(QTest.Press, self.widget, key, QtCore.Qt.NoModifier)
70        QTest.keyEvent(QTest.Press, self.widget, key, QtCore.Qt.NoModifier)
71        QTest.qWait(100)
72
73        # Assure the mass density field is set
74        self.assertEqual(self.widget.ui.editMassDensity.text(), '18.0153')
75
76        # Change mass density
77        self.widget.ui.editMassDensity.insert("16.0")
78        # Send shift-tab to update the molar volume field
79        key =  QtCore.Qt.Key_Tab
80        QTest.keyEvent(QTest.Press, self.widget, key, QtCore.Qt.ShiftModifier)
81        QTest.qWait(100)
82
83        # Assure the molar volume field got updated
84        self.assertEqual(self.widget.ui.editMolarVolume.text(), '1.12595625')
85
86    def testComplexEntryAndReset(self):
87        ''' User entered compound calculations and subsequent reset'''
88
89        self.widget.ui.editMolecularFormula.clear()
90        self.widget.ui.editMolecularFormula.insert("KMnO4")
91        self.widget.ui.editMolarVolume.insert("2.0")
92
93        self.widget.show()
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        QTest.keyEvent(QTest.Press, self.widget, key, QtCore.Qt.NoModifier)
99        QTest.qWait(100)
100
101        # Assure the mass density field is set
102        self.assertEqual(self.widget.ui.editMassDensity.text(), '79.017')
103
104        # Reset the widget
105        self.widget.modelReset()
106
107        self.assertEqual(self.widget.ui.editMolecularFormula.text(), "H2O")
108        self.assertEqual(self.widget.ui.editMolarMass.text(), "18.0153")
109        self.assertEqual(self.widget.ui.editMolarVolume.text(), "")
110        self.assertEqual(self.widget.ui.editMassDensity.text(), "")
111
112        #self.widget.exec_()
113
114    def testHelp(self):
115        """ Assure help file is shown """
116
117        # this should not rise
118        self.widget.displayHelp()
119
120if __name__ == "__main__":
121    unittest.main()
Note: See TracBrowser for help on using the repository browser.