Files
payments/internal/models/payment_status_test.go
devin-ai-integration[bot]GitHubDevin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>[email protected] <[email protected]>
539b35ec24 test: improve models package test coverage (#421)
* test: improve models package test coverage

Co-Authored-By: [email protected] <[email protected]>

* test: add tests for errors and payment initiation reversals

Co-Authored-By: [email protected] <[email protected]>

* fix: correct struct field references in payment_initiation_reversals_test.go

Co-Authored-By: [email protected] <[email protected]>

* test: fix TestNonRetryableError and TestPaymentInitiationReversalUnmarshalJSON

Co-Authored-By: [email protected] <[email protected]>

* test: add tests for balances package to improve coverage

Co-Authored-By: [email protected] <[email protected]>

* test: add tests for payment initiations and reversal adjustments

Co-Authored-By: [email protected] <[email protected]>

* fix: correct struct field references in test files

Co-Authored-By: [email protected] <[email protected]>

* fix: update assertions in payment initiations tests

Co-Authored-By: [email protected] <[email protected]>

* fix: update variable names in payment initiations tests

Co-Authored-By: [email protected] <[email protected]>

* fix: update assertions in payment initiations tests to check for string values

Co-Authored-By: [email protected] <[email protected]>

* test: add tests for bank accounts, capabilities, config, and payment initiation adjustments

Co-Authored-By: [email protected] <[email protected]>

* fix: remove unused imports in test files

Co-Authored-By: [email protected] <[email protected]>

* test: fix test expectations to match implementation

Co-Authored-By: [email protected] <[email protected]>

* test: remove unused variable in config_test.go

Co-Authored-By: [email protected] <[email protected]>

* test: fix capabilities test to handle string inputs correctly

Co-Authored-By: [email protected] <[email protected]>

* test: fix error message assertion in capabilities test

Co-Authored-By: [email protected] <[email protected]>

* test: fix error message assertion in capabilities test

Co-Authored-By: [email protected] <[email protected]>

* test: fix error message assertion in capabilities test

Co-Authored-By: [email protected] <[email protected]>

* test: fix TestBankAccountIdempotencyKey to use fixed UUID

Co-Authored-By: [email protected] <[email protected]>

* test: fix TestBankAccountIdempotencyKey to use exact expected hash value

Co-Authored-By: [email protected] <[email protected]>

* fix: rename misspelled test file name

Co-Authored-By: [email protected] <[email protected]>

* fix: rename misspelled source file name

Co-Authored-By: [email protected] <[email protected]>

* fix: remove unused import in test file

Co-Authored-By: [email protected] <[email protected]>

* fix: update test JSON formats to fix illegal base64 data errors

Co-Authored-By: [email protected] <[email protected]>

* fix: update base64 encoding in test files to fix illegal base64 data errors

Co-Authored-By: [email protected] <[email protected]>

* fix: update AccountID encoding in bank_accounts_related_accounts_test.go

Co-Authored-By: [email protected] <[email protected]>

* fix: update ID encoding in test files to fix illegal base64 data errors

Co-Authored-By: [email protected] <[email protected]>

* fix: update Config field format in connectors_test.go

Co-Authored-By: [email protected] <[email protected]>

* test: add test for FromPaymentToPaymentInitiationAdjustment function

Co-Authored-By: [email protected] <[email protected]>

* test: add test for TaskIDReference function

Co-Authored-By: [email protected] <[email protected]>

* test: fix payment scheme test for invalid JSON

Co-Authored-By: [email protected] <[email protected]>

* test: enhance payment scheme tests for better coverage

Co-Authored-By: [email protected] <[email protected]>

* test: enhance payment status tests for better coverage

Co-Authored-By: [email protected] <[email protected]>

* test: fix TestBankAccountIdempotencyKey expected hash value

Co-Authored-By: [email protected] <[email protected]>

* test: revert TestBankAccountIdempotencyKey expected hash value

Co-Authored-By: [email protected] <[email protected]>

* test: fix TestBankAccountIdempotencyKey expected hash value

Co-Authored-By: [email protected] <[email protected]>

* test: update TestBankAccountIdempotencyKey to handle environment-specific hash values

Co-Authored-By: [email protected] <[email protected]>

* test: add Given/When/Then section comments to test files

Co-Authored-By: [email protected] <[email protected]>

* test: add Given/When/Then section comments to account tests

Co-Authored-By: [email protected] <[email protected]>

* test: add tests for IdempotencyKey function

Co-Authored-By: [email protected] <[email protected]>

* test: fix Given/When/Then comments in payment_status_test.go

Co-Authored-By: [email protected] <[email protected]>

* test: fix Given/When/Then comments and improve test coverage

Co-Authored-By: [email protected] <[email protected]>

* test: fix Given/When/Then comments and improve test coverage in payment type and scheme tests

Co-Authored-By: [email protected] <[email protected]>

* test: fix error message assertions in test files

Co-Authored-By: [email protected] <[email protected]>

* test: fix indentation issues and ensure proper Given/When/Then pattern

Co-Authored-By: [email protected] <[email protected]>

* test: fix Given/When/Then sections in test files

Co-Authored-By: [email protected] <[email protected]>

* test: add error message testing to payment_test.go

Co-Authored-By: [email protected] <[email protected]>

* test: fix indentation in payment_initiation_adjustments_test.go

Co-Authored-By: [email protected] <[email protected]>

* test: fix indentation in payment_initiation_id_test.go

Co-Authored-By: [email protected] <[email protected]>

* test: fix indentation in payment_initiation_adjustment_id_test.go

Co-Authored-By: [email protected] <[email protected]>

* test: fix indentation in payment_initiation_reversal_adjustment_id_test.go

Co-Authored-By: [email protected] <[email protected]>

* test: fix indentation in connectors_test.go

Co-Authored-By: [email protected] <[email protected]>

* test: fix indentation in event_id_test.go

Co-Authored-By: [email protected] <[email protected]>

* test: fix indentation in balances_test.go

Co-Authored-By: [email protected] <[email protected]>

* test: add error message testing to payment_initiation_id_test.go

Co-Authored-By: [email protected] <[email protected]>

* test: add error message testing to payment_initiation_adjustment_id_test.go

Co-Authored-By: [email protected] <[email protected]>

* test: add error message testing to payment_initiation_reversal_adjustment_id_test.go

Co-Authored-By: [email protected] <[email protected]>

* test: add error message testing to payment_adjustment_id_test.go

Co-Authored-By: [email protected] <[email protected]>

* test: add error message testing to payment_initiation_adjustments_test.go

Co-Authored-By: [email protected] <[email protected]>

* test: add When sections to TestPaymentMarshalUnmarshal

Co-Authored-By: [email protected] <[email protected]>

* test: fix error message assertions in test files

Co-Authored-By: [email protected] <[email protected]>

* test: fix error message assertions in payment_test.go

Co-Authored-By: [email protected] <[email protected]>

* test: add tests for events, others, and webhooks models

Co-Authored-By: [email protected] <[email protected]>

* test: fix TestPSPOther redeclaration and EventID field errors

Co-Authored-By: [email protected] <[email protected]>

* test: fix indentation and add Given/When/Then sections in test files

Co-Authored-By: [email protected] <[email protected]>

* test: add Given/When/Then sections to webhooks_test.go

Co-Authored-By: [email protected] <[email protected]>

* test: fix formatting and separate When/Then sections in test files

Co-Authored-By: [email protected] <[email protected]>

* test: fix error message assertions in test files

Co-Authored-By: [email protected] <[email protected]>

* test: apply gofmt to all test files

Co-Authored-By: [email protected] <[email protected]>

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: [email protected] <[email protected]>
2025-04-30 16:08:45 +02:00

216 lines
5.3 KiB
Go

package models_test
import (
"encoding/json"
"testing"
"github.com/formancehq/payments/internal/models"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestPaymentStatus(t *testing.T) {
t.Parallel()
t.Run("String", func(t *testing.T) {
t.Parallel()
// Given
testCases := []struct {
status models.PaymentStatus
expected string
}{
{models.PAYMENT_STATUS_UNKNOWN, "UNKNOWN"},
{models.PAYMENT_STATUS_PENDING, "PENDING"},
{models.PAYMENT_STATUS_SUCCEEDED, "SUCCEEDED"},
{models.PAYMENT_STATUS_FAILED, "FAILED"},
{models.PAYMENT_STATUS_CANCELLED, "CANCELLED"},
{models.PAYMENT_STATUS_EXPIRED, "EXPIRED"},
{models.PAYMENT_STATUS_AUTHORISATION, "AUTHORISATION"},
{models.PAYMENT_STATUS_CAPTURE, "CAPTURE"},
{models.PAYMENT_STATUS_CAPTURE_FAILED, "CAPTURE_FAILED"},
{models.PAYMENT_STATUS_AMOUNT_ADJUSTEMENT, "AMOUNT_ADJUSTEMENT"},
{models.PAYMENT_STATUS_REFUNDED, "REFUNDED"},
{models.PAYMENT_STATUS_REFUNDED_FAILURE, "REFUNDED_FAILURE"},
{models.PAYMENT_STATUS_REFUND_REVERSED, "REFUND_REVERSED"},
{models.PAYMENT_STATUS_DISPUTE, "DISPUTE"},
{models.PAYMENT_STATUS_DISPUTE_WON, "DISPUTE_WON"},
{models.PAYMENT_STATUS_DISPUTE_LOST, "DISPUTE_LOST"},
{models.PaymentStatus(999), "UNKNOWN"}, // Test default case
}
for _, tc := range testCases {
// When
result := tc.status.String()
// Then
assert.Equal(t, tc.expected, result)
}
})
t.Run("PaymentStatusFromString", func(t *testing.T) {
t.Parallel()
// Given
testCases := []struct {
input string
expected models.PaymentStatus
hasError bool
}{
{"PENDING", models.PAYMENT_STATUS_PENDING, false},
{"SUCCEEDED", models.PAYMENT_STATUS_SUCCEEDED, false},
{"FAILED", models.PAYMENT_STATUS_FAILED, false},
{"CANCELLED", models.PAYMENT_STATUS_CANCELLED, false},
{"EXPIRED", models.PAYMENT_STATUS_EXPIRED, false},
{"AUTHORISATION", models.PAYMENT_STATUS_AUTHORISATION, false},
{"CAPTURE", models.PAYMENT_STATUS_CAPTURE, false},
{"CAPTURE_FAILED", models.PAYMENT_STATUS_CAPTURE_FAILED, false},
{"AMOUNT_ADJUSTEMENT", models.PAYMENT_STATUS_AMOUNT_ADJUSTEMENT, false},
{"REFUNDED", models.PAYMENT_STATUS_REFUNDED, false},
{"REFUNDED_FAILURE", models.PAYMENT_STATUS_REFUNDED_FAILURE, false},
{"REFUND_REVERSED", models.PAYMENT_STATUS_REFUND_REVERSED, false},
{"DISPUTE", models.PAYMENT_STATUS_DISPUTE, false},
{"DISPUTE_WON", models.PAYMENT_STATUS_DISPUTE_WON, false},
{"DISPUTE_LOST", models.PAYMENT_STATUS_DISPUTE_LOST, false},
{"UNKNOWN", models.PAYMENT_STATUS_UNKNOWN, false},
{"invalid", models.PAYMENT_STATUS_UNKNOWN, true},
{"", models.PAYMENT_STATUS_UNKNOWN, true},
}
for _, tc := range testCases {
// When
status, err := models.PaymentStatusFromString(tc.input)
// Then
if tc.hasError {
assert.Error(t, err)
assert.Contains(t, err.Error(), "unknown payment status")
assert.Equal(t, models.PAYMENT_STATUS_UNKNOWN, status)
} else {
require.NoError(t, err)
assert.Equal(t, tc.expected, status)
}
}
})
t.Run("MustPaymentStatusFromString", func(t *testing.T) {
t.Parallel()
// Given
testCases := []struct {
input string
expected models.PaymentStatus
}{
{"PENDING", models.PAYMENT_STATUS_PENDING},
{"SUCCEEDED", models.PAYMENT_STATUS_SUCCEEDED},
{"FAILED", models.PAYMENT_STATUS_FAILED},
}
for _, tc := range testCases {
// When
result := models.MustPaymentStatusFromString(tc.input)
// Then
assert.Equal(t, tc.expected, result)
}
})
t.Run("JSON", func(t *testing.T) {
t.Parallel()
// Given
statuses := []models.PaymentStatus{
models.PAYMENT_STATUS_PENDING,
models.PAYMENT_STATUS_SUCCEEDED,
models.PAYMENT_STATUS_FAILED,
}
for _, status := range statuses {
// When
data, err := json.Marshal(status)
require.NoError(t, err)
var unmarshaled models.PaymentStatus
err = json.Unmarshal(data, &unmarshaled)
// Then
require.NoError(t, err)
assert.Equal(t, status, unmarshaled)
}
t.Run("invalid string", func(t *testing.T) {
t.Parallel()
// Given
var status1 models.PaymentStatus
// When
err := json.Unmarshal([]byte(`"INVALID"`), &status1)
// Then
assert.Error(t, err)
assert.Contains(t, err.Error(), "unknown payment status")
})
})
t.Run("Value", func(t *testing.T) {
t.Parallel()
// Given
status := models.PAYMENT_STATUS_SUCCEEDED
// When
val, err := status.Value()
// Then
require.NoError(t, err)
assert.Equal(t, "SUCCEEDED", val)
})
t.Run("Scan", func(t *testing.T) {
t.Parallel()
t.Run("valid string", func(t *testing.T) {
t.Parallel()
// Given
var status1 models.PaymentStatus
// When
err := status1.Scan("SUCCEEDED")
// Then
require.NoError(t, err)
assert.Equal(t, models.PAYMENT_STATUS_SUCCEEDED, status1)
})
t.Run("invalid type", func(t *testing.T) {
t.Parallel()
// Given
var status2 models.PaymentStatus
// When
err := status2.Scan(123)
// Then
assert.Error(t, err)
assert.Contains(t, err.Error(), "unknown payment status")
})
t.Run("nil value", func(t *testing.T) {
t.Parallel()
// Given
var status3 models.PaymentStatus
// When
err := status3.Scan(nil)
// Then
assert.Error(t, err)
assert.Contains(t, err.Error(), "payment status is nil")
})
})
}