|
| 1 | +from datetime import datetime |
| 2 | +from decimal import Decimal |
| 3 | + |
| 4 | +import pytest |
| 5 | +from stellar_sdk import Keypair |
| 6 | + |
| 7 | +import shade |
| 8 | +from shade import InvalidRequestError, ShadeObject, Transfer, TransferStatus |
| 9 | + |
| 10 | +SOURCE_ADDRESS = Keypair.random().public_key |
| 11 | +DESTINATION_ADDRESS = Keypair.random().public_key |
| 12 | + |
| 13 | + |
| 14 | +def _api_response(**overrides): |
| 15 | + """A representative camelCase backend payload.""" |
| 16 | + data = { |
| 17 | + "id": "trf_123", |
| 18 | + "sourceAddress": SOURCE_ADDRESS, |
| 19 | + "destinationAddress": DESTINATION_ADDRESS, |
| 20 | + "amount": "150.25", |
| 21 | + "asset": "USDC", |
| 22 | + "status": "pending", |
| 23 | + "stellarTxHash": None, |
| 24 | + "fee": "0.5", |
| 25 | + "createdAt": "2026-07-20T12:00:00Z", |
| 26 | + } |
| 27 | + data.update(overrides) |
| 28 | + return data |
| 29 | + |
| 30 | + |
| 31 | +def test_from_dict_maps_camelcase_to_snake_case(): |
| 32 | + transfer = Transfer.from_dict(_api_response()) |
| 33 | + |
| 34 | + assert transfer.id == "trf_123" |
| 35 | + assert transfer.source_address == SOURCE_ADDRESS |
| 36 | + assert transfer.destination_address == DESTINATION_ADDRESS |
| 37 | + assert transfer.amount == Decimal("150.25") |
| 38 | + assert transfer.asset == "USDC" |
| 39 | + assert transfer.status == TransferStatus.PENDING |
| 40 | + assert transfer.stellar_tx_hash is None |
| 41 | + assert transfer.fee == Decimal("0.5") |
| 42 | + assert transfer.created_at == datetime.fromisoformat("2026-07-20T12:00:00+00:00") |
| 43 | + |
| 44 | + |
| 45 | +def test_amount_and_fee_are_decimal(): |
| 46 | + transfer = Transfer.from_dict(_api_response(amount="99.99", fee="1.10")) |
| 47 | + assert isinstance(transfer.amount, Decimal) |
| 48 | + assert isinstance(transfer.fee, Decimal) |
| 49 | + |
| 50 | + |
| 51 | +def test_asset_defaults_to_xlm_when_absent(): |
| 52 | + payload = _api_response() |
| 53 | + del payload["asset"] |
| 54 | + transfer = Transfer.from_dict(payload) |
| 55 | + assert transfer.asset == "XLM" |
| 56 | + |
| 57 | + |
| 58 | +def test_asset_defaults_to_xlm_when_null(): |
| 59 | + transfer = Transfer.from_dict(_api_response(asset=None)) |
| 60 | + assert transfer.asset == "XLM" |
| 61 | + |
| 62 | + |
| 63 | +def test_malformed_asset_is_rejected_not_defaulted(): |
| 64 | + # False/0 are falsy but must not be silently coerced to "XLM" — they are |
| 65 | + # the wrong type and should surface as a validation error instead. |
| 66 | + with pytest.raises(InvalidRequestError) as exc_info: |
| 67 | + Transfer.from_dict(_api_response(asset=False)) |
| 68 | + assert exc_info.value.param == "asset" |
| 69 | + |
| 70 | + |
| 71 | +def test_status_is_transfer_status_enum(): |
| 72 | + for raw in ("pending", "processing", "completed", "failed"): |
| 73 | + transfer = Transfer.from_dict(_api_response(status=raw)) |
| 74 | + assert isinstance(transfer.status, TransferStatus) |
| 75 | + assert transfer.status.value == raw |
| 76 | + |
| 77 | + |
| 78 | +def test_invalid_status_raises(): |
| 79 | + with pytest.raises(InvalidRequestError) as exc_info: |
| 80 | + Transfer.from_dict(_api_response(status="bogus")) |
| 81 | + assert exc_info.value.param == "status" |
| 82 | + |
| 83 | + |
| 84 | +def test_completed_transfer_has_stellar_tx_hash(): |
| 85 | + transfer = Transfer.from_dict( |
| 86 | + _api_response(status="completed", stellarTxHash="a" * 64) |
| 87 | + ) |
| 88 | + assert transfer.status is TransferStatus.COMPLETED |
| 89 | + assert transfer.stellar_tx_hash == "a" * 64 |
| 90 | + |
| 91 | + |
| 92 | +def test_fee_defaults_to_none(): |
| 93 | + payload = _api_response() |
| 94 | + del payload["fee"] |
| 95 | + transfer = Transfer.from_dict(payload) |
| 96 | + assert transfer.fee is None |
| 97 | + |
| 98 | + |
| 99 | +def test_transfer_is_exported_from_package(): |
| 100 | + assert shade.Transfer is Transfer |
| 101 | + assert shade.TransferStatus is TransferStatus |
| 102 | + assert issubclass(Transfer, ShadeObject) |
| 103 | + |
| 104 | + |
| 105 | +def test_from_dict_requires_a_mapping(): |
| 106 | + with pytest.raises(InvalidRequestError): |
| 107 | + Transfer.from_dict([("id", "x")]) # type: ignore[arg-type] |
| 108 | + |
| 109 | + |
| 110 | +def test_missing_id_raises_clear_validation_error(): |
| 111 | + payload = _api_response() |
| 112 | + del payload["id"] |
| 113 | + with pytest.raises(InvalidRequestError) as exc_info: |
| 114 | + Transfer.from_dict(payload) |
| 115 | + assert exc_info.value.param == "id" |
| 116 | + |
| 117 | + |
| 118 | +def test_invalid_source_address_is_rejected(): |
| 119 | + with pytest.raises(InvalidRequestError) as exc_info: |
| 120 | + Transfer.from_dict(_api_response(sourceAddress="not-a-stellar-key")) |
| 121 | + assert exc_info.value.param == "sourceAddress" |
| 122 | + |
| 123 | + |
| 124 | +def test_invalid_destination_address_is_rejected(): |
| 125 | + with pytest.raises(InvalidRequestError) as exc_info: |
| 126 | + Transfer.from_dict(_api_response(destinationAddress="not-a-stellar-key")) |
| 127 | + assert exc_info.value.param == "destinationAddress" |
| 128 | + |
| 129 | + |
| 130 | +def test_non_positive_amount_is_rejected(): |
| 131 | + with pytest.raises(InvalidRequestError) as exc_info: |
| 132 | + Transfer.from_dict(_api_response(amount="0")) |
| 133 | + assert exc_info.value.param == "amount" |
| 134 | + |
| 135 | + |
| 136 | +def test_negative_fee_is_rejected(): |
| 137 | + with pytest.raises(InvalidRequestError) as exc_info: |
| 138 | + Transfer.from_dict(_api_response(fee="-1")) |
| 139 | + assert exc_info.value.param == "fee" |
| 140 | + |
| 141 | + |
| 142 | +def test_from_dict_preserves_unknown_keys(): |
| 143 | + transfer = Transfer.from_dict(_api_response(description="Payout for order #9")) |
| 144 | + assert transfer.to_dict()["description"] == "Payout for order #9" |
| 145 | + |
| 146 | + |
| 147 | +def test_to_dict_round_trips_to_camelcase(): |
| 148 | + payload = _api_response() |
| 149 | + transfer = Transfer.from_dict(payload) |
| 150 | + round_tripped = transfer.to_dict() |
| 151 | + assert round_tripped["sourceAddress"] == SOURCE_ADDRESS |
| 152 | + assert round_tripped["destinationAddress"] == DESTINATION_ADDRESS |
| 153 | + assert round_tripped["stellarTxHash"] is None |
| 154 | + assert Transfer.from_dict(round_tripped) == transfer |
0 commit comments