[0532d7c1] | 1 | import sys |
---|
| 2 | import unittest |
---|
| 3 | from PyQt4 import QtGui |
---|
| 4 | from PyQt4.QtTest import QTest |
---|
| 5 | from PyQt4.QtCore import Qt |
---|
| 6 | from mock import MagicMock |
---|
| 7 | |
---|
| 8 | # set up import paths |
---|
| 9 | import path_prepare |
---|
| 10 | |
---|
| 11 | from SlitSizeCalculator import SlitSizeCalculator |
---|
| 12 | from sas.sascalc.dataloader.loader import Loader |
---|
| 13 | |
---|
| 14 | app = QtGui.QApplication(sys.argv) |
---|
| 15 | |
---|
| 16 | |
---|
| 17 | class 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 testBrowseButton(self): |
---|
| 41 | browseButton = self.widget.browseButton |
---|
| 42 | |
---|
| 43 | filename = "beam_profile.DAT" |
---|
| 44 | |
---|
| 45 | # Return no files. |
---|
| 46 | QtGui.QFileDialog.getOpenFileName = MagicMock(return_value=None) |
---|
| 47 | |
---|
| 48 | # Click on the Browse button |
---|
| 49 | QTest.mouseClick(browseButton, Qt.LeftButton) |
---|
| 50 | |
---|
| 51 | # Test the getOpenFileName() dialog called once |
---|
| 52 | self.assertTrue(QtGui.QFileDialog.getOpenFileName.called) |
---|
| 53 | QtGui.QFileDialog.getOpenFileName.assert_called_once() |
---|
| 54 | |
---|
| 55 | # Now, return a single file |
---|
| 56 | QtGui.QFileDialog.getOpenFileName = MagicMock(return_value=filename) |
---|
| 57 | |
---|
| 58 | # Click on the Load button |
---|
| 59 | QTest.mouseClick(browseButton, Qt.LeftButton) |
---|
| 60 | QtGui.qApp.processEvents() |
---|
| 61 | |
---|
| 62 | # Test the getOpenFileName() dialog called once |
---|
| 63 | self.assertTrue(QtGui.QFileDialog.getOpenFileName.called) |
---|
| 64 | QtGui.QFileDialog.getOpenFileName.assert_called_once() |
---|
| 65 | |
---|
[debf5c3] | 66 | |
---|
| 67 | def testCalculateSlitSize(self): |
---|
| 68 | """ Test slit size calculated value """ |
---|
| 69 | |
---|
| 70 | filename = "beam_profile.DAT" |
---|
| 71 | loader = Loader() |
---|
| 72 | data = loader.load(filename) |
---|
| 73 | |
---|
| 74 | self.widget.calculateSlitSize(data) |
---|
| 75 | # The value "5.5858" was obtained by manual calculation. |
---|
| 76 | # It turns out our slit length is FWHM/2 |
---|
| 77 | self.assertAlmostEqual(float(self.widget.slit_length_out.text()), 5.5858/2, 3) |
---|
| 78 | |
---|
| 79 | def testWrongInput(self): |
---|
| 80 | """ Test on wrong input data """ |
---|
| 81 | |
---|
| 82 | filename = "Dec07031.ASC" |
---|
| 83 | loader = Loader() |
---|
| 84 | data = loader.load(filename) |
---|
[886d2f2c] | 85 | self.assertRaisesRegexp(RuntimeError, |
---|
[debf5c3] | 86 | "Slit Length cannot be computed for 2D Data", |
---|
| 87 | self.widget.calculateSlitSize, data) |
---|
| 88 | |
---|
| 89 | filename = "empty_file.txt" |
---|
| 90 | loader = Loader() |
---|
| 91 | data = loader.load(filename) |
---|
| 92 | self.assertRaisesRegexp(RuntimeError, |
---|
| 93 | "ERROR: Data hasn't been loaded correctly", |
---|
| 94 | self.widget.calculateSlitSize, data) |
---|
| 95 | |
---|
| 96 | data = None |
---|
| 97 | self.assertRaisesRegexp(RuntimeError, |
---|
| 98 | "ERROR: Data hasn't been loaded correctly", |
---|
| 99 | self.widget.calculateSlitSize, data) |
---|
| 100 | |
---|
[0532d7c1] | 101 | |
---|
| 102 | if __name__ == "__main__": |
---|
| 103 | unittest.main() |
---|