1 | import sys |
---|
2 | import unittest |
---|
3 | import webbrowser |
---|
4 | |
---|
5 | from PyQt4 import QtGui |
---|
6 | from PyQt4.QtTest import QTest |
---|
7 | from PyQt4 import QtCore |
---|
8 | from mock import MagicMock |
---|
9 | |
---|
10 | ####### TEMP |
---|
11 | import sas.qtgui.path_prepare |
---|
12 | ####### |
---|
13 | |
---|
14 | # Local |
---|
15 | from sas.qtgui.Calculators.DensityPanel import DensityPanel |
---|
16 | from sas.qtgui.Calculators.DensityPanel import toMolarMass |
---|
17 | from sas.qtgui.Utilities.GuiUtils import FormulaValidator |
---|
18 | |
---|
19 | import sas.qtgui.Utilities.LocalConfig |
---|
20 | |
---|
21 | app = QtGui.QApplication(sys.argv) |
---|
22 | |
---|
23 | class 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 | |
---|
40 | class 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 | |
---|
120 | if __name__ == "__main__": |
---|
121 | unittest.main() |
---|