@@ -391,29 +391,29 @@ class Meta:
391
391
is_human = BooleanAttribute ()
392
392
393
393
394
- class TreeLeaf2 (MapAttribute ):
394
+ class TreeLeaf (MapAttribute ):
395
395
value = NumberAttribute ()
396
396
397
397
398
- class TreeLeaf1 (MapAttribute ):
398
+ class TreeNode2 (MapAttribute ):
399
399
value = NumberAttribute ()
400
- left = TreeLeaf2 ()
401
- right = TreeLeaf2 ()
400
+ left = TreeLeaf ()
401
+ right = TreeLeaf ()
402
402
403
403
404
- class TreeLeaf (MapAttribute ):
404
+ class TreeNode1 (MapAttribute ):
405
405
value = NumberAttribute ()
406
- left = TreeLeaf1 ()
407
- right = TreeLeaf1 ()
406
+ left = TreeNode2 ()
407
+ right = TreeNode2 ()
408
408
409
409
410
410
class TreeModel (Model ):
411
411
class Meta :
412
412
table_name = 'TreeModelTable'
413
413
414
414
tree_key = UnicodeAttribute (hash_key = True )
415
- left = TreeLeaf ()
416
- right = TreeLeaf ()
415
+ left = TreeNode1 ()
416
+ right = TreeNode1 ()
417
417
418
418
419
419
class ExplicitRawMapModel (Model ):
@@ -2911,41 +2911,61 @@ def test_deserializing_new_style_bool_true_works(self):
2911
2911
self .assertTrue (item .is_human )
2912
2912
2913
2913
def test_serializing_map_with_null_check (self ):
2914
- item = TreeModel (
2914
+ class TreeModelWithList (TreeModel ):
2915
+ leaves = ListAttribute (of = TreeLeaf )
2916
+
2917
+ item = TreeModelWithList (
2915
2918
tree_key = 'test' ,
2916
- left = TreeLeaf (
2919
+ left = TreeNode1 (
2917
2920
value = 42 ,
2918
- left = TreeLeaf1 (
2921
+ left = TreeNode2 (
2919
2922
value = 42 ,
2920
- left = TreeLeaf2 (value = 42 ),
2921
- right = TreeLeaf2 (value = 42 ),
2923
+ left = TreeLeaf (value = 42 ),
2924
+ right = TreeLeaf (value = 42 ),
2922
2925
),
2923
- right = TreeLeaf1 (
2926
+ right = TreeNode2 (
2924
2927
value = 42 ,
2925
- left = TreeLeaf2 (value = 42 ),
2926
- right = TreeLeaf2 (value = 42 ),
2928
+ left = TreeLeaf (value = 42 ),
2929
+ right = TreeLeaf (value = 42 ),
2927
2930
),
2928
2931
),
2929
- right = TreeLeaf (
2932
+ right = TreeNode1 (
2930
2933
value = 42 ,
2931
- left = TreeLeaf1 (
2934
+ left = TreeNode2 (
2932
2935
value = 42 ,
2933
- left = TreeLeaf2 (value = 42 ),
2934
- right = TreeLeaf2 (value = 42 ),
2936
+ left = TreeLeaf (value = 42 ),
2937
+ right = TreeLeaf (value = 42 ),
2935
2938
),
2936
- right = TreeLeaf1 (
2939
+ right = TreeNode2 (
2937
2940
value = 42 ,
2938
- left = TreeLeaf2 (value = 42 ),
2939
- right = TreeLeaf2 (value = 42 ),
2941
+ left = TreeLeaf (value = 42 ),
2942
+ right = TreeLeaf (value = 42 ),
2940
2943
),
2941
2944
),
2945
+ leaves = [
2946
+ TreeLeaf (value = 42 ),
2947
+ ],
2942
2948
)
2943
2949
item .serialize (null_check = False )
2944
2950
2945
2951
# now let's nullify an attribute a few levels deep to test that `null_check` propagates
2946
2952
item .left .left .left .value = None
2947
2953
item .serialize (null_check = False )
2948
2954
2955
+ # now with null check
2956
+ with pytest .raises (Exception , match = "Attribute 'left.value' cannot be None" ):
2957
+ item .serialize (null_check = True )
2958
+
2959
+ # now let's nullify an attribute of a map in a list to test that `null_check` propagates
2960
+ item .left .left .left .value = 42
2961
+ item .leaves [0 ].value = None
2962
+ item .serialize (null_check = False )
2963
+
2964
+ # now with null check
2965
+ with pytest .raises (Exception , match = r"Attribute 'value' cannot be None" ):
2966
+ item .serialize (null_check = True )
2967
+
2968
+
2949
2969
def test_deserializing_map_four_layers_deep_works (self ):
2950
2970
fake_db = self .database_mocker (TreeModel ,
2951
2971
TREE_MODEL_TABLE_DATA ,
@@ -3401,8 +3421,8 @@ def test_subclassed_map_attribute_with_map_attributes_member_with_dict_init(self
3401
3421
def test_subclassed_map_attribute_with_map_attribute_member_with_initialized_instance_init (self ):
3402
3422
left = self ._get_bin_tree ()
3403
3423
right = self ._get_bin_tree (multiplier = 2 )
3404
- left_instance = TreeLeaf (** left )
3405
- right_instance = TreeLeaf (** right )
3424
+ left_instance = TreeNode1 (** left )
3425
+ right_instance = TreeNode1 (** right )
3406
3426
actual = TreeModel (tree_key = 'key' , left = left_instance , right = right_instance )
3407
3427
self .assertEqual (actual .left .left .right .value , left_instance .left .right .value )
3408
3428
self .assertEqual (actual .left .left .value , left_instance .left .value )
0 commit comments