|
| 1 | +from .models import DATE, DATETIME, MONEY, TIME, ModernFlatModel, ModernNestedModel |
| 2 | + |
| 3 | + |
| 4 | +def test_modern_flat_model_basic(get_instance): |
| 5 | + """Checks to_dict method of modern flat model with no predefined options""" |
| 6 | + i = get_instance(ModernFlatModel) |
| 7 | + data = i.to_dict() |
| 8 | + |
| 9 | + # Check SQLAlchemy fields |
| 10 | + assert "id" in data |
| 11 | + assert "string" in data and data["string"] == i.string |
| 12 | + assert "date_field" in data |
| 13 | + assert "time_field" in data |
| 14 | + assert "datetime_field" in data |
| 15 | + assert "bool_field" in data and data["bool_field"] == i.bool_field |
| 16 | + assert "null" in data and data["null"] is None |
| 17 | + assert "uuid" in data and str(i.uuid) == data["uuid"] |
| 18 | + |
| 19 | + # Check non-sql fields (not included in this case, need to be defined explicitly) |
| 20 | + assert "list" not in data |
| 21 | + assert "set" not in data |
| 22 | + assert "dict" not in data |
| 23 | + assert "prop" not in data |
| 24 | + assert "method" not in data |
| 25 | + assert "_protected_method" not in data |
| 26 | + assert "money" not in data |
| 27 | + |
| 28 | + |
| 29 | +def test_modern_flat_model_with_properties(get_instance): |
| 30 | + "Checks to_dict method of modern flat model with automatic serialization of @properties" |
| 31 | + |
| 32 | + class AutoPropModernFlatModel(ModernFlatModel): |
| 33 | + auto_serialize_properties = True |
| 34 | + |
| 35 | + i = get_instance(AutoPropModernFlatModel) |
| 36 | + data = i.to_dict() |
| 37 | + |
| 38 | + # Check SQLAlchemy fields |
| 39 | + assert "id" in data |
| 40 | + assert "string" in data and data["string"] == i.string |
| 41 | + assert "date_field" in data |
| 42 | + assert "time_field" in data |
| 43 | + assert "datetime_field" in data |
| 44 | + assert "bool_field" in data and data["bool_field"] == i.bool_field |
| 45 | + assert "null" in data and data["null"] is None |
| 46 | + assert "uuid" in data and str(i.uuid) == data["uuid"] |
| 47 | + |
| 48 | + # Properties |
| 49 | + assert "prop" in data |
| 50 | + assert "prop_with_bytes" in data |
| 51 | + |
| 52 | + # Check non-sql fields |
| 53 | + assert "list" not in data |
| 54 | + assert "set" not in data |
| 55 | + assert "dict" not in data |
| 56 | + assert "method" not in data |
| 57 | + assert "_protected_method" not in data |
| 58 | + assert "money" not in data |
| 59 | + |
| 60 | + |
| 61 | +def test_modern_models_formats(get_instance): |
| 62 | + """Check date/datetime/time/decimal default formats for modern models""" |
| 63 | + i = get_instance(ModernFlatModel) |
| 64 | + |
| 65 | + # Default formats |
| 66 | + d_format = i.date_format |
| 67 | + dt_format = i.datetime_format |
| 68 | + t_format = i.time_format |
| 69 | + decimal_format = i.decimal_format |
| 70 | + |
| 71 | + # Include non-SQL field to check decimal_format and bytes |
| 72 | + data = i.to_dict(rules=("money", "prop_with_bytes")) |
| 73 | + |
| 74 | + assert "date_field" in data |
| 75 | + assert data["date_field"] == DATE.strftime(d_format) |
| 76 | + assert "datetime_field" in data |
| 77 | + assert data["datetime_field"] == DATETIME.strftime(dt_format) |
| 78 | + assert "time_field" in data |
| 79 | + assert data["time_field"] == TIME.strftime(t_format) |
| 80 | + |
| 81 | + assert "money" in data |
| 82 | + assert data["money"] == decimal_format.format(MONEY) |
| 83 | + |
| 84 | + assert "prop_with_bytes" in data |
| 85 | + assert data["prop_with_bytes"] == i.prop_with_bytes.decode() |
| 86 | + |
| 87 | + |
| 88 | +def test_modern_nested_model(get_instance): |
| 89 | + """Test relationship serialization with modern nested model""" |
| 90 | + flat = get_instance(ModernFlatModel) |
| 91 | + nested = get_instance(ModernNestedModel, model_id=flat.id) |
| 92 | + |
| 93 | + # Test basic serialization |
| 94 | + data = nested.to_dict() |
| 95 | + assert "id" in data |
| 96 | + assert "string" in data |
| 97 | + assert "model_id" in data |
| 98 | + assert data["model_id"] == flat.id |
| 99 | + |
| 100 | + # Test relationship serialization |
| 101 | + data_with_relation = nested.to_dict(rules=("model",)) |
| 102 | + assert "model" in data_with_relation |
| 103 | + assert isinstance(data_with_relation["model"], dict) |
| 104 | + assert data_with_relation["model"]["id"] == flat.id |
| 105 | + assert data_with_relation["model"]["string"] == flat.string |
| 106 | + |
| 107 | + |
| 108 | +def test_modern_models_rules(get_instance): |
| 109 | + """Test rules and only parameters with modern models""" |
| 110 | + i = get_instance(ModernFlatModel) |
| 111 | + |
| 112 | + # Test rules parameter |
| 113 | + data = i.to_dict(rules=("-id", "prop", "method")) |
| 114 | + assert "id" not in data |
| 115 | + assert "string" in data |
| 116 | + assert "prop" in data |
| 117 | + assert data["prop"] == i.prop |
| 118 | + assert "method" in data |
| 119 | + assert data["method"] == i.method() |
| 120 | + |
| 121 | + # Test only parameter |
| 122 | + data_only = i.to_dict(only=("id", "string", "prop")) |
| 123 | + assert "id" in data_only |
| 124 | + assert "string" in data_only |
| 125 | + assert "prop" in data_only |
| 126 | + assert len(data_only.keys()) == 3 |
0 commit comments