##// END OF EJS Templates
test jsonutil handling of dates
MinRK -
Show More
@@ -11,6 +11,7 b''
11 # Imports
11 # Imports
12 #-----------------------------------------------------------------------------
12 #-----------------------------------------------------------------------------
13 # stdlib
13 # stdlib
14 import datetime
14 import json
15 import json
15 from base64 import decodestring
16 from base64 import decodestring
16
17
@@ -19,6 +20,7 b' import nose.tools as nt'
19
20
20 # our own
21 # our own
21 from IPython.testing import decorators as dec
22 from IPython.testing import decorators as dec
23 from IPython.utils import jsonutil, tz
22 from ..jsonutil import json_clean, encode_images
24 from ..jsonutil import json_clean, encode_images
23 from ..py3compat import unicode_to_str, str_to_bytes
25 from ..py3compat import unicode_to_str, str_to_bytes
24
26
@@ -94,6 +96,33 b' def test_lambda():'
94 assert '<lambda>' in jc
96 assert '<lambda>' in jc
95 json.dumps(jc)
97 json.dumps(jc)
96
98
99 def test_extract_dates():
100 timestamps = [
101 '2013-07-03T16:34:52.249482',
102 '2013-07-03T16:34:52.249482Z',
103 '2013-07-03T16:34:52.249482Z-0800',
104 '2013-07-03T16:34:52.249482Z+0800',
105 '2013-07-03T16:34:52.249482Z+08:00',
106 '2013-07-03T16:34:52.249482Z-08:00',
107 '2013-07-03T16:34:52.249482-0800',
108 '2013-07-03T16:34:52.249482+0800',
109 '2013-07-03T16:34:52.249482+08:00',
110 '2013-07-03T16:34:52.249482-08:00',
111 ]
112 extracted = jsonutil.extract_dates(timestamps)
113 ref = extracted[0]
114 for dt in extracted:
115 nt.assert_true(isinstance(dt, datetime.datetime))
116 nt.assert_equal(dt, ref)
117
118 def test_date_default():
119 data = dict(today=datetime.datetime.now(), utcnow=tz.utcnow())
120 jsondata = json.dumps(data, default=jsonutil.date_default)
121 nt.assert_in("+00", jsondata)
122 nt.assert_equal(jsondata.count("+00"), 1)
123 extracted = jsonutil.extract_dates(json.loads(jsondata))
124 for dt in extracted.values():
125 nt.assert_true(isinstance(dt, datetime.datetime))
97
126
98 def test_exception():
127 def test_exception():
99 bad_dicts = [{1:'number', '1':'string'},
128 bad_dicts = [{1:'number', '1':'string'},
General Comments 0
You need to be logged in to leave comments. Login now