~vpec/maus/tof_calib_read

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#  This file is part of MAUS: http://micewww.pp.rl.ac.uk:8080/projects/maus
# 
#  MAUS is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
# 
#  MAUS is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
# 
#  You should have received a copy of the GNU General Public License
#  along with MAUS.  If not, see <http://www.gnu.org/licenses/>.

"""
Test error handler
"""

#pylint: disable=R0904

import StringIO
import sys

import json
import unittest

import ErrorHandler

class ErrorHandlerTestCase(unittest.TestCase):
    """
    Test error handler
    """
    def test_error_handler(self): #pylint: disable=R0201
        """
        Test initialisation of the error handler
        """
        error_handler = ErrorHandler.ErrorHandler()
        assert(error_handler.error_to_stderr == True)
        assert(error_handler.error_to_json == True)
        assert(error_handler.on_error == 'none')

    def test_errors_to_json(self):
        """Check we send error to json correctly"""
        error_handler = ErrorHandler.ErrorHandler()
        doc = {}
        try:
            raise RuntimeError("Test error 1")
        except RuntimeError:
            doc = error_handler.ErrorsToJson(doc, self)
        try:
            raise RuntimeError("Test error 2")
        except RuntimeError:
            doc = error_handler.ErrorsToJson(doc, self)
        self.assertEqual(doc["errors"]["ErrorHandlerTestCase"], 
                         "<type 'exceptions.RuntimeError'>: Test error 2"
        )

    def test_errors_to_user(self): #pylint: disable=R0201
        """Check that we send errors to sys.stderr if required"""
        stderr = sys.stderr
        test_err = StringIO.StringIO(u'')
        sys.stderr = test_err
        error_handler = ErrorHandler.ErrorHandler()
        try:
            raise RuntimeError("Test error 1")
        except RuntimeError:
            error_handler.ErrorsToUser()
        lines = test_err.getvalue().split('\n')
        assert(lines[3] == 'RuntimeError: Test error 1')
        sys.stderr = stderr

    def test_handle_exception_json(self):
        """Check the flag that controls sending errors to json works"""
        error_handler = ErrorHandler.ErrorHandler()
        error_handler.error_to_json = True
        doc = {}
        try:
            raise RuntimeError("Test error 1")
        except RuntimeError:
            doc = error_handler.HandleException(doc, self)
        self.assertEqual(doc["errors"]["ErrorHandlerTestCase"],
                         "<type 'exceptions.RuntimeError'>: Test error 1",
        )
        error_handler.error_to_json = False
        try:
            raise RuntimeError("Test error 2")
        except RuntimeError:
            doc = error_handler.HandleException(doc, self)
        self.assertEqual(doc["errors"]["ErrorHandlerTestCase"],
                         "<type 'exceptions.RuntimeError'>: Test error 1",
        )
        error_handler.error_to_json = True
        try:
            raise RuntimeError("Test error 3")
        except RuntimeError:
            doc = error_handler.HandleException(doc, self)
        self.assertEqual(doc["errors"]["ErrorHandlerTestCase"],
                    "<type 'exceptions.RuntimeError'>: Test error 3",
        )

    def test_handle_exception_user(self):
        """Check the flag that controls sending errors to stderr"""
        error_handler = ErrorHandler.ErrorHandler()
        doc = {}
        error_handler.error_to_stderr = True
        stderr = sys.stderr
        test_err = StringIO.StringIO(u'')
        print test_err.getvalue()
        sys.stderr = test_err
        try:
            raise RuntimeError("Test error 4")
        except RuntimeError:
            doc = error_handler.HandleException(doc, self)
        error_handler.error_to_stderr = False
        try:
            raise RuntimeError("Test error 4")
        except RuntimeError:
            doc = error_handler.HandleException(doc, self)
        lines = test_err.getvalue().split('\n')
        assert(lines[3] == 'RuntimeError: Test error 4')
        sys.stderr = stderr

    def test_handle_exception_on_error(self):
        """Check that we handle the exception correctly (raise, halt, etc)"""
        error_handler = ErrorHandler.ErrorHandler()
        doc = {}
        error_handler.on_error = 'halt'
        try:
            raise RuntimeError("Test error 1")
        except RuntimeError:
            try:
                doc = error_handler.HandleException(doc, self)
            except SystemExit:
                assert(sys.exc_info()[0] == type(SystemExit()))
        error_handler.on_error = 'raise'
        try:
            raise RuntimeError("Test error 1")
        except RuntimeError:
            try:
                doc = error_handler.HandleException(doc, self)
            except RuntimeError:
                assert(sys.exc_info()[0] == type(RuntimeError()))
        error_handler.on_error = 'none'
        try:
            raise RuntimeError("Test error 1")
        except RuntimeError:
            doc = error_handler.HandleException(doc, self)  
        error_handler.on_error = 'BAD_STRING'
        try:
            raise RuntimeError("Test error 1")
        except RuntimeError:
            with self.assertRaises(KeyError):
                error_handler.HandleException(doc, self)

    def test_default_handler(self):
        """
        Check default handler calls correctly
        """
        ErrorHandler.DefaultHandler().error_to_stderr = False
        ErrorHandler.DefaultHandler().error_to_json = True
        ErrorHandler.DefaultHandler().on_error = 'none'
        doc = {}
        try:
            raise RuntimeError("Test error 1")
        except RuntimeError:
            doc = ErrorHandler.HandleException(doc, self)
        assert(doc["errors"]["ErrorHandlerTestCase"] == 
                      "<type 'exceptions.RuntimeError'>: Test error 1"
        )
        try:
            raise RuntimeError("Test error 2")
        except RuntimeError:
            doc = json.loads(ErrorHandler.HandleCppException(json.dumps(doc),
                             "some caller", "Test error 3"))
        self.assertEqual(doc["errors"]["some caller"],
                         "<class 'ErrorHandler.CppError'>: Test error 3",
        )

    def test_cpp_error(self): #pylint: disable = R0201
        """
        Test CppError type works okay
        """
        my_error = ErrorHandler.CppError("Some message")
        assert(my_error.args == ("Some message",))
        assert(repr(my_error) == "Some message")

    def test_config_to_error_handler(self):
        """
        Test error handler setup from configuration
        """
        config_test = {'verbose_level':0, 'errors_to_stderr':None, 
                       'errors_to_json':True, 'on_error':'none'}

        error_handler = ErrorHandler.ErrorHandler()
        error_handler.ConfigurationToErrorHandler(config_test)
        self.assertTrue(error_handler.error_to_stderr)

        config_test['verbose_level'] = 4
        error_handler.ConfigurationToErrorHandler(config_test)
        self.assertFalse(error_handler.error_to_stderr)

        self.assertTrue(error_handler.error_to_json)
        self.assertEqual(error_handler.on_error, 'none')

        config_test = {'verbose_level':4, 'errors_to_stderr':True,
                      'errors_to_json':False, 'on_error':'raise'}

        error_handler.ConfigurationToErrorHandler(config_test)
        self.assertTrue(error_handler.error_to_stderr)
        self.assertFalse(error_handler.error_to_json)
        self.assertEqual(error_handler.on_error, 'raise')


if __name__ == '__main__':
    unittest.main()