source: sasview/src/sas/qtgui/UnitTesting/SlitSizeCalculatorTest.py @ 0532d7c1

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 0532d7c1 was 0532d7c1, checked in by wojciech, 7 years ago

Unit tests added

  • Property mode set to 100755
File size: 3.5 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.calculator.slit_length_calculator import SlitlengthCalculator as calculator
13from sas.sascalc.dataloader.loader import Loader
14
15app = 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 testComputeSlit(self):
42        """ User entered compound calculations and subsequent reset"""
43
44        filename = "beam_profile.DAT"
45        loader = Loader()
46        data = loader.load(filename)
47        # Push Compute with the left mouse button
48        cal = calculator()
49        cal.set_data(x = data.x, y = data.y)
50        slitlength = cal.calculate_slit_length()
51
52        # The value "5.5858" was obtained by manual calculation.
53        # It turns out our slit length is FWHM/2
54        self.assertAlmostEqual(slitlength,5.5858/2, 3)
55
56    def testBrowseButton(self):
57        browseButton = self.widget.browseButton
58
59        filename = "beam_profile.DAT"
60
61        # Return no files.
62        QtGui.QFileDialog.getOpenFileName = MagicMock(return_value=None)
63
64        # Click on the Browse button
65        QTest.mouseClick(browseButton, Qt.LeftButton)
66
67        # Test the getOpenFileName() dialog called once
68        self.assertTrue(QtGui.QFileDialog.getOpenFileName.called)
69        QtGui.QFileDialog.getOpenFileName.assert_called_once()
70
71        # Now, return a single file
72        QtGui.QFileDialog.getOpenFileName = MagicMock(return_value=filename)
73
74        # Click on the Load button
75        QTest.mouseClick(browseButton, Qt.LeftButton)
76        QtGui.qApp.processEvents()
77
78        # Test the getOpenFileName() dialog called once
79        self.assertTrue(QtGui.QFileDialog.getOpenFileName.called)
80        QtGui.QFileDialog.getOpenFileName.assert_called_once()
81
82    # def testDataEntryNumbers(self):
83    #     """ User entered compound calculations and subsequent reset"""
84    #
85    #     self.widget.data_file.clear()
86    #     self.widget.data_file.insert("beam profile.DAT")
87    #     #
88    #     # Push Compute with the left mouse button
89    #     computeButton = self.widget.computeButton
90    #     QTest.mouseClick(computeButton, Qt.LeftButton)
91    #     self.assertEqual(self.widget.lengthscale_out.text(), '6.283')
92    #
93    #
94    # def testComplexEntryLetters(self):
95    #     """ User entered compound calculations and subsequent reset"""
96    #
97    #     self.widget.deltaq_in.insert("xyz")
98    #
99    #     # Push Compute with the left mouse button
100    #     computeButton = self.widget.computeButton
101    #     QTest.mouseClick(computeButton, Qt.LeftButton)
102    #     self.assertEqual(self.widget.lengthscale_out.text(), '')
103
104if __name__ == "__main__":
105    unittest.main()
Note: See TracBrowser for help on using the repository browser.