|
| 1 | +from .models import FlatModel, NestedModel |
| 2 | + |
| 3 | + |
| 4 | +def test_serialize_columns_class_level(get_instance): |
| 5 | + """Test class-level serialize_columns attribute""" |
| 6 | + |
| 7 | + class CustomFlatModel(FlatModel): |
| 8 | + serialize_columns = { |
| 9 | + "id": lambda v: str(v), |
| 10 | + "string": lambda v: v.upper() if v else None, |
| 11 | + } |
| 12 | + |
| 13 | + i = get_instance(CustomFlatModel) |
| 14 | + data = i.to_dict() |
| 15 | + |
| 16 | + # Custom serializer should be applied |
| 17 | + assert "id" in data |
| 18 | + assert isinstance(data["id"], str) |
| 19 | + assert data["id"] == str(i.id) |
| 20 | + |
| 21 | + assert "string" in data |
| 22 | + assert data["string"] == i.string.upper() |
| 23 | + |
| 24 | + # Other fields should use normal serialization |
| 25 | + assert "bool" in data |
| 26 | + assert data["bool"] == i.bool |
| 27 | + |
| 28 | + |
| 29 | +def test_serialize_columns_parameter_level(get_instance): |
| 30 | + """Test parameter-level serialize_columns in to_dict()""" |
| 31 | + i = get_instance(FlatModel) |
| 32 | + data = i.to_dict( |
| 33 | + serialize_columns={ |
| 34 | + "id": lambda v: f"ID_{v}", |
| 35 | + "string": lambda v: v.lower() if v else None, |
| 36 | + } |
| 37 | + ) |
| 38 | + |
| 39 | + # Custom serializer should be applied |
| 40 | + assert "id" in data |
| 41 | + assert data["id"] == f"ID_{i.id}" |
| 42 | + |
| 43 | + assert "string" in data |
| 44 | + assert data["string"] == i.string.lower() |
| 45 | + |
| 46 | + # Other fields should use normal serialization |
| 47 | + assert "bool" in data |
| 48 | + assert data["bool"] == i.bool |
| 49 | + |
| 50 | + |
| 51 | +def test_serialize_columns_replaces_normal_serialization(get_instance): |
| 52 | + """Test that custom serializer replaces normal serialization""" |
| 53 | + i = get_instance(FlatModel) |
| 54 | + |
| 55 | + # Custom serializer that returns a fixed value |
| 56 | + data = i.to_dict( |
| 57 | + serialize_columns={ |
| 58 | + "id": lambda _: "CUSTOM_ID", |
| 59 | + "bool": lambda _: "CUSTOM_BOOL", |
| 60 | + } |
| 61 | + ) |
| 62 | + |
| 63 | + # Custom serializers should be used instead of normal serialization |
| 64 | + assert data["id"] == "CUSTOM_ID" |
| 65 | + assert data["bool"] == "CUSTOM_BOOL" |
| 66 | + |
| 67 | + # Other fields should still use normal serialization |
| 68 | + assert data["string"] == i.string |
| 69 | + |
| 70 | + |
| 71 | +def test_serialize_columns_with_none_value(get_instance): |
| 72 | + """Test custom serializer with None values""" |
| 73 | + i = get_instance(FlatModel) |
| 74 | + i.null = None |
| 75 | + |
| 76 | + data = i.to_dict( |
| 77 | + serialize_columns={ |
| 78 | + "null": lambda v: "NULL_VALUE" if v is None else v, |
| 79 | + } |
| 80 | + ) |
| 81 | + |
| 82 | + assert "null" in data |
| 83 | + assert data["null"] == "NULL_VALUE" |
| 84 | + |
| 85 | + |
| 86 | +def test_serialize_columns_with_callable_value(get_instance): |
| 87 | + """Test custom serializer with callable values""" |
| 88 | + i = get_instance(FlatModel) |
| 89 | + |
| 90 | + # The serializer should receive the result of the callable, not the callable itself |
| 91 | + data = i.to_dict( |
| 92 | + serialize_columns={ |
| 93 | + "method": lambda v: f"METHOD_RESULT: {v}", |
| 94 | + }, |
| 95 | + rules=("method",), |
| 96 | + ) |
| 97 | + |
| 98 | + assert "method" in data |
| 99 | + assert "METHOD_RESULT:" in data["method"] |
| 100 | + assert i.method() in data["method"] |
| 101 | + |
| 102 | + |
| 103 | +def test_serialize_columns_backward_compatibility(get_instance): |
| 104 | + """Test that empty dict default doesn't break anything""" |
| 105 | + i = get_instance(FlatModel) |
| 106 | + |
| 107 | + # Should work exactly as before when serialize_columns is not provided |
| 108 | + data = i.to_dict() |
| 109 | + |
| 110 | + assert "id" in data |
| 111 | + assert data["id"] == i.id |
| 112 | + assert "string" in data |
| 113 | + assert data["string"] == i.string |
| 114 | + assert "bool" in data |
| 115 | + assert data["bool"] == i.bool |
| 116 | + |
| 117 | + |
| 118 | +def test_serialize_columns_parameter_overrides_class(get_instance): |
| 119 | + """Test that parameter-level serialize_columns overrides class-level""" |
| 120 | + |
| 121 | + class CustomFlatModel(FlatModel): |
| 122 | + serialize_columns = { |
| 123 | + "id": lambda v: f"CLASS_{v}", |
| 124 | + } |
| 125 | + |
| 126 | + i = get_instance(CustomFlatModel) |
| 127 | + |
| 128 | + # Parameter should override class-level |
| 129 | + data = i.to_dict( |
| 130 | + serialize_columns={ |
| 131 | + "id": lambda v: f"PARAM_{v}", |
| 132 | + } |
| 133 | + ) |
| 134 | + |
| 135 | + assert "id" in data |
| 136 | + assert data["id"] == f"PARAM_{i.id}" |
| 137 | + assert "CLASS_" not in data["id"] |
| 138 | + |
| 139 | + |
| 140 | +def test_serialize_columns_with_nested_model(get_instance): |
| 141 | + """Test custom serializer with nested models/relationships""" |
| 142 | + flat = get_instance(FlatModel) |
| 143 | + nested = get_instance(NestedModel, model_id=flat.id) |
| 144 | + nested.model = flat |
| 145 | + |
| 146 | + data = nested.to_dict( |
| 147 | + rules=("model",), |
| 148 | + serialize_columns={ |
| 149 | + "model": lambda v: {"custom": "serialized", "id": v.id} if v else None, |
| 150 | + }, |
| 151 | + ) |
| 152 | + |
| 153 | + assert "model" in data |
| 154 | + assert data["model"]["custom"] == "serialized" |
| 155 | + assert data["model"]["id"] == flat.id |
| 156 | + # Normal serialization should be bypassed |
| 157 | + assert "string" not in data["model"] |
| 158 | + |
| 159 | + |
| 160 | +def test_serialize_columns_with_dict(get_instance): |
| 161 | + """Test custom serializer with nested dictionaries""" |
| 162 | + i = get_instance(FlatModel) |
| 163 | + i.dict = {"key": 123, "key2": 456} |
| 164 | + |
| 165 | + data = i.to_dict( |
| 166 | + rules=("dict",), |
| 167 | + serialize_columns={ |
| 168 | + "dict": lambda v: {"custom": "dict", "original": v}, |
| 169 | + }, |
| 170 | + ) |
| 171 | + |
| 172 | + assert "dict" in data |
| 173 | + assert data["dict"]["custom"] == "dict" |
| 174 | + assert data["dict"]["original"] == {"key": 123, "key2": 456} |
| 175 | + |
| 176 | + |
| 177 | +def test_serialize_columns_passed_through_fork(get_instance): |
| 178 | + """Test that custom serializer is passed through forks correctly""" |
| 179 | + flat = get_instance(FlatModel) |
| 180 | + nested = get_instance(NestedModel, model_id=flat.id) |
| 181 | + nested.model = flat |
| 182 | + |
| 183 | + # Custom serializer for nested field |
| 184 | + data = nested.to_dict( |
| 185 | + rules=("model", "model.id"), |
| 186 | + serialize_columns={ |
| 187 | + "id": lambda v: f"ID_{v}", # Should apply to nested model's id |
| 188 | + }, |
| 189 | + ) |
| 190 | + |
| 191 | + assert "model" in data |
| 192 | + assert "id" in data["model"] |
| 193 | + # The custom serializer should be applied to the nested model's id |
| 194 | + assert data["model"]["id"] == f"ID_{flat.id}" |
| 195 | + |
| 196 | + |
| 197 | +def test_serialize_columns_only_applies_to_matching_keys(get_instance): |
| 198 | + """Test that custom serializers only apply to matching column names""" |
| 199 | + i = get_instance(FlatModel) |
| 200 | + |
| 201 | + data = i.to_dict( |
| 202 | + serialize_columns={ |
| 203 | + "id": lambda _: "CUSTOM", |
| 204 | + "nonexistent": lambda _: "SHOULD_NOT_APPEAR", |
| 205 | + } |
| 206 | + ) |
| 207 | + |
| 208 | + # Only matching keys should use custom serializer |
| 209 | + assert data["id"] == "CUSTOM" |
| 210 | + assert "nonexistent" not in data |
| 211 | + |
| 212 | + # Other fields should use normal serialization |
| 213 | + assert data["string"] == i.string |
| 214 | + assert data["bool"] == i.bool |
| 215 | + |
| 216 | + |
| 217 | +def test_serialize_columns_with_multiple_fields(get_instance): |
| 218 | + """Test custom serializers with multiple fields""" |
| 219 | + i = get_instance(FlatModel) |
| 220 | + |
| 221 | + data = i.to_dict( |
| 222 | + serialize_columns={ |
| 223 | + "id": lambda v: str(v), |
| 224 | + "string": lambda v: v.upper() if v else None, |
| 225 | + "bool": lambda v: "YES" if v else "NO", |
| 226 | + } |
| 227 | + ) |
| 228 | + |
| 229 | + assert isinstance(data["id"], str) |
| 230 | + assert data["string"] == i.string.upper() |
| 231 | + assert data["bool"] == "YES" # i.bool is True by default |
| 232 | + |
| 233 | + |
| 234 | +def test_serialize_columns_class_level_with_nested(get_instance): |
| 235 | + """Test class-level serialize_columns with nested structures""" |
| 236 | + |
| 237 | + class CustomNestedModel(NestedModel): |
| 238 | + serialize_columns = { |
| 239 | + "id": lambda v: f"NESTED_ID_{v}", |
| 240 | + } |
| 241 | + |
| 242 | + flat = get_instance(FlatModel) |
| 243 | + nested = get_instance(CustomNestedModel, model_id=flat.id) |
| 244 | + nested.model = flat |
| 245 | + |
| 246 | + data = nested.to_dict(rules=("id", "model", "model.id")) |
| 247 | + |
| 248 | + # Class-level custom serializer should apply |
| 249 | + assert data["id"] == f"NESTED_ID_{nested.id}" |
| 250 | + assert data["model"]["id"] == f"NESTED_ID_{flat.id}" |
0 commit comments