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