github.com/stripe/stripe-go/v76@v76.25.0/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  func TestRangeQueryParamsAppendTo(t *testing.T) {
    14  	type testParams struct {
    15  		CreatedRange *stripe.RangeQueryParams `form:"created"`
    16  	}
    17  
    18  	{
    19  		body := &form.Values{}
    20  
    21  		// Try it with an empty set of parameters
    22  		params := testParams{
    23  			CreatedRange: &stripe.RangeQueryParams{},
    24  		}
    25  		form.AppendTo(body, params)
    26  		assert.True(t, body.Empty())
    27  	}
    28  
    29  	{
    30  		body := &form.Values{}
    31  
    32  		params := testParams{
    33  			CreatedRange: &stripe.RangeQueryParams{GreaterThan: 99},
    34  		}
    35  		form.AppendTo(body, params)
    36  		assert.Equal(t, []string{"99"}, body.Get("created[gt]"))
    37  	}
    38  
    39  	{
    40  		body := &form.Values{}
    41  
    42  		params := testParams{
    43  			CreatedRange: &stripe.RangeQueryParams{GreaterThanOrEqual: 99},
    44  		}
    45  		form.AppendTo(body, params)
    46  		assert.Equal(t, []string{"99"}, body.Get("created[gte]"))
    47  	}
    48  
    49  	{
    50  		body := &form.Values{}
    51  
    52  		params := testParams{
    53  			CreatedRange: &stripe.RangeQueryParams{LesserThan: 99},
    54  		}
    55  		form.AppendTo(body, params)
    56  		assert.Equal(t, []string{"99"}, body.Get("created[lt]"))
    57  	}
    58  
    59  	{
    60  		body := &form.Values{}
    61  
    62  		params := testParams{
    63  			CreatedRange: &stripe.RangeQueryParams{LesserThanOrEqual: 99},
    64  		}
    65  		form.AppendTo(body, params)
    66  		assert.Equal(t, []string{"99"}, body.Get("created[lte]"))
    67  	}
    68  }
    69  
    70  type testListParams struct {
    71  	stripe.ListParams `form:"*"`
    72  	Field             string `form:"field"`
    73  }
    74  
    75  func TestListParams_Nested(t *testing.T) {
    76  	params := &testListParams{
    77  		Field: "field_value",
    78  		ListParams: stripe.ListParams{
    79  			EndingBefore:  stripe.String("acct_123"),
    80  			Limit:         stripe.Int64(100),
    81  			StartingAfter: stripe.String("acct_123"),
    82  		},
    83  	}
    84  
    85  	body := &form.Values{}
    86  	form.AppendTo(body, params)
    87  
    88  	assert.Equal(t, valuesFromArray([][2]string{
    89  		{"ending_before", "acct_123"},
    90  		{"limit", "100"},
    91  		{"starting_after", "acct_123"},
    92  		{"field", "field_value"},
    93  	}), body)
    94  }
    95  
    96  func TestParams_AppendTo_Extra(t *testing.T) {
    97  	testCases := []struct {
    98  		InitialBody  [][2]string
    99  		Extras       [][2]string
   100  		ExpectedBody [][2]string
   101  	}{
   102  		{
   103  			InitialBody:  [][2]string{{"foo", "bar"}},
   104  			Extras:       [][2]string{},
   105  			ExpectedBody: [][2]string{{"foo", "bar"}},
   106  		},
   107  		{
   108  			InitialBody:  [][2]string{{"foo", "bar"}},
   109  			Extras:       [][2]string{{"foo", "baz"}},
   110  			ExpectedBody: [][2]string{{"foo", "bar"}, {"foo", "baz"}},
   111  		},
   112  	}
   113  
   114  	for _, testCase := range testCases {
   115  		p := &testParams{}
   116  
   117  		for _, extra := range testCase.Extras {
   118  			p.AddExtra(extra[0], extra[1])
   119  		}
   120  
   121  		body := valuesFromArray(testCase.InitialBody)
   122  		form.AppendTo(body, p)
   123  		assert.Equal(t, valuesFromArray(testCase.ExpectedBody), body)
   124  	}
   125  }
   126  
   127  type testParams struct {
   128  	stripe.Params `form:"*"`
   129  	Field         string         `form:"field"`
   130  	SubParams     *testSubParams `form:"sub_params"`
   131  }
   132  
   133  // AppendTo is implemented for testParams so that we can verify that Params is
   134  // encoded properly even in the case where a leaf struct does a custom
   135  // override.
   136  func (p *testParams) AppendTo(body *form.Values, keyParts []string) {
   137  }
   138  
   139  type testSubParams struct {
   140  	stripe.Params `form:"*"`
   141  	SubField      string `form:"sub_field"`
   142  }
   143  
   144  func TestParams_AppendTo_Nested(t *testing.T) {
   145  	params := &testParams{
   146  		Field: "field_value",
   147  		Params: stripe.Params{
   148  			Metadata: map[string]string{
   149  				"foo": "bar",
   150  			},
   151  		},
   152  		SubParams: &testSubParams{
   153  			Params: stripe.Params{
   154  				Metadata: map[string]string{
   155  					"sub_foo": "bar",
   156  				},
   157  			},
   158  			SubField: "sub_field_value",
   159  		},
   160  	}
   161  
   162  	body := &form.Values{}
   163  	form.AppendTo(body, params)
   164  
   165  	assert.Equal(t, valuesFromArray([][2]string{
   166  		{"metadata[foo]", "bar"},
   167  		{"field", "field_value"},
   168  		{"sub_params[metadata][sub_foo]", "bar"},
   169  		{"sub_params[sub_field]", "sub_field_value"},
   170  	}), body)
   171  }
   172  
   173  func TestListParams_Filters(t *testing.T) {
   174  	p := &testListParams{}
   175  	p.Filters.AddFilter("created", "gt", "123")
   176  
   177  	body := &form.Values{}
   178  	form.AppendTo(body, p)
   179  
   180  	assert.Equal(t, valuesFromArray([][2]string{
   181  		{"created[gt]", "123"},
   182  	}), body)
   183  }
   184  
   185  func TestListParams_Expand(t *testing.T) {
   186  	testCases := []struct {
   187  		InitialBody  [][2]string
   188  		Expand       []string
   189  		ExpectedBody [][2]string
   190  	}{
   191  		{
   192  			InitialBody:  [][2]string{{"foo", "bar"}},
   193  			Expand:       []string{},
   194  			ExpectedBody: [][2]string{{"foo", "bar"}},
   195  		},
   196  		{
   197  			InitialBody:  [][2]string{{"foo", "bar"}, {"foo", "baz"}},
   198  			Expand:       []string{"data", "data.foo"},
   199  			ExpectedBody: [][2]string{{"foo", "bar"}, {"foo", "baz"}, {"expand[0]", "data"}, {"expand[1]", "data.foo"}},
   200  		},
   201  	}
   202  
   203  	for _, testCase := range testCases {
   204  		p := stripe.ListParams{}
   205  
   206  		for _, exp := range testCase.Expand {
   207  			p.AddExpand(exp)
   208  		}
   209  
   210  		body := valuesFromArray(testCase.InitialBody)
   211  		form.AppendTo(body, p)
   212  		assert.Equal(t, valuesFromArray(testCase.ExpectedBody), body)
   213  	}
   214  }
   215  
   216  func TestListParams_SetStripeAccount(t *testing.T) {
   217  	p := &stripe.ListParams{}
   218  	p.SetStripeAccount(TestMerchantID)
   219  	assert.Equal(t, TestMerchantID, *p.StripeAccount)
   220  }
   221  
   222  func TestListParams_ToParams(t *testing.T) {
   223  	listParams := &stripe.ListParams{
   224  		Context: context.Background(),
   225  	}
   226  	listParams.SetStripeAccount(TestMerchantID)
   227  	params := listParams.ToParams()
   228  	assert.Equal(t, listParams.Context, params.Context)
   229  	assert.Equal(t, *listParams.StripeAccount, *params.StripeAccount)
   230  }
   231  
   232  func TestParams_SetIdempotencyKey(t *testing.T) {
   233  	p := &stripe.Params{}
   234  	p.SetIdempotencyKey("my-idempotency-key")
   235  	assert.Equal(t, "my-idempotency-key", *p.IdempotencyKey)
   236  }
   237  
   238  func TestParams_SetStripeAccount(t *testing.T) {
   239  	p := &stripe.Params{}
   240  	p.SetStripeAccount(TestMerchantID)
   241  	assert.Equal(t, TestMerchantID, *p.StripeAccount)
   242  }
   243  
   244  //
   245  // ---
   246  //
   247  
   248  // Converts a collection of key/value tuples in a two dimensional slice/array
   249  // into form.Values form. The purpose of this is that it's much cleaner to
   250  // initialize the array all at once on a single line.
   251  func valuesFromArray(arr [][2]string) *form.Values {
   252  	body := &form.Values{}
   253  	for _, v := range arr {
   254  		body.Add(v[0], v[1])
   255  	}
   256  	return body
   257  }