Skip to content

Commit

Permalink
Merge pull request #233 from EasyPost/error_comparison
Browse files Browse the repository at this point in the history
fix: use the newer preferred method for error comparison in tests
  • Loading branch information
Justintime50 committed Aug 9, 2024
2 parents 09021fd + f43d9cf commit 4943c56
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
15 changes: 9 additions & 6 deletions tests/error_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package easypost_test

import (
"errors"
"io"
"net/http"
"strings"
Expand All @@ -18,13 +19,15 @@ func (c *ClientTests) TestApiError() {
_, err := client.CreateShipment(&easypost.Shipment{})

require.Error(err)
if err, ok := err.(*easypost.InvalidRequestError); ok {
assert.Equal(422, err.StatusCode)
assert.Equal("PARAMETER.REQUIRED", err.Code)
assert.Equal("Missing required parameter.", err.Message)
assert.Equal(1, len(err.Errors))

subError := err.Errors[0]
var eperr *easypost.InvalidRequestError
if errors.As(err, &eperr) {
assert.Equal(422, eperr.StatusCode)
assert.Equal("PARAMETER.REQUIRED", eperr.Code)
assert.Equal("Missing required parameter.", eperr.Message)
assert.Equal(1, len(eperr.Errors))

subError := eperr.Errors[0]
assert.Equal("shipment", subError.Field)
assert.Equal("cannot be blank", subError.Message)
}
Expand Down
31 changes: 19 additions & 12 deletions tests/referral_customer_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package easypost_test

import (
"errors"
"reflect"
"strings"

Expand All @@ -13,10 +14,12 @@ func (c *ClientTests) TestBetaReferralAddPaymentMethod() {

_, err := client.BetaAddPaymentMethod("cus_123", "ba_123", easypost.PrimaryPaymentMethodPriority)
require.Error(err)
if err, ok := err.(*easypost.APIError); ok {
assert.Equal(422, err.StatusCode)
assert.Equal("BILLING.INVALID_PAYMENT_GATEWAY_REFERENCE", err.Code)
assert.Equal("Invalid Payment Gateway Reference.", err.Message)

var eperr *easypost.APIError
if errors.As(err, &eperr) {
assert.Equal(422, eperr.StatusCode)
assert.Equal("BILLING.INVALID_PAYMENT_GATEWAY_REFERENCE", eperr.Code)
assert.Equal("Invalid Payment Gateway Reference.", eperr.Message)
}
}

Expand All @@ -26,10 +29,12 @@ func (c *ClientTests) TestBetaReferralRefundByAmount() {

_, err := client.BetaRefundByAmount(2000)
require.Error(err)
if err, ok := err.(*easypost.APIError); ok {
assert.Equal(422, err.StatusCode)
assert.Equal("TRANSACTION.AMOUNT_INVALID", err.Code)
assert.Equal("Refund amount is invalid. Please use a valid amount or escalate to finance.", err.Message)

var eperr *easypost.APIError
if errors.As(err, &eperr) {
assert.Equal(422, eperr.StatusCode)
assert.Equal("TRANSACTION.AMOUNT_INVALID", eperr.Code)
assert.Equal("Refund amount is invalid. Please use a valid amount or escalate to finance.", eperr.Message)
}
}

Expand All @@ -39,10 +44,12 @@ func (c *ClientTests) TestBetaReferralRefundByPaymentLogId() {

_, err := client.BetaRefundByPaymentLog("paylog_...")
require.Error(err)
if err, ok := err.(*easypost.APIError); ok {
assert.Equal(422, err.StatusCode)
assert.Equal("TRANSACTION.DOES_NOT_EXIST", err.Code)
assert.Equal("We could not find a transaction with that id.", err.Message)

var eperr *easypost.APIError
if errors.As(err, &eperr) {
assert.Equal(422, eperr.StatusCode)
assert.Equal("TRANSACTION.DOES_NOT_EXIST", eperr.Code)
assert.Equal("We could not find a transaction with that id.", eperr.Message)
}
}

Expand Down

0 comments on commit 4943c56

Please sign in to comment.