|
| 1 | +import math |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from .models import FlatModel, NestedModel, RecursiveModel |
| 6 | + |
| 7 | + |
| 8 | +@pytest.fixture |
| 9 | +def three_level_recursive_model(get_instance): |
| 10 | + """Create a 3-level recursive model structure: root -> child -> grandchild""" |
| 11 | + root = get_instance(RecursiveModel) |
| 12 | + child_full = get_instance(RecursiveModel, parent_id=root.id) |
| 13 | + get_instance(RecursiveModel, parent_id=child_full.id) |
| 14 | + return root |
| 15 | + |
| 16 | + |
| 17 | +def test_default_unlimited_depth(three_level_recursive_model): |
| 18 | + """Test that default behavior maintains unlimited depth (backward compatible)""" |
| 19 | + # Default should be unlimited (math.inf) |
| 20 | + data = three_level_recursive_model.to_dict() |
| 21 | + |
| 22 | + assert "children" in data |
| 23 | + assert "children" in data["children"][0] |
| 24 | + assert "children" in data["children"][0]["children"][0] |
| 25 | + |
| 26 | + assert "name" in data |
| 27 | + assert "name" in data["children"][0] |
| 28 | + assert "name" in data["children"][0]["children"][0] |
| 29 | + |
| 30 | + |
| 31 | +def test_model_level_max_depth(three_level_recursive_model): |
| 32 | + """Test max_serialization_depth set on model class""" |
| 33 | + # Set max depth to 1 (only serialize direct children, not grandchildren) |
| 34 | + three_level_recursive_model.max_serialization_depth = 1 |
| 35 | + data = three_level_recursive_model.to_dict() |
| 36 | + |
| 37 | + assert "children" in data |
| 38 | + # But grandchildren should not be serialized |
| 39 | + assert "children" not in data["children"][0] |
| 40 | + |
| 41 | + assert "name" in data |
| 42 | + assert "name" in data["children"][0] # Name is still there |
| 43 | + |
| 44 | + |
| 45 | +def test_per_call_max_depth(three_level_recursive_model): |
| 46 | + """Test max_serialization_depth passed to to_dict() method""" |
| 47 | + # Per-call max depth should override model-level setting |
| 48 | + three_level_recursive_model.max_serialization_depth = 2 |
| 49 | + data = three_level_recursive_model.to_dict(max_serialization_depth=0) |
| 50 | + |
| 51 | + # At depth 0, no relationships should be serialized |
| 52 | + assert "children" not in data |
| 53 | + assert "name" in data |
| 54 | + |
| 55 | + |
| 56 | +def test_depth_limit_enforcement_nested_model(get_instance): |
| 57 | + """Test depth limit enforcement with NestedModel""" |
| 58 | + nested = get_instance(NestedModel, model_id=get_instance(FlatModel).id) |
| 59 | + |
| 60 | + # At depth 0, nested relationships should not be serialized |
| 61 | + data = nested.to_dict(max_serialization_depth=0) |
| 62 | + assert "model" not in data |
| 63 | + |
| 64 | + # At depth 1, one level of nesting should work |
| 65 | + data = nested.to_dict(max_serialization_depth=1) |
| 66 | + assert "model" in data |
| 67 | + nested_model = data["model"] |
| 68 | + assert "id" in nested_model |
| 69 | + assert "string" in nested_model |
| 70 | + # FlatModel doesn't have a relationship back, so no further nesting |
| 71 | + |
| 72 | + |
| 73 | +def test_depth_limit_with_rules(three_level_recursive_model): |
| 74 | + """Test that depth limit works together with existing rules""" |
| 75 | + # Combine depth limit with rules |
| 76 | + data = three_level_recursive_model.to_dict( |
| 77 | + max_serialization_depth=1, rules=("-children.id",) |
| 78 | + ) |
| 79 | + |
| 80 | + assert "children" in data |
| 81 | + assert "id" not in data["children"][0] # Rule applied |
| 82 | + assert "name" in data["children"][0] |
| 83 | + # Depth limit still enforced |
| 84 | + assert "children" not in data["children"][0] |
| 85 | + |
| 86 | + |
| 87 | +def test_depth_limit_zero(get_instance): |
| 88 | + """Test that depth 0 prevents all relationship serialization""" |
| 89 | + root = get_instance(RecursiveModel) |
| 90 | + get_instance(RecursiveModel, parent_id=root.id) |
| 91 | + |
| 92 | + data = root.to_dict(max_serialization_depth=0) |
| 93 | + |
| 94 | + # Only top-level fields should be present |
| 95 | + assert "id" in data |
| 96 | + assert "name" in data |
| 97 | + assert "children" not in data |
| 98 | + |
| 99 | + |
| 100 | +@pytest.mark.parametrize("depth", [1, 2]) |
| 101 | +def test_depth_limit_multiple_levels(three_level_recursive_model, depth): |
| 102 | + data = three_level_recursive_model.to_dict(max_serialization_depth=depth) |
| 103 | + |
| 104 | + for i in range(depth): |
| 105 | + assert "id" in data |
| 106 | + assert "name" in data |
| 107 | + if i < depth: |
| 108 | + assert "children" in data |
| 109 | + data = data["children"][0] |
| 110 | + |
| 111 | + |
| 112 | +def test_depth_limit_infinity(three_level_recursive_model): |
| 113 | + """Test that math.inf allows unlimited depth""" |
| 114 | + data = three_level_recursive_model.to_dict(max_serialization_depth=math.inf) |
| 115 | + |
| 116 | + # Should serialize all levels |
| 117 | + assert "children" in data |
| 118 | + assert "children" in data["children"][0] |
| 119 | + assert "children" in data["children"][0]["children"][0] |
| 120 | + |
| 121 | + |
| 122 | +def test_model_level_depth_override(get_instance): |
| 123 | + """Test that per-call parameter overrides model-level setting""" |
| 124 | + root = get_instance(RecursiveModel) |
| 125 | + get_instance(RecursiveModel, parent_id=root.id) |
| 126 | + |
| 127 | + # Set model-level depth to 1 |
| 128 | + root.max_serialization_depth = 1 |
| 129 | + # But override with per-call parameter |
| 130 | + data = root.to_dict(max_serialization_depth=0) |
| 131 | + |
| 132 | + # Per-call parameter should win |
| 133 | + assert "children" not in data |
| 134 | + |
| 135 | + |
| 136 | +def test_to_dict_param_overrides_class_level_max_depth(three_level_recursive_model): |
| 137 | + """Test that max_serialization_depth parameter in to_dict() |
| 138 | + overrides class-level setting""" |
| 139 | + three_level_recursive_model.max_serialization_depth = 1 |
| 140 | + |
| 141 | + # First, verify class-level setting works when no parameter is passed |
| 142 | + data_without_param = three_level_recursive_model.to_dict() |
| 143 | + assert "children" in data_without_param |
| 144 | + assert "children" not in data_without_param["children"][0] # Child has children field |
| 145 | + |
| 146 | + # Now verify that passing parameter overrides class-level setting |
| 147 | + # Override with depth 2 (should allow grandchildren) |
| 148 | + data_with_override = three_level_recursive_model.to_dict(max_serialization_depth=2) |
| 149 | + assert "children" in data_with_override |
| 150 | + # Parameter override allows deeper nesting |
| 151 | + assert "children" in data_with_override["children"][0] |
| 152 | + |
| 153 | + # Override with depth 0 (should prevent all relationships) |
| 154 | + data_with_zero = three_level_recursive_model.to_dict(max_serialization_depth=0) |
| 155 | + assert "children" not in data_with_zero |
| 156 | + assert "id" in data_with_zero |
| 157 | + assert "name" in data_with_zero |
| 158 | + |
| 159 | + # Override with math.inf (should allow unlimited depth) |
| 160 | + data_with_inf = three_level_recursive_model.to_dict(max_serialization_depth=math.inf) |
| 161 | + assert "children" in data_with_inf |
| 162 | + assert "children" in data_with_inf["children"][0] |
| 163 | + assert "children" in data_with_inf["children"][0]["children"][0] |
0 commit comments