forked from stripe/stripe-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
balance_transaction_test.go
56 lines (47 loc) · 1.33 KB
/
balance_transaction_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package stripe
import (
"encoding/json"
"testing"
assert "github.com/stretchr/testify/require"
)
func TestBalanceTransaction_UnmarshalJSON(t *testing.T) {
// Unmarshals from a JSON string
{
var v BalanceTransaction
err := json.Unmarshal([]byte(`"bt_123"`), &v)
assert.NoError(t, err)
assert.Equal(t, "bt_123", v.ID)
}
// Unmarshals from a JSON object
{
v := BalanceTransaction{ID: "bt_123"}
data, err := json.Marshal(&v)
assert.NoError(t, err)
err = json.Unmarshal(data, &v)
assert.NoError(t, err)
assert.Equal(t, "bt_123", v.ID)
}
}
func TestBalanceTransactionSource_UnmarshalJSON(t *testing.T) {
// Unmarshals from a JSON string
{
var v BalanceTransactionSource
err := json.Unmarshal([]byte(`"ch_123"`), &v)
assert.NoError(t, err)
assert.Equal(t, "ch_123", v.ID)
}
// Unmarshals from a JSON object
{
// We build the JSON object manually here because it's key that the
// `object` field is included so that the source knows what type to
// decode
data := []byte(`{"id":"ch_123", "object":"charge"}`)
var v BalanceTransactionSource
err := json.Unmarshal(data, &v)
assert.NoError(t, err)
assert.Equal(t, BalanceTransactionSourceTypeCharge, v.Type)
// The source has a field for each possible type, so the charge is
// located one level down
assert.Equal(t, "ch_123", v.Charge.ID)
}
}