source: sasview/src/sas/qtgui/Calculators/UnitTesting/SlitSizeCalculatorTest.py @ 60a4e71

Last change on this file since 60a4e71 was e90988c, checked in by Piotr Rozyczko <rozyczko@…>, 7 years ago

Show help pages in default browser. Fixed some help links and modified unit tests. SASVIEW-800

  • Property mode set to 100644
File size: 3.2 KB
Line 
1import sys
2import unittest
3import logging
4
5from PyQt5 import QtGui, QtWidgets
6from PyQt5.QtTest import QTest
7from PyQt5.QtCore import Qt
8from unittest.mock import MagicMock
9
10# set up import paths
11import sas.qtgui.path_prepare
12
13from sas.qtgui.Calculators.SlitSizeCalculator import SlitSizeCalculator
14from sas.sascalc.dataloader.loader import Loader
15
16if not QtWidgets.QApplication.instance():
17    app = QtWidgets.QApplication(sys.argv)
18
19
20class 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        self.widget._parent = QtWidgets.QWidget()
40        self.widget._parent.showHelp = MagicMock()
41        self.widget.onHelp()
42        self.assertTrue(self.widget._parent.showHelp.called_once())
43        args = self.widget._parent.showHelp.call_args
44        self.assertIn('slit_calculator_help.html', args[0][0])
45
46    def testBrowseButton(self):
47        browseButton = self.widget.browseButton
48
49        filename = "beam_profile.DAT"
50
51        # Return no files.
52        QtWidgets.QFileDialog.getOpenFileName = MagicMock(return_value=('',''))
53
54        # Click on the Browse button
55        QTest.mouseClick(browseButton, Qt.LeftButton)
56
57        # Test the getOpenFileName() dialog called once
58        self.assertTrue(QtWidgets.QFileDialog.getOpenFileName.called)
59        QtWidgets.QFileDialog.getOpenFileName.assert_called_once()
60
61        # Now, return a single file
62        QtWidgets.QFileDialog.getOpenFileName = MagicMock(return_value=(filename,''))
63
64        # Click on the Load button
65        QTest.mouseClick(browseButton, Qt.LeftButton)
66        QtWidgets.qApp.processEvents()
67
68        # Test the getOpenFileName() dialog called once
69        self.assertTrue(QtWidgets.QFileDialog.getOpenFileName.called)
70        QtWidgets.QFileDialog.getOpenFileName.assert_called_once()
71
72
73    def notestCalculateSlitSize(self):
74        """ Test slit size calculated value """
75
76        filename = "beam_profile.DAT"
77        loader = Loader()
78        data = loader.load(filename)[0]
79
80        self.widget.calculateSlitSize(data)
81        # The value "5.5858" was obtained by manual calculation.
82        # It turns out our slit length is FWHM/2
83        self.assertAlmostEqual(float(self.widget.slit_length_out.text()), 5.5858/2, 3)
84
85    def testWrongInput(self):
86        """ Test on wrong input data """
87
88        filename = "Dec07031.ASC"
89        loader = Loader()
90        data = loader.load(filename)[0]
91
92        logging.error = MagicMock()
93
94        self.widget.calculateSlitSize(data)
95
96        self.assertTrue(logging.error.called_once())
97
98        data = None
99        self.widget.calculateSlitSize(data)
100        self.assertTrue(logging.error.call_count == 2)
101
102
103if __name__ == "__main__":
104    unittest.main()
Note: See TracBrowser for help on using the repository browser.