github.com/stripe/stripe-go/v76@v76.25.0/payout_test.go (about)

     1  package stripe
     2  
     3  import (
     4  	"encoding/json"
     5  	"testing"
     6  
     7  	assert "github.com/stretchr/testify/require"
     8  )
     9  
    10  func TestPayout_UnmarshalJSON(t *testing.T) {
    11  	// Unmarshals from a JSON string
    12  	{
    13  		var v Payout
    14  		err := json.Unmarshal([]byte(`"po_123"`), &v)
    15  		assert.NoError(t, err)
    16  		assert.Equal(t, "po_123", v.ID)
    17  	}
    18  
    19  	// Unmarshals from a JSON object
    20  	{
    21  		v := Payout{ID: "po_123"}
    22  		data, err := json.Marshal(&v)
    23  		assert.NoError(t, err)
    24  
    25  		err = json.Unmarshal(data, &v)
    26  		assert.NoError(t, err)
    27  		assert.Equal(t, "po_123", v.ID)
    28  	}
    29  }
    30  
    31  func TestPayoutDestination_UnmarshalJSON(t *testing.T) {
    32  	// Unmarshals from a JSON string
    33  	{
    34  		var v PayoutDestination
    35  		err := json.Unmarshal([]byte(`"ba_123"`), &v)
    36  		assert.NoError(t, err)
    37  		assert.Equal(t, "ba_123", v.ID)
    38  	}
    39  
    40  	// Unmarshals from a JSON object
    41  	{
    42  		// We build the JSON object manually here because it's key that the
    43  		// `object` field is included so that the source knows what type to
    44  		// decode
    45  		data := []byte(`{"id":"ba_123", "object":"bank_account"}`)
    46  
    47  		var v PayoutDestination
    48  		err := json.Unmarshal(data, &v)
    49  		assert.NoError(t, err)
    50  		assert.Equal(t, PayoutDestinationTypeBankAccount, v.Type)
    51  
    52  		// The destination has a field for each possible type, so the bank
    53  		// account is located one level down
    54  		assert.Equal(t, "ba_123", v.BankAccount.ID)
    55  	}
    56  }