1
- import unittest
2
- import logging
3
1
from datetime import datetime
4
2
from decimal import Decimal
5
-
3
+ from io import BytesIO
4
+ import unittest
5
+ import logging
6
6
import json
7
+
8
+
9
+ from django .core .handlers .wsgi import WSGIRequest
10
+ from django .conf import settings
7
11
import ujson
8
12
import simplejson
9
13
25
29
DATETIME = datetime (2015 , 9 , 1 , 6 , 9 , 42 , 797203 )
26
30
DATETIME_ISO = u'2015-09-01T06:09:42.797203'
27
31
32
+ settings .configure (DEBUG = True )
33
+
28
34
29
35
class TestCase (unittest .TestCase ):
30
36
def tearDown (self ):
@@ -100,11 +106,49 @@ class JsonLibTest(TestCase):
100
106
def setUp (self ):
101
107
json_handler .setFormatter (JSONFormatter ())
102
108
103
- def test_error_when_decimal_is_passed (self ):
104
- with self .assertRaises (TypeError ):
105
- logger .log (lvl = 0 , msg = 'Payment was sent' , extra = {
106
- 'amount' : Decimal ('0.00497265' )
107
- })
109
+ def test_builtin_types_are_serialized (self ):
110
+ logger .log (level = logging .ERROR , msg = 'Payment was sent' , extra = {
111
+ 'first_name' : 'bob' ,
112
+ 'amount' : 0.00497265 ,
113
+ 'context' : {
114
+ 'tags' : ['fizz' , 'bazz' ],
115
+ },
116
+ 'things' : ('a' , 'b' ),
117
+ 'ok' : True ,
118
+ 'none' : None ,
119
+ })
120
+
121
+ json_record = json .loads (log_buffer .getvalue ())
122
+ self .assertEqual (json_record ['first_name' ], 'bob' )
123
+ self .assertEqual (json_record ['amount' ], 0.00497265 )
124
+ self .assertEqual (json_record ['context' ], {'tags' : ['fizz' , 'bazz' ]})
125
+ self .assertEqual (json_record ['things' ], ['a' , 'b' ])
126
+ self .assertEqual (json_record ['ok' ], True )
127
+ self .assertEqual (json_record ['none' ], None )
128
+
129
+ def test_decimal_is_serialized_as_string (self ):
130
+ logger .log (level = logging .ERROR , msg = 'Payment was sent' , extra = {
131
+ 'amount' : Decimal ('0.00497265' )
132
+ })
133
+ expected_amount = '"amount": "0.00497265"'
134
+ self .assertIn (expected_amount , log_buffer .getvalue ())
135
+
136
+ def test_django_wsgi_request_is_serialized_as_dict (self ):
137
+ request = WSGIRequest ({
138
+ 'PATH_INFO' : 'bogus' ,
139
+ 'REQUEST_METHOD' : 'bogus' ,
140
+ 'CONTENT_TYPE' : 'text/html; charset=utf8' ,
141
+ 'wsgi.input' : BytesIO (b'' ),
142
+ })
143
+
144
+ logger .log (level = logging .ERROR , msg = 'Django response error' , extra = {
145
+ 'status_code' : 500 ,
146
+ 'request' : request
147
+ })
148
+ json_record = json .loads (log_buffer .getvalue ())
149
+ self .assertEqual (json_record ['status_code' ], 500 )
150
+ self .assertEqual (json_record ['request' ]['path' ], '/bogus' )
151
+ self .assertEqual (json_record ['request' ]['method' ], 'BOGUS' )
108
152
109
153
110
154
class UjsonLibTest (TestCase ):
@@ -113,6 +157,26 @@ def setUp(self):
113
157
formatter .json_lib = ujson
114
158
json_handler .setFormatter (formatter )
115
159
160
+ def test_builtin_types_are_serialized (self ):
161
+ logger .log (level = logging .ERROR , msg = 'Payment was sent' , extra = {
162
+ 'first_name' : 'bob' ,
163
+ 'amount' : 0.00497265 ,
164
+ 'context' : {
165
+ 'tags' : ['fizz' , 'bazz' ],
166
+ },
167
+ 'things' : ('a' , 'b' ),
168
+ 'ok' : True ,
169
+ 'none' : None ,
170
+ })
171
+
172
+ json_record = json .loads (log_buffer .getvalue ())
173
+ self .assertEqual (json_record ['first_name' ], 'bob' )
174
+ self .assertEqual (json_record ['amount' ], 0.00497265 )
175
+ self .assertEqual (json_record ['context' ], {'tags' : ['fizz' , 'bazz' ]})
176
+ self .assertEqual (json_record ['things' ], ['a' , 'b' ])
177
+ self .assertEqual (json_record ['ok' ], True )
178
+ self .assertEqual (json_record ['none' ], None )
179
+
116
180
def test_decimal_is_serialized_as_number (self ):
117
181
logger .info ('Payment was sent' , extra = {
118
182
'amount' : Decimal ('0.00497265' )
@@ -127,13 +191,49 @@ def test_zero_expected_when_decimal_is_in_scientific_notation(self):
127
191
expected_amount = '"amount":0.0'
128
192
self .assertIn (expected_amount , log_buffer .getvalue ())
129
193
194
+ def test_django_wsgi_request_is_serialized_as_empty_list (self ):
195
+ request = WSGIRequest ({
196
+ 'PATH_INFO' : 'bogus' ,
197
+ 'REQUEST_METHOD' : 'bogus' ,
198
+ 'CONTENT_TYPE' : 'text/html; charset=utf8' ,
199
+ 'wsgi.input' : BytesIO (b'' ),
200
+ })
201
+
202
+ logger .log (level = logging .ERROR , msg = 'Django response error' , extra = {
203
+ 'status_code' : 500 ,
204
+ 'request' : request
205
+ })
206
+ json_record = json .loads (log_buffer .getvalue ())
207
+ self .assertEqual (json_record ['status_code' ], 500 )
208
+ self .assertEqual (json_record ['request' ], [])
209
+
130
210
131
211
class SimplejsonLibTest (TestCase ):
132
212
def setUp (self ):
133
213
formatter = JSONFormatter ()
134
214
formatter .json_lib = simplejson
135
215
json_handler .setFormatter (formatter )
136
216
217
+ def test_builtin_types_are_serialized (self ):
218
+ logger .log (level = logging .ERROR , msg = 'Payment was sent' , extra = {
219
+ 'first_name' : 'bob' ,
220
+ 'amount' : 0.00497265 ,
221
+ 'context' : {
222
+ 'tags' : ['fizz' , 'bazz' ],
223
+ },
224
+ 'things' : ('a' , 'b' ),
225
+ 'ok' : True ,
226
+ 'none' : None ,
227
+ })
228
+
229
+ json_record = json .loads (log_buffer .getvalue ())
230
+ self .assertEqual (json_record ['first_name' ], 'bob' )
231
+ self .assertEqual (json_record ['amount' ], 0.00497265 )
232
+ self .assertEqual (json_record ['context' ], {'tags' : ['fizz' , 'bazz' ]})
233
+ self .assertEqual (json_record ['things' ], ['a' , 'b' ])
234
+ self .assertEqual (json_record ['ok' ], True )
235
+ self .assertEqual (json_record ['none' ], None )
236
+
137
237
def test_decimal_is_serialized_as_number (self ):
138
238
logger .info ('Payment was sent' , extra = {
139
239
'amount' : Decimal ('0.00497265' )
@@ -147,3 +247,20 @@ def test_decimal_is_serialized_as_it_is_when_it_is_in_scientific_notation(self):
147
247
})
148
248
expected_amount = '"amount": 0E-8'
149
249
self .assertIn (expected_amount , log_buffer .getvalue ())
250
+
251
+ def test_django_wsgi_request_is_serialized_as_dict (self ):
252
+ request = WSGIRequest ({
253
+ 'PATH_INFO' : 'bogus' ,
254
+ 'REQUEST_METHOD' : 'bogus' ,
255
+ 'CONTENT_TYPE' : 'text/html; charset=utf8' ,
256
+ 'wsgi.input' : BytesIO (b'' ),
257
+ })
258
+
259
+ logger .log (level = logging .ERROR , msg = 'Django response error' , extra = {
260
+ 'status_code' : 500 ,
261
+ 'request' : request
262
+ })
263
+ json_record = json .loads (log_buffer .getvalue ())
264
+ self .assertEqual (json_record ['status_code' ], 500 )
265
+ self .assertEqual (json_record ['request' ]['path' ], '/bogus' )
266
+ self .assertEqual (json_record ['request' ]['method' ], 'BOGUS' )
0 commit comments