Skip to content

Commit

Permalink
Refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gdeandradero authored and eltinMeli committed Feb 9, 2024
1 parent 5bce1d7 commit 16c25a2
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 63 deletions.
25 changes: 10 additions & 15 deletions pkg/payment/payment.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ import (
"strconv"
"strings"

"github.com/mercadopago/sdk-go/pkg/credential"
"github.com/mercadopago/sdk-go/pkg/config"
"github.com/mercadopago/sdk-go/pkg/internal/httpclient"
"github.com/mercadopago/sdk-go/pkg/option"
)

const (
Expand Down Expand Up @@ -52,17 +51,13 @@ type Client interface {

// client is the implementation of Client.
type client struct {
credential *credential.Credential
config *option.ClientOptions
config *config.Config
}

// NewClient returns a new Payments API Client.
func NewClient(cdt *credential.Credential, opts ...option.ClientOption) Client {
c := option.ApplyClientOptions(opts...)

func NewClient(c *config.Config) Client {
return &client{
credential: cdt,
config: c,
config: c,
}
}

Expand All @@ -77,7 +72,7 @@ func (c *client) Create(ctx context.Context, dto Request) (*Response, error) {
return nil, fmt.Errorf("error creating request: %w", err)
}

res, err := httpclient.Send(ctx, c.credential, c.config.Requester, req)
res, err := httpclient.Send(ctx, c.config, req)
if err != nil {
return nil, err
}
Expand All @@ -97,7 +92,7 @@ func (c *client) Search(ctx context.Context, dto SearchRequest) (*SearchResponse
return nil, fmt.Errorf("error creating request: %w", err)
}

res, err := httpclient.Send(ctx, c.credential, c.config.Requester, req)
res, err := httpclient.Send(ctx, c.config, req)
if err != nil {
return nil, err
}
Expand All @@ -118,7 +113,7 @@ func (c *client) Get(ctx context.Context, id int64) (*Response, error) {
return nil, fmt.Errorf("error creating request: %w", err)
}

res, err := httpclient.Send(ctx, c.credential, c.config.Requester, req)
res, err := httpclient.Send(ctx, c.config, req)
if err != nil {
return nil, err
}
Expand All @@ -144,7 +139,7 @@ func (c *client) Cancel(ctx context.Context, id int64) (*Response, error) {
return nil, fmt.Errorf("error creating request: %w", err)
}

res, err := httpclient.Send(ctx, c.credential, c.config.Requester, req)
res, err := httpclient.Send(ctx, c.config, req)
if err != nil {
return nil, err
}
Expand All @@ -170,7 +165,7 @@ func (c *client) Capture(ctx context.Context, id int64) (*Response, error) {
return nil, fmt.Errorf("error creating request: %w", err)
}

res, err := httpclient.Send(ctx, c.credential, c.config.Requester, req)
res, err := httpclient.Send(ctx, c.config, req)
if err != nil {
return nil, err
}
Expand All @@ -196,7 +191,7 @@ func (c *client) CaptureAmount(ctx context.Context, id int64, amount float64) (*
return nil, fmt.Errorf("error creating request: %w", err)
}

res, err := httpclient.Send(ctx, c.credential, c.config.Requester, req)
res, err := httpclient.Send(ctx, c.config, req)
if err != nil {
return nil, err
}
Expand Down
78 changes: 31 additions & 47 deletions pkg/payment/payment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,18 @@ import (
"testing"
"time"

"github.com/mercadopago/sdk-go/pkg/credential"
"github.com/mercadopago/sdk-go/pkg/config"
"github.com/mercadopago/sdk-go/pkg/internal/httpclient"
"github.com/mercadopago/sdk-go/pkg/option"
)

var (
cred, _ = credential.New("")

createResponseJSON, _ = os.Open("../../resources/mocks/payment/create_response.json")
createResponse, _ = io.ReadAll(createResponseJSON)
)

func TestCreate(t *testing.T) {
type fields struct {
credential *credential.Credential
config *option.ClientOptions
config *config.Config
}
type args struct {
ctx context.Context
Expand All @@ -42,8 +38,7 @@ func TestCreate(t *testing.T) {
{
name: "should_fail_to_marshal_dto",
fields: fields{
credential: cred,
config: nil,
config: nil,
},
args: args{
ctx: nil,
Expand All @@ -59,8 +54,7 @@ func TestCreate(t *testing.T) {
{
name: "should_fail_to_create_request",
fields: fields{
credential: cred,
config: nil,
config: nil,
},
args: args{
ctx: nil,
Expand All @@ -71,16 +65,13 @@ func TestCreate(t *testing.T) {
{
name: "should_fail_to_send_request",
fields: fields{
credential: cred,
config: option.ApplyClientOptions(
option.WithCustomClient(
&httpclient.Mock{
DoMock: func(req *http.Request) (*http.Response, error) {
return nil, fmt.Errorf("some error")
},
config: &config.Config{
HTTPClient: &httpclient.Mock{
DoMock: func(req *http.Request) (*http.Response, error) {
return nil, fmt.Errorf("some error")
},
),
),
},
},
},
args: args{
ctx: context.Background(),
Expand All @@ -91,20 +82,17 @@ func TestCreate(t *testing.T) {
{
name: "should_fail_to_unmarshaling_response",
fields: fields{
credential: cred,
config: option.ApplyClientOptions(
option.WithCustomClient(
&httpclient.Mock{
DoMock: func(req *http.Request) (*http.Response, error) {
stringReader := strings.NewReader("invalid json")
stringReadCloser := io.NopCloser(stringReader)
return &http.Response{
Body: stringReadCloser,
}, nil
},
config: &config.Config{
HTTPClient: &httpclient.Mock{
DoMock: func(req *http.Request) (*http.Response, error) {
stringReader := strings.NewReader("invalid json")
stringReadCloser := io.NopCloser(stringReader)
return &http.Response{
Body: stringReadCloser,
}, nil
},
),
),
},
},
},
args: args{
ctx: context.Background(),
Expand All @@ -115,20 +103,17 @@ func TestCreate(t *testing.T) {
{
name: "should_return_formatted_response",
fields: fields{
credential: cred,
config: option.ApplyClientOptions(
option.WithCustomClient(
&httpclient.Mock{
DoMock: func(req *http.Request) (*http.Response, error) {
stringReader := strings.NewReader(string(createResponse))
stringReadCloser := io.NopCloser(stringReader)
return &http.Response{
Body: stringReadCloser,
}, nil
},
config: &config.Config{
HTTPClient: &httpclient.Mock{
DoMock: func(req *http.Request) (*http.Response, error) {
stringReader := strings.NewReader(string(createResponse))
stringReadCloser := io.NopCloser(stringReader)
return &http.Response{
Body: stringReadCloser,
}, nil
},
),
),
},
},
},
args: args{
ctx: context.Background(),
Expand Down Expand Up @@ -174,8 +159,7 @@ func TestCreate(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &client{
credential: tt.fields.credential,
config: tt.fields.config,
config: tt.fields.config,
}
got, err := c.Create(tt.args.ctx, tt.args.dto)
gotErr := ""
Expand Down
2 changes: 1 addition & 1 deletion pkg/paymentmethod/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func TestList(t *testing.T) {
ctx: context.Background(),
},
want: nil,
wantErr: "invalid character 'i' looking for beginning of value",
wantErr: "error unmarshaling response: invalid character 'i' looking for beginning of value",
},
{
name: "should_return_formatted_response",
Expand Down

0 comments on commit 16c25a2

Please sign in to comment.