-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_objects.py
531 lines (450 loc) · 15.3 KB
/
test_objects.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
import pytest
import RestResponse
import requests
from datetime import datetime, date
from decimal import Decimal
import json
from tests.models import DBModel, Model, Ref, OverridesModel
def test_none_prop():
obj = RestResponse.parse({})
assert not obj.missing_prop
obj.none_prop['test'] = 'this'
assert obj.none_prop.test == 'this'
assert isinstance(obj.prop, RestResponse.NoneProp)
assert 'test' not in obj.missing_prop
assert [x for x in obj.missing_prop] == []
for none in obj.missing_prop:
raise AssertionError('NoneProp should have nothing to yield')
def test_rest_obj(binary):
obj = RestResponse.parse({})
assert isinstance(obj, RestResponse.RestObject)
obj.id = 1
assert 'id' in obj
assert 'id' in obj.keys()
assert len(obj) == 1
assert obj.pop('id') == 1
assert obj.pop('foo') is None
assert obj.pop('foo', 'bar') == 'bar'
assert len(obj) == 0
obj.id = 1
assert obj.popitem() == ('id', 1)
assert len(obj) == 0
obj.nested.prop = None
assert isinstance(obj.nested, RestResponse.RestObject)
assert 'prop' in obj.nested
assert 'prop' in obj.nested.keys()
obj.update(RestResponse.parse({
'test': 'this',
'callable': lambda x: x + 1,
'binary': binary
}))
assert obj.test == 'this'
assert obj.callable(1) == 2
assert not RestResponse.utils.istext(obj.binary)
obj.restobj = RestResponse.parse({
'test': 'this',
'callable': lambda x: x + 1
})
assert obj.restobj.test == 'this'
assert obj.restobj.callable(1) == 2
obj.none_prop = obj.missing_prop
assert obj().get('none_prop') is None
assert obj['test'] == 'this'
assert isinstance(obj['missing_key'], RestResponse.NoneProp)
obj['key'] = obj.restobj
assert obj['key'].test == 'this' == obj.key.test == obj['key']['test']
assert obj['key'].callable(1) == 2
obj['new_key'].some_prop = [1]
assert len(obj.new_key.some_prop) == 1
def test_callable():
base = RestResponse.parse({
'trigger': lambda x: x + 1
})
t = RestResponse.parse(base())
assert t.trigger(1) == 2
t = RestResponse.loads(base.pretty_print())
assert t.trigger(1) == 2
def test_binary(binary):
base = RestResponse.parse({
'binary': binary
})
t = RestResponse.parse(base())
assert t.binary == binary
t = RestResponse.loads(base.pretty_print())
assert t.binary == binary
def test_rest_list(binary):
lst = RestResponse.parse([])
assert isinstance(lst, RestResponse.RestList)
lst.append('item')
assert 'item' in lst
assert len(lst) == 1
assert lst.pop() == 'item'
lst.append(u'\U0001f44d')
assert lst.pop(0) == u'\U0001f44d'
assert len(lst) == 0
lst.insert(0, u'\U0001f44d')
lst.append(b'byte string')
assert u'\U0001f44d' in lst
assert b'byte string' in lst
assert lst.pop() == b'byte string'
assert lst.pop() == u'\U0001f44d'
lst.append(RestResponse.parse({
'test': 'RestObj',
'callable': lambda x: x + 1,
'binary': binary,
'unicode': u'\U0001f44d',
'bytes': b'byte string'
}))
assert lst[-1].test == 'RestObj'
assert lst[-1].callable(1) == 2
assert lst[-1].unicode == u'\U0001f44d'
assert lst[-1].bytes == b'byte string'
assert not RestResponse.utils.istext(lst[-1].binary)
lst.append(RestResponse.parse([lst[-1]]))
assert isinstance(lst[-1], RestResponse.RestList)
assert isinstance(lst[-1][-1], RestResponse.RestObject)
lst.insert(0, RestResponse.parse({
'test': 'RestObj',
'callable': lambda x: x + 1,
'binary': binary,
'unicode': u'\U0001f44d'
}))
assert lst[0].test == 'RestObj'
assert lst[0].callable(1) == 2
assert lst[0].unicode == u'\U0001f44d'
assert not RestResponse.utils.istext(lst[0].binary)
lst.append(lst[0].none_prop)
assert lst()[-1] is not None
lst[0] = {
'id': 1,
'foo': 'bar'
}
assert isinstance(lst[0], RestResponse.RestObject)
with pytest.raises(IndexError):
lst[100] = 'foo'
data = {
'lst': ['test', 'encodedness']
}
obj = RestResponse.parse(data)
assert obj.lst == data['lst']
lst = RestResponse.parse(['foo'])
lst[-1] = ['foo', {'foo': 'bar'}]
assert isinstance(lst[-1], RestResponse.RestList)
assert isinstance(lst[-1][-1], RestResponse.RestObject)
def test_pretty_print(requests_mock, user_text):
requests_mock.get('http://jsonplaceholder.typicode.com/users/1', text=user_text)
r = requests.get('http://jsonplaceholder.typicode.com/users/1')
user = RestResponse.parse(r.json())
assert len(user.pretty_print().split('\n')) == len(r.text.split('\n'))
def test_encode_on_request(binary, requests_mock, user_text):
requests_mock.get('http://jsonplaceholder.typicode.com/users/1', text=user_text)
r = requests.get('http://jsonplaceholder.typicode.com/users/1')
user = RestResponse.parse(r.json())
user.update({
'name': 'Test New Name',
'new_field': 'Test New Field',
'binary': binary,
'callable': lambda x: x + 1,
'unicode': u'\U0001f44d'
})
requests_mock.put('http://jsonplaceholder.typicode.com/users/{0}'.format(user.id), text=repr(user))
r = requests.put('http://jsonplaceholder.typicode.com/users/{0}'.format(user.id), json=user)
assert r.ok
r = requests.put('http://jsonplaceholder.typicode.com/users/{0}'.format(user.id), json=user())
assert r.ok
user = RestResponse.parse(r.json())
assert user.name == 'Test New Name'
assert user.new_field == 'Test New Field'
user = RestResponse.loads(r.text)
assert user.name == 'Test New Name'
assert user.new_field == 'Test New Field'
assert user.unicode == u'\U0001f44d'
for key in user:
assert key in user.keys()
user.clear()
assert len(user) == 0
def test_supported_encoder_types(binary):
d1 = datetime.utcnow()
d2 = d1.date()
decimal = Decimal('3.1459')
data = RestResponse.parse({
'datetime': d1,
'date': d2,
'decimal': decimal,
'binary': binary,
'callable': lambda x: x + 1,
'unicode': u'\U0001f44d',
'ascii_unicode': u'test'
})
assert data.datetime == d1
assert data.date == d2
assert isinstance(data.decimal, Decimal)
assert float(data.decimal) == float(Decimal('3.1459'))
assert data.binary == binary
assert data.callable(1) == 2
assert data.unicode == u'\U0001f44d'
assert data.ascii_unicode == u'test'
raw = json.loads(repr(data))
assert raw['binary'].startswith('__binary__: ')
assert RestResponse.utils._decode_binary(raw['binary']) == binary
assert raw['callable'].startswith('__callable__: ')
func = RestResponse.utils._decode_callable(raw['callable'])
assert func.__code__ == data.callable.__code__
assert func(1) == 2
assert raw['ascii_unicode'] == 'test'
def test_orm_sqlalchemy(db, db_model, binary):
d1 = datetime.utcnow()
d2 = d1.date()
decimal = Decimal('3.1459')
db_model.data = RestResponse.parse({
'datetime': d1,
'date': d2,
'decimal': decimal,
'binary': binary,
'callable': lambda x: x + 1,
'unicode': u'\U0001f44d',
'ascii_unicode': u'test'
})
db.session.add(db_model)
db.session.commit()
session = db.create_scoped_session(dict(expire_on_commit=False))
committed = session.query(DBModel).first()
assert committed.data.datetime == RestResponse.utils.encode_item(d1)
assert committed.data.date == RestResponse.utils.encode_item(d2)
assert committed.data.decimal == float(Decimal('3.1459'))
assert committed.data.binary == binary
assert committed.data.callable(1) == 2
assert committed.data.unicode == u'\U0001f44d'
assert committed.data.ascii_unicode == u'test'
db_model.data = {}
db.session.commit()
session = db.create_scoped_session(dict(expire_on_commit=False))
committed = session.query(DBModel).first()
assert isinstance(committed.data, RestResponse.RestObject)
db_model.data = []
db.session.commit()
session = db.create_scoped_session(dict(expire_on_commit=False))
committed = session.query(DBModel).first()
assert isinstance(committed.data, RestResponse.RestList)
db_model.data = None
db.session.commit()
session = db.create_scoped_session(dict(expire_on_commit=False))
committed = session.query(DBModel).first()
assert isinstance(committed.data, RestResponse.RestObject)
def test_api_model(binary):
model = OverridesModel({
'_foo': 'bar',
'foo': 'bar'
})
assert '_foo' in model._data
assert 'foo' not in model._data
assert model._foo == 'bar'
Model.__opts__.update({
'encode_binary': False,
'encode_callable': False,
'decode_binary': False,
'decode_callable': False,
'_overrides': ['_foo']
})
d = datetime.utcnow()
d2 = d.date()
model = Model({
'id': 5,
'string': 'foo',
'floating_point': float(4.0),
'date_time': d,
'date': d2,
'ref': {
'id': 5,
'string': 'string',
'foo': 'bar'
},
'int_collection': [1, 2, 3],
'ref_collection': [
{
'id': 1,
'string': 'string'
},
{
'id': 2,
'string': 'string'
}
],
'foo': 'bar',
'_foo': 'bar',
'_bar': 'foo'
})
model2 = Model({
'id': 5,
'string': 'foo',
'floating_point': float(4.0),
'date_time': d,
'date': d2,
'int_collection': [1, 2, 3],
'ref_collection': [
{
'id': 1,
'string': 'string'
},
{
'id': 2,
'string': 'string'
}
],
'foo': 'bar',
'_foo': 'bar',
'_bar': 'foo'
})
with pytest.raises(AssertionError):
assert model == model2
assert bool(model2.ref) is False
model2.ref = Ref({
'id': 5,
'string': 'string',
'foo': 'bar'
})
assert model == model2
as_dict = model._as_json
repr(as_dict)
assert as_dict['id'] == 5
assert as_dict['string'] == 'foo'
assert as_dict['floating_point'] == float(4.0)
assert as_dict['date_time'] == Model.__opts__['encode_datetime'](d)
assert as_dict['date'] == Model.__opts__['encode_date'](d2)
model = Model(as_dict)
assert 'foo' not in model._data
assert 'foo' not in model.ref._data
assert '_bar' not in model._data
assert '_foo' in model._data
assert model._foo == 'bar'
assert isinstance(model.id, int)
assert model.id == 5
assert isinstance(model.string, str)
assert model.string == 'foo'
assert isinstance(model.floating_point, float)
assert model.floating_point == float(4.0)
assert isinstance(model.date_time, datetime)
assert model.date_time == d.replace(microsecond=0)
assert isinstance(model.date, date)
assert model.date == d2
assert isinstance(model.ref, Ref)
assert model.ref.id == 5
assert model.ref.string == 'string'
assert isinstance(model.int_collection, list)
assert len(model.int_collection) == 3
for ref in model.ref_collection:
assert isinstance(ref, Ref)
model.id = 10
assert model.id == 10
model.string = 'bar'
assert model.string == 'bar'
model.floating_point = Decimal(5.3)
assert model.floating_point == float(5.3)
model.int_collection.append(1337)
assert 1337 in model.int_collection
model.ref_collection.append(Ref({
'id': 1337,
'string': 'foo'
}))
assert model.ref_collection[-1].id == 1337
assert model.ref_collection[-1].string == 'foo'
model.id = '5'
assert model.id == 5
with pytest.raises(ValueError):
model.id = 'test'
model.id_doesnt_raise = 'test'
assert model.id_doesnt_raise is None
with pytest.raises(ValueError):
model.string = 5
model.string_doesnt_raise = 5
assert model.string_doesnt_raise == '5'
with pytest.raises(ValueError):
model.floating_point = 'test'
model.floating_point_doesnt_raise = 'test'
assert model.floating_point_doesnt_raise is None
with pytest.raises(ValueError):
model.func = lambda x: x + 1
as_dict = model._as_json
model.binary = binary
with pytest.raises(ValueError):
as_dict = model._as_json
with pytest.raises(ValueError):
model.int_collection.append('test')
with pytest.raises(ValueError):
model.ref_collection.append('test')
Model.__opts__.update({
'encode_binary': True,
'encode_callable': True,
'decode_binary': True,
'decode_callable': True
})
model = Model({
'binary': binary,
'func': lambda x: x + 1
})
as_dict = model._as_json
assert isinstance(as_dict, dict)
assert RestResponse.utils._decode_binary(as_dict.get('binary')) == model.binary
func = RestResponse.utils._decode_callable(as_dict.get('func'))
assert func.__code__ == model.func.__code__
def test_api_collection():
models = RestResponse.ApiCollection(Model, [{
'id': 5,
'string': 'foo',
'floating_point': float(4.0),
'ref': {
'id': 5,
'string': 'string',
'foo': 'bar'
},
'int_collection': [1, 2, 3],
'int_collection_doesnt_raise': ['test'],
'ref_collection': [
{
'id': 1,
'string': 'string'
},
{
'id': 2,
'string': 'string'
}
],
'foo': 'bar'
} for x in range(3)])
with pytest.raises(ValueError):
models[0].int_collection.append('test')
assert len(models[0].int_collection_doesnt_raise) == 0
models[0].int_collection_doesnt_raise.append('test')
assert len(models[0].int_collection_doesnt_raise) == 0
models[0].int_collection_doesnt_raise.append(5)
assert models[0].int_collection_doesnt_raise[0] == 5
assert len(models) == 3
as_list = models._as_json
assert isinstance(as_list, list)
for m in models:
ref = Ref({
'id': datetime.utcnow().strftime('%s'),
'string': datetime.utcnow().isoformat()
})
m.ref_collection.append(ref)
assert m.ref_collection[-1].id == ref.id
assert m.ref_collection[-1].string == ref.string
def test_default():
obj = RestResponse.parse({
'callable': lambda x: x + 1
})
data = RestResponse.RestEncoder().default(obj)
assert data['callable'].startswith('__callable__')
assert RestResponse.utils.decode_item(data['callable'])(2) == 3
data = RestResponse.RestEncoderSimple().default(obj)
assert data['callable'].startswith('__callable__')
assert RestResponse.utils.decode_item(data['callable'])(2) == 3
obj = RestResponse.parse([
lambda x: x + 1
])
data = RestResponse.RestEncoder().default(obj)
assert data[0].startswith('__callable__')
assert RestResponse.utils.decode_item(data[0])(2) == 3
data = RestResponse.RestEncoderSimple().default(obj)
assert data[0].startswith('__callable__')
assert RestResponse.utils.decode_item(data[0])(2) == 3