-
Notifications
You must be signed in to change notification settings - Fork 1
/
tests.py
139 lines (105 loc) · 3.67 KB
/
tests.py
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
import datetime
import decimal
try:
import unittest2 as unittest
except ImportError:
import unittest
if not hasattr(unittest, 'expectedFailure'):
import functools
def _expectedFailure(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
try:
func(*args, **kwargs)
except AssertionError:
pass
else:
raise AssertionError("UnexpectedSuccess")
return wrapper
unittest.expectedFailure = _expectedFailure
class JS_OBJ_ATTR(object):
@property
def __json__(self):
return {'a': 'b',
'c': [1, 2, 3]}
class JS_OBJ(object):
def __json__(self):
return {'a': 'b',
'c': [1, 2, 3]}
ndt = datetime.datetime.now()
nd = ndt.date()
t = ndt.time()
dec = decimal.Decimal('1.231230018234413213141241421')
dec2 = decimal.Decimal('2.2')
test_cases = [
# test, orgvalue, json value
('testint', 1, '1'),
('testlist', [1, 2, 3], '[1, 2, 3]'),
('mixedlist', ['a', {'a':'b'}, 3], '["a", {"a": "b"}, 3]'),
('datetime', ndt, '"%s"' % ndt.isoformat()[:-3]),
('datetime.date', nd, '"%s"' % nd.isoformat()),
('datetime.time', t, '"%s"' % t.isoformat()[:-3]),
('set', set([1, 3, 2]), '[1, 2, 3]'),
('decimal', dec, '"%s"' % dec),
('decimal', dec2, '"%s"' % dec2),
('float', 1.234, '%s' % 1.234),
('__json__', JS_OBJ(), '{"a": "b", "c": [1, 2, 3]}'),
('__json__attr', JS_OBJ_ATTR(), '{"a": "b", "c": [1, 2, 3]}'),
]
def _make_dump_test(name, orgval, jsonval):
def test_serialize(self):
retjsonval = self.json.dumps(orgval)
self.assertEqual(jsonval, retjsonval)
return test_serialize
#def _make_load_test():
# def test_serialize(self):
# retjsonval = self.json.dumps(orgval)
# loadedjsonval = self.json.loads(retjsonval)
# self.assertEqual(orgval, loadedjsonval)
# return test_serialize
class TestJSONEncoder(unittest.TestCase):
def setUp(self):
from ext_json import stdlibjson
self.json = stdlibjson
class TestSIMPLEJSONEncoder(unittest.TestCase):
def setUp(self):
from ext_json import simplejson
self.json = simplejson
class Promise(object):
def __init__(self, title):
self.title = title
def __repr__(self):
return 'ImPromise'
class TestOverrideEncoder(unittest.TestCase):
def test_override_json(self):
import simplejson
class LazyEncoder(simplejson.JSONEncoder):
"""Encodes django's lazy i18n strings.
"""
def default(self, obj):
if isinstance(obj, Promise):
return unicode(obj)
return obj
result = simplejson.dumps({
"html": '<span></span>',
"message": Promise(u"Data has been saved."),
}, cls=LazyEncoder)
assert result == '{"message": "ImPromise", "html": "<span></span>"}'
def test_override_simplejson(self):
import json
def default_enc(obj):
if isinstance(obj, Promise):
return unicode(obj)
return obj
result = json.dumps({
"html": '<span></span>',
"message": Promise(u"Data has been saved."),
}, default=default_enc)
assert result == '{"message": "ImPromise", "html": "<span></span>"}'
for name, orgval, jsonval in test_cases:
setattr(TestJSONEncoder, "test_JSON_%s" % name,
_make_dump_test(name, orgval, jsonval))
setattr(TestSIMPLEJSONEncoder, "test_SIMPLEJSON_%s" % name,
_make_dump_test(name, orgval, jsonval))
if __name__ == '__main__':
unittest.main()