source: sasview/src/sas/qtgui/Calculators/UnitTesting/SlitSizeCalculatorTest.py @ 50bfab0

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalc
Last change on this file since 50bfab0 was 53c771e, checked in by Piotr Rozyczko <rozyczko@…>, 7 years ago

Converted unit tests

  • Property mode set to 100644
File size: 3.0 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
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
100if __name__ == "__main__":
101    unittest.main()
Note: See TracBrowser for help on using the repository browser.