-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtests.py
105 lines (84 loc) · 4.07 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
from collections import OrderedDict
import datetime
import six
import ejson
import unittest
class EJSONTestCase(unittest.TestCase):
def test_escape_encode(self):
s = ejson.dumps({
"foo": "bar",
"bat": {
"$escape": {
"baz": "bazam"
}
}
}, sort_keys=True)
self.assertEqual('{"bat": {"$escape": {"$escape": {"baz": "bazam"}}}, "foo": "bar"}', s)
def test_escape_decode(self):
self.assertEqual({
"foo": "bar",
"bat": {
"$escape": {
"baz": "bazam"
}
}
}, ejson.loads('{"bat": {"$escape": {"$escape": {"baz": "bazam"}}}, "foo": "bar"}'))
def test_escape_escape(self):
self.assertEqual({
"$date": datetime.datetime(1970, 1, 1, 0, 0, 32, tzinfo=ejson.timezone.utc)
}, ejson.loads('{"$escape": {"$date": {"$date": 32000}}}'))
def test_date(self):
s1 = ejson.dumps(datetime.date(2015, 1, 25))
self.assertEqual('{"$date": 1422144000000}', s1)
self.assertEqual(datetime.datetime(2015, 1, 25, tzinfo=ejson.timezone.utc), ejson.loads(s1))
s2 = ejson.dumps(datetime.datetime(2015, 1, 25, 10, 30, 1, tzinfo=ejson.timezone.utc))
self.assertEqual('{"$date": 1422181801000}', s2)
self.assertEqual(datetime.datetime(2015, 1, 25, 10, 30, 1, tzinfo=ejson.timezone.utc), ejson.loads(s2))
def test_binary(self):
if six.PY3:
s = ejson.dumps([six.binary_type('foo', 'ascii'), 'foo'])
self.assertEqual('[{"$binary": "Zm9v"}, "foo"]', s)
self.assertEqual([six.binary_type('foo', 'ascii'), 'foo'], ejson.loads(s))
else:
s = ejson.dumps(['foo', six.text_type('foo')])
self.assertEqual('["foo", "foo"]', s)
self.assertEqual(['foo', 'foo'], ejson.loads('[{"$binary": "Zm9v"}, "foo"]'))
def test_custom_type_encode(self):
encoder = ejson.EJSONEncoder(custom_type_hooks=[
(set, 'set', sorted)
], sort_keys=True)
self.assertEqual('{"foo": {"$type": "set", "$value": [1, 2, 3]}}', encoder.encode({
"foo": {1, 2, 3}
}))
def test_unknown_type_decode(self):
with self.assertRaises(ejson.UnknownTypeError):
ejson.loads('{"foo": {"$type": "pow", "$value": 5}}')
# TODO better error message for UnknownTypeError
def test_custom_type_decode(self):
decoder = ejson.EJSONDecoder(custom_type_hooks=[
('pow', lambda v: v ** 2),
('set', set)
])
self.assertEqual({
'foo': [25, 1.23],
'things': {1, 2, 4}
}, decoder.decode('{"foo": [{"$type": "pow", "$value": 5}, 1.23],'
' "things": {"$type": "set", "$value": [1, 1, 2, 4]}}'))
def test_orderd_dict_encode(self):
encoder = ejson.EJSONEncoder()
self.assertEqual('{"foo": 3, "bar": 1, "baz": {"$escape": {"$escape": true, "foo": "foo"}}}', encoder.encode(
OrderedDict([('foo', 3),
('bar', 1),
('baz', OrderedDict([
('$escape', True),
('foo', 'foo')
]))])))
def test_object_pairs_hook_decode(self):
decoder = ejson.EJSONDecoder(object_pairs_hook=OrderedDict)
obj = decoder.decode('{"foo": 1, "bar": {"$escape": {"$escape": true}}}')
self.assertEqual(OrderedDict([('foo', 1),
('bar', OrderedDict([('$escape', True)]))]), obj)
self.assertIsInstance(obj, OrderedDict)
self.assertIsInstance(obj['bar'], OrderedDict)
if __name__ == '__main__':
unittest.main()