forked from ebhomengo/niki
1
0
Fork 0
niki/validator/benefactor/kind_box/get_test.go

137 lines
4.5 KiB
Go
Raw Normal View History

package benefactorkindboxvalidator
import (
"context"
"errors"
"testing"
benefactorparam "git.gocasts.ir/ebhomengo/niki/param/benefactor/benefactor"
param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
"github.com/stretchr/testify/assert"
)
func TestValidator_ValidateGetRequest(t *testing.T) {
mockRepository := NewMockRepository(t)
mockBenefactorSvc := NewMockBenefactorSvc(t)
mockAddressSvc := NewMockAddressSvc(t)
mockReferTimeSvc := NewMockReferTimeSvc(t)
validator := New(mockRepository, mockBenefactorSvc, mockAddressSvc, mockReferTimeSvc)
ctx := context.Background()
t.Run("Successful validation", func(t *testing.T) {
req := param.KindBoxGetRequest{
BenefactorID: 1,
KindBoxID: 1,
}
mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorparam.BenefactorExistByIDRequest{ID: 1}).
Return(benefactorparam.BenefactorExistByIDResponse{Existed: true}, nil).Once()
mockRepository.EXPECT().BenefactorKindBoxExist(ctx, uint(1), uint(1)).Return(true, nil).Once()
fieldErrors, err := validator.ValidateGetRequest(ctx, req)
assert.NoError(t, err)
assert.Nil(t, fieldErrors)
})
t.Run("Empty benefactor id", func(t *testing.T) {
req := param.KindBoxGetRequest{
BenefactorID: 0,
KindBoxID: 1,
}
mockRepository.EXPECT().BenefactorKindBoxExist(ctx, uint(0), uint(1)).Return(true, nil).Once()
fieldErrors, err := validator.ValidateGetRequest(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 := param.KindBoxGetRequest{
BenefactorID: 1,
KindBoxID: 1,
}
mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorparam.BenefactorExistByIDRequest{ID: 1}).
Return(benefactorparam.BenefactorExistByIDResponse{Existed: false}, nil).Once()
mockRepository.EXPECT().BenefactorKindBoxExist(ctx, uint(1), uint(1)).Return(true, nil).Once()
fieldErrors, err := validator.ValidateGetRequest(ctx, req)
assert.Error(t, err)
assert.NotNil(t, fieldErrors)
assert.Equal(t, errmsg.ErrorMsgBenefactorNotFound, fieldErrors["BenefactorID"])
})
t.Run("Benefactor exist error", func(t *testing.T) {
req := param.KindBoxGetRequest{
BenefactorID: 1,
KindBoxID: 1,
}
mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorparam.BenefactorExistByIDRequest{ID: 1}).
Return(benefactorparam.BenefactorExistByIDResponse{Existed: false}, errors.New("service error")).Once()
mockRepository.EXPECT().BenefactorKindBoxExist(ctx, uint(1), uint(1)).Return(true, nil).Once()
fieldErrors, err := validator.ValidateGetRequest(ctx, req)
assert.Error(t, err)
assert.NotNil(t, fieldErrors)
assert.Equal(t, errmsg.ErrorMsgSomethingWentWrong, fieldErrors["BenefactorID"])
})
t.Run("Empty KindBox id", func(t *testing.T) {
req := param.KindBoxGetRequest{
BenefactorID: 1,
KindBoxID: 0,
}
mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorparam.BenefactorExistByIDRequest{ID: 1}).
Return(benefactorparam.BenefactorExistByIDResponse{Existed: true}, nil).Once()
fieldErrors, err := validator.ValidateGetRequest(ctx, req)
assert.Error(t, err)
assert.NotNil(t, fieldErrors)
assert.Contains(t, fieldErrors, "KindBoxID")
})
t.Run("KindBox does not exist", func(t *testing.T) {
req := param.KindBoxGetRequest{
BenefactorID: 1,
KindBoxID: 1,
}
mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorparam.BenefactorExistByIDRequest{ID: 1}).
Return(benefactorparam.BenefactorExistByIDResponse{Existed: true}, nil).Once()
mockRepository.EXPECT().BenefactorKindBoxExist(ctx, uint(1), uint(1)).Return(false, nil).Once()
fieldErrors, err := validator.ValidateGetRequest(ctx, req)
assert.Error(t, err)
assert.NotNil(t, fieldErrors)
assert.Equal(t, errmsg.ErrorMsgNotFound, fieldErrors["KindBoxID"])
})
t.Run("KindBox exist error", func(t *testing.T) {
req := param.KindBoxGetRequest{
BenefactorID: 1,
KindBoxID: 1,
}
mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorparam.BenefactorExistByIDRequest{ID: 1}).
Return(benefactorparam.BenefactorExistByIDResponse{Existed: true}, nil).Once()
mockRepository.EXPECT().BenefactorKindBoxExist(ctx, uint(1), uint(1)).Return(false, errors.New("repo error")).Once()
fieldErrors, err := validator.ValidateGetRequest(ctx, req)
assert.Error(t, err)
assert.NotNil(t, fieldErrors)
assert.Equal(t, errmsg.ErrorMsgSomethingWentWrong, fieldErrors["KindBoxID"])
})
}