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 path_prepare |
---|
10 | |
---|
11 | from SlitSizeCalculator import SlitSizeCalculator |
---|
12 | from sas.sascalc.dataloader.loader import Loader |
---|
13 | |
---|
14 | app = QtGui.QApplication(sys.argv) |
---|
15 | |
---|
16 | |
---|
17 | class SlitSizeCalculatorTest(unittest.TestCase): |
---|
18 | """Test the SlitSizeCalculator""" |
---|
19 | def setUp(self): |
---|
20 | """Create the SlitSizeCalculator""" |
---|
21 | self.widget = SlitSizeCalculator(None) |
---|
22 | |
---|
23 | def tearDown(self): |
---|
24 | """Destroy the SlitSizeCalculator""" |
---|
25 | self.widget.close() |
---|
26 | self.widget = None |
---|
27 | |
---|
28 | def testDefaults(self): |
---|
29 | """Test the GUI in its default state""" |
---|
30 | self.assertIsInstance(self.widget, QtGui.QWidget) |
---|
31 | self.assertEqual(self.widget.windowTitle(), "Slit Size Calculator") |
---|
32 | self.assertEqual(self.widget.sizePolicy().Policy(), QtGui.QSizePolicy.Fixed) |
---|
33 | |
---|
34 | def testHelp(self): |
---|
35 | """ Assure help file is shown """ |
---|
36 | |
---|
37 | # this should not rise |
---|
38 | self.widget.onHelp() |
---|
39 | |
---|
40 | def testCalculateSlitSize(self): |
---|
41 | """ User entered compound calculations and subsequent reset""" |
---|
42 | |
---|
43 | filename = "beam_profile.DAT" |
---|
44 | loader = Loader() |
---|
45 | data = loader.load(filename) |
---|
46 | |
---|
47 | self.widget.calculateSlitSize(data) |
---|
48 | # The value "5.5858" was obtained by manual calculation. |
---|
49 | # It turns out our slit length is FWHM/2 |
---|
50 | self.assertAlmostEqual(float(self.widget.slit_length_out.text()), 5.5858/2, 3) |
---|
51 | |
---|
52 | def testCalculateSlitSize2D(self): |
---|
53 | """ User entered compound calculations and subsequent reset""" |
---|
54 | |
---|
55 | #TODO: Test for 2D and check if exception is being raised |
---|
56 | filename = "beam_profile.DAT" |
---|
57 | loader = Loader() |
---|
58 | data = loader.load(filename) |
---|
59 | |
---|
60 | self.widget.calculateSlitSize(data) |
---|
61 | # The value "5.5858" was obtained by manual calculation. |
---|
62 | # It turns out our slit length is FWHM/2 |
---|
63 | self.assertAlmostEqual(float(self.widget.slit_length_out.text()), 5.5858/2, 3) |
---|
64 | |
---|
65 | # def testDataEntryNumbers(self): |
---|
66 | # """ User entered compound calculations and subsequent reset""" |
---|
67 | # |
---|
68 | # self.widget.data_file.clear() |
---|
69 | # self.widget.data_file.insert("beam profile.DAT") |
---|
70 | # # |
---|
71 | # # Push Compute with the left mouse button |
---|
72 | # computeButton = self.widget.computeButton |
---|
73 | # QTest.mouseClick(computeButton, Qt.LeftButton) |
---|
74 | # self.assertEqual(self.widget.lengthscale_out.text(), '6.283') |
---|
75 | |
---|
76 | def testBrowseButton(self): |
---|
77 | browseButton = self.widget.browseButton |
---|
78 | |
---|
79 | filename = "beam_profile.DAT" |
---|
80 | |
---|
81 | # Return no files. |
---|
82 | QtGui.QFileDialog.getOpenFileName = MagicMock(return_value=None) |
---|
83 | |
---|
84 | # Click on the Browse button |
---|
85 | QTest.mouseClick(browseButton, Qt.LeftButton) |
---|
86 | |
---|
87 | # Test the getOpenFileName() dialog called once |
---|
88 | self.assertTrue(QtGui.QFileDialog.getOpenFileName.called) |
---|
89 | QtGui.QFileDialog.getOpenFileName.assert_called_once() |
---|
90 | |
---|
91 | # Now, return a single file |
---|
92 | QtGui.QFileDialog.getOpenFileName = MagicMock(return_value=filename) |
---|
93 | |
---|
94 | # Click on the Load button |
---|
95 | QTest.mouseClick(browseButton, Qt.LeftButton) |
---|
96 | QtGui.qApp.processEvents() |
---|
97 | |
---|
98 | # Test the getOpenFileName() dialog called once |
---|
99 | self.assertTrue(QtGui.QFileDialog.getOpenFileName.called) |
---|
100 | QtGui.QFileDialog.getOpenFileName.assert_called_once() |
---|
101 | |
---|
102 | # def testDataEntryNumbers(self): |
---|
103 | # """ User entered compound calculations and subsequent reset""" |
---|
104 | # |
---|
105 | # self.widget.data_file.clear() |
---|
106 | # self.widget.data_file.insert("beam profile.DAT") |
---|
107 | # # |
---|
108 | # # Push Compute with the left mouse button |
---|
109 | # computeButton = self.widget.computeButton |
---|
110 | # QTest.mouseClick(computeButton, Qt.LeftButton) |
---|
111 | # self.assertEqual(self.widget.lengthscale_out.text(), '6.283') |
---|
112 | # |
---|
113 | # |
---|
114 | # def testComplexEntryLetters(self): |
---|
115 | # """ User entered compound calculations and subsequent reset""" |
---|
116 | # |
---|
117 | # self.widget.deltaq_in.insert("xyz") |
---|
118 | # |
---|
119 | # # Push Compute with the left mouse button |
---|
120 | # computeButton = self.widget.computeButton |
---|
121 | # QTest.mouseClick(computeButton, Qt.LeftButton) |
---|
122 | # self.assertEqual(self.widget.lengthscale_out.text(), '') |
---|
123 | |
---|
124 | if __name__ == "__main__": |
---|
125 | unittest.main() |
---|