forked from ebhomengo/niki
176 lines
5.7 KiB
Go
176 lines
5.7 KiB
Go
package benefactorkindboxreqvalidator
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
|
|
"git.gocasts.ir/ebhomengo/niki/entity"
|
|
benefactorparam "git.gocasts.ir/ebhomengo/niki/param/benefactor/benefactor"
|
|
kindboxreqparam "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box_req"
|
|
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestValidator_ValidateDeleteRequest(t *testing.T) {
|
|
mockRepository := NewMockRepository(t)
|
|
mockBenefactorSvc := NewMockBenefactorSvc(t)
|
|
mockAddressSvc := NewMockAddressSvc(t)
|
|
mockReferTimeSvc := NewMockReferTimeSvc(t)
|
|
validator := New(mockBenefactorSvc, mockAddressSvc, mockReferTimeSvc, mockRepository)
|
|
ctx := context.Background()
|
|
|
|
t.Run("Successful validation", func(t *testing.T) {
|
|
req := kindboxreqparam.KindBoxReqDeleteRequest{
|
|
BenefactorID: 1,
|
|
KindBoxReqID: 1,
|
|
}
|
|
|
|
mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorparam.BenefactorExistByIDRequest{ID: 1}).
|
|
Return(benefactorparam.BenefactorExistByIDResponse{Existed: true}, nil).Once()
|
|
mockRepository.EXPECT().GetKindBoxReqByID(ctx, uint(1)).Return(
|
|
entity.KindBoxReq{BenefactorID: 1, Status: entity.KindBoxReqPendingStatus},
|
|
nil,
|
|
).Twice()
|
|
|
|
fieldErrors, err := validator.ValidateDeleteRequest(ctx, req)
|
|
|
|
assert.NoError(t, err)
|
|
assert.Nil(t, fieldErrors)
|
|
})
|
|
|
|
t.Run("Empty benefactor id", func(t *testing.T) {
|
|
req := kindboxreqparam.KindBoxReqDeleteRequest{
|
|
BenefactorID: 0,
|
|
KindBoxReqID: 1,
|
|
}
|
|
|
|
mockRepository.EXPECT().GetKindBoxReqByID(ctx, uint(1)).Return(
|
|
entity.KindBoxReq{BenefactorID: 1, Status: entity.KindBoxReqPendingStatus},
|
|
nil,
|
|
).Twice()
|
|
|
|
fieldErrors, err := validator.ValidateDeleteRequest(ctx, req)
|
|
|
|
assert.Error(t, err)
|
|
assert.NotNil(t, fieldErrors)
|
|
assert.Contains(t, fieldErrors, "BenefactorID")
|
|
})
|
|
|
|
t.Run("Benefactor does not exist", func(t *testing.T) {
|
|
req := kindboxreqparam.KindBoxReqDeleteRequest{
|
|
BenefactorID: 2,
|
|
KindBoxReqID: 1,
|
|
}
|
|
|
|
mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorparam.BenefactorExistByIDRequest{ID: 2}).
|
|
Return(benefactorparam.BenefactorExistByIDResponse{Existed: false}, nil).Once()
|
|
mockRepository.EXPECT().GetKindBoxReqByID(ctx, uint(1)).Return(
|
|
entity.KindBoxReq{BenefactorID: 1, Status: entity.KindBoxReqPendingStatus},
|
|
nil,
|
|
).Twice()
|
|
|
|
fieldErrors, err := validator.ValidateDeleteRequest(ctx, req)
|
|
|
|
assert.Error(t, err)
|
|
assert.NotNil(t, fieldErrors)
|
|
assert.Contains(t, fieldErrors, "BenefactorID")
|
|
})
|
|
|
|
t.Run("Benefactor exist error", func(t *testing.T) {
|
|
req := kindboxreqparam.KindBoxReqDeleteRequest{
|
|
BenefactorID: 2,
|
|
KindBoxReqID: 1,
|
|
}
|
|
|
|
mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorparam.BenefactorExistByIDRequest{ID: 2}).
|
|
Return(benefactorparam.BenefactorExistByIDResponse{Existed: false}, errors.New("service error")).Once()
|
|
mockRepository.EXPECT().GetKindBoxReqByID(ctx, uint(1)).Return(
|
|
entity.KindBoxReq{BenefactorID: 1, Status: entity.KindBoxReqPendingStatus},
|
|
nil,
|
|
).Twice()
|
|
|
|
fieldErrors, err := validator.ValidateDeleteRequest(ctx, req)
|
|
|
|
assert.Error(t, err)
|
|
assert.NotNil(t, fieldErrors)
|
|
assert.Equal(t, errmsg.ErrorMsgSomethingWentWrong, fieldErrors["BenefactorID"])
|
|
})
|
|
|
|
t.Run("Empty KindBoxReq id", func(t *testing.T) {
|
|
req := kindboxreqparam.KindBoxReqDeleteRequest{
|
|
BenefactorID: 1,
|
|
KindBoxReqID: 0,
|
|
}
|
|
|
|
mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorparam.BenefactorExistByIDRequest{ID: 1}).
|
|
Return(benefactorparam.BenefactorExistByIDResponse{Existed: true}, nil).Once()
|
|
|
|
fieldErrors, err := validator.ValidateDeleteRequest(ctx, req)
|
|
|
|
assert.Error(t, err)
|
|
assert.NotNil(t, fieldErrors)
|
|
assert.Contains(t, fieldErrors, "KindBoxReqID")
|
|
})
|
|
|
|
t.Run("Invalid KindBoxReq status", func(t *testing.T) {
|
|
req := kindboxreqparam.KindBoxReqDeleteRequest{
|
|
BenefactorID: 1,
|
|
KindBoxReqID: 2,
|
|
}
|
|
|
|
mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorparam.BenefactorExistByIDRequest{ID: 1}).
|
|
Return(benefactorparam.BenefactorExistByIDResponse{Existed: true}, nil).Once()
|
|
mockRepository.EXPECT().GetKindBoxReqByID(ctx, uint(2)).Return(
|
|
entity.KindBoxReq{BenefactorID: 1, Status: entity.KindBoxReqDeliveredStatus},
|
|
nil,
|
|
).Once()
|
|
|
|
fieldErrors, err := validator.ValidateDeleteRequest(ctx, req)
|
|
|
|
assert.Error(t, err)
|
|
assert.NotNil(t, fieldErrors)
|
|
assert.Contains(t, fieldErrors, "KindBoxReqID")
|
|
})
|
|
|
|
t.Run("KindBoxReq does not belong to benefactor", func(t *testing.T) {
|
|
req := kindboxreqparam.KindBoxReqDeleteRequest{
|
|
BenefactorID: 1,
|
|
KindBoxReqID: 3,
|
|
}
|
|
|
|
mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorparam.BenefactorExistByIDRequest{ID: 1}).
|
|
Return(benefactorparam.BenefactorExistByIDResponse{Existed: true}, nil).Once()
|
|
mockRepository.EXPECT().GetKindBoxReqByID(ctx, uint(3)).Return(
|
|
entity.KindBoxReq{BenefactorID: 4, Status: entity.KindBoxReqPendingStatus},
|
|
nil,
|
|
).Twice()
|
|
|
|
fieldErrors, err := validator.ValidateDeleteRequest(ctx, req)
|
|
|
|
assert.Error(t, err)
|
|
assert.NotNil(t, fieldErrors)
|
|
assert.Contains(t, fieldErrors, "KindBoxReqID")
|
|
})
|
|
|
|
t.Run("KindBoxReq service error", func(t *testing.T) {
|
|
req := kindboxreqparam.KindBoxReqDeleteRequest{
|
|
BenefactorID: 1,
|
|
KindBoxReqID: 1,
|
|
}
|
|
|
|
mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorparam.BenefactorExistByIDRequest{ID: 1}).
|
|
Return(benefactorparam.BenefactorExistByIDResponse{Existed: true}, nil).Once()
|
|
mockRepository.EXPECT().GetKindBoxReqByID(ctx, uint(1)).Return(
|
|
entity.KindBoxReq{BenefactorID: 1, Status: entity.KindBoxReqPendingStatus},
|
|
errors.New("service error"),
|
|
).Once()
|
|
|
|
fieldErrors, err := validator.ValidateDeleteRequest(ctx, req)
|
|
|
|
assert.Error(t, err)
|
|
assert.NotNil(t, fieldErrors)
|
|
assert.Equal(t, errmsg.ErrorMsgNotFound, fieldErrors["KindBoxReqID"])
|
|
})
|
|
}
|