/
test_attributes.py
950 lines (789 loc) · 29.1 KB
/
test_attributes.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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
"""
pynamodb attributes tests
"""
import json
import six
from base64 import b64encode
from datetime import datetime
from dateutil.tz import tzutc
from mock import patch, Mock, call
import pytest
from pynamodb.attributes import (
BinarySetAttribute, BinaryAttribute, NumberSetAttribute, NumberAttribute,
UnicodeAttribute, UnicodeSetAttribute, UTCDateTimeAttribute, BooleanAttribute,
MapAttribute, ListAttribute, JSONAttribute, _get_value_for_deserialize, _fast_parse_utc_datestring,
)
from pynamodb.constants import (
DATETIME_FORMAT, DEFAULT_ENCODING, NUMBER, STRING, STRING_SET, NUMBER_SET, BINARY_SET,
BINARY, BOOLEAN,
)
from pynamodb.models import Model
UTC = tzutc()
class AttributeTestModel(Model):
class Meta:
host = 'http://localhost:8000'
table_name = 'test'
binary_attr = BinaryAttribute()
binary_set_attr = BinarySetAttribute()
number_attr = NumberAttribute()
number_set_attr = NumberSetAttribute()
unicode_attr = UnicodeAttribute()
unicode_set_attr = UnicodeSetAttribute()
datetime_attr = UTCDateTimeAttribute()
bool_attr = BooleanAttribute()
json_attr = JSONAttribute()
map_attr = MapAttribute()
class CustomAttrMap(MapAttribute):
overridden_number_attr = NumberAttribute(attr_name="number_attr")
overridden_unicode_attr = UnicodeAttribute(attr_name="unicode_attr")
class DefaultsMap(MapAttribute):
map_field = MapAttribute(default={})
string_field = UnicodeAttribute(null=True)
class TestAttributeDescriptor:
"""
Test Attribute Descriptors
"""
def setup(self):
self.instance = AttributeTestModel()
def test_binary_attr(self):
"""
Binary attribute descriptor
"""
self.instance.binary_attr = b'test'
assert self.instance.binary_attr == b'test'
def test_binary_set_attr(self):
"""
Binary set attribute descriptor
"""
self.instance.binary_set_attr = {b'test', b'test2'}
assert self.instance.binary_set_attr == {b'test', b'test2'}
def test_number_attr(self):
"""
Number attribute descriptor
"""
self.instance.number_attr = 42
assert self.instance.number_attr == 42
def test_number_set_attr(self):
"""
Number set attribute descriptor
"""
self.instance.number_set_attr = {1, 2}
assert self.instance.number_set_attr == {1, 2}
def test_unicode_attr(self):
"""
Unicode attribute descriptor
"""
self.instance.unicode_attr = u"test"
assert self.instance.unicode_attr == u"test"
def test_unicode_set_attr(self):
"""
Unicode set attribute descriptor
"""
self.instance.unicode_set_attr = {u"test", u"test2"}
assert self.instance.unicode_set_attr == {u"test", u"test2"}
def test_datetime_attr(self):
"""
Datetime attribute descriptor
"""
now = datetime.now()
self.instance.datetime_attr = now
assert self.instance.datetime_attr == now
def test_bool_attr(self):
"""
Boolean attribute descriptor
"""
self.instance.bool_attr = True
assert self.instance.bool_attr is True
def test_json_attr(self):
"""
JSON attribute descriptor
"""
self.instance.json_attr = {'foo': 'bar', 'bar': 42}
assert self.instance.json_attr == {'foo': 'bar', 'bar': 42}
class TestUTCDateTimeAttribute:
"""
Tests UTCDateTime attributes
"""
def test_utc_datetime_attribute(self):
"""
UTCDateTimeAttribute.default
"""
attr = UTCDateTimeAttribute()
assert attr is not None
assert attr.attr_type == STRING
tstamp = datetime.now()
attr = UTCDateTimeAttribute(default=tstamp)
assert attr.default == tstamp
def test_utc_date_time_deserialize(self):
"""
UTCDateTimeAttribute.deserialize
"""
tstamp = datetime.now(UTC)
attr = UTCDateTimeAttribute()
assert attr.deserialize(tstamp.strftime(DATETIME_FORMAT)) == tstamp
def test_dateutil_parser_fallback(self):
"""
UTCDateTimeAttribute.deserialize
"""
expected_value = datetime(2047, 1, 6, 8, 21, tzinfo=tzutc())
attr = UTCDateTimeAttribute()
assert attr.deserialize('January 6, 2047 at 8:21:00AM UTC') == expected_value
@patch('pynamodb.attributes.datetime')
@patch('pynamodb.attributes.parse')
def test_utc_date_time_deserialize_parse_args(self, parse_mock, datetime_mock):
"""
UTCDateTimeAttribute.deserialize
"""
tstamp = datetime.now(UTC)
attr = UTCDateTimeAttribute()
tstamp_str = tstamp.strftime(DATETIME_FORMAT)
attr.deserialize(tstamp_str)
parse_mock.assert_not_called()
datetime_mock.strptime.assert_not_called()
def test_utc_date_time_serialize(self):
"""
UTCDateTimeAttribute.serialize
"""
tstamp = datetime.now()
attr = UTCDateTimeAttribute()
assert attr.serialize(tstamp) == tstamp.replace(tzinfo=UTC).strftime(DATETIME_FORMAT)
def test__fast_parse_utc_datestring_roundtrips(self):
tstamp = datetime.now(UTC)
tstamp_str = tstamp.strftime(DATETIME_FORMAT)
assert _fast_parse_utc_datestring(tstamp_str) == tstamp
def test__fast_parse_utc_datestring_no_microseconds(self):
expected_value = datetime(2047, 1, 6, 8, 21, tzinfo=tzutc())
assert _fast_parse_utc_datestring('2047-01-06T08:21:00.0+0000') == expected_value
@pytest.mark.parametrize(
"invalid_string",
[
'2.47-01-06T08:21:00.0+0000',
'2047-01-06T08:21:00.+0000',
'2047-01-06T08:21:00.0',
'2047-01-06 08:21:00.0+0000',
'abcd-01-06T08:21:00.0+0000',
'2047-ab-06T08:21:00.0+0000',
'2047-01-abT08:21:00.0+0000',
'2047-01-06Tab:21:00.0+0000',
'2047-01-06T08:ab:00.0+0000',
'2047-01-06T08:ab:00.0+0000',
'2047-01-06T08:21:00.a+0000',
'2047-01-06T08:21:00.0.1+0000',
'2047-01-06T08:21:00.0+00000'
]
)
def test__fast_parse_utc_datestring_invalid_input(self, invalid_string):
with pytest.raises(ValueError, match="does not match format"):
_fast_parse_utc_datestring(invalid_string)
class TestBinaryAttribute:
"""
Tests binary attributes
"""
def test_binary_attribute(self):
"""
BinaryAttribute.default
"""
attr = BinaryAttribute()
assert attr is not None
assert attr.attr_type == BINARY
attr = BinaryAttribute(default=b'foo')
assert attr.default == b'foo'
def test_binary_round_trip(self):
"""
BinaryAttribute round trip
"""
attr = BinaryAttribute()
value = b'foo'
serial = attr.serialize(value)
assert attr.deserialize(serial) == value
def test_binary_serialize(self):
"""
BinaryAttribute.serialize
"""
attr = BinaryAttribute()
serial = b64encode(b'foo').decode(DEFAULT_ENCODING)
assert attr.serialize(b'foo') == serial
def test_binary_deserialize(self):
"""
BinaryAttribute.deserialize
"""
attr = BinaryAttribute()
serial = b64encode(b'foo').decode(DEFAULT_ENCODING)
assert attr.deserialize(serial) == b'foo'
def test_binary_set_serialize(self):
"""
BinarySetAttribute.serialize
"""
attr = BinarySetAttribute()
assert attr.attr_type == BINARY_SET
assert attr.serialize({b'foo', b'bar'}) == [
b64encode(val).decode(DEFAULT_ENCODING) for val in sorted({b'foo', b'bar'})
]
assert attr.serialize(None) is None
def test_binary_set_round_trip(self):
"""
BinarySetAttribute round trip
"""
attr = BinarySetAttribute()
value = {b'foo', b'bar'}
serial = attr.serialize(value)
assert attr.deserialize(serial) == value
def test_binary_set_deserialize(self):
"""
BinarySetAttribute.deserialize
"""
attr = BinarySetAttribute()
value = {b'foo', b'bar'}
assert attr.deserialize(
[b64encode(val).decode(DEFAULT_ENCODING) for val in sorted(value)]
) == value
def test_binary_set_attribute(self):
"""
BinarySetAttribute.serialize
"""
attr = BinarySetAttribute()
assert attr is not None
attr = BinarySetAttribute(default={b'foo', b'bar'})
assert attr.default == {b'foo', b'bar'}
class TestNumberAttribute:
"""
Tests number attributes
"""
def test_number_attribute(self):
"""
NumberAttribute.default
"""
attr = NumberAttribute()
assert attr is not None
assert attr.attr_type == NUMBER
attr = NumberAttribute(default=1)
assert attr.default == 1
def test_number_serialize(self):
"""
NumberAttribute.serialize
"""
attr = NumberAttribute()
assert attr.serialize(3.141) == '3.141'
assert attr.serialize(1) == '1'
assert attr.serialize(12345678909876543211234234324234) == '12345678909876543211234234324234'
def test_number_deserialize(self):
"""
NumberAttribute.deserialize
"""
attr = NumberAttribute()
assert attr.deserialize('1') == 1
assert attr.deserialize('3.141') == 3.141
assert attr.deserialize('12345678909876543211234234324234') == 12345678909876543211234234324234
def test_number_set_deserialize(self):
"""
NumberSetAttribute.deserialize
"""
attr = NumberSetAttribute()
assert attr.attr_type == NUMBER_SET
assert attr.deserialize([json.dumps(val) for val in sorted({1, 2})]) == {1, 2}
def test_number_set_serialize(self):
"""
NumberSetAttribute.serialize
"""
attr = NumberSetAttribute()
assert attr.serialize({1, 2}) == [json.dumps(val) for val in sorted({1, 2})]
assert attr.serialize(None) is None
def test_number_set_attribute(self):
"""
NumberSetAttribute.default
"""
attr = NumberSetAttribute()
assert attr is not None
attr = NumberSetAttribute(default={1, 2})
assert attr.default == {1, 2}
class TestUnicodeAttribute:
"""
Tests unicode attributes
"""
def test_unicode_attribute(self):
"""
UnicodeAttribute.default
"""
attr = UnicodeAttribute()
assert attr is not None
assert attr.attr_type == STRING
attr = UnicodeAttribute(default=six.u('foo'))
assert attr.default == six.u('foo')
def test_unicode_serialize(self):
"""
UnicodeAttribute.serialize
"""
attr = UnicodeAttribute()
assert attr.serialize('foo') == six.u('foo')
assert attr.serialize(u'foo') == six.u('foo')
assert attr.serialize(u'') is None
assert attr.serialize(None) is None
def test_unicode_deserialize(self):
"""
UnicodeAttribute.deserialize
"""
attr = UnicodeAttribute()
assert attr.deserialize('foo') == six.u('foo')
assert attr.deserialize(u'foo') == six.u('foo')
def test_unicode_set_serialize(self):
"""
UnicodeSetAttribute.serialize
"""
attr = UnicodeSetAttribute()
assert attr.attr_type == STRING_SET
assert attr.deserialize(None) is None
expected = sorted([six.u('foo'), six.u('bar')])
assert attr.serialize({six.u('foo'), six.u('bar')}) == expected
expected = sorted([six.u('True'), six.u('False')])
assert attr.serialize({six.u('True'), six.u('False')}) == expected
expected = sorted([six.u('true'), six.u('false')])
assert attr.serialize({six.u('true'), six.u('false')}) == expected
def test_round_trip_unicode_set(self):
"""
Round trip a unicode set
"""
attr = UnicodeSetAttribute()
orig = {six.u('foo'), six.u('bar')}
assert orig == attr.deserialize(attr.serialize(orig))
orig = {six.u('true'), six.u('false')}
assert orig == attr.deserialize(attr.serialize(orig))
orig = {six.u('1'), six.u('2.8')}
assert orig == attr.deserialize(attr.serialize(orig))
orig = {six.u('[1,2,3]'), six.u('2.8')}
assert orig == attr.deserialize(attr.serialize(orig))
def test_unicode_set_deserialize(self):
"""
UnicodeSetAttribute.deserialize
"""
attr = UnicodeSetAttribute()
value = {six.u('foo'), six.u('bar')}
assert attr.deserialize(value) == value
value = {six.u('True'), six.u('False')}
assert attr.deserialize(value) == value
value = {six.u('true'), six.u('false')}
assert attr.deserialize(value) == value
value = {six.u('1'), six.u('2.8')}
assert attr.deserialize(value) == value
def test_unicode_set_attribute(self):
"""
UnicodeSetAttribute.default
"""
attr = UnicodeSetAttribute()
assert attr is not None
assert attr.attr_type == STRING_SET
attr = UnicodeSetAttribute(default={six.u('foo'), six.u('bar')})
assert attr.default == {six.u('foo'), six.u('bar')}
class TestBooleanAttribute:
"""
Tests boolean attributes
"""
def test_boolean_attribute(self):
"""
BooleanAttribute.default
"""
attr = BooleanAttribute()
assert attr is not None
assert attr.attr_type == BOOLEAN
attr = BooleanAttribute(default=True)
assert attr.default is True
def test_boolean_serialize(self):
"""
BooleanAttribute.serialize
"""
attr = BooleanAttribute()
assert attr.serialize(True) is True
assert attr.serialize(False) is False
assert attr.serialize(None) is None
def test_boolean_deserialize(self):
"""
BooleanAttribute.deserialize
"""
attr = BooleanAttribute()
assert attr.deserialize(True) is True
assert attr.deserialize(False) is False
class TestJSONAttribute:
"""
Tests json attributes
"""
def test_quoted_json(self):
attr = JSONAttribute()
serialized = attr.serialize('\\t')
assert attr.deserialize(serialized) == '\\t'
serialized = attr.serialize('"')
assert attr.deserialize(serialized) == '"'
def test_json_attribute(self):
"""
JSONAttribute.default
"""
attr = JSONAttribute()
assert attr is not None
assert attr.attr_type == STRING
attr = JSONAttribute(default={})
assert attr.default == {}
def test_json_serialize(self):
"""
JSONAttribute.serialize
"""
attr = JSONAttribute()
item = {'foo': 'bar', 'bool': True, 'number': 3.141}
assert attr.serialize(item) == six.u(json.dumps(item))
assert attr.serialize({}) == six.u('{}')
assert attr.serialize(None) is None
def test_json_deserialize(self):
"""
JSONAttribute.deserialize
"""
attr = JSONAttribute()
item = {'foo': 'bar', 'bool': True, 'number': 3.141}
encoded = six.u(json.dumps(item))
assert attr.deserialize(encoded) == item
def test_control_chars(self):
"""
JSONAttribute with control chars
"""
attr = JSONAttribute()
item = {'foo\t': 'bar\n', 'bool': True, 'number': 3.141}
encoded = six.u(json.dumps(item))
assert attr.deserialize(encoded) == item
class TestMapAttribute:
"""
Tests map with str, int, float
"""
def test_attribute_children(self):
person_attribute = {
'name': 'Justin',
'age': 12345678909876543211234234324234,
'height': 187.96
}
attr = MapAttribute()
serialized = attr.serialize(person_attribute)
assert attr.deserialize(serialized) == person_attribute
# Special case for raw map attributes
def test_null_attribute_raw_map(self):
null_attribute = {
'skip': None
}
attr = MapAttribute()
serialized = attr.serialize(null_attribute)
assert serialized == {'skip': {'NULL': True}}
def test_null_attribute_subclassed_map(self):
null_attribute = {
'map_field': None
}
attr = DefaultsMap()
serialized = attr.serialize(null_attribute)
assert serialized == {}
def test_null_attribute_map_after_serialization(self):
null_attribute = {
'string_field': '',
}
attr = DefaultsMap()
serialized = attr.serialize(null_attribute)
assert serialized == {}
def test_map_of_map(self):
attribute = {
'name': 'Justin',
'metrics': {
'age': 31,
'height': 187.96
}
}
attr = MapAttribute()
serialized = attr.serialize(attribute)
assert attr.deserialize(serialized) == attribute
def test_map_overridden_attrs_accessors(self):
attr = CustomAttrMap(**{
'overridden_number_attr': 10,
'overridden_unicode_attr': "Hello"
})
assert attr.overridden_number_attr == 10
assert attr.overridden_unicode_attr == "Hello"
def test_map_overridden_attrs_serialize(self):
attribute = {
'overridden_number_attr': 10,
'overridden_unicode_attr': "Hello"
}
expected = {'number_attr': {'N': '10'}, 'unicode_attr': {'S': six.u('Hello')}}
assert CustomAttrMap().serialize(attribute) == expected
def test_additional_attrs_deserialize(self):
raw_data = {
'number_attr': {
'N': '10'},
'unicode_attr': {
'S': six.u('Hello')
},
'undeclared_attr': {
'S': six.u('Goodbye')
}
}
expected = {
'overridden_number_attr': 10,
'overridden_unicode_attr': "Hello"
}
assert CustomAttrMap().deserialize(raw_data).attribute_values == expected
def test_defaults(self):
item = DefaultsMap()
assert item.validate()
assert DefaultsMap().serialize(item) == {
'map_field': {
'M': {}
}
}
def test_raw_set_attr(self):
item = AttributeTestModel()
item.map_attr = {}
item.map_attr.foo = 'bar'
item.map_attr.num = 3
item.map_attr.nested = {'nestedfoo': 'nestedbar'}
assert item.map_attr['foo'] == 'bar'
assert item.map_attr['num'] == 3
assert item.map_attr['nested']['nestedfoo'] == 'nestedbar'
def test_raw_set_item(self):
item = AttributeTestModel()
item.map_attr = {}
item.map_attr['foo'] = 'bar'
item.map_attr['num'] = 3
item.map_attr['nested'] = {'nestedfoo': 'nestedbar'}
assert item.map_attr['foo'] == 'bar'
assert item.map_attr['num'] == 3
assert item.map_attr['nested']['nestedfoo'] == 'nestedbar'
def test_raw_map_from_dict(self):
item = AttributeTestModel(
map_attr={
"foo": "bar",
"num": 3,
"nested": {
"nestedfoo": "nestedbar"
}
}
)
assert item.map_attr['foo'] == 'bar'
assert item.map_attr['num'] == 3
def test_raw_map_access(self):
raw = {
"foo": "bar",
"num": 3,
"nested": {
"nestedfoo": "nestedbar"
}
}
attr = MapAttribute(**raw)
for k, v in six.iteritems(raw):
assert attr[k] == v
def test_raw_map_iter(self):
raw = {
"foo": "bar",
"num": 3,
"nested": {
"nestedfoo": "nestedbar"
}
}
attr = MapAttribute(**raw)
assert list(iter(raw)) == list(iter(attr))
def test_raw_map_json_serialize(self):
raw = {
"foo": "bar",
"num": 3,
"nested": {
"nestedfoo": "nestedbar"
}
}
serialized_raw = json.dumps(raw, sort_keys=True)
serialized_attr_from_raw = json.dumps(
AttributeTestModel(map_attr=raw).map_attr.as_dict(),
sort_keys=True)
serialized_attr_from_map = json.dumps(
AttributeTestModel(map_attr=MapAttribute(**raw)).map_attr.as_dict(),
sort_keys=True)
assert serialized_attr_from_raw == serialized_raw
assert serialized_attr_from_map == serialized_raw
def test_typed_and_raw_map_json_serialize(self):
class TypedMap(MapAttribute):
map_attr = MapAttribute()
class SomeModel(Model):
typed_map = TypedMap()
item = SomeModel(
typed_map=TypedMap(map_attr={'foo': 'bar'})
)
assert json.dumps({'map_attr': {'foo': 'bar'}}) == json.dumps(item.typed_map.as_dict())
def test_json_serialize(self):
class JSONMapAttribute(MapAttribute):
arbitrary_data = JSONAttribute()
def __eq__(self, other):
return self.arbitrary_data == other.arbitrary_data
item = {'foo': 'bar', 'bool': True, 'number': 3.141}
json_map = JSONMapAttribute(arbitrary_data=item)
serialized = json_map.serialize(json_map)
deserialized = json_map.deserialize(serialized)
assert isinstance(deserialized, JSONMapAttribute)
assert deserialized == json_map
assert deserialized.arbitrary_data == item
def test_serialize_datetime(self):
class CustomMapAttribute(MapAttribute):
date_attr = UTCDateTimeAttribute()
cm = CustomMapAttribute(date_attr=datetime(2017, 1, 1))
serialized_datetime = cm.serialize(cm)
expected_serialized_value = {
'date_attr': {
'S': u'2017-01-01T00:00:00.000000+0000'
}
}
assert serialized_datetime == expected_serialized_value
def test_complex_map_accessors(self):
class NestedThing(MapAttribute):
double_nested = MapAttribute()
double_nested_renamed = MapAttribute(attr_name='something_else')
class ThingModel(Model):
nested = NestedThing()
t = ThingModel(nested=NestedThing(
double_nested={'hello': 'world'},
double_nested_renamed={'foo': 'bar'})
)
assert t.nested.double_nested.as_dict() == {'hello': 'world'}
assert t.nested.double_nested_renamed.as_dict() == {'foo': 'bar'}
assert t.nested.double_nested.hello == 'world'
assert t.nested.double_nested_renamed.foo == 'bar'
assert t.nested['double_nested'].as_dict() == {'hello': 'world'}
assert t.nested['double_nested_renamed'].as_dict() == {'foo': 'bar'}
assert t.nested['double_nested']['hello'] == 'world'
assert t.nested['double_nested_renamed']['foo'] == 'bar'
with pytest.raises(AttributeError):
bad = t.nested.double_nested.bad
with pytest.raises(AttributeError):
bad = t.nested.bad
with pytest.raises(AttributeError):
bad = t.nested.something_else
with pytest.raises(KeyError):
bad = t.nested.double_nested['bad']
with pytest.raises(KeyError):
bad = t.nested['something_else']
def test_attribute_paths_subclassing(self):
class SubMapAttribute(MapAttribute):
foo = UnicodeAttribute(attr_name='dyn_foo')
class SubSubMapAttribute(SubMapAttribute):
bar = UnicodeAttribute(attr_name='dyn_bar')
class SubModel(Model):
sub_map = SubMapAttribute(attr_name='dyn_sub_map')
class SubSubModel(SubModel):
sub_sub_map = SubSubMapAttribute()
assert SubModel.sub_map.foo.attr_name == 'dyn_foo'
assert SubModel.sub_map.foo.attr_path == ['dyn_sub_map', 'dyn_foo']
assert SubSubModel.sub_map.foo.attr_name == 'dyn_foo'
assert SubSubModel.sub_map.foo.attr_path == ['dyn_sub_map', 'dyn_foo']
assert SubSubModel.sub_sub_map.foo.attr_name == 'dyn_foo'
assert SubSubModel.sub_sub_map.foo.attr_path == ['sub_sub_map', 'dyn_foo']
assert SubSubModel.sub_sub_map.bar.attr_name == 'dyn_bar'
assert SubSubModel.sub_sub_map.bar.attr_path == ['sub_sub_map', 'dyn_bar']
def test_attribute_paths_wrapping(self):
class InnerMapAttribute(MapAttribute):
map_attr = MapAttribute(attr_name='dyn_map_attr')
class MiddleMapAttributeA(MapAttribute):
inner_map = InnerMapAttribute(attr_name='dyn_in_map_a')
class MiddleMapAttributeB(MapAttribute):
inner_map = InnerMapAttribute(attr_name='dyn_in_map_b')
class OuterMapAttribute(MapAttribute):
mid_map_a = MiddleMapAttributeA()
mid_map_b = MiddleMapAttributeB()
class MyModel(Model):
outer_map = OuterMapAttribute(attr_name='dyn_out_map')
mid_map_a_map_attr = MyModel.outer_map.mid_map_a.inner_map.map_attr
mid_map_b_map_attr = MyModel.outer_map.mid_map_b.inner_map.map_attr
assert mid_map_a_map_attr.attr_name == 'dyn_map_attr'
assert mid_map_a_map_attr.attr_path == ['dyn_out_map', 'mid_map_a', 'dyn_in_map_a', 'dyn_map_attr']
assert mid_map_b_map_attr.attr_name == 'dyn_map_attr'
assert mid_map_b_map_attr.attr_path == ['dyn_out_map', 'mid_map_b', 'dyn_in_map_b', 'dyn_map_attr']
class TestValueDeserialize:
def test__get_value_for_deserialize(self):
expected = '3'
data = {'N': '3'}
actual = _get_value_for_deserialize(data)
assert expected == actual
def test__get_value_for_deserialize_null(self):
data = {'NULL': 'True'}
actual = _get_value_for_deserialize(data)
assert actual is None
class TestMapAndListAttribute:
def test_map_of_list(self):
grocery_list = {
'fruit': ['apple', 'pear', 32],
'veggies': ['broccoli', 'potatoes', 5]
}
serialized = MapAttribute().serialize(grocery_list)
assert MapAttribute().deserialize(serialized) == grocery_list
def test_map_of_list_of_map(self):
family_attributes = {
'phillips': [
{
'name': 'Justin',
'age': 31,
'height': 187.96
},
{
'name': 'Galen',
'age': 29,
'height': 193.04,
'male': True
},
{
'name': 'Colin',
'age': 32,
'height': 203.2,
'male': True,
'hasChild': True
}
],
'workman': [
{
'name': 'Mandy',
'age': 29,
'height': 157.48,
'female': True
},
{
'name': 'Rodney',
'age': 31,
'height': 175.26,
'hasChild': False
}
]
}
serialized = MapAttribute().serialize(family_attributes)
assert MapAttribute().deserialize(serialized) == family_attributes
def test_list_of_map_with_of(self):
class Person(MapAttribute):
name = UnicodeAttribute()
age = NumberAttribute()
def __lt__(self, other):
return self.name < other.name
def __eq__(self, other):
return (self.name == other.name and
self.age == other.age)
person1 = Person()
person1.name = 'john'
person1.age = 40
person2 = Person()
person2.name = 'Dana'
person2.age = 41
inp = [person1, person2]
list_attribute = ListAttribute(default=[], of=Person)
serialized = list_attribute.serialize(inp)
deserialized = list_attribute.deserialize(serialized)
assert sorted(deserialized) == sorted(inp)
def test_list_of_map_with_of_and_custom_attribute(self, mocker):
class CustomMapAttribute(MapAttribute):
custom = NumberAttribute()
def __eq__(self, other):
return self.custom == other.custom
serialize_mock = mocker.spy(CustomMapAttribute.custom, 'serialize',)
deserialize_mock = mocker.spy(CustomMapAttribute.custom, 'deserialize')
attribute1 = CustomMapAttribute()
attribute1.custom = 1
attribute2 = CustomMapAttribute()
attribute2.custom = 2
inp = [attribute1, attribute2]
list_attribute = ListAttribute(default=[], of=CustomMapAttribute)
serialized = list_attribute.serialize(inp)
deserialized = list_attribute.deserialize(serialized)
assert deserialized == inp
assert serialize_mock.call_args_list == [call(1), call(2)]
assert deserialize_mock.call_args_list == [call('1'), call('2')]