forked from dimagi/jsonobject
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_stringconversions.py
116 lines (96 loc) · 3.51 KB
/
test_stringconversions.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
from decimal import Decimal
import datetime
from jsonobject.exceptions import BadValueError
from jsonobject import JsonObject, ObjectProperty, DateTimeProperty
import unittest2
from jsonobject.base import get_settings
class StringConversionsTest(unittest2.TestCase):
EXAMPLES = {
'decimal': '1.2',
'date': '2014-02-04',
'datetime': '2014-01-03T01:02:03Z',
'dict': {
'decimal': '1.4',
},
'list': ['1.0', '2000-01-01'],
}
EXAMPLES_CONVERTED = {
'decimal': Decimal('1.2'),
'date': datetime.date(2014, 2, 4),
'dict': {
'decimal': Decimal('1.4'),
},
'list': [Decimal('1.0'), datetime.date(2000, 1, 1)],
'datetime': datetime.datetime(2014, 1, 3, 1, 2, 3)
}
def test_default_conversions(self):
class Foo(JsonObject):
pass
foo = Foo.wrap(self.EXAMPLES)
for key, value in self.EXAMPLES_CONVERTED.items():
self.assertEqual(getattr(foo, key), value)
def test_no_conversions(self):
class Foo(JsonObject):
class Meta(object):
string_conversions = ()
foo = Foo.wrap(self.EXAMPLES)
for key, value in self.EXAMPLES.items():
self.assertEqual(getattr(foo, key), value)
def test_nested_1(self):
class Bar(JsonObject):
# default string conversions
pass
class Foo(JsonObject):
bar = ObjectProperty(Bar)
class Meta(object):
string_conversions = ()
foo = Foo.wrap({
# don't convert
'decimal': '1.0',
# do convert
'bar': {'decimal': '2.4'}
})
self.assertEqual(foo.decimal, '1.0')
self.assertNotEqual(foo.decimal, Decimal('1.0'))
self.assertEqual(foo.bar.decimal, Decimal('2.4'))
def test_nested_2(self):
class Bar(JsonObject):
class Meta(object):
string_conversions = ()
class Foo(JsonObject):
# default string conversions
bar = ObjectProperty(Bar)
foo = Foo.wrap({
# do convert
'decimal': '1.0',
# don't convert
'bar': {'decimal': '2.4'}
})
self.assertNotEqual(foo.decimal, '1.0')
self.assertEqual(foo.decimal, Decimal('1.0'))
self.assertEqual(foo.bar.decimal, '2.4')
def test_update_properties(self):
class Foo(JsonObject):
class Meta(object):
update_properties = {datetime.datetime: ExactDateTimeProperty}
self.assertEqual(
get_settings(Foo).type_config.properties[datetime.datetime],
ExactDateTimeProperty
)
with self.assertRaisesRegexp(BadValueError,
'is not a datetime-formatted string'):
Foo.wrap(self.EXAMPLES)
examples = self.EXAMPLES.copy()
examples['datetime'] = '2014-01-03T01:02:03.012345Z'
examples_converted = self.EXAMPLES_CONVERTED.copy()
examples_converted['datetime'] = datetime.datetime(
2014, 1, 3, 1, 2, 3, 12345)
foo = Foo.wrap(examples)
for key, value in examples_converted.items():
self.assertEqual(getattr(foo, key), value)
class ExactDateTimeProperty(DateTimeProperty):
def __init__(self, **kwargs):
if 'exact' in kwargs:
assert kwargs['exact'] is True
kwargs['exact'] = True
super(ExactDateTimeProperty, self).__init__(**kwargs)