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 unittest.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 | if not QtGui.QApplication.instance(): |
---|
22 | app = QtGui.QApplication(sys.argv) |
---|
23 | |
---|
24 | class 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 | |
---|
41 | class DensityCalculatorTest(unittest.TestCase): |
---|
42 | '''Test the DensityCalculator''' |
---|
43 | def setUp(self): |
---|
44 | '''Create the DensityCalculator''' |
---|
45 | self.widget = DensityPanel(None) |
---|
46 | |
---|
47 | def tearDown(self): |
---|
48 | '''Destroy the DensityCalculator''' |
---|
49 | self.widget.close() |
---|
50 | self.widget = None |
---|
51 | |
---|
52 | def testDefaults(self): |
---|
53 | '''Test the GUI in its default state''' |
---|
54 | self.assertIsInstance(self.widget, QtGui.QWidget) |
---|
55 | self.assertEqual(self.widget.windowTitle(), "Density/Volume Calculator") |
---|
56 | self.assertIsInstance(self.widget.ui.editMolecularFormula.validator(), FormulaValidator) |
---|
57 | self.assertEqual(self.widget.ui.editMolecularFormula.styleSheet(), '') |
---|
58 | self.assertEqual(self.widget.model.columnCount(), 1) |
---|
59 | self.assertEqual(self.widget.model.rowCount(), 4) |
---|
60 | self.assertEqual(self.widget.sizePolicy().Policy(), QtGui.QSizePolicy.Fixed) |
---|
61 | |
---|
62 | def testSimpleEntry(self): |
---|
63 | ''' Default compound calculations ''' |
---|
64 | self.widget.ui.editMolarVolume.insert("1.0") |
---|
65 | |
---|
66 | self.widget.show() |
---|
67 | # Send tab x3 |
---|
68 | key = QtCore.Qt.Key_Tab |
---|
69 | QTest.keyEvent(QTest.Press, self.widget, key, QtCore.Qt.NoModifier) |
---|
70 | QTest.keyEvent(QTest.Press, self.widget, key, QtCore.Qt.NoModifier) |
---|
71 | QTest.keyEvent(QTest.Press, self.widget, key, QtCore.Qt.NoModifier) |
---|
72 | QTest.qWait(100) |
---|
73 | |
---|
74 | # Assure the mass density field is set |
---|
75 | self.assertEqual(self.widget.ui.editMassDensity.text(), '18.0153') |
---|
76 | |
---|
77 | # Change mass density |
---|
78 | self.widget.ui.editMassDensity.insert("16.0") |
---|
79 | # Send shift-tab to update the molar volume field |
---|
80 | key = QtCore.Qt.Key_Tab |
---|
81 | QTest.keyEvent(QTest.Press, self.widget, key, QtCore.Qt.ShiftModifier) |
---|
82 | QTest.qWait(100) |
---|
83 | |
---|
84 | # Assure the molar volume field got updated |
---|
85 | self.assertEqual(self.widget.ui.editMolarVolume.text(), '1.12595625') |
---|
86 | |
---|
87 | def testComplexEntryAndReset(self): |
---|
88 | ''' User entered compound calculations and subsequent reset''' |
---|
89 | |
---|
90 | self.widget.ui.editMolecularFormula.clear() |
---|
91 | self.widget.ui.editMolecularFormula.insert("KMnO4") |
---|
92 | self.widget.ui.editMolarVolume.insert("2.0") |
---|
93 | |
---|
94 | self.widget.show() |
---|
95 | # Send tab x3 |
---|
96 | key = QtCore.Qt.Key_Tab |
---|
97 | QTest.keyEvent(QTest.Press, self.widget, key, QtCore.Qt.NoModifier) |
---|
98 | QTest.keyEvent(QTest.Press, self.widget, key, QtCore.Qt.NoModifier) |
---|
99 | QTest.keyEvent(QTest.Press, self.widget, key, QtCore.Qt.NoModifier) |
---|
100 | QTest.qWait(100) |
---|
101 | |
---|
102 | # Assure the mass density field is set |
---|
103 | self.assertEqual(self.widget.ui.editMassDensity.text(), '79.017') |
---|
104 | |
---|
105 | # Reset the widget |
---|
106 | self.widget.modelReset() |
---|
107 | |
---|
108 | self.assertEqual(self.widget.ui.editMolecularFormula.text(), "H2O") |
---|
109 | self.assertEqual(self.widget.ui.editMolarMass.text(), "18.0153") |
---|
110 | self.assertEqual(self.widget.ui.editMolarVolume.text(), "") |
---|
111 | self.assertEqual(self.widget.ui.editMassDensity.text(), "") |
---|
112 | |
---|
113 | #self.widget.exec_() |
---|
114 | |
---|
115 | def testHelp(self): |
---|
116 | """ Assure help file is shown """ |
---|
117 | |
---|
118 | # this should not rise |
---|
119 | self.widget.displayHelp() |
---|
120 | |
---|
121 | if __name__ == "__main__": |
---|
122 | unittest.main() |
---|