31e354e31c
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_013nMthbVEmQquxBRKb9Fj8u
52 lines
1.5 KiB
Go
52 lines
1.5 KiB
Go
package store_test
|
|
|
|
import (
|
|
"errors"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/wangjia/pay/internal/model"
|
|
"github.com/wangjia/pay/internal/store"
|
|
)
|
|
|
|
func TestGetOrderAndAttemptByRef(t *testing.T) {
|
|
s := store.NewOrderStore(model.OpenTestDB(t))
|
|
seedOrder(t, s, "PAY-Q1") // 复用 order_test.go 的 seedOrder(建 order + attempt provider_ref="P-PAY-Q1")
|
|
|
|
o, err := s.GetOrder("PAY-Q1")
|
|
if err != nil || o.OutTradeNo != "PAY-Q1" {
|
|
t.Fatalf("GetOrder = %+v, %v", o, err)
|
|
}
|
|
if _, err := s.GetOrder("NOPE"); !errors.Is(err, store.ErrOrderNotFound) {
|
|
t.Fatalf("缺单应 ErrOrderNotFound, got %v", err)
|
|
}
|
|
|
|
att, err := s.AttemptByProviderRef("P-PAY-Q1")
|
|
if err != nil || att.OutTradeNo != "PAY-Q1" || att.Channel != "crypto" {
|
|
t.Fatalf("AttemptByProviderRef = %+v, %v", att, err)
|
|
}
|
|
if _, err := s.AttemptByProviderRef("P-UNKNOWN"); !errors.Is(err, store.ErrAttemptNotFound) {
|
|
t.Fatalf("缺尝试应 ErrAttemptNotFound, got %v", err)
|
|
}
|
|
}
|
|
|
|
func TestListPendingAndExpire(t *testing.T) {
|
|
s := store.NewOrderStore(model.OpenTestDB(t))
|
|
seedOrder(t, s, "PAY-Q2")
|
|
|
|
pending, err := s.ListAttemptsByStatus(model.AttemptPending, 10)
|
|
if err != nil || len(pending) != 1 {
|
|
t.Fatalf("ListAttemptsByStatus pending = %d, %v", len(pending), err)
|
|
}
|
|
|
|
n, err := s.ExpirePendingAttempts("PAY-Q2")
|
|
if err != nil || n != 1 {
|
|
t.Fatalf("ExpirePendingAttempts = %d, %v", n, err)
|
|
}
|
|
pending2, _ := s.ListAttemptsByStatus(model.AttemptPending, 10)
|
|
if len(pending2) != 0 {
|
|
t.Fatalf("弃过期后应无 pending, got %d", len(pending2))
|
|
}
|
|
_ = time.Now
|
|
}
|