1 | import sys |
---|
2 | import unittest |
---|
3 | from PyQt4 import QtGui |
---|
4 | from PyQt4.QtTest import QTest |
---|
5 | from PyQt4.QtCore import Qt |
---|
6 | from mock import MagicMock |
---|
7 | |
---|
8 | # set up import paths |
---|
9 | import sas.qtgui.path_prepare |
---|
10 | |
---|
11 | from sas.qtgui.Calculators.SlitSizeCalculator import SlitSizeCalculator |
---|
12 | from sas.sascalc.dataloader.loader import Loader |
---|
13 | |
---|
14 | if not QtGui.QApplication.instance(): |
---|
15 | app = QtGui.QApplication(sys.argv) |
---|
16 | |
---|
17 | |
---|
18 | class SlitSizeCalculatorTest(unittest.TestCase): |
---|
19 | """Test the SlitSizeCalculator""" |
---|
20 | def setUp(self): |
---|
21 | """Create the SlitSizeCalculator""" |
---|
22 | self.widget = SlitSizeCalculator(None) |
---|
23 | |
---|
24 | def tearDown(self): |
---|
25 | """Destroy the SlitSizeCalculator""" |
---|
26 | self.widget.close() |
---|
27 | self.widget = None |
---|
28 | |
---|
29 | def testDefaults(self): |
---|
30 | """Test the GUI in its default state""" |
---|
31 | self.assertIsInstance(self.widget, QtGui.QWidget) |
---|
32 | self.assertEqual(self.widget.windowTitle(), "Slit Size Calculator") |
---|
33 | self.assertEqual(self.widget.sizePolicy().Policy(), QtGui.QSizePolicy.Fixed) |
---|
34 | |
---|
35 | def testHelp(self): |
---|
36 | """ Assure help file is shown """ |
---|
37 | |
---|
38 | # this should not rise |
---|
39 | self.widget.onHelp() |
---|
40 | |
---|
41 | def testBrowseButton(self): |
---|
42 | browseButton = self.widget.browseButton |
---|
43 | |
---|
44 | filename = "beam_profile.DAT" |
---|
45 | |
---|
46 | # Return no files. |
---|
47 | QtGui.QFileDialog.getOpenFileName = MagicMock(return_value=None) |
---|
48 | |
---|
49 | # Click on the Browse button |
---|
50 | QTest.mouseClick(browseButton, Qt.LeftButton) |
---|
51 | |
---|
52 | # Test the getOpenFileName() dialog called once |
---|
53 | self.assertTrue(QtGui.QFileDialog.getOpenFileName.called) |
---|
54 | QtGui.QFileDialog.getOpenFileName.assert_called_once() |
---|
55 | |
---|
56 | # Now, return a single file |
---|
57 | QtGui.QFileDialog.getOpenFileName = MagicMock(return_value=filename) |
---|
58 | |
---|
59 | # Click on the Load button |
---|
60 | QTest.mouseClick(browseButton, Qt.LeftButton) |
---|
61 | QtGui.qApp.processEvents() |
---|
62 | |
---|
63 | # Test the getOpenFileName() dialog called once |
---|
64 | self.assertTrue(QtGui.QFileDialog.getOpenFileName.called) |
---|
65 | QtGui.QFileDialog.getOpenFileName.assert_called_once() |
---|
66 | |
---|
67 | |
---|
68 | def notestCalculateSlitSize(self): |
---|
69 | """ Test slit size calculated value """ |
---|
70 | |
---|
71 | filename = "beam_profile.DAT" |
---|
72 | loader = Loader() |
---|
73 | data = loader.load(filename) |
---|
74 | |
---|
75 | self.widget.calculateSlitSize(data) |
---|
76 | # The value "5.5858" was obtained by manual calculation. |
---|
77 | # It turns out our slit length is FWHM/2 |
---|
78 | self.assertAlmostEqual(float(self.widget.slit_length_out.text()), 5.5858/2, 3) |
---|
79 | |
---|
80 | def testWrongInput(self): |
---|
81 | """ Test on wrong input data """ |
---|
82 | |
---|
83 | filename = "Dec07031.ASC" |
---|
84 | loader = Loader() |
---|
85 | data = loader.load(filename) |
---|
86 | self.assertRaisesRegexp(RuntimeError, |
---|
87 | "Slit Length cannot be computed for 2D Data", |
---|
88 | self.widget.calculateSlitSize, data) |
---|
89 | |
---|
90 | data = None |
---|
91 | self.assertRaisesRegexp(RuntimeError, |
---|
92 | "ERROR: Data hasn't been loaded correctly", |
---|
93 | self.widget.calculateSlitSize, data) |
---|
94 | |
---|
95 | |
---|
96 | if __name__ == "__main__": |
---|
97 | unittest.main() |
---|