Show More
@@ -1,149 +1,151 b'' | |||||
1 | """Test suite for our JSON utilities. |
|
1 | """Test suite for our JSON utilities. | |
2 | """ |
|
2 | """ | |
3 | #----------------------------------------------------------------------------- |
|
3 | #----------------------------------------------------------------------------- | |
4 | # Copyright (C) 2010-2011 The IPython Development Team |
|
4 | # Copyright (C) 2010-2011 The IPython Development Team | |
5 | # |
|
5 | # | |
6 | # Distributed under the terms of the BSD License. The full license is in |
|
6 | # Distributed under the terms of the BSD License. The full license is in | |
7 | # the file COPYING.txt, distributed as part of this software. |
|
7 | # the file COPYING.txt, distributed as part of this software. | |
8 | #----------------------------------------------------------------------------- |
|
8 | #----------------------------------------------------------------------------- | |
9 |
|
9 | |||
10 | #----------------------------------------------------------------------------- |
|
10 | #----------------------------------------------------------------------------- | |
11 | # Imports |
|
11 | # Imports | |
12 | #----------------------------------------------------------------------------- |
|
12 | #----------------------------------------------------------------------------- | |
13 | # stdlib |
|
13 | # stdlib | |
14 | import datetime |
|
14 | import datetime | |
15 | import json |
|
15 | import json | |
16 | from base64 import decodestring |
|
16 | from base64 import decodestring | |
17 |
|
17 | |||
18 | # third party |
|
18 | # third party | |
19 | import nose.tools as nt |
|
19 | import nose.tools as nt | |
20 |
|
20 | |||
21 | # our own |
|
21 | # our own | |
22 | from IPython.utils import jsonutil, tz |
|
22 | from IPython.utils import jsonutil, tz | |
23 | from ..jsonutil import json_clean, encode_images |
|
23 | from ..jsonutil import json_clean, encode_images | |
24 | from ..py3compat import unicode_to_str, str_to_bytes, iteritems |
|
24 | from ..py3compat import unicode_to_str, str_to_bytes, iteritems | |
25 |
|
25 | |||
26 | #----------------------------------------------------------------------------- |
|
26 | #----------------------------------------------------------------------------- | |
27 | # Test functions |
|
27 | # Test functions | |
28 | #----------------------------------------------------------------------------- |
|
28 | #----------------------------------------------------------------------------- | |
29 | class Int(int): |
|
29 | class Int(int): | |
30 | def __str__(self): |
|
30 | def __str__(self): | |
31 | return 'Int(%i)' % self |
|
31 | return 'Int(%i)' % self | |
32 |
|
32 | |||
33 | def test(): |
|
33 | def test(): | |
34 | # list of input/expected output. Use None for the expected output if it |
|
34 | # list of input/expected output. Use None for the expected output if it | |
35 | # can be the same as the input. |
|
35 | # can be the same as the input. | |
36 | pairs = [(1, None), # start with scalars |
|
36 | pairs = [(1, None), # start with scalars | |
37 | (1.0, None), |
|
37 | (1.0, None), | |
38 | ('a', None), |
|
38 | ('a', None), | |
39 | (True, None), |
|
39 | (True, None), | |
40 | (False, None), |
|
40 | (False, None), | |
41 | (None, None), |
|
41 | (None, None), | |
42 | # complex numbers for now just go to strings, as otherwise they |
|
42 | # complex numbers for now just go to strings, as otherwise they | |
43 | # are unserializable |
|
43 | # are unserializable | |
44 | (1j, '1j'), |
|
44 | (1j, '1j'), | |
45 | # Containers |
|
45 | # Containers | |
46 | ([1, 2], None), |
|
46 | ([1, 2], None), | |
47 | ((1, 2), [1, 2]), |
|
47 | ((1, 2), [1, 2]), | |
48 | (set([1, 2]), [1, 2]), |
|
48 | (set([1, 2]), [1, 2]), | |
49 | (dict(x=1), None), |
|
49 | (dict(x=1), None), | |
50 | ({'x': 1, 'y':[1,2,3], '1':'int'}, None), |
|
50 | ({'x': 1, 'y':[1,2,3], '1':'int'}, None), | |
51 | # More exotic objects |
|
51 | # More exotic objects | |
52 | ((x for x in range(3)), [0, 1, 2]), |
|
52 | ((x for x in range(3)), [0, 1, 2]), | |
53 | (iter([1, 2]), [1, 2]), |
|
53 | (iter([1, 2]), [1, 2]), | |
54 | (Int(5), 5), |
|
54 | (Int(5), 5), | |
55 | ] |
|
55 | ] | |
56 |
|
56 | |||
57 | for val, jval in pairs: |
|
57 | for val, jval in pairs: | |
58 | if jval is None: |
|
58 | if jval is None: | |
59 | jval = val |
|
59 | jval = val | |
60 | out = json_clean(val) |
|
60 | out = json_clean(val) | |
61 | # validate our cleanup |
|
61 | # validate our cleanup | |
62 | nt.assert_equal(out, jval) |
|
62 | nt.assert_equal(out, jval) | |
63 | # and ensure that what we return, indeed encodes cleanly |
|
63 | # and ensure that what we return, indeed encodes cleanly | |
64 | json.loads(json.dumps(out)) |
|
64 | json.loads(json.dumps(out)) | |
65 |
|
65 | |||
66 |
|
66 | |||
67 |
|
67 | |||
68 | def test_encode_images(): |
|
68 | def test_encode_images(): | |
69 | # invalid data, but the header and footer are from real files |
|
69 | # invalid data, but the header and footer are from real files | |
70 | pngdata = b'\x89PNG\r\n\x1a\nblahblahnotactuallyvalidIEND\xaeB`\x82' |
|
70 | pngdata = b'\x89PNG\r\n\x1a\nblahblahnotactuallyvalidIEND\xaeB`\x82' | |
71 | jpegdata = b'\xff\xd8\xff\xe0\x00\x10JFIFblahblahjpeg(\xa0\x0f\xff\xd9' |
|
71 | jpegdata = b'\xff\xd8\xff\xe0\x00\x10JFIFblahblahjpeg(\xa0\x0f\xff\xd9' | |
72 | pdfdata = b'%PDF-1.\ntrailer<</Root<</Pages<</Kids[<</MediaBox[0 0 3 3]>>]>>>>>>' |
|
72 | pdfdata = b'%PDF-1.\ntrailer<</Root<</Pages<</Kids[<</MediaBox[0 0 3 3]>>]>>>>>>' | |
73 |
|
73 | |||
74 | fmt = { |
|
74 | fmt = { | |
75 | 'image/png' : pngdata, |
|
75 | 'image/png' : pngdata, | |
76 | 'image/jpeg' : jpegdata, |
|
76 | 'image/jpeg' : jpegdata, | |
77 | 'application/pdf' : pdfdata |
|
77 | 'application/pdf' : pdfdata | |
78 | } |
|
78 | } | |
79 | encoded = encode_images(fmt) |
|
79 | encoded = encode_images(fmt) | |
80 | for key, value in iteritems(fmt): |
|
80 | for key, value in iteritems(fmt): | |
81 | # encoded has unicode, want bytes |
|
81 | # encoded has unicode, want bytes | |
82 | decoded = decodestring(encoded[key].encode('ascii')) |
|
82 | decoded = decodestring(encoded[key].encode('ascii')) | |
83 | nt.assert_equal(decoded, value) |
|
83 | nt.assert_equal(decoded, value) | |
84 | encoded2 = encode_images(encoded) |
|
84 | encoded2 = encode_images(encoded) | |
85 | nt.assert_equal(encoded, encoded2) |
|
85 | nt.assert_equal(encoded, encoded2) | |
86 |
|
86 | |||
87 | b64_str = {} |
|
87 | b64_str = {} | |
88 | for key, encoded in iteritems(encoded): |
|
88 | for key, encoded in iteritems(encoded): | |
89 | b64_str[key] = unicode_to_str(encoded) |
|
89 | b64_str[key] = unicode_to_str(encoded) | |
90 | encoded3 = encode_images(b64_str) |
|
90 | encoded3 = encode_images(b64_str) | |
91 | nt.assert_equal(encoded3, b64_str) |
|
91 | nt.assert_equal(encoded3, b64_str) | |
92 | for key, value in iteritems(fmt): |
|
92 | for key, value in iteritems(fmt): | |
93 | # encoded3 has str, want bytes |
|
93 | # encoded3 has str, want bytes | |
94 | decoded = decodestring(str_to_bytes(encoded3[key])) |
|
94 | decoded = decodestring(str_to_bytes(encoded3[key])) | |
95 | nt.assert_equal(decoded, value) |
|
95 | nt.assert_equal(decoded, value) | |
96 |
|
96 | |||
97 | def test_lambda(): |
|
97 | def test_lambda(): | |
98 | jc = json_clean(lambda : 1) |
|
98 | jc = json_clean(lambda : 1) | |
99 |
assert |
|
99 | nt.assert_is_instance(jc, str) | |
100 |
assert |
|
100 | nt.assert_in('<lambda>', jc) | |
101 | json.dumps(jc) |
|
101 | json.dumps(jc) | |
102 |
|
102 | |||
103 | def test_extract_dates(): |
|
103 | def test_extract_dates(): | |
104 | timestamps = [ |
|
104 | timestamps = [ | |
105 | '2013-07-03T16:34:52.249482', |
|
105 | '2013-07-03T16:34:52.249482', | |
106 | '2013-07-03T16:34:52.249482Z', |
|
106 | '2013-07-03T16:34:52.249482Z', | |
107 | '2013-07-03T16:34:52.249482Z-0800', |
|
107 | '2013-07-03T16:34:52.249482Z-0800', | |
108 | '2013-07-03T16:34:52.249482Z+0800', |
|
108 | '2013-07-03T16:34:52.249482Z+0800', | |
109 | '2013-07-03T16:34:52.249482Z+08:00', |
|
109 | '2013-07-03T16:34:52.249482Z+08:00', | |
110 | '2013-07-03T16:34:52.249482Z-08:00', |
|
110 | '2013-07-03T16:34:52.249482Z-08:00', | |
111 | '2013-07-03T16:34:52.249482-0800', |
|
111 | '2013-07-03T16:34:52.249482-0800', | |
112 | '2013-07-03T16:34:52.249482+0800', |
|
112 | '2013-07-03T16:34:52.249482+0800', | |
113 | '2013-07-03T16:34:52.249482+08:00', |
|
113 | '2013-07-03T16:34:52.249482+08:00', | |
114 | '2013-07-03T16:34:52.249482-08:00', |
|
114 | '2013-07-03T16:34:52.249482-08:00', | |
115 | ] |
|
115 | ] | |
116 | extracted = jsonutil.extract_dates(timestamps) |
|
116 | extracted = jsonutil.extract_dates(timestamps) | |
117 | ref = extracted[0] |
|
117 | ref = extracted[0] | |
118 | for dt in extracted: |
|
118 | for dt in extracted: | |
119 | nt.assert_true(isinstance(dt, datetime.datetime)) |
|
119 | nt.assert_true(isinstance(dt, datetime.datetime)) | |
120 | nt.assert_equal(dt, ref) |
|
120 | nt.assert_equal(dt, ref) | |
121 |
|
121 | |||
122 | def test_parse_ms_precision(): |
|
122 | def test_parse_ms_precision(): | |
123 |
base = '2013-07-03T16:34:52 |
|
123 | base = '2013-07-03T16:34:52' | |
124 | digits = '1234567890' |
|
124 | digits = '1234567890' | |
125 |
|
125 | |||
|
126 | parsed = jsonutil.parse_date(base) | |||
|
127 | nt.assert_is_instance(parsed, datetime.datetime) | |||
126 | for i in range(len(digits)): |
|
128 | for i in range(len(digits)): | |
127 | ts = base + digits[:i] |
|
129 | ts = base + '.' + digits[:i] | |
128 | parsed = jsonutil.parse_date(ts) |
|
130 | parsed = jsonutil.parse_date(ts) | |
129 | if i >= 1 and i <= 6: |
|
131 | if i >= 1 and i <= 6: | |
130 |
assert |
|
132 | nt.assert_is_instance(parsed, datetime.datetime) | |
131 | else: |
|
133 | else: | |
132 |
assert |
|
134 | nt.assert_is_instance(parsed, str) | |
133 |
|
135 | |||
134 | def test_date_default(): |
|
136 | def test_date_default(): | |
135 | data = dict(today=datetime.datetime.now(), utcnow=tz.utcnow()) |
|
137 | data = dict(today=datetime.datetime.now(), utcnow=tz.utcnow()) | |
136 | jsondata = json.dumps(data, default=jsonutil.date_default) |
|
138 | jsondata = json.dumps(data, default=jsonutil.date_default) | |
137 | nt.assert_in("+00", jsondata) |
|
139 | nt.assert_in("+00", jsondata) | |
138 | nt.assert_equal(jsondata.count("+00"), 1) |
|
140 | nt.assert_equal(jsondata.count("+00"), 1) | |
139 | extracted = jsonutil.extract_dates(json.loads(jsondata)) |
|
141 | extracted = jsonutil.extract_dates(json.loads(jsondata)) | |
140 | for dt in extracted.values(): |
|
142 | for dt in extracted.values(): | |
141 |
nt.assert_ |
|
143 | nt.assert_is_instance(dt, datetime.datetime) | |
142 |
|
144 | |||
143 | def test_exception(): |
|
145 | def test_exception(): | |
144 | bad_dicts = [{1:'number', '1':'string'}, |
|
146 | bad_dicts = [{1:'number', '1':'string'}, | |
145 | {True:'bool', 'True':'string'}, |
|
147 | {True:'bool', 'True':'string'}, | |
146 | ] |
|
148 | ] | |
147 | for d in bad_dicts: |
|
149 | for d in bad_dicts: | |
148 | nt.assert_raises(ValueError, json_clean, d) |
|
150 | nt.assert_raises(ValueError, json_clean, d) | |
149 |
|
151 |
General Comments 0
You need to be logged in to leave comments.
Login now