github.com/stripe/stripe-go/v76@v76.25.0/balance_transaction_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 TestBalanceTransaction_UnmarshalJSON(t *testing.T) {
    11  	// Unmarshals from a JSON string
    12  	{
    13  		var v BalanceTransaction
    14  		err := json.Unmarshal([]byte(`"bt_123"`), &v)
    15  		assert.NoError(t, err)
    16  		assert.Equal(t, "bt_123", v.ID)
    17  	}
    18  
    19  	// Unmarshals from a JSON object
    20  	{
    21  		v := BalanceTransaction{ID: "bt_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, "bt_123", v.ID)
    28  	}
    29  }
    30  
    31  func TestBalanceTransactionSource_UnmarshalJSON(t *testing.T) {
    32  	// Unmarshals from a JSON string
    33  	{
    34  		var v BalanceTransactionSource
    35  		err := json.Unmarshal([]byte(`"ch_123"`), &v)
    36  		assert.NoError(t, err)
    37  		assert.Equal(t, "ch_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":"ch_123", "object":"charge"}`)
    46  
    47  		var v BalanceTransactionSource
    48  		err := json.Unmarshal(data, &v)
    49  		assert.NoError(t, err)
    50  		assert.Equal(t, BalanceTransactionSourceTypeCharge, v.Type)
    51  
    52  		// The source has a field for each possible type, so the charge is
    53  		// located one level down
    54  		assert.Equal(t, "ch_123", v.Charge.ID)
    55  	}
    56  }