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