forked from ebhomengo/niki
1
0
Fork 0
niki/validator/benefactor/address/delete_test.go

141 lines
4.5 KiB
Go

package benefactoraddressvalidator
import (
"context"
"errors"
"testing"
addressparam "git.gocasts.ir/ebhomengo/niki/param/benefactor/address"
benefactorparam "git.gocasts.ir/ebhomengo/niki/param/benefactor/benefactor"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
"github.com/stretchr/testify/assert"
)
func TestValidator_ValidateDeleteRequest(t *testing.T) {
mockBenefactorSvc := NewMockBenefactorSvc(t)
mockRepository := NewMockRepository(t)
validator := New(mockBenefactorSvc, mockRepository)
ctx := context.Background()
t.Run("Successful validation", func(t *testing.T) {
req := addressparam.DeleteAddressRequest{
AddressID: 1,
BenefactorID: 1,
}
mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorparam.BenefactorExistByIDRequest{ID: 1}).
Return(benefactorparam.BenefactorExistByIDResponse{Existed: true}, nil).Once()
mockRepository.EXPECT().IsExistAddressByID(ctx, uint(1), uint(1)).
Return(true, nil).Once()
fieldErrors, err := validator.ValidateDeleteRequest(ctx, req)
assert.NoError(t, err)
assert.Nil(t, fieldErrors)
})
t.Run("Empty benefactor id", func(t *testing.T) {
req := addressparam.DeleteAddressRequest{
AddressID: 1,
BenefactorID: 0,
}
mockRepository.EXPECT().IsExistAddressByID(ctx, uint(1), uint(0)).
Return(true, nil).Once()
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 := addressparam.DeleteAddressRequest{
AddressID: 1,
BenefactorID: 1,
}
mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorparam.BenefactorExistByIDRequest{ID: 1}).
Return(benefactorparam.BenefactorExistByIDResponse{Existed: false}, nil).Once()
mockRepository.EXPECT().IsExistAddressByID(ctx, uint(1), uint(1)).
Return(true, nil).Once()
fieldErrors, err := validator.ValidateDeleteRequest(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 := addressparam.DeleteAddressRequest{
AddressID: 1,
BenefactorID: 1,
}
mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorparam.BenefactorExistByIDRequest{ID: 1}).
Return(benefactorparam.BenefactorExistByIDResponse{Existed: false}, errors.New("service error")).Once()
mockRepository.EXPECT().IsExistAddressByID(ctx, uint(1), uint(1)).
Return(true, nil).Once()
fieldErrors, err := validator.ValidateDeleteRequest(ctx, req)
assert.Error(t, err)
assert.NotNil(t, fieldErrors)
assert.Equal(t, errmsg.ErrorMsgSomethingWentWrong, fieldErrors["BenefactorID"])
})
t.Run("Empty address id", func(t *testing.T) {
req := addressparam.DeleteAddressRequest{
AddressID: 0,
BenefactorID: 1,
}
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, "AddressID")
})
t.Run("Address does not exist", func(t *testing.T) {
req := addressparam.DeleteAddressRequest{
AddressID: 1,
BenefactorID: 1,
}
mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorparam.BenefactorExistByIDRequest{ID: 1}).
Return(benefactorparam.BenefactorExistByIDResponse{Existed: true}, nil).Once()
mockRepository.EXPECT().IsExistAddressByID(ctx, uint(1), uint(1)).
Return(false, nil).Once()
fieldErrors, err := validator.ValidateDeleteRequest(ctx, req)
assert.Error(t, err)
assert.NotNil(t, fieldErrors)
assert.Equal(t, errmsg.ErrorMsgNotFound, fieldErrors["AddressID"])
})
t.Run("Address exist error", func(t *testing.T) {
req := addressparam.DeleteAddressRequest{
AddressID: 1,
BenefactorID: 1,
}
mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorparam.BenefactorExistByIDRequest{ID: 1}).
Return(benefactorparam.BenefactorExistByIDResponse{Existed: true}, nil).Once()
mockRepository.EXPECT().IsExistAddressByID(ctx, uint(1), uint(1)).
Return(false, errors.New("repository error")).Once()
fieldErrors, err := validator.ValidateDeleteRequest(ctx, req)
assert.Error(t, err)
assert.NotNil(t, fieldErrors)
assert.Equal(t, errmsg.ErrorMsgSomethingWentWrong, fieldErrors["AddressID"])
})
}