@@ -447,6 +447,11 @@ class ModelTestCase(TestCase):
447
447
"""
448
448
Tests for the models API
449
449
"""
450
+ @staticmethod
451
+ def init_table_meta (model_clz , table_data ):
452
+ with patch (PATCH_METHOD ) as req :
453
+ req .return_value = table_data
454
+ model_clz ._get_meta_data ()
450
455
451
456
def assert_dict_lists_equal (self , list1 , list2 ):
452
457
"""
@@ -3347,7 +3352,7 @@ def test_raw_map_deserializes(self):
3347
3352
instance = ExplicitRawMapModel (map_attr = map_native )
3348
3353
instance ._deserialize (map_serialized )
3349
3354
actual = instance .map_attr
3350
- for k ,v in six .iteritems (map_native ):
3355
+ for k , v in six .iteritems (map_native ):
3351
3356
self .assertEqual (v , actual [k ])
3352
3357
3353
3358
def test_raw_map_from_raw_data_works (self ):
@@ -3361,10 +3366,10 @@ def test_raw_map_from_raw_data_works(self):
3361
3366
EXPLICIT_RAW_MAP_MODEL_ITEM_DATA ,
3362
3367
'map_id' , 'N' ,
3363
3368
'123' )
3364
- with patch (PATCH_METHOD , new = fake_db ) as req :
3369
+ with patch (PATCH_METHOD , new = fake_db ):
3365
3370
item = ExplicitRawMapModel .get (123 )
3366
3371
actual = item .map_attr
3367
- self .assertEqual (map_native .get ('listy' )[2 ], actual . get ( 'listy' ) [2 ])
3372
+ self .assertEqual (map_native .get ('listy' )[2 ], actual [ 'listy' ] [2 ])
3368
3373
for k , v in six .iteritems (map_native ):
3369
3374
self .assertEqual (v , actual [k ])
3370
3375
@@ -3396,11 +3401,11 @@ def _get_raw_map_as_sub_map_test_data(self):
3396
3401
map_serialized = {
3397
3402
'M' : {
3398
3403
'foo' : {'S' : 'bar' },
3399
- 'num' : {'N' : 1 },
3404
+ 'num' : {'N' : '1' },
3400
3405
'bool_type' : {'BOOL' : True },
3401
3406
'other_b_type' : {'BOOL' : False },
3402
- 'floaty' : {'N' : 1.2 },
3403
- 'listy' : {'L' : [{'N' : 1 }, {'N' : 2 }, {'N' : 3 }]},
3407
+ 'floaty' : {'N' : ' 1.2' },
3408
+ 'listy' : {'L' : [{'N' : '1' }, {'N' : '2' }, {'N' : '3' }]},
3404
3409
'mapy' : {'M' : {'baz' : {'S' : 'bongo' }}}
3405
3410
}
3406
3411
}
@@ -3415,13 +3420,22 @@ def _get_raw_map_as_sub_map_test_data(self):
3415
3420
)
3416
3421
return map_native , map_serialized , sub_attr , instance
3417
3422
3418
- def test_raw_map_as_sub_map_deserializes (self ):
3423
+ def test_raw_map_as_sub_map (self ):
3419
3424
map_native , map_serialized , sub_attr , instance = self ._get_raw_map_as_sub_map_test_data ()
3420
- instance ._deserialize (map_serialized )
3421
3425
actual = instance .sub_attr
3422
3426
self .assertEqual (sub_attr , actual )
3423
- self .assertEqual (sub_attr .map_field .get ('floaty' ), map_native .get ('floaty' ))
3424
- self .assertEqual (sub_attr .map_field .get ('mapy' , {}).get ('baz' ), map_native .get ('mapy' , {}).get ('baz' ))
3427
+ self .assertEqual (actual .map_field ['floaty' ], map_native .get ('floaty' ))
3428
+ self .assertEqual (actual .map_field ['mapy' ]['baz' ], map_native .get ('mapy' ).get ('baz' ))
3429
+
3430
+ def test_raw_map_as_sub_map_deserialize (self ):
3431
+ map_native , map_serialized , _ , _ = self ._get_raw_map_as_sub_map_test_data ()
3432
+
3433
+ actual = MapAttrSubClassWithRawMapAttr ().deserialize ({
3434
+ "map_field" : map_serialized
3435
+ })
3436
+
3437
+ for k , v in six .iteritems (map_native ):
3438
+ self .assertEqual (actual .map_field [k ], v )
3425
3439
3426
3440
def test_raw_map_as_sub_map_from_raw_data_works (self ):
3427
3441
map_native , map_serialized , sub_attr , instance = self ._get_raw_map_as_sub_map_test_data ()
@@ -3430,9 +3444,94 @@ def test_raw_map_as_sub_map_from_raw_data_works(self):
3430
3444
EXPLICIT_RAW_MAP_MODEL_AS_SUB_MAP_IN_TYPED_MAP_ITEM_DATA ,
3431
3445
'map_id' , 'N' ,
3432
3446
'123' )
3433
- with patch (PATCH_METHOD , new = fake_db ) as req :
3447
+ with patch (PATCH_METHOD , new = fake_db ):
3434
3448
item = ExplicitRawMapAsMemberOfSubClass .get (123 )
3435
- self .assertEqual (sub_attr .map_field .get ('floaty' ),
3449
+ actual = item .sub_attr
3450
+ self .assertEqual (sub_attr .map_field ['floaty' ],
3436
3451
map_native .get ('floaty' ))
3437
- self .assertEqual (sub_attr .map_field .get ('mapy' , {}).get ('baz' ),
3438
- map_native .get ('mapy' , {}).get ('baz' ))
3452
+ self .assertEqual (actual .map_field ['mapy' ]['baz' ],
3453
+ map_native .get ('mapy' ).get ('baz' ))
3454
+
3455
+
3456
+ class ModelInitTestCase (TestCase ):
3457
+
3458
+ def test_raw_map_attribute_with_dict_init (self ):
3459
+ attribute = {
3460
+ 'foo' : 123 ,
3461
+ 'bar' : 'baz'
3462
+ }
3463
+ actual = ExplicitRawMapModel (map_id = 3 , map_attr = attribute )
3464
+ self .assertEquals (actual .map_attr ['foo' ], attribute ['foo' ])
3465
+
3466
+ def test_raw_map_attribute_with_initialized_instance_init (self ):
3467
+ attribute = {
3468
+ 'foo' : 123 ,
3469
+ 'bar' : 'baz'
3470
+ }
3471
+ initialized_instance = MapAttribute (** attribute )
3472
+ actual = ExplicitRawMapModel (map_id = 3 , map_attr = initialized_instance )
3473
+ self .assertEquals (actual .map_attr ['foo' ], initialized_instance ['foo' ])
3474
+ self .assertEquals (actual .map_attr ['foo' ], attribute ['foo' ])
3475
+
3476
+ def test_subclassed_map_attribute_with_dict_init (self ):
3477
+ attribute = {
3478
+ 'make' : 'Volkswagen' ,
3479
+ 'model' : 'Super Beetle'
3480
+ }
3481
+ expected_model = CarInfoMap (** attribute )
3482
+ actual = CarModel (car_id = 1 , car_info = attribute )
3483
+ self .assertEquals (expected_model .make , actual .car_info .make )
3484
+ self .assertEquals (expected_model .model , actual .car_info .model )
3485
+
3486
+ def test_subclassed_map_attribute_with_initialized_instance_init (self ):
3487
+ attribute = {
3488
+ 'make' : 'Volkswagen' ,
3489
+ 'model' : 'Super Beetle'
3490
+ }
3491
+ expected_model = CarInfoMap (** attribute )
3492
+ actual = CarModel (car_id = 1 , car_info = expected_model )
3493
+ self .assertEquals (expected_model .make , actual .car_info .make )
3494
+ self .assertEquals (expected_model .model , actual .car_info .model )
3495
+
3496
+ def _get_bin_tree (self , multiplier = 1 ):
3497
+ return {
3498
+ 'value' : 5 * multiplier ,
3499
+ 'left' : {
3500
+ 'value' : 2 * multiplier ,
3501
+ 'left' : {
3502
+ 'value' : 1 * multiplier
3503
+ },
3504
+ 'right' : {
3505
+ 'value' : 3 * multiplier
3506
+ }
3507
+ },
3508
+ 'right' : {
3509
+ 'value' : 7 * multiplier ,
3510
+ 'left' : {
3511
+ 'value' : 6 * multiplier
3512
+ },
3513
+ 'right' : {
3514
+ 'value' : 8 * multiplier
3515
+ }
3516
+ }
3517
+ }
3518
+
3519
+ def test_subclassed_map_attribute_with_map_attributes_member_with_dict_init (self ):
3520
+ left = self ._get_bin_tree ()
3521
+ right = self ._get_bin_tree (multiplier = 2 )
3522
+ actual = TreeModel (tree_key = 'key' , left = left , right = right )
3523
+ self .assertEquals (actual .left .left .right .value , 3 )
3524
+ self .assertEquals (actual .left .left .value , 2 )
3525
+ self .assertEquals (actual .right .right .left .value , 12 )
3526
+ self .assertEquals (actual .right .right .value , 14 )
3527
+
3528
+ def test_subclassed_map_attribute_with_map_attribute_member_with_initialized_instance_init (self ):
3529
+ left = self ._get_bin_tree ()
3530
+ right = self ._get_bin_tree (multiplier = 2 )
3531
+ left_instance = TreeLeaf (** left )
3532
+ right_instance = TreeLeaf (** right )
3533
+ actual = TreeModel (tree_key = 'key' , left = left_instance , right = right_instance )
3534
+ self .assertEquals (actual .left .left .right .value , left_instance .left .right .value )
3535
+ self .assertEquals (actual .left .left .value , left_instance .left .value )
3536
+ self .assertEquals (actual .right .right .left .value , right_instance .right .left .value )
3537
+ self .assertEquals (actual .right .right .value , right_instance .right .value )
0 commit comments