2026-06-08 17:35:25 +08:00

64 lines
1.7 KiB
Go

package resource
import (
"net/http/httptest"
"testing"
"hyapp-admin-server/internal/appctx"
"hyapp-admin-server/internal/middleware"
"github.com/gin-gonic/gin"
)
func TestGiftRequestProtoKeepsExplicitCPRelationType(t *testing.T) {
req := giftRequest{
GiftTypeCode: "cp",
CPRelationType: "brother",
PresentationJSON: `{"cp_relation_type":"sister"}`,
}
proto := req.updateProto(newGiftRequestTestContext(), "love")
if proto.GetCpRelationType() != "brother" {
t.Fatalf("explicit cp relation type should win, got %q", proto.GetCpRelationType())
}
}
func TestGiftRequestProtoFallsBackToPresentationCPRelationType(t *testing.T) {
req := giftRequest{
GiftID: "love",
GiftTypeCode: "cp",
PresentationJSON: `{"cp_relation_type":"sister"}`,
}
proto := req.createProto(newGiftRequestTestContext())
if proto.GetCpRelationType() != "sister" {
t.Fatalf("presentation cp relation type should be forwarded, got %q", proto.GetCpRelationType())
}
}
func TestGiftRequestProtoClearsCPRelationTypeForNonCPGift(t *testing.T) {
req := giftRequest{
GiftTypeCode: "normal",
CPRelationType: "brother",
PresentationJSON: `{"cp_relation_type":"brother"}`,
}
proto := req.updateProto(newGiftRequestTestContext(), "rose")
if proto.GetCpRelationType() != "" {
t.Fatalf("non-cp gift should not forward cp relation type, got %q", proto.GetCpRelationType())
}
}
func newGiftRequestTestContext() *gin.Context {
gin.SetMode(gin.TestMode)
recorder := httptest.NewRecorder()
c, _ := gin.CreateTestContext(recorder)
request := httptest.NewRequest("POST", "/admin/gifts", nil)
c.Request = request.WithContext(appctx.WithContext(request.Context(), "lalu"))
c.Set(middleware.ContextRequestID, "resource-test-request")
return c
}