-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
292 lines (221 loc) · 10.6 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import unittest
import json
from json2py.models import TextField
from json2py.models import IntegerField
from json2py.models import FloatField
from json2py.models import NestedField
from json2py.models import ListField
from json2py.models import BooleanField
from json2py.models import ParseException
from json2py.models import InvalidAttribute
from json2py.models import DateField
from datetime import datetime
__author__ = 'Victor'
class NestedObjTest(NestedField):
id = IntegerField()
key = IntegerField(name = 'clave')
valor = TextField(name = 'value')
class ListObjTest(ListField):
__model__ = NestedObjTest
class ForbiddenNameTest(NestedField):
name = TextField()
class ForbiddenValueTest(NestedField):
value = TextField()
class ForbiddenRequiredTest(NestedField):
required = TextField()
class RequiredTest(unittest.TestCase):
def test_required(self):
class Req(NestedField):
req = IntegerField(required = True)
self.assertRaises(LookupError, Req.__init__, Req(), {})
def test_norequired(self):
class NoReq(NestedField):
no_req = IntegerField(required = False)
self.assertEqual(NoReq({}).no_req.value, None)
self.assertEqual(NoReq({"no_req": 10}).no_req.value, 10)
class BooleanTest(unittest.TestCase):
def test_value(self):
self.assertEqual(BooleanField(None).value, None)
self.assertEqual(BooleanField(True).value, True)
self.assertEqual(BooleanField(False).value, False)
self.assertRaises(ParseException, BooleanField.__init__, BooleanField(), 5)
def test_encode(self):
self.assertEqual(BooleanField(True).json_encode(), 'true')
self.assertEqual(BooleanField(False).json_encode(), 'false')
class NoneTest(unittest.TestCase):
def test_value(self):
self.assertEqual(TextField(None).value, None)
self.assertEqual(IntegerField(None).value, None)
self.assertEqual(FloatField(None).value, None)
self.assertEqual(NestedField(None).value, {})
self.assertEqual(len(ListObjTest(None)), 0)
def test_encode(self):
self.assertEqual(TextField(None).json_encode(), "null")
self.assertEqual(IntegerField(None).json_encode(), "null")
self.assertEqual(FloatField(None).json_encode(), "null")
self.assertEqual(BooleanField(None).json_encode(), "null")
self.assertEqual(NestedField(None).json_encode(), "{}")
self.assertEqual(ListObjTest(None).json_encode(), "[]")
def test_decode(self):
t = TextField(None)
t.json_decode("null")
self.assertEqual(t.value, None)
t = IntegerField(None)
t.json_decode("null")
self.assertEqual(t.value, None)
t = FloatField(None)
t.json_decode("null")
self.assertEqual(t.value, None)
t = BooleanField(None)
t.json_decode("null")
self.assertEqual(t.value, None)
t = NestedField(None)
t.json_decode("null")
self.assertEqual(t.value, {})
t = ListObjTest(None)
t.json_decode("null")
self.assertEqual(t.value, [])
class TextTest(unittest.TestCase):
def test_init(self):
self.assertEqual(TextField('''"aBc"''').value, '''"aBc"''')
self.assertEqual(TextField("5").value, "5")
self.assertRaises(ParseException, TextField.__init__, TextField(), 5)
# with self.assertRaises(ParseException):
# TextField(5)
def test_encode(self):
self.assertEqual(TextField("aBc").json_encode(), '"aBc"')
self.assertEqual(TextField("5").json_encode(), '"5"')
self.assertEqual(TextField(None).json_encode(), 'null')
class IntegerTest(unittest.TestCase):
def test_init(self):
self.assertEqual(IntegerField(10).value, 10)
# self.assertEqual(IntegerField(10L).value, 10)
self.assertRaises(ParseException, IntegerField.__init__, IntegerField(), '5')
# with self.assertRaises(ParseException):
# IntegerField('5')
self.assertRaises(ParseException, IntegerField.__init__, IntegerField(), 10.0)
# with self.assertRaises(ParseException):
# IntegerField(10.0)
def test_encode(self):
self.assertEqual(IntegerField(10).json_encode(), '10')
# self.assertEqual(IntegerField(10L).json_encode(), '10')
class FloatTest(unittest.TestCase):
def test_init(self):
self.assertEqual(FloatField(10).value, float(10))
self.assertEqual(FloatField(10.0).value, float(10))
# self.assertEqual(FloatField(10L).value, float(10))
self.assertEqual(FloatField(10.5).value, 10.5)
self.assertRaises(ParseException, FloatField.__init__, FloatField(), '10')
# with self.assertRaises(ParseException):
# FloatField('10')
def test_encode(self):
self.assertEqual(FloatField(10.5).json_encode(), '10.5')
# self.assertEqual(FloatField(10L).json_encode(), '10')
class NestedTest(unittest.TestCase):
def __init__(self, *args, **kwargs):
super(NestedTest, self).__init__(*args, **kwargs)
self.testObj = NestedObjTest({'id': 1234, 'clave': 1, 'value': 'aValue'})
def test_init(self):
self.assertEqual(self.testObj.id.value, 1234)
self.assertEqual(self.testObj.key.value, 1) # clave field
self.assertEqual(self.testObj.valor.value, 'aValue')
self.assertRaises(ParseException, NestedField.__init__, NestedField(), 10)
# with self.assertRaises(ParseException):
# NestedField(10)
self.assertRaises(ParseException, NestedField.__init__, NestedField(), 10.5)
# with self.assertRaises(ParseException):
# NestedField(10.5)
self.assertRaises(ParseException, NestedField.__init__, NestedField(), "aString")
# with self.assertRaises(ParseException):
# NestedField("aString")
self.assertRaises(ParseException, NestedField.__init__, NestedField(), [])
# with self.assertRaises(ParseException):
# NestedField([])
self.assertRaises(AttributeError, NestedField.__getattr__, self.testObj, 'unknownAttr')
# with self.assertRaises(AttributeError):
# self.testObj.unknownAttr
self.assertRaises(AttributeError, NestedField.__getattr__, NestedField({}), 'unknownAttr')
# with self.assertRaises(AttributeError):
# NestedField({}).unknownAttr
def test_encode(self):
self.assertEqual(NestedField({}).json_encode(), '{}')
self.assertEqual(json.loads(self.testObj.json_encode()), json.loads('{"id": 1234, "clave": 1, "value": "aValue"}'))
def test_forbidden(self):
## Tricky test
self.assertRaises(InvalidAttribute, ForbiddenNameTest.__new__, ForbiddenNameTest)
self.assertRaises(InvalidAttribute, ForbiddenValueTest.__new__, ForbiddenValueTest)
self.assertRaises(InvalidAttribute, ForbiddenRequiredTest.__new__, ForbiddenRequiredTest)
class ListTest(unittest.TestCase):
def __init__(self, *args, **kwargs):
super(ListTest, self).__init__(*args, **kwargs)
self.testObj = ListObjTest([
{'id': 1234, 'clave': 1, 'value': 'aValue'},
{'id': 4321, 'clave': 2, 'value': 'anotherValue'}
])
def test_init(self):
self.assertEqual(self.testObj[0].id.value, 1234)
self.assertEqual(self.testObj[1].id.value, 4321)
self.assertEqual(self.testObj[0].key.value, 1) # clave field
self.assertEqual(self.testObj[1].key.value, 2) # clave field
self.assertEqual(self.testObj[0].valor.value, 'aValue')
self.assertEqual(self.testObj[1].valor.value, 'anotherValue')
self.assertRaises(ParseException, ListField.__init__, ListObjTest(), 10)
# with self.assertRaises(ParseException):
# ListObjTest(10)
self.assertRaises(ParseException, ListField.__init__, ListObjTest(), 10.5)
# with self.assertRaises(ParseException):
# ListObjTest(10.5)
self.assertRaises(ParseException, ListField.__init__, ListObjTest(), "aString")
# with self.assertRaises(ParseException):
# ListObjTest("aString")
self.assertRaises(ParseException, ListField.__init__, ListObjTest(), {})
# with self.assertRaises(ParseException):
# ListObjTest({})
self.assertRaises(IndexError, ListField.__getitem__, self.testObj, 2)
# with self.assertRaises(IndexError):
# self.testObj[2]
self.assertRaises(IndexError, ListField.__getitem__, ListObjTest([]), 0)
# with self.assertRaises(IndexError):
# ListObjTest([])[0]
def test_slice(self):
self.assertEqual(len(self.testObj), 2)
self.assertEqual(len(self.testObj[:1]), 1)
self.assertEqual(len(self.testObj[1:]), 1)
self.assertEqual(self.testObj[-1].id.value, 4321)
self.assertEqual(self.testObj[-1].key.value, 2) # clave field
self.assertEqual(self.testObj[-1].valor.value, 'anotherValue')
def test_encode(self):
self.assertEqual(ListObjTest([]).json_encode(), '[]')
self.assertEqual(json.loads(self.testObj.json_encode()), json.loads('[{"id": 1234, "clave": 1, "value": "aValue"},{"id": 4321, "clave": 2, "value": "anotherValue"}]'))
class DateTest(unittest.TestCase):
def test_init(self):
self.assertEqual(DateField("2000-01-02 03:04:05", formatting = "%Y-%m-%d %H:%M:%S").value, datetime(2000, 1, 2, 3, 4, 5))
self.assertEqual(DateField("2000-01-02T03:04:05Z").value, datetime(2000, 1, 2, 3, 4, 5))
self.assertEqual(DateField("2000-01-02 03:04:05", formatting = "auto").value, datetime(2000, 1, 2, 3, 4, 5))
self.assertEqual(DateField(1458854751, formatting = "timestamp").value, datetime(2016, 3, 24, 21, 25, 51))
self.assertRaises(
ParseException,
DateField.__init__,
DateField(),
[],
{"value": "anything", "formatting": "timestamp"}
)
self.assertRaises(
ParseException,
DateField.__init__,
DateField(),
12345
)
def test_encode(self):
self.assertEqual(DateField(None).json_encode(), 'null')
self.assertEqual(DateField(1458854751, formatting = "timestamp").json_encode(), '1458854751')
self.assertEqual(
DateField("2000-01-02 03:04:05", formatting = "auto").json_encode(),
'"2000-01-02T03:04:05Z"'
)
self.assertEqual(
DateField("2000-01-02 03:04:05", formatting = "%Y-%m-%d %H:%M:%S").json_encode(),
'"2000-01-02 03:04:05"'
)
if __name__ == '__main__':
unittest.main()