fix
This commit is contained in:
124
pkg/checkout/mutation_payment_event_test.go
Normal file
124
pkg/checkout/mutation_payment_event_test.go
Normal file
@@ -0,0 +1,124 @@
|
||||
package checkout
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
checkout_messages "git.k6n.net/go-cart-actor/proto/checkout"
|
||||
"google.golang.org/protobuf/types/known/anypb"
|
||||
)
|
||||
|
||||
func TestHandlePaymentEvent(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
initialPayments []*Payment
|
||||
event *checkout_messages.PaymentEvent
|
||||
expectedEvents []PaymentEvent
|
||||
expectError bool
|
||||
}{
|
||||
{
|
||||
name: "add event to existing payment",
|
||||
initialPayments: []*Payment{
|
||||
{
|
||||
PaymentId: "pay123",
|
||||
Events: []PaymentEvent{
|
||||
{Name: "init", Success: true, Data: json.RawMessage(`{"key":"value"}`)},
|
||||
},
|
||||
},
|
||||
},
|
||||
event: &checkout_messages.PaymentEvent{
|
||||
PaymentId: "pay123",
|
||||
Name: "capture",
|
||||
Success: true,
|
||||
Data: &anypb.Any{Value: []byte(`{"amount":100}`)},
|
||||
},
|
||||
expectedEvents: []PaymentEvent{
|
||||
{Name: "init", Success: true, Data: json.RawMessage(`{"key":"value"}`)},
|
||||
{Name: "capture", Success: true, Data: json.RawMessage(`{"amount":100}`)},
|
||||
},
|
||||
expectError: false,
|
||||
},
|
||||
{
|
||||
name: "add event to payment with no initial events",
|
||||
initialPayments: []*Payment{
|
||||
{
|
||||
PaymentId: "pay456",
|
||||
Events: []PaymentEvent{},
|
||||
},
|
||||
},
|
||||
event: &checkout_messages.PaymentEvent{
|
||||
PaymentId: "pay456",
|
||||
Name: "refund",
|
||||
Success: false,
|
||||
Data: &anypb.Any{Value: []byte(`{"reason":"failed"}`)},
|
||||
},
|
||||
expectedEvents: []PaymentEvent{
|
||||
{Name: "refund", Success: false, Data: json.RawMessage(`{"reason":"failed"}`)},
|
||||
},
|
||||
expectError: false,
|
||||
},
|
||||
{
|
||||
name: "payment not found",
|
||||
initialPayments: []*Payment{
|
||||
{
|
||||
PaymentId: "pay123",
|
||||
},
|
||||
},
|
||||
event: &checkout_messages.PaymentEvent{
|
||||
PaymentId: "nonexistent",
|
||||
Name: "test",
|
||||
Success: true,
|
||||
},
|
||||
expectedEvents: nil,
|
||||
expectError: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
grain := &CheckoutGrain{
|
||||
Payments: tt.initialPayments,
|
||||
}
|
||||
|
||||
err := HandlePaymentEvent(grain, tt.event)
|
||||
|
||||
if tt.expectError {
|
||||
if err == nil {
|
||||
t.Errorf("expected error, got nil")
|
||||
} else if err != ErrPaymentNotFound {
|
||||
t.Errorf("expected ErrPaymentNotFound, got %v", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
payment, found := grain.FindPayment(tt.event.PaymentId)
|
||||
if !found {
|
||||
t.Errorf("payment not found after handling event")
|
||||
return
|
||||
}
|
||||
|
||||
if len(payment.Events) != len(tt.expectedEvents) {
|
||||
t.Errorf("expected %d events, got %d", len(tt.expectedEvents), len(payment.Events))
|
||||
return
|
||||
}
|
||||
|
||||
for i, expected := range tt.expectedEvents {
|
||||
actual := payment.Events[i]
|
||||
if actual.Name != expected.Name {
|
||||
t.Errorf("event %d name mismatch: got %s, expected %s", i, actual.Name, expected.Name)
|
||||
}
|
||||
if actual.Success != expected.Success {
|
||||
t.Errorf("event %d success mismatch: got %t, expected %t", i, actual.Success, expected.Success)
|
||||
}
|
||||
if string(actual.Data) != string(expected.Data) {
|
||||
t.Errorf("event %d data mismatch: got %s, expected %s", i, string(actual.Data), string(expected.Data))
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user