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