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 path_prepare |
---|
12 | ####### |
---|
13 | |
---|
14 | # Local |
---|
15 | from SldPanel import SldResult |
---|
16 | from SldPanel import SldPanel |
---|
17 | from SldPanel import sldAlgorithm |
---|
18 | from GuiUtils import FormulaValidator |
---|
19 | |
---|
20 | import LocalConfig |
---|
21 | |
---|
22 | app = QtGui.QApplication(sys.argv) |
---|
23 | |
---|
24 | class SldResultTest(unittest.TestCase): |
---|
25 | """ Test the simple container class""" |
---|
26 | def testObjectSize(self): |
---|
27 | results = SldResult(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, |
---|
28 | 0.0, 0.0, 0.0, 0.0, 0.0, 0.0) |
---|
29 | self.assertEqual(sys.getsizeof(results), 56) |
---|
30 | |
---|
31 | |
---|
32 | class SldAlgorithmTest(unittest.TestCase): |
---|
33 | """ Test the periodictable wrapper """ |
---|
34 | def testSldAlgorithm1(self): |
---|
35 | molecular_formula = "H2O" |
---|
36 | mass_density = 1.0 |
---|
37 | wavelength = 6.0 |
---|
38 | |
---|
39 | results = sldAlgorithm( molecular_formula, |
---|
40 | mass_density, |
---|
41 | wavelength) |
---|
42 | self.assertIsInstance(results, SldResult) |
---|
43 | self.assertAlmostEqual(results.neutron_length, 0.175463, 5) |
---|
44 | self.assertAlmostEqual(results.neutron_inc_xs, 5.365857, 5) |
---|
45 | self.assertAlmostEqual(results.neutron_abs_xs, 0.074224, 5) |
---|
46 | |
---|
47 | def testSldAlgorithm2(self): |
---|
48 | molecular_formula = "C29O[18]5+7NH[2]3" |
---|
49 | mass_density = 3.0 |
---|
50 | wavelength = 666.0 |
---|
51 | |
---|
52 | results = sldAlgorithm( molecular_formula, |
---|
53 | mass_density, |
---|
54 | wavelength) |
---|
55 | self.assertIsInstance(results, SldResult) |
---|
56 | self.assertAlmostEqual(results.neutron_length, 0.059402, 5) |
---|
57 | self.assertAlmostEqual(results.neutron_inc_xs, 0.145427, 5) |
---|
58 | self.assertAlmostEqual(results.neutron_abs_xs, 15.512215, 5) |
---|
59 | self.assertAlmostEqual(results.neutron_sld_real, 1.3352833e-05, 5) |
---|
60 | self.assertAlmostEqual(results.neutron_sld_imag, 1.1645807e-10, 5) |
---|
61 | |
---|
62 | |
---|
63 | class SLDCalculatorTest(unittest.TestCase): |
---|
64 | '''Test the SLDCalculator''' |
---|
65 | def setUp(self): |
---|
66 | '''Create the SLDCalculator''' |
---|
67 | self.widget = SldPanel(None) |
---|
68 | |
---|
69 | def tearDown(self): |
---|
70 | '''Destroy the DensityCalculator''' |
---|
71 | self.widget.close() |
---|
72 | self.widget = None |
---|
73 | |
---|
74 | def testDefaults(self): |
---|
75 | '''Test the GUI in its default state''' |
---|
76 | self.assertIsInstance(self.widget, QtGui.QWidget) |
---|
77 | self.assertEqual(self.widget.windowTitle(), "SLD Calculator") |
---|
78 | self.assertIsInstance(self.widget.ui.editMolecularFormula.validator(), FormulaValidator) |
---|
79 | self.assertEqual(self.widget.ui.editMolecularFormula.styleSheet(), '') |
---|
80 | self.assertEqual(self.widget.model.columnCount(), 1) |
---|
81 | self.assertEqual(self.widget.model.rowCount(), 12) |
---|
82 | self.assertEqual(self.widget.sizePolicy().Policy(), QtGui.QSizePolicy.Fixed) |
---|
83 | |
---|
84 | def testSimpleEntry(self): |
---|
85 | ''' Default compound calculations ''' |
---|
86 | |
---|
87 | self.widget.show() |
---|
88 | |
---|
89 | self.widget.ui.editMassDensity.clear() |
---|
90 | self.widget.ui.editMassDensity.insert("1.0") |
---|
91 | # Send tab x3 |
---|
92 | key = QtCore.Qt.Key_Tab |
---|
93 | QTest.keyEvent(QTest.Press, self.widget, key, QtCore.Qt.NoModifier) |
---|
94 | QTest.keyEvent(QTest.Press, self.widget, key, QtCore.Qt.NoModifier) |
---|
95 | QtGui.qApp.processEvents() |
---|
96 | QTest.qWait(100) |
---|
97 | |
---|
98 | # Assure the output fields are set |
---|
99 | self.assertEqual(self.widget.ui.editNeutronIncXs.text(), '5.37') |
---|
100 | |
---|
101 | # Change mass density |
---|
102 | self.widget.ui.editWavelength.clear() |
---|
103 | self.widget.ui.editWavelength.setText("666.0") |
---|
104 | |
---|
105 | # Send shift-tab to update the molar volume field |
---|
106 | QTest.keyEvent(QTest.Press, self.widget, key, QtCore.Qt.NoModifier) |
---|
107 | QTest.keyEvent(QTest.Press, self.widget, key, QtCore.Qt.NoModifier) |
---|
108 | QtGui.qApp.processEvents() |
---|
109 | QTest.qWait(100) |
---|
110 | |
---|
111 | # Assure the molar volume field got updated |
---|
112 | self.assertEqual(self.widget.ui.editNeutronAbsXs.text(), '8.24') |
---|
113 | |
---|
114 | def testComplexEntryAndReset(self): |
---|
115 | ''' User entered compound calculations and subsequent reset''' |
---|
116 | |
---|
117 | self.widget.ui.editMolecularFormula.clear() |
---|
118 | self.widget.ui.editMolecularFormula.insert("CaCO[18]3+6H2O") |
---|
119 | self.widget.ui.editMassDensity.insert("5.0") |
---|
120 | |
---|
121 | self.widget.show() |
---|
122 | # Send tab x2 |
---|
123 | key = QtCore.Qt.Key_Tab |
---|
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.editNeutronIncXs.text(), '40.6') |
---|
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.editMassDensity.text(), "1") |
---|
136 | self.assertEqual(self.widget.ui.editWavelength.text(), "6") |
---|
137 | |
---|
138 | def testHelp(self): |
---|
139 | """ Assure help file is shown """ |
---|
140 | |
---|
141 | # this should not rise |
---|
142 | self.widget.displayHelp() |
---|
143 | |
---|
144 | if __name__ == "__main__": |
---|
145 | unittest.main() |
---|