Show More
@@ -1,104 +1,133 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 json |
|
15 | import json | |
15 | from base64 import decodestring |
|
16 | from base64 import decodestring | |
16 |
|
17 | |||
17 | # third party |
|
18 | # third party | |
18 | import nose.tools as nt |
|
19 | 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 | |||
25 | #----------------------------------------------------------------------------- |
|
27 | #----------------------------------------------------------------------------- | |
26 | # Test functions |
|
28 | # Test functions | |
27 | #----------------------------------------------------------------------------- |
|
29 | #----------------------------------------------------------------------------- | |
28 |
|
30 | |||
29 | def test(): |
|
31 | def test(): | |
30 | # list of input/expected output. Use None for the expected output if it |
|
32 | # list of input/expected output. Use None for the expected output if it | |
31 | # can be the same as the input. |
|
33 | # can be the same as the input. | |
32 | pairs = [(1, None), # start with scalars |
|
34 | pairs = [(1, None), # start with scalars | |
33 | (1.0, None), |
|
35 | (1.0, None), | |
34 | ('a', None), |
|
36 | ('a', None), | |
35 | (True, None), |
|
37 | (True, None), | |
36 | (False, None), |
|
38 | (False, None), | |
37 | (None, None), |
|
39 | (None, None), | |
38 | # complex numbers for now just go to strings, as otherwise they |
|
40 | # complex numbers for now just go to strings, as otherwise they | |
39 | # are unserializable |
|
41 | # are unserializable | |
40 | (1j, '1j'), |
|
42 | (1j, '1j'), | |
41 | # Containers |
|
43 | # Containers | |
42 | ([1, 2], None), |
|
44 | ([1, 2], None), | |
43 | ((1, 2), [1, 2]), |
|
45 | ((1, 2), [1, 2]), | |
44 | (set([1, 2]), [1, 2]), |
|
46 | (set([1, 2]), [1, 2]), | |
45 | (dict(x=1), None), |
|
47 | (dict(x=1), None), | |
46 | ({'x': 1, 'y':[1,2,3], '1':'int'}, None), |
|
48 | ({'x': 1, 'y':[1,2,3], '1':'int'}, None), | |
47 | # More exotic objects |
|
49 | # More exotic objects | |
48 | ((x for x in range(3)), [0, 1, 2]), |
|
50 | ((x for x in range(3)), [0, 1, 2]), | |
49 | (iter([1, 2]), [1, 2]), |
|
51 | (iter([1, 2]), [1, 2]), | |
50 | ] |
|
52 | ] | |
51 |
|
53 | |||
52 | for val, jval in pairs: |
|
54 | for val, jval in pairs: | |
53 | if jval is None: |
|
55 | if jval is None: | |
54 | jval = val |
|
56 | jval = val | |
55 | out = json_clean(val) |
|
57 | out = json_clean(val) | |
56 | # validate our cleanup |
|
58 | # validate our cleanup | |
57 | nt.assert_equal(out, jval) |
|
59 | nt.assert_equal(out, jval) | |
58 | # and ensure that what we return, indeed encodes cleanly |
|
60 | # and ensure that what we return, indeed encodes cleanly | |
59 | json.loads(json.dumps(out)) |
|
61 | json.loads(json.dumps(out)) | |
60 |
|
62 | |||
61 |
|
63 | |||
62 |
|
64 | |||
63 | @dec.parametric |
|
65 | @dec.parametric | |
64 | def test_encode_images(): |
|
66 | def test_encode_images(): | |
65 | # invalid data, but the header and footer are from real files |
|
67 | # invalid data, but the header and footer are from real files | |
66 | pngdata = b'\x89PNG\r\n\x1a\nblahblahnotactuallyvalidIEND\xaeB`\x82' |
|
68 | pngdata = b'\x89PNG\r\n\x1a\nblahblahnotactuallyvalidIEND\xaeB`\x82' | |
67 | jpegdata = b'\xff\xd8\xff\xe0\x00\x10JFIFblahblahjpeg(\xa0\x0f\xff\xd9' |
|
69 | jpegdata = b'\xff\xd8\xff\xe0\x00\x10JFIFblahblahjpeg(\xa0\x0f\xff\xd9' | |
68 |
|
70 | |||
69 | fmt = { |
|
71 | fmt = { | |
70 | 'image/png' : pngdata, |
|
72 | 'image/png' : pngdata, | |
71 | 'image/jpeg' : jpegdata, |
|
73 | 'image/jpeg' : jpegdata, | |
72 | } |
|
74 | } | |
73 | encoded = encode_images(fmt) |
|
75 | encoded = encode_images(fmt) | |
74 | for key, value in fmt.iteritems(): |
|
76 | for key, value in fmt.iteritems(): | |
75 | # encoded has unicode, want bytes |
|
77 | # encoded has unicode, want bytes | |
76 | decoded = decodestring(encoded[key].encode('ascii')) |
|
78 | decoded = decodestring(encoded[key].encode('ascii')) | |
77 | yield nt.assert_equal(decoded, value) |
|
79 | yield nt.assert_equal(decoded, value) | |
78 | encoded2 = encode_images(encoded) |
|
80 | encoded2 = encode_images(encoded) | |
79 | yield nt.assert_equal(encoded, encoded2) |
|
81 | yield nt.assert_equal(encoded, encoded2) | |
80 |
|
82 | |||
81 | b64_str = {} |
|
83 | b64_str = {} | |
82 | for key, encoded in encoded.iteritems(): |
|
84 | for key, encoded in encoded.iteritems(): | |
83 | b64_str[key] = unicode_to_str(encoded) |
|
85 | b64_str[key] = unicode_to_str(encoded) | |
84 | encoded3 = encode_images(b64_str) |
|
86 | encoded3 = encode_images(b64_str) | |
85 | yield nt.assert_equal(encoded3, b64_str) |
|
87 | yield nt.assert_equal(encoded3, b64_str) | |
86 | for key, value in fmt.iteritems(): |
|
88 | for key, value in fmt.iteritems(): | |
87 | # encoded3 has str, want bytes |
|
89 | # encoded3 has str, want bytes | |
88 | decoded = decodestring(str_to_bytes(encoded3[key])) |
|
90 | decoded = decodestring(str_to_bytes(encoded3[key])) | |
89 | yield nt.assert_equal(decoded, value) |
|
91 | yield nt.assert_equal(decoded, value) | |
90 |
|
92 | |||
91 | def test_lambda(): |
|
93 | def test_lambda(): | |
92 | jc = json_clean(lambda : 1) |
|
94 | jc = json_clean(lambda : 1) | |
93 | assert isinstance(jc, str) |
|
95 | assert isinstance(jc, str) | |
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'}, | |
100 | {True:'bool', 'True':'string'}, |
|
129 | {True:'bool', 'True':'string'}, | |
101 | ] |
|
130 | ] | |
102 | for d in bad_dicts: |
|
131 | for d in bad_dicts: | |
103 | nt.assert_raises(ValueError, json_clean, d) |
|
132 | nt.assert_raises(ValueError, json_clean, d) | |
104 |
|
133 |
General Comments 0
You need to be logged in to leave comments.
Login now