source: sasview/src/sas/qtgui/UnitTesting/SlitSizeCalculatorTest.py @ 3a0ce4f

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 3a0ce4f was 3a0ce4f, checked in by wojciech, 5 years ago

Added a few unit tests

  • Property mode set to 100755
File size: 4.3 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 path_prepare
10
11from SlitSizeCalculator import SlitSizeCalculator
12from sas.sascalc.dataloader.loader import Loader
13
14app = QtGui.QApplication(sys.argv)
15
16
17class 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
124if __name__ == "__main__":
125    unittest.main()
Note: See TracBrowser for help on using the repository browser.