|
| 1 | +from datetime import datetime |
| 2 | +from typing import ClassVar, Optional |
| 3 | + |
| 4 | +import pytest |
| 5 | +from pydantic import Field |
| 6 | + |
| 7 | +from shade import InvalidRequestError, ShadeObject |
| 8 | + |
| 9 | + |
| 10 | +class Payment(ShadeObject): |
| 11 | + id: str |
| 12 | + amount: int |
| 13 | + currency: str = "USDC" |
| 14 | + created_at: Optional[datetime] = None |
| 15 | + |
| 16 | + |
| 17 | +class Invoice(ShadeObject): |
| 18 | + _id_field: ClassVar[str] = "invoice_id" |
| 19 | + |
| 20 | + invoice_id: str = Field(alias="invoiceId") |
| 21 | + total: int |
| 22 | + |
| 23 | + |
| 24 | +class Account(ShadeObject): |
| 25 | + name: str |
| 26 | + |
| 27 | + |
| 28 | +def test_from_dict_builds_model(): |
| 29 | + payment = Payment.from_dict({"id": "pay_1", "amount": 500}) |
| 30 | + |
| 31 | + assert payment.id == "pay_1" |
| 32 | + assert payment.amount == 500 |
| 33 | + assert payment.currency == "USDC" |
| 34 | + |
| 35 | + |
| 36 | +def test_round_trip_preserves_data(): |
| 37 | + data = { |
| 38 | + "id": "pay_1", |
| 39 | + "amount": 500, |
| 40 | + "currency": "XLM", |
| 41 | + "created_at": datetime(2026, 7, 22, 12, 30), |
| 42 | + } |
| 43 | + payment = Payment.from_dict(data) |
| 44 | + |
| 45 | + assert Payment.from_dict(payment.to_dict()).to_dict() == payment.to_dict() |
| 46 | + assert payment.to_dict() == data |
| 47 | + |
| 48 | + |
| 49 | +def test_round_trip_preserves_aliased_fields(): |
| 50 | + invoice = Invoice.from_dict({"invoiceId": "inv_1", "total": 900}) |
| 51 | + |
| 52 | + dumped = invoice.to_dict() |
| 53 | + assert dumped == {"invoiceId": "inv_1", "total": 900} |
| 54 | + assert Invoice.from_dict(dumped).invoice_id == "inv_1" |
| 55 | + |
| 56 | + |
| 57 | +def test_aliased_fields_also_accept_the_field_name(): |
| 58 | + invoice = Invoice.from_dict({"invoice_id": "inv_2", "total": 10}) |
| 59 | + |
| 60 | + assert invoice.invoice_id == "inv_2" |
| 61 | + |
| 62 | + |
| 63 | +def test_unknown_fields_are_accepted_and_preserved(): |
| 64 | + payment = Payment.from_dict( |
| 65 | + {"id": "pay_1", "amount": 500, "settlement_network": "stellar"} |
| 66 | + ) |
| 67 | + |
| 68 | + assert payment.settlement_network == "stellar" |
| 69 | + assert payment.to_dict()["settlement_network"] == "stellar" |
| 70 | + |
| 71 | + |
| 72 | +def test_type_coercion(): |
| 73 | + payment = Payment.from_dict({"id": "pay_1", "amount": "500"}) |
| 74 | + |
| 75 | + assert payment.amount == 500 |
| 76 | + |
| 77 | + |
| 78 | +def test_repr_shows_class_name_and_id(): |
| 79 | + payment = Payment.from_dict({"id": "pay_1", "amount": 500}) |
| 80 | + |
| 81 | + assert repr(payment) == "<Payment id='pay_1'>" |
| 82 | + |
| 83 | + |
| 84 | +def test_repr_uses_overridden_id_field(): |
| 85 | + invoice = Invoice.from_dict({"invoiceId": "inv_1", "total": 900}) |
| 86 | + |
| 87 | + assert repr(invoice) == "<Invoice invoice_id='inv_1'>" |
| 88 | + |
| 89 | + |
| 90 | +def test_repr_without_an_id_field(): |
| 91 | + assert repr(Account.from_dict({"name": "acme"})) == "<Account>" |
| 92 | + |
| 93 | + |
| 94 | +def test_validation_error_surfaces_as_invalid_request_error(): |
| 95 | + with pytest.raises(InvalidRequestError) as excinfo: |
| 96 | + Payment.from_dict({"id": "pay_1"}) |
| 97 | + |
| 98 | + error = excinfo.value |
| 99 | + assert "1 validation error for Payment" in str(error) |
| 100 | + assert error.param == "amount" |
| 101 | + assert "amount" in error.field_errors |
| 102 | + |
| 103 | + |
| 104 | +def test_validation_error_on_direct_construction(): |
| 105 | + with pytest.raises(InvalidRequestError): |
| 106 | + Payment(id="pay_1", amount="not-a-number") |
| 107 | + |
| 108 | + |
| 109 | +def test_from_dict_rejects_non_dict_input(): |
| 110 | + with pytest.raises(InvalidRequestError) as excinfo: |
| 111 | + Payment.from_dict(["id", "pay_1"]) |
| 112 | + |
| 113 | + assert "expects a dict" in str(excinfo.value) |
0 commit comments