-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
executable file
·225 lines (186 loc) · 7.83 KB
/
test.py
File metadata and controls
executable file
·225 lines (186 loc) · 7.83 KB
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
218
219
220
221
222
223
224
225
#!/usr/bin/env python
# coding: UTF-8
# @copyright ©2013, Rodrigo Cacilhας <batalema@cacilhas.info>
# Cesar Barros <cesarb@cesarb.eti.br>
import logging
from unittest import main, TestCase
try:
from unittest.mock import patch
except ImportError:
from mock import patch
import flask_reportable_error
class TestInit(TestCase):
class Application(object):
def __init__(self):
self.handlers = {}
self.config = {}
self.logged = []
self.logger = type('Logger', (object, ), {
'log': lambda logger, *args: self.logged.append(args),
})()
def errorhandler(self, exc):
def register(callback):
self.handlers[exc] = callback
return register
def setUp(self):
cls = type(self)
app = self.app = cls.Application()
flask_reportable_error.init(app)
self.handler = app.handlers[
flask_reportable_error.ReportableErrorMixin
]
def test_handle_default_headers(self):
app = self.app
app.config = {
'REPORTABLE_ERROR': {
'HEADERS': { 'Content-Type': 'plain/text' },
},
}
flask_reportable_error.init(app)
exc = flask_reportable_error.reportable(ValueError)('some value error')
report, status_code, headers = self.handler(exc)
self.assertEqual(headers, { 'Content-Type': 'plain/text' })
def test_custom_headers(self):
class ExceptionWithHeaders(ValueError):
headers = { 'Content-Type': 'plain/text' }
exc = flask_reportable_error.reportable(ExceptionWithHeaders)('some value error')
report, status_code, headers = self.handler(exc)
self.assertEqual(headers, { 'Content-Type': 'plain/text' })
@patch.object(flask_reportable_error, 'render_template')
def test_handle_template(self, render_template):
app = self.app
app.config = {
'REPORTABLE_ERROR': {
'TEMPLATE': 'application/error.html',
},
}
flask_reportable_error.init(app)
exc = flask_reportable_error.reportable(ValueError)('some value error')
body, status_code, headers = self.handler(exc)
render_template.assert_called_once_with(
'application/error.html', exc=exc)
self.assertEqual(body, render_template.return_value)
@patch.object(flask_reportable_error, 'render_template')
def test_custom_template(self, render_template):
app = self.app
app.config = {
'REPORTABLE_ERROR': {
'TEMPLATE': 'application/error.html'
}
}
class ExceptionWithTemplate(ValueError):
template = 'application/error.html'
flask_reportable_error.init(app)
exc = flask_reportable_error.reportable(ExceptionWithTemplate)('some value error')
body, status_code, headers = self.handler(exc)
render_template.assert_called_once_with(
'application/error.html', exc=exc)
self.assertEqual(body, render_template.return_value)
def test_raises_on_none_app(self):
# Reset flask_reportable_error
flask_reportable_error.config.app = None
self.assertRaises(RuntimeError,
lambda: flask_reportable_error.config.settings)
def test_register_application(self):
self.assertEqual(self.handler.__name__,
'reportable_error_handler')
self.assertEqual(flask_reportable_error.config.app,
self.app)
def test_handle_error_500(self):
s = 'test reportable error'
exc = flask_reportable_error.ReportableErrorMixin(s)
report, status_code, headers = self.handler(exc)
self.assertEqual(report, s)
self.assertEqual(status_code, 500)
self.assertEqual(headers, {})
def test_log_error(self):
s = 'test reportable error'
exc = flask_reportable_error.ReportableErrorMixin(s)
self.handler(exc)
self.assertEqual(self.app.logged, [
(logging.ERROR, '(reported %s) %s', 'ReportableErrorMixin', exc),
])
def test_inhibit_log(self):
s = 'test reportable error'
app = self.app
app.config['REPORTABLE_ERROR'] = {
'LOGLEVEL': logging.DEBUG,
}
exc = flask_reportable_error.ReportableErrorMixin(s)
self.handler(exc)
self.assertEqual(app.logged, [
(logging.DEBUG, '(reported %s) %s', 'ReportableErrorMixin', exc),
])
def test_handle_error_400(self):
s = 'test reportable error'
self.app.config['REPORTABLE_ERROR'] = {
'DEFAULT_STATUS_CODE': 400,
}
exc = flask_reportable_error.ReportableErrorMixin(s)
report, status_code, headers = self.handler(exc)
self.assertEqual(report, s)
self.assertEqual(status_code, 400)
self.assertEqual(headers, {})
def test_handle_own_status(self):
s = 'test reportable error'
exc = flask_reportable_error.ReportableErrorMixin(s)
exc.status_code = 404
report, status_code, headers = self.handler(exc)
self.assertEqual(report, s)
self.assertEqual(status_code, 404)
self.assertEqual(headers, {})
def test_cannot_unset_mixin(self):
# Reset mixins
flask_reportable_error.config.mixins = set()
exc = flask_reportable_error.reportable(ValueError)
self.assertTrue(issubclass(exc, ValueError))
self.assertTrue(issubclass(exc,
flask_reportable_error.ReportableErrorMixin))
class TestReportableErrorMixin(TestCase):
def test_reportable_error_report(self):
s = 'test reportable error'
exc = flask_reportable_error.ReportableErrorMixin(s)
self.assertEqual(exc.report(), s)
def test_reportable_type_name(self):
exc_class = flask_reportable_error.reportable(ValueError)
self.assertEqual(exc_class.type_name, 'ValueError')
def test_reportable_factory_return_reportable_error(self):
exc_class = flask_reportable_error.reportable(ValueError)
self.assertTrue(issubclass(exc_class,
flask_reportable_error.ReportableErrorMixin))
self.assertTrue(issubclass(exc_class, ValueError))
def test_reportable_factory_response_report_error(self):
s = 'test reportable error'
exc = flask_reportable_error.reportable(ValueError)(s)
self.assertEqual(exc.report(), s)
def test_reportable_factory_response_be_memoized(self):
exc1 = flask_reportable_error.reportable(ValueError)
exc2 = flask_reportable_error.reportable(ValueError)
exc3 = flask_reportable_error.reportable(AttributeError)
self.assertIs(exc1, exc2)
self.assertNotEqual(exc1, exc3)
class TestAddMixins(TestCase):
def test_mixin_decorator(self):
@flask_reportable_error.mixin
class Mixin(object):
pass
class SomeError1(ValueError):
pass
exc_class = flask_reportable_error.reportable(SomeError1)
self.assertTrue(issubclass(exc_class, Mixin))
self.assertTrue(issubclass(exc_class, SomeError1))
self.assertTrue(issubclass(exc_class,
flask_reportable_error.ReportableErrorMixin))
def test_add_mixins(self):
class Mixin(object):
pass
class SomeError2(ValueError):
pass
flask_reportable_error.add_mixins(Mixin)
exc_class = flask_reportable_error.reportable(SomeError2)
self.assertTrue(issubclass(exc_class, Mixin))
self.assertTrue(issubclass(exc_class, SomeError2))
self.assertTrue(issubclass(exc_class,
flask_reportable_error.ReportableErrorMixin))
if __name__ == '__main__':
main()