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

     1  package stripe_test
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	assert "github.com/stretchr/testify/require"
     8  	stripe "github.com/stripe/stripe-go/v76"
     9  	"github.com/stripe/stripe-go/v76/form"
    10  	. "github.com/stripe/stripe-go/v76/testing"
    11  )
    12  
    13  type testSearchParams struct {
    14  	stripe.SearchParams `form:"*"`
    15  	Page                *string `form:"page"`
    16  }
    17  
    18  func TestSearchParams_Nested(t *testing.T) {
    19  	params := &testSearchParams{
    20  		Page: stripe.String("page_value"),
    21  		SearchParams: stripe.SearchParams{
    22  			Query: "query_value",
    23  		},
    24  	}
    25  
    26  	body := &form.Values{}
    27  	form.AppendTo(body, params)
    28  
    29  	assert.Equal(t, valuesFromArray([][2]string{
    30  		{"query", "query_value"},
    31  		{"page", "page_value"},
    32  	}), body)
    33  }
    34  
    35  func TestSearchParams_Expand(t *testing.T) {
    36  	testCases := []struct {
    37  		InitialBody  [][2]string
    38  		Expand       []string
    39  		ExpectedBody [][2]string
    40  	}{
    41  		{
    42  			InitialBody:  [][2]string{{"foo", "bar"}},
    43  			Expand:       []string{},
    44  			ExpectedBody: [][2]string{{"foo", "bar"}},
    45  		},
    46  		{
    47  			InitialBody:  [][2]string{{"foo", "bar"}, {"foo", "baz"}},
    48  			Expand:       []string{"data", "data.foo"},
    49  			ExpectedBody: [][2]string{{"foo", "bar"}, {"foo", "baz"}, {"expand[0]", "data"}, {"expand[1]", "data.foo"}},
    50  		},
    51  	}
    52  
    53  	for _, testCase := range testCases {
    54  		p := stripe.SearchParams{}
    55  
    56  		for _, exp := range testCase.Expand {
    57  			p.AddExpand(exp)
    58  		}
    59  
    60  		body := valuesFromArray(testCase.InitialBody)
    61  		form.AppendTo(body, p)
    62  		assert.Equal(t, valuesFromArray(testCase.ExpectedBody), body)
    63  	}
    64  }
    65  
    66  func TestSearchParams_SetStripeAccount(t *testing.T) {
    67  	p := &stripe.SearchParams{}
    68  	p.SetStripeAccount(TestMerchantID)
    69  	assert.Equal(t, TestMerchantID, *p.StripeAccount)
    70  }
    71  
    72  func TestSearchParams_ToParams(t *testing.T) {
    73  	SearchParams := &stripe.SearchParams{
    74  		Context: context.Background(),
    75  	}
    76  	SearchParams.SetStripeAccount(TestMerchantID)
    77  	params := SearchParams.ToParams()
    78  	assert.Equal(t, SearchParams.Context, params.Context)
    79  	assert.Equal(t, *SearchParams.StripeAccount, *params.StripeAccount)
    80  }