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

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 b0b09b9 was b0b09b9, checked in by Piotr Rozyczko <rozyczko@…>, 7 years ago

Initial changes to make SasView? run with python3

  • Property mode set to 100755
File size: 3.1 KB
Line 
1import sys
2import unittest
3from PyQt4 import QtGui
4from PyQt4.QtTest import QTest
5from PyQt4.QtCore import Qt
6from mock import MagicMock
7
8# set up import paths
9import sas.qtgui.path_prepare
10
11from sas.qtgui.Calculators.SlitSizeCalculator import SlitSizeCalculator
12from sas.sascalc.dataloader.loader import Loader
13
14if not QtGui.QApplication.instance():
15    app = QtGui.QApplication(sys.argv)
16
17
18class 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)[0]
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 = "P123_D2O_10_percent.dat"
84        loader = Loader()
85        data = loader.load(filename)[0]
86        self.assertRaisesRegex(RuntimeError,
87                                "Slit Length cannot be computed for 2D Data",
88                                self.widget.calculateSlitSize, data)
89
90        data = None
91        self.assertRaisesRegex(RuntimeError,
92                                "ERROR: Data hasn't been loaded correctly",
93                                self.widget.calculateSlitSize, data)
94
95
96if __name__ == "__main__":
97    unittest.main()
Note: See TracBrowser for help on using the repository browser.