diff --git a/param/benefactor/kind_box_req/add.go b/param/benefactor/kind_box_req/add.go index 0daaefe..86d1c3a 100644 --- a/param/benefactor/kind_box_req/add.go +++ b/param/benefactor/kind_box_req/add.go @@ -7,7 +7,7 @@ import ( ) type KindBoxReqAddRequest struct { - BenefactorID uint `json:"benefactor_id" example:"1"` + BenefactorID uint `json:"-"` KindBoxType entity.KindBoxType `json:"kind_box_type" example:"on-table"` DeliverAddressID uint `json:"deliver_address_id" example:"1"` DeliverReferDate time.Time `json:"deliver_refer_date" example:"2025-01-02T15:04:05Z"` diff --git a/pkg/err_msg/message.go b/pkg/err_msg/message.go index ced8d71..0968013 100644 --- a/pkg/err_msg/message.go +++ b/pkg/err_msg/message.go @@ -10,7 +10,7 @@ const ( ErrorMsgEmailIsNotUnique = "email is not unique" ErrorMsgPhoneNumberIsNotValid = "phone number is not valid" ErrorMsgUserNotAllowed = "user not allowed" - ErrorMsgUserNotFound = "benefactor not found" + ErrorMsgBenefactorNotFound = "benefactor not found" ErrorMsgOtpCodeExist = "please wait a little bit" ErrorMsgOtpCodeIsNotValid = "verification code is not valid" ErrorMsgCantScanQueryResult = "can't scan query result" diff --git a/validator/benefactor/address/add_address.go b/validator/benefactor/address/add_address.go index b3a6a94..9cead2c 100644 --- a/validator/benefactor/address/add_address.go +++ b/validator/benefactor/address/add_address.go @@ -14,11 +14,14 @@ func (v Validator) ValidateAddAddress(ctx context.Context, req param.BenefactorA const op = "benefactorvalidator.ValidateAddRequest" if err := validation.ValidateStruct(&req, - validation.Field(&req.Address, validation.Required), - validation.Field(&req.BenefactorID, validation.Required, validation.By(v.doesBenefactorExist(ctx))), + validation.Field(&req.CityID, validation.Required, + validation.By(v.doesCityExist(ctx))), + + validation.Field(&req.Address, validation.Required), + validation.Field(&req.Lon, validation.Required), validation.Field(&req.Lat, validation.Required), @@ -26,9 +29,6 @@ func (v Validator) ValidateAddAddress(ctx context.Context, req param.BenefactorA validation.Field(&req.Name, validation.Required), validation.Field(&req.PostalCode, validation.Required), - - validation.Field(&req.CityID, validation.Required, - validation.By(v.doesCityExist(ctx))), ); err != nil { fieldErrors := make(map[string]string) @@ -49,5 +49,6 @@ func (v Validator) ValidateAddAddress(ctx context.Context, req param.BenefactorA WithErr(err) } - return map[string]string{}, nil + // nolint + return nil, nil } diff --git a/validator/benefactor/address/add_address_test.go b/validator/benefactor/address/add_address_test.go new file mode 100644 index 0000000..523319a --- /dev/null +++ b/validator/benefactor/address/add_address_test.go @@ -0,0 +1,288 @@ +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_ValidateAddAddress(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.BenefactorAddAddressRequest{ + Address: "tehran", + BenefactorID: 1, + Lon: 45.0, + Lat: 90.0, + Name: "Test Name", + PostalCode: "1234567890", + CityID: 1, + } + + mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorparam.BenefactorExistByIDRequest{ID: 1}). + Return(benefactorparam.BenefactorExistByIDResponse{Existed: true}, nil).Once() + mockRepository.EXPECT().IsExistCityByID(ctx, uint(1)). + Return(true, nil).Once() + + fieldErrors, err := validator.ValidateAddAddress(ctx, req) + + assert.NoError(t, err) + assert.Nil(t, fieldErrors) + }) + + t.Run("Empty benefactor id", func(t *testing.T) { + req := addressparam.BenefactorAddAddressRequest{ + Address: "tehran", + BenefactorID: 0, + Lon: 45.0, + Lat: 90.0, + Name: "Test Name", + PostalCode: "1234567890", + CityID: 1, + } + + mockRepository.EXPECT().IsExistCityByID(ctx, uint(1)). + Return(true, nil).Once() + + fieldErrors, err := validator.ValidateAddAddress(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.BenefactorAddAddressRequest{ + Address: "tehran", + BenefactorID: 1, + Lon: 45.0, + Lat: 90.0, + Name: "Test Name", + PostalCode: "1234567890", + CityID: 1, + } + + mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorparam.BenefactorExistByIDRequest{ID: 1}). + Return(benefactorparam.BenefactorExistByIDResponse{Existed: false}, nil).Once() + mockRepository.EXPECT().IsExistCityByID(ctx, uint(1)). + Return(true, nil).Once() + + fieldErrors, err := validator.ValidateAddAddress(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.BenefactorAddAddressRequest{ + Address: "tehran", + BenefactorID: 1, + Lon: 45.0, + Lat: 90.0, + Name: "Test Name", + PostalCode: "1234567890", + CityID: 1, + } + + mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorparam.BenefactorExistByIDRequest{ID: 1}). + Return(benefactorparam.BenefactorExistByIDResponse{Existed: false}, errors.New("service error")).Once() + mockRepository.EXPECT().IsExistCityByID(ctx, uint(1)). + Return(true, nil).Once() + + fieldErrors, err := validator.ValidateAddAddress(ctx, req) + + assert.Error(t, err) + assert.NotNil(t, fieldErrors) + assert.Equal(t, errmsg.ErrorMsgSomethingWentWrong, fieldErrors["BenefactorID"]) + }) + + t.Run("Empty city id", func(t *testing.T) { + req := addressparam.BenefactorAddAddressRequest{ + Address: "tehran", + BenefactorID: 1, + Lon: 45.0, + Lat: 90.0, + Name: "Test Name", + PostalCode: "1234567890", + CityID: 0, + } + + mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorparam.BenefactorExistByIDRequest{ID: 1}). + Return(benefactorparam.BenefactorExistByIDResponse{Existed: true}, nil).Once() + + fieldErrors, err := validator.ValidateAddAddress(ctx, req) + + assert.Error(t, err) + assert.NotNil(t, fieldErrors) + assert.Contains(t, fieldErrors, "city_id") + }) + + t.Run("City does not exist", func(t *testing.T) { + req := addressparam.BenefactorAddAddressRequest{ + Address: "tehran", + BenefactorID: 1, + Lon: 45.0, + Lat: 90.0, + Name: "Test Name", + PostalCode: "1234567890", + CityID: 1, + } + + mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorparam.BenefactorExistByIDRequest{ID: 1}). + Return(benefactorparam.BenefactorExistByIDResponse{Existed: true}, nil).Once() + mockRepository.EXPECT().IsExistCityByID(ctx, uint(1)). + Return(false, nil).Once() + fieldErrors, err := validator.ValidateAddAddress(ctx, req) + + assert.Error(t, err) + assert.NotNil(t, fieldErrors) + assert.Equal(t, errmsg.ErrorMsgNotFound, fieldErrors["city_id"]) + }) + + t.Run("City exist error", func(t *testing.T) { + req := addressparam.BenefactorAddAddressRequest{ + Address: "tehran", + BenefactorID: 1, + Lon: 45.0, + Lat: 90.0, + Name: "Test Name", + PostalCode: "1234567890", + CityID: 1, + } + + mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorparam.BenefactorExistByIDRequest{ID: 1}). + Return(benefactorparam.BenefactorExistByIDResponse{Existed: true}, nil).Once() + mockRepository.EXPECT().IsExistCityByID(ctx, uint(1)). + Return(false, errors.New("repository error")).Once() + fieldErrors, err := validator.ValidateAddAddress(ctx, req) + + assert.Error(t, err) + assert.NotNil(t, fieldErrors) + assert.Equal(t, errmsg.ErrorMsgSomethingWentWrong, fieldErrors["city_id"]) + }) + + t.Run("Empty address", func(t *testing.T) { + req := addressparam.BenefactorAddAddressRequest{ + Address: "", + BenefactorID: 1, + Lon: 45.0, + Lat: 90.0, + Name: "Test Name", + PostalCode: "1234567890", + CityID: 1, + } + + mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorparam.BenefactorExistByIDRequest{ID: 1}). + Return(benefactorparam.BenefactorExistByIDResponse{Existed: true}, nil).Once() + mockRepository.EXPECT().IsExistCityByID(ctx, uint(1)). + Return(true, nil).Once() + + fieldErrors, err := validator.ValidateAddAddress(ctx, req) + + assert.Error(t, err) + assert.NotNil(t, fieldErrors) + assert.Contains(t, fieldErrors, "address") + }) + + t.Run("Empty lat", func(t *testing.T) { + req := addressparam.BenefactorAddAddressRequest{ + Address: "tehran", + BenefactorID: 1, + Lon: 45.0, + Lat: 0, + Name: "Test Name", + PostalCode: "1234567890", + CityID: 1, + } + + mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorparam.BenefactorExistByIDRequest{ID: 1}). + Return(benefactorparam.BenefactorExistByIDResponse{Existed: true}, nil).Once() + mockRepository.EXPECT().IsExistCityByID(ctx, uint(1)). + Return(true, nil).Once() + + fieldErrors, err := validator.ValidateAddAddress(ctx, req) + + assert.Error(t, err) + assert.NotNil(t, fieldErrors) + assert.Contains(t, fieldErrors, "lat") + }) + + t.Run("Empty lon", func(t *testing.T) { + req := addressparam.BenefactorAddAddressRequest{ + Address: "tehran", + BenefactorID: 1, + Lon: 0, + Lat: 90.0, + Name: "Test Name", + PostalCode: "1234567890", + CityID: 1, + } + + mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorparam.BenefactorExistByIDRequest{ID: 1}). + Return(benefactorparam.BenefactorExistByIDResponse{Existed: true}, nil).Once() + mockRepository.EXPECT().IsExistCityByID(ctx, uint(1)). + Return(true, nil).Once() + + fieldErrors, err := validator.ValidateAddAddress(ctx, req) + + assert.Error(t, err) + assert.NotNil(t, fieldErrors) + assert.Contains(t, fieldErrors, "lon") + }) + + t.Run("Empty name", func(t *testing.T) { + req := addressparam.BenefactorAddAddressRequest{ + Address: "tehran", + BenefactorID: 1, + Lon: 45.0, + Lat: 90.0, + Name: "", + PostalCode: "1234567890", + CityID: 1, + } + + mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorparam.BenefactorExistByIDRequest{ID: 1}). + Return(benefactorparam.BenefactorExistByIDResponse{Existed: true}, nil).Once() + mockRepository.EXPECT().IsExistCityByID(ctx, uint(1)). + Return(true, nil).Once() + + fieldErrors, err := validator.ValidateAddAddress(ctx, req) + + assert.Error(t, err) + assert.NotNil(t, fieldErrors) + assert.Contains(t, fieldErrors, "name") + }) + + t.Run("Empty postal code", func(t *testing.T) { + req := addressparam.BenefactorAddAddressRequest{ + Address: "tehran", + BenefactorID: 1, + Lon: 45.0, + Lat: 90.0, + Name: "Test Name", + PostalCode: "", + CityID: 1, + } + + mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorparam.BenefactorExistByIDRequest{ID: 1}). + Return(benefactorparam.BenefactorExistByIDResponse{Existed: true}, nil).Once() + mockRepository.EXPECT().IsExistCityByID(ctx, uint(1)). + Return(true, nil).Once() + + fieldErrors, err := validator.ValidateAddAddress(ctx, req) + + assert.Error(t, err) + assert.NotNil(t, fieldErrors) + assert.Contains(t, fieldErrors, "postal_code") + }) +} diff --git a/validator/benefactor/address/delete.go b/validator/benefactor/address/delete.go index a62518e..6c1e01a 100644 --- a/validator/benefactor/address/delete.go +++ b/validator/benefactor/address/delete.go @@ -39,5 +39,6 @@ func (v Validator) ValidateDeleteRequest(ctx context.Context, req param.DeleteAd WithErr(err) } - return map[string]string{}, nil + // nolint + return nil, nil } diff --git a/validator/benefactor/address/delete_test.go b/validator/benefactor/address/delete_test.go new file mode 100644 index 0000000..db82ec6 --- /dev/null +++ b/validator/benefactor/address/delete_test.go @@ -0,0 +1,140 @@ +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"]) + }) +} diff --git a/validator/benefactor/address/get.go b/validator/benefactor/address/get.go index beb6b95..daa00b8 100644 --- a/validator/benefactor/address/get.go +++ b/validator/benefactor/address/get.go @@ -39,5 +39,6 @@ func (v Validator) ValidateGetAddress(ctx context.Context, req param.GetAddressR WithErr(err) } - return map[string]string{}, nil + // nolint + return nil, nil } diff --git a/validator/benefactor/address/get_test.go b/validator/benefactor/address/get_test.go new file mode 100644 index 0000000..9392621 --- /dev/null +++ b/validator/benefactor/address/get_test.go @@ -0,0 +1,95 @@ +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_ValidateGetAddress(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.GetAddressRequest{ + AddressID: 1, + BenefactorID: 1, + } + + mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorparam.BenefactorExistByIDRequest{ID: 1}). + Return(benefactorparam.BenefactorExistByIDResponse{Existed: true}, nil).Once() + + fieldErrors, err := validator.ValidateGetAddress(ctx, req) + + assert.NoError(t, err) + assert.Nil(t, fieldErrors) + }) + + t.Run("Empty benefactor id", func(t *testing.T) { + req := addressparam.GetAddressRequest{ + AddressID: 1, + BenefactorID: 0, + } + + fieldErrors, err := validator.ValidateGetAddress(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.GetAddressRequest{ + AddressID: 1, + BenefactorID: 1, + } + + mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorparam.BenefactorExistByIDRequest{ID: 1}). + Return(benefactorparam.BenefactorExistByIDResponse{Existed: false}, nil).Once() + + fieldErrors, err := validator.ValidateGetAddress(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.GetAddressRequest{ + AddressID: 1, + BenefactorID: 1, + } + + mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorparam.BenefactorExistByIDRequest{ID: 1}). + Return(benefactorparam.BenefactorExistByIDResponse{Existed: false}, errors.New("service error")).Once() + + fieldErrors, err := validator.ValidateGetAddress(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.GetAddressRequest{ + AddressID: 0, + BenefactorID: 1, + } + + mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorparam.BenefactorExistByIDRequest{ID: 1}). + Return(benefactorparam.BenefactorExistByIDResponse{Existed: true}, nil).Once() + + fieldErrors, err := validator.ValidateGetAddress(ctx, req) + + assert.Error(t, err) + assert.NotNil(t, fieldErrors) + assert.Contains(t, fieldErrors, "AddressID") + }) +} diff --git a/validator/benefactor/address/mock_BenefactorSvc_test.go b/validator/benefactor/address/mock_BenefactorSvc_test.go new file mode 100644 index 0000000..4da745b --- /dev/null +++ b/validator/benefactor/address/mock_BenefactorSvc_test.go @@ -0,0 +1,94 @@ +// Code generated by mockery v2.41.0. DO NOT EDIT. + +package benefactoraddressvalidator + +import ( + context "context" + + benefactoreparam "git.gocasts.ir/ebhomengo/niki/param/benefactor/benefactor" + mock "github.com/stretchr/testify/mock" +) + +// MockBenefactorSvc is an autogenerated mock type for the BenefactorSvc type +type MockBenefactorSvc struct { + mock.Mock +} + +type MockBenefactorSvc_Expecter struct { + mock *mock.Mock +} + +func (_m *MockBenefactorSvc) EXPECT() *MockBenefactorSvc_Expecter { + return &MockBenefactorSvc_Expecter{mock: &_m.Mock} +} + +// BenefactorExistByID provides a mock function with given fields: ctx, request +func (_m *MockBenefactorSvc) BenefactorExistByID(ctx context.Context, request benefactoreparam.BenefactorExistByIDRequest) (benefactoreparam.BenefactorExistByIDResponse, error) { + ret := _m.Called(ctx, request) + + if len(ret) == 0 { + panic("no return value specified for BenefactorExistByID") + } + + var r0 benefactoreparam.BenefactorExistByIDResponse + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, benefactoreparam.BenefactorExistByIDRequest) (benefactoreparam.BenefactorExistByIDResponse, error)); ok { + return rf(ctx, request) + } + if rf, ok := ret.Get(0).(func(context.Context, benefactoreparam.BenefactorExistByIDRequest) benefactoreparam.BenefactorExistByIDResponse); ok { + r0 = rf(ctx, request) + } else { + r0 = ret.Get(0).(benefactoreparam.BenefactorExistByIDResponse) + } + + if rf, ok := ret.Get(1).(func(context.Context, benefactoreparam.BenefactorExistByIDRequest) error); ok { + r1 = rf(ctx, request) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// MockBenefactorSvc_BenefactorExistByID_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'BenefactorExistByID' +type MockBenefactorSvc_BenefactorExistByID_Call struct { + *mock.Call +} + +// BenefactorExistByID is a helper method to define mock.On call +// - ctx context.Context +// - request benefactoreparam.BenefactorExistByIDRequest +func (_e *MockBenefactorSvc_Expecter) BenefactorExistByID(ctx interface{}, request interface{}) *MockBenefactorSvc_BenefactorExistByID_Call { + return &MockBenefactorSvc_BenefactorExistByID_Call{Call: _e.mock.On("BenefactorExistByID", ctx, request)} +} + +func (_c *MockBenefactorSvc_BenefactorExistByID_Call) Run(run func(ctx context.Context, request benefactoreparam.BenefactorExistByIDRequest)) *MockBenefactorSvc_BenefactorExistByID_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(benefactoreparam.BenefactorExistByIDRequest)) + }) + return _c +} + +func (_c *MockBenefactorSvc_BenefactorExistByID_Call) Return(_a0 benefactoreparam.BenefactorExistByIDResponse, _a1 error) *MockBenefactorSvc_BenefactorExistByID_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *MockBenefactorSvc_BenefactorExistByID_Call) RunAndReturn(run func(context.Context, benefactoreparam.BenefactorExistByIDRequest) (benefactoreparam.BenefactorExistByIDResponse, error)) *MockBenefactorSvc_BenefactorExistByID_Call { + _c.Call.Return(run) + return _c +} + +// NewMockBenefactorSvc creates a new instance of MockBenefactorSvc. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +// The first argument is typically a *testing.T value. +func NewMockBenefactorSvc(t interface { + mock.TestingT + Cleanup(func()) +}) *MockBenefactorSvc { + mock := &MockBenefactorSvc{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/validator/benefactor/address/mock_Repository_test.go b/validator/benefactor/address/mock_Repository_test.go new file mode 100644 index 0000000..f80f859 --- /dev/null +++ b/validator/benefactor/address/mock_Repository_test.go @@ -0,0 +1,151 @@ +// Code generated by mockery v2.41.0. DO NOT EDIT. + +package benefactoraddressvalidator + +import ( + context "context" + + mock "github.com/stretchr/testify/mock" +) + +// MockRepository is an autogenerated mock type for the Repository type +type MockRepository struct { + mock.Mock +} + +type MockRepository_Expecter struct { + mock *mock.Mock +} + +func (_m *MockRepository) EXPECT() *MockRepository_Expecter { + return &MockRepository_Expecter{mock: &_m.Mock} +} + +// IsExistAddressByID provides a mock function with given fields: ctx, addressID, benefactorID +func (_m *MockRepository) IsExistAddressByID(ctx context.Context, addressID uint, benefactorID uint) (bool, error) { + ret := _m.Called(ctx, addressID, benefactorID) + + if len(ret) == 0 { + panic("no return value specified for IsExistAddressByID") + } + + var r0 bool + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, uint, uint) (bool, error)); ok { + return rf(ctx, addressID, benefactorID) + } + if rf, ok := ret.Get(0).(func(context.Context, uint, uint) bool); ok { + r0 = rf(ctx, addressID, benefactorID) + } else { + r0 = ret.Get(0).(bool) + } + + if rf, ok := ret.Get(1).(func(context.Context, uint, uint) error); ok { + r1 = rf(ctx, addressID, benefactorID) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// MockRepository_IsExistAddressByID_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'IsExistAddressByID' +type MockRepository_IsExistAddressByID_Call struct { + *mock.Call +} + +// IsExistAddressByID is a helper method to define mock.On call +// - ctx context.Context +// - addressID uint +// - benefactorID uint +func (_e *MockRepository_Expecter) IsExistAddressByID(ctx interface{}, addressID interface{}, benefactorID interface{}) *MockRepository_IsExistAddressByID_Call { + return &MockRepository_IsExistAddressByID_Call{Call: _e.mock.On("IsExistAddressByID", ctx, addressID, benefactorID)} +} + +func (_c *MockRepository_IsExistAddressByID_Call) Run(run func(ctx context.Context, addressID uint, benefactorID uint)) *MockRepository_IsExistAddressByID_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(uint), args[2].(uint)) + }) + return _c +} + +func (_c *MockRepository_IsExistAddressByID_Call) Return(_a0 bool, _a1 error) *MockRepository_IsExistAddressByID_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *MockRepository_IsExistAddressByID_Call) RunAndReturn(run func(context.Context, uint, uint) (bool, error)) *MockRepository_IsExistAddressByID_Call { + _c.Call.Return(run) + return _c +} + +// IsExistCityByID provides a mock function with given fields: ctx, id +func (_m *MockRepository) IsExistCityByID(ctx context.Context, id uint) (bool, error) { + ret := _m.Called(ctx, id) + + if len(ret) == 0 { + panic("no return value specified for IsExistCityByID") + } + + var r0 bool + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, uint) (bool, error)); ok { + return rf(ctx, id) + } + if rf, ok := ret.Get(0).(func(context.Context, uint) bool); ok { + r0 = rf(ctx, id) + } else { + r0 = ret.Get(0).(bool) + } + + if rf, ok := ret.Get(1).(func(context.Context, uint) error); ok { + r1 = rf(ctx, id) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// MockRepository_IsExistCityByID_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'IsExistCityByID' +type MockRepository_IsExistCityByID_Call struct { + *mock.Call +} + +// IsExistCityByID is a helper method to define mock.On call +// - ctx context.Context +// - id uint +func (_e *MockRepository_Expecter) IsExistCityByID(ctx interface{}, id interface{}) *MockRepository_IsExistCityByID_Call { + return &MockRepository_IsExistCityByID_Call{Call: _e.mock.On("IsExistCityByID", ctx, id)} +} + +func (_c *MockRepository_IsExistCityByID_Call) Run(run func(ctx context.Context, id uint)) *MockRepository_IsExistCityByID_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(uint)) + }) + return _c +} + +func (_c *MockRepository_IsExistCityByID_Call) Return(_a0 bool, _a1 error) *MockRepository_IsExistCityByID_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *MockRepository_IsExistCityByID_Call) RunAndReturn(run func(context.Context, uint) (bool, error)) *MockRepository_IsExistCityByID_Call { + _c.Call.Return(run) + return _c +} + +// NewMockRepository creates a new instance of MockRepository. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +// The first argument is typically a *testing.T value. +func NewMockRepository(t interface { + mock.TestingT + Cleanup(func()) +}) *MockRepository { + mock := &MockRepository{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/validator/benefactor/address/update.go b/validator/benefactor/address/update.go index e539831..f0bc9f4 100644 --- a/validator/benefactor/address/update.go +++ b/validator/benefactor/address/update.go @@ -24,6 +24,10 @@ func (v Validator) ValidateUpdateAddress(ctx context.Context, req param.UpdateAd validation.Field(&req.Name, validation.Required), + validation.Field(&req.Lon, validation.Required), + + validation.Field(&req.Lat, validation.Required), + validation.Field(&req.PostalCode, validation.Required), validation.Field(&req.CityID, validation.Required, @@ -47,5 +51,6 @@ func (v Validator) ValidateUpdateAddress(ctx context.Context, req param.UpdateAd WithErr(err) } - return map[string]string{}, nil + // nolint + return nil, nil } diff --git a/validator/benefactor/address/update_test.go b/validator/benefactor/address/update_test.go new file mode 100644 index 0000000..d67e2a4 --- /dev/null +++ b/validator/benefactor/address/update_test.go @@ -0,0 +1,402 @@ +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_ValidateUpdateAddress(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.UpdateAddressRequest{ + ID: uint(1), + Address: "tehran", + BenefactorID: 1, + Lon: 45.0, + Lat: 90.0, + Name: "Test Name", + PostalCode: "1234567890", + CityID: 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() + mockRepository.EXPECT().IsExistCityByID(ctx, uint(1)). + Return(true, nil).Once() + + fieldErrors, err := validator.ValidateUpdateAddress(ctx, req) + + assert.NoError(t, err) + assert.Nil(t, fieldErrors) + }) + + t.Run("Empty address id", func(t *testing.T) { + req := addressparam.UpdateAddressRequest{ + ID: uint(0), + Address: "tehran", + BenefactorID: 1, + Lon: 45.0, + Lat: 90.0, + Name: "Test Name", + PostalCode: "1234567890", + CityID: 1, + } + + mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorparam.BenefactorExistByIDRequest{ID: 1}). + Return(benefactorparam.BenefactorExistByIDResponse{Existed: true}, nil).Once() + mockRepository.EXPECT().IsExistCityByID(ctx, uint(1)). + Return(true, nil).Once() + + fieldErrors, err := validator.ValidateUpdateAddress(ctx, req) + + assert.Error(t, err) + assert.NotNil(t, fieldErrors) + assert.Contains(t, fieldErrors, "ID") + }) + + t.Run("Address does not exist", func(t *testing.T) { + req := addressparam.UpdateAddressRequest{ + ID: uint(1), + Address: "tehran", + BenefactorID: 1, + Lon: 45.0, + Lat: 90.0, + Name: "Test Name", + PostalCode: "1234567890", + CityID: 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() + mockRepository.EXPECT().IsExistCityByID(ctx, uint(1)). + Return(true, nil).Once() + + fieldErrors, err := validator.ValidateUpdateAddress(ctx, req) + + assert.Error(t, err) + assert.NotNil(t, fieldErrors) + assert.Equal(t, errmsg.ErrorMsgNotFound, fieldErrors["ID"]) + }) + + t.Run("Address exist error", func(t *testing.T) { + req := addressparam.UpdateAddressRequest{ + ID: uint(1), + Address: "tehran", + BenefactorID: 1, + Lon: 45.0, + Lat: 90.0, + Name: "Test Name", + PostalCode: "1234567890", + CityID: 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() + mockRepository.EXPECT().IsExistCityByID(ctx, uint(1)). + Return(true, nil).Once() + + fieldErrors, err := validator.ValidateUpdateAddress(ctx, req) + + assert.Error(t, err) + assert.NotNil(t, fieldErrors) + assert.Equal(t, errmsg.ErrorMsgSomethingWentWrong, fieldErrors["ID"]) + }) + + t.Run("Empty benefactor id", func(t *testing.T) { + req := addressparam.UpdateAddressRequest{ + ID: uint(1), + Address: "tehran", + BenefactorID: 0, + Lon: 45.0, + Lat: 90.0, + Name: "Test Name", + PostalCode: "1234567890", + CityID: 1, + } + + mockRepository.EXPECT().IsExistAddressByID(ctx, uint(1), uint(0)). + Return(true, nil).Once() + mockRepository.EXPECT().IsExistCityByID(ctx, uint(1)). + Return(true, nil).Once() + + fieldErrors, err := validator.ValidateUpdateAddress(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.UpdateAddressRequest{ + ID: uint(1), + Address: "tehran", + BenefactorID: 1, + Lon: 45.0, + Lat: 90.0, + Name: "Test Name", + PostalCode: "1234567890", + CityID: 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() + mockRepository.EXPECT().IsExistCityByID(ctx, uint(1)). + Return(true, nil).Once() + + fieldErrors, err := validator.ValidateUpdateAddress(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.UpdateAddressRequest{ + ID: uint(1), + Address: "tehran", + BenefactorID: 1, + Lon: 45.0, + Lat: 90.0, + Name: "Test Name", + PostalCode: "1234567890", + CityID: 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() + mockRepository.EXPECT().IsExistCityByID(ctx, uint(1)). + Return(true, nil).Once() + + fieldErrors, err := validator.ValidateUpdateAddress(ctx, req) + + assert.Error(t, err) + assert.NotNil(t, fieldErrors) + assert.Equal(t, errmsg.ErrorMsgSomethingWentWrong, fieldErrors["BenefactorID"]) + }) + + t.Run("Empty city id", func(t *testing.T) { + req := addressparam.UpdateAddressRequest{ + ID: uint(1), + Address: "tehran", + BenefactorID: 1, + Lon: 45.0, + Lat: 90.0, + Name: "Test Name", + PostalCode: "1234567890", + CityID: 0, + } + + 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.ValidateUpdateAddress(ctx, req) + + assert.Error(t, err) + assert.NotNil(t, fieldErrors) + assert.Contains(t, fieldErrors, "city_id") + }) + + t.Run("City does not exist", func(t *testing.T) { + req := addressparam.UpdateAddressRequest{ + ID: uint(1), + Address: "tehran", + BenefactorID: 1, + Lon: 45.0, + Lat: 90.0, + Name: "Test Name", + PostalCode: "1234567890", + CityID: 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() + mockRepository.EXPECT().IsExistCityByID(ctx, uint(1)). + Return(false, nil).Once() + + fieldErrors, err := validator.ValidateUpdateAddress(ctx, req) + + assert.Error(t, err) + assert.NotNil(t, fieldErrors) + assert.Equal(t, errmsg.ErrorMsgNotFound, fieldErrors["city_id"]) + }) + + t.Run("City exist error", func(t *testing.T) { + req := addressparam.UpdateAddressRequest{ + ID: uint(1), + Address: "tehran", + BenefactorID: 1, + Lon: 45.0, + Lat: 90.0, + Name: "Test Name", + PostalCode: "1234567890", + CityID: 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() + mockRepository.EXPECT().IsExistCityByID(ctx, uint(1)). + Return(false, errors.New("repository error")).Once() + + fieldErrors, err := validator.ValidateUpdateAddress(ctx, req) + + assert.Error(t, err) + assert.NotNil(t, fieldErrors) + assert.Equal(t, errmsg.ErrorMsgSomethingWentWrong, fieldErrors["city_id"]) + }) + + t.Run("Empty address", func(t *testing.T) { + req := addressparam.UpdateAddressRequest{ + ID: uint(1), + Address: "", + BenefactorID: 1, + Lon: 45.0, + Lat: 90.0, + Name: "Test Name", + PostalCode: "1234567890", + CityID: 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() + mockRepository.EXPECT().IsExistCityByID(ctx, uint(1)). + Return(true, nil).Once() + + fieldErrors, err := validator.ValidateUpdateAddress(ctx, req) + + assert.Error(t, err) + assert.NotNil(t, fieldErrors) + assert.Contains(t, fieldErrors, "address") + }) + + t.Run("Empty lat", func(t *testing.T) { + req := addressparam.UpdateAddressRequest{ + ID: uint(1), + Address: "tehran", + BenefactorID: 1, + Lon: 45.0, + Lat: 0, + Name: "Test Name", + PostalCode: "1234567890", + CityID: 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() + mockRepository.EXPECT().IsExistCityByID(ctx, uint(1)). + Return(true, nil).Once() + + fieldErrors, err := validator.ValidateUpdateAddress(ctx, req) + + assert.Error(t, err) + assert.NotNil(t, fieldErrors) + assert.Contains(t, fieldErrors, "lat") + }) + + t.Run("Empty lon", func(t *testing.T) { + req := addressparam.UpdateAddressRequest{ + ID: uint(1), + Address: "tehran", + BenefactorID: 1, + Lon: 0, + Lat: 90.0, + Name: "Test Name", + PostalCode: "1234567890", + CityID: 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() + mockRepository.EXPECT().IsExistCityByID(ctx, uint(1)). + Return(true, nil).Once() + + fieldErrors, err := validator.ValidateUpdateAddress(ctx, req) + + assert.Error(t, err) + assert.NotNil(t, fieldErrors) + assert.Contains(t, fieldErrors, "lon") + }) + + t.Run("Empty name", func(t *testing.T) { + req := addressparam.UpdateAddressRequest{ + ID: uint(1), + Address: "tehran", + BenefactorID: 1, + Lon: 45.0, + Lat: 90.0, + Name: "", + PostalCode: "1234567890", + CityID: 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() + mockRepository.EXPECT().IsExistCityByID(ctx, uint(1)). + Return(true, nil).Once() + + fieldErrors, err := validator.ValidateUpdateAddress(ctx, req) + + assert.Error(t, err) + assert.NotNil(t, fieldErrors) + assert.Contains(t, fieldErrors, "name") + }) + + t.Run("Empty postal code", func(t *testing.T) { + req := addressparam.UpdateAddressRequest{ + ID: uint(1), + Address: "tehran", + BenefactorID: 1, + Lon: 45.0, + Lat: 90.0, + Name: "Test Name", + PostalCode: "", + CityID: 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() + mockRepository.EXPECT().IsExistCityByID(ctx, uint(1)). + Return(true, nil).Once() + + fieldErrors, err := validator.ValidateUpdateAddress(ctx, req) + + assert.Error(t, err) + assert.NotNil(t, fieldErrors) + assert.Contains(t, fieldErrors, "postal_code") + }) +} diff --git a/validator/benefactor/address/validator.go b/validator/benefactor/address/validator.go index bba62be..1287fea 100644 --- a/validator/benefactor/address/validator.go +++ b/validator/benefactor/address/validator.go @@ -9,9 +9,12 @@ import ( validation "github.com/go-ozzo/ozzo-validation/v4" ) +//go:generate mockery --name BenefactorSvc type BenefactorSvc interface { BenefactorExistByID(ctx context.Context, request param.BenefactorExistByIDRequest) (param.BenefactorExistByIDResponse, error) } + +//go:generate mockery --name Repository type Repository interface { IsExistCityByID(ctx context.Context, id uint) (bool, error) IsExistAddressByID(ctx context.Context, addressID uint, benefactorID uint) (bool, error) @@ -31,11 +34,15 @@ func (v Validator) doesBenefactorExist(ctx context.Context) validation.RuleFunc if !ok { return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) } - _, err := v.benefactorSvc.BenefactorExistByID(ctx, param.BenefactorExistByIDRequest{ID: benefactorID}) + res, err := v.benefactorSvc.BenefactorExistByID(ctx, param.BenefactorExistByIDRequest{ID: benefactorID}) if err != nil { - return fmt.Errorf(errmsg.ErrorMsgNotFound) + return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) } - // TODO: check benefactor ID given from user most check with claims (benefactorID) + + if !res.Existed { + return fmt.Errorf(errmsg.ErrorMsgBenefactorNotFound) + } + return nil } } @@ -48,7 +55,7 @@ func (v Validator) doesCityExist(ctx context.Context) validation.RuleFunc { } isExisted, err := v.repository.IsExistCityByID(ctx, cityID) if err != nil { - return fmt.Errorf(errmsg.ErrorMsgNotFound) + return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) } if !isExisted { return fmt.Errorf(errmsg.ErrorMsgNotFound) @@ -66,7 +73,7 @@ func (v Validator) doesAddressExist(ctx context.Context, benefactorID uint) vali } isExisted, err := v.repository.IsExistAddressByID(ctx, addressID, benefactorID) if err != nil { - return fmt.Errorf(errmsg.ErrorMsgNotFound) + return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) } if !isExisted { return fmt.Errorf(errmsg.ErrorMsgNotFound) diff --git a/validator/benefactor/benefactor/login_register_test.go b/validator/benefactor/benefactor/login_register_test.go new file mode 100644 index 0000000..317e693 --- /dev/null +++ b/validator/benefactor/benefactor/login_register_test.go @@ -0,0 +1,46 @@ +package benefactorvalidator + +import ( + "testing" + + benefactoreparam "git.gocasts.ir/ebhomengo/niki/param/benefactor/benefactor" + errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" + "github.com/stretchr/testify/assert" +) + +func TestValidator_ValidateLoginRegisterRequest(t *testing.T) { + validator := New() + validPhoneNumber := "09123456789" + + t.Run("Valid request", func(t *testing.T) { + req := benefactoreparam.LoginOrRegisterRequest{ + PhoneNumber: validPhoneNumber, + } + + fieldErrors, err := validator.ValidateLoginRegisterRequest(req) + assert.NoError(t, err) + assert.Nil(t, fieldErrors) + }) + + t.Run("Empty phone number", func(t *testing.T) { + req := benefactoreparam.LoginOrRegisterRequest{ + PhoneNumber: "", + } + + fieldErrors, err := validator.ValidateLoginRegisterRequest(req) + assert.Error(t, err) + assert.NotNil(t, fieldErrors) + assert.Contains(t, fieldErrors, "phone_number") + }) + + t.Run("Invalid phone number format", func(t *testing.T) { + req := benefactoreparam.LoginOrRegisterRequest{ + PhoneNumber: "12345", + } + + fieldErrors, err := validator.ValidateLoginRegisterRequest(req) + assert.Error(t, err) + assert.NotNil(t, fieldErrors) + assert.Equal(t, errmsg.ErrorMsgPhoneNumberIsNotValid, fieldErrors["phone_number"]) + }) +} diff --git a/validator/benefactor/benefactor/send_otp_test.go b/validator/benefactor/benefactor/send_otp_test.go new file mode 100644 index 0000000..e39d877 --- /dev/null +++ b/validator/benefactor/benefactor/send_otp_test.go @@ -0,0 +1,46 @@ +package benefactorvalidator + +import ( + "testing" + + benefactoreparam "git.gocasts.ir/ebhomengo/niki/param/benefactor/benefactor" + errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" + "github.com/stretchr/testify/assert" +) + +func TestValidator_ValidateSendOtpRequest(t *testing.T) { + validator := New() + validPhoneNumber := "09123456789" + + t.Run("Valid request", func(t *testing.T) { + req := benefactoreparam.SendOtpRequest{ + PhoneNumber: validPhoneNumber, + } + + fieldErrors, err := validator.ValidateSendOtpRequest(req) + assert.NoError(t, err) + assert.Nil(t, fieldErrors) + }) + + t.Run("Empty phone number", func(t *testing.T) { + req := benefactoreparam.SendOtpRequest{ + PhoneNumber: "", + } + + fieldErrors, err := validator.ValidateSendOtpRequest(req) + assert.Error(t, err) + assert.NotNil(t, fieldErrors) + assert.Contains(t, fieldErrors, "phone_number") + }) + + t.Run("Invalid phone number format", func(t *testing.T) { + req := benefactoreparam.SendOtpRequest{ + PhoneNumber: "12345", + } + + fieldErrors, err := validator.ValidateSendOtpRequest(req) + assert.Error(t, err) + assert.NotNil(t, fieldErrors) + assert.Equal(t, errmsg.ErrorMsgPhoneNumberIsNotValid, fieldErrors["phone_number"]) + }) +} diff --git a/validator/benefactor/kind_box/get.go b/validator/benefactor/kind_box/get.go index e74ce92..2cfb197 100644 --- a/validator/benefactor/kind_box/get.go +++ b/validator/benefactor/kind_box/get.go @@ -39,5 +39,6 @@ func (v Validator) ValidateGetRequest(ctx context.Context, req param.KindBoxGetR WithErr(err) } - return map[string]string{}, nil + // nolint + return nil, nil } diff --git a/validator/benefactor/kind_box/get_all.go b/validator/benefactor/kind_box/get_all.go index fbe0cb9..290cc56 100644 --- a/validator/benefactor/kind_box/get_all.go +++ b/validator/benefactor/kind_box/get_all.go @@ -42,5 +42,6 @@ func (v Validator) ValidateGetAll(req param.KindBoxGetAllRequest) (map[string]st WithErr(err) } - return map[string]string{}, nil + // nolint + return nil, nil } diff --git a/validator/benefactor/kind_box/get_all_test.go b/validator/benefactor/kind_box/get_all_test.go new file mode 100644 index 0000000..ff6499d --- /dev/null +++ b/validator/benefactor/kind_box/get_all_test.go @@ -0,0 +1,84 @@ +package benefactorkindboxvalidator + +import ( + "testing" + + params "git.gocasts.ir/ebhomengo/niki/param" + param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box" + "github.com/stretchr/testify/assert" +) + +func TestValidator_ValidateGetAll(t *testing.T) { + mockRepository := NewMockRepository(t) + mockBenefactorSvc := NewMockBenefactorSvc(t) + mockAddressSvc := NewMockAddressSvc(t) + mockReferTimeSvc := NewMockReferTimeSvc(t) + validator := New(mockRepository, mockBenefactorSvc, mockAddressSvc, mockReferTimeSvc) + + t.Run("Successful validation", func(t *testing.T) { + req := param.KindBoxGetAllRequest{ + Pagination: params.PaginationRequest{}, + Sort: params.SortRequest{ + Field: "id", + Direction: "asc", + }, + Filter: map[string]any{"id": 1}, + } + + fieldErrors, err := validator.ValidateGetAll(req) + + assert.NoError(t, err) + assert.Nil(t, fieldErrors) + }) + + t.Run("Invalid sort field", func(t *testing.T) { + req := param.KindBoxGetAllRequest{ + Pagination: params.PaginationRequest{}, + Sort: params.SortRequest{ + Field: "invalid", + Direction: "asc", + }, + Filter: map[string]any{"id": 1}, + } + + fieldErrors, err := validator.ValidateGetAll(req) + + assert.Error(t, err) + assert.NotNil(t, fieldErrors) + assert.Contains(t, fieldErrors, "Sort") + }) + + t.Run("Invalid sort direction", func(t *testing.T) { + req := param.KindBoxGetAllRequest{ + Pagination: params.PaginationRequest{}, + Sort: params.SortRequest{ + Field: "id", + Direction: "invalid", + }, + Filter: map[string]any{"id": 1}, + } + + fieldErrors, err := validator.ValidateGetAll(req) + + assert.Error(t, err) + assert.NotNil(t, fieldErrors) + assert.Contains(t, fieldErrors, "Sort") + }) + + t.Run("Invalid filter", func(t *testing.T) { + req := param.KindBoxGetAllRequest{ + Pagination: params.PaginationRequest{}, + Sort: params.SortRequest{ + Field: "id", + Direction: "desc", + }, + Filter: map[string]any{"invalid": 1}, + } + + fieldErrors, err := validator.ValidateGetAll(req) + + assert.Error(t, err) + assert.NotNil(t, fieldErrors) + assert.Contains(t, fieldErrors, "Filter") + }) +} diff --git a/validator/benefactor/kind_box/get_test.go b/validator/benefactor/kind_box/get_test.go new file mode 100644 index 0000000..1bd9b28 --- /dev/null +++ b/validator/benefactor/kind_box/get_test.go @@ -0,0 +1,136 @@ +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"]) + }) +} diff --git a/validator/benefactor/kind_box/mock_AddressSvc_test.go b/validator/benefactor/kind_box/mock_AddressSvc_test.go new file mode 100644 index 0000000..7158211 --- /dev/null +++ b/validator/benefactor/kind_box/mock_AddressSvc_test.go @@ -0,0 +1,94 @@ +// Code generated by mockery v2.41.0. DO NOT EDIT. + +package benefactorkindboxvalidator + +import ( + context "context" + + addressparam "git.gocasts.ir/ebhomengo/niki/param/benefactor/address" + mock "github.com/stretchr/testify/mock" +) + +// MockAddressSvc is an autogenerated mock type for the AddressSvc type +type MockAddressSvc struct { + mock.Mock +} + +type MockAddressSvc_Expecter struct { + mock *mock.Mock +} + +func (_m *MockAddressSvc) EXPECT() *MockAddressSvc_Expecter { + return &MockAddressSvc_Expecter{mock: &_m.Mock} +} + +// AddressExistByID provides a mock function with given fields: ctx, request +func (_m *MockAddressSvc) AddressExistByID(ctx context.Context, request addressparam.GetAddressByIDRequest) (addressparam.GetAddressByIDResponse, error) { + ret := _m.Called(ctx, request) + + if len(ret) == 0 { + panic("no return value specified for AddressExistByID") + } + + var r0 addressparam.GetAddressByIDResponse + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, addressparam.GetAddressByIDRequest) (addressparam.GetAddressByIDResponse, error)); ok { + return rf(ctx, request) + } + if rf, ok := ret.Get(0).(func(context.Context, addressparam.GetAddressByIDRequest) addressparam.GetAddressByIDResponse); ok { + r0 = rf(ctx, request) + } else { + r0 = ret.Get(0).(addressparam.GetAddressByIDResponse) + } + + if rf, ok := ret.Get(1).(func(context.Context, addressparam.GetAddressByIDRequest) error); ok { + r1 = rf(ctx, request) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// MockAddressSvc_AddressExistByID_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'AddressExistByID' +type MockAddressSvc_AddressExistByID_Call struct { + *mock.Call +} + +// AddressExistByID is a helper method to define mock.On call +// - ctx context.Context +// - request addressparam.GetAddressByIDRequest +func (_e *MockAddressSvc_Expecter) AddressExistByID(ctx interface{}, request interface{}) *MockAddressSvc_AddressExistByID_Call { + return &MockAddressSvc_AddressExistByID_Call{Call: _e.mock.On("AddressExistByID", ctx, request)} +} + +func (_c *MockAddressSvc_AddressExistByID_Call) Run(run func(ctx context.Context, request addressparam.GetAddressByIDRequest)) *MockAddressSvc_AddressExistByID_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(addressparam.GetAddressByIDRequest)) + }) + return _c +} + +func (_c *MockAddressSvc_AddressExistByID_Call) Return(_a0 addressparam.GetAddressByIDResponse, _a1 error) *MockAddressSvc_AddressExistByID_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *MockAddressSvc_AddressExistByID_Call) RunAndReturn(run func(context.Context, addressparam.GetAddressByIDRequest) (addressparam.GetAddressByIDResponse, error)) *MockAddressSvc_AddressExistByID_Call { + _c.Call.Return(run) + return _c +} + +// NewMockAddressSvc creates a new instance of MockAddressSvc. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +// The first argument is typically a *testing.T value. +func NewMockAddressSvc(t interface { + mock.TestingT + Cleanup(func()) +}) *MockAddressSvc { + mock := &MockAddressSvc{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/validator/benefactor/kind_box/mock_BenefactorSvc_test.go b/validator/benefactor/kind_box/mock_BenefactorSvc_test.go new file mode 100644 index 0000000..9f6a4d9 --- /dev/null +++ b/validator/benefactor/kind_box/mock_BenefactorSvc_test.go @@ -0,0 +1,94 @@ +// Code generated by mockery v2.41.0. DO NOT EDIT. + +package benefactorkindboxvalidator + +import ( + context "context" + + benefactoreparam "git.gocasts.ir/ebhomengo/niki/param/benefactor/benefactor" + mock "github.com/stretchr/testify/mock" +) + +// MockBenefactorSvc is an autogenerated mock type for the BenefactorSvc type +type MockBenefactorSvc struct { + mock.Mock +} + +type MockBenefactorSvc_Expecter struct { + mock *mock.Mock +} + +func (_m *MockBenefactorSvc) EXPECT() *MockBenefactorSvc_Expecter { + return &MockBenefactorSvc_Expecter{mock: &_m.Mock} +} + +// BenefactorExistByID provides a mock function with given fields: ctx, request +func (_m *MockBenefactorSvc) BenefactorExistByID(ctx context.Context, request benefactoreparam.BenefactorExistByIDRequest) (benefactoreparam.BenefactorExistByIDResponse, error) { + ret := _m.Called(ctx, request) + + if len(ret) == 0 { + panic("no return value specified for BenefactorExistByID") + } + + var r0 benefactoreparam.BenefactorExistByIDResponse + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, benefactoreparam.BenefactorExistByIDRequest) (benefactoreparam.BenefactorExistByIDResponse, error)); ok { + return rf(ctx, request) + } + if rf, ok := ret.Get(0).(func(context.Context, benefactoreparam.BenefactorExistByIDRequest) benefactoreparam.BenefactorExistByIDResponse); ok { + r0 = rf(ctx, request) + } else { + r0 = ret.Get(0).(benefactoreparam.BenefactorExistByIDResponse) + } + + if rf, ok := ret.Get(1).(func(context.Context, benefactoreparam.BenefactorExistByIDRequest) error); ok { + r1 = rf(ctx, request) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// MockBenefactorSvc_BenefactorExistByID_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'BenefactorExistByID' +type MockBenefactorSvc_BenefactorExistByID_Call struct { + *mock.Call +} + +// BenefactorExistByID is a helper method to define mock.On call +// - ctx context.Context +// - request benefactoreparam.BenefactorExistByIDRequest +func (_e *MockBenefactorSvc_Expecter) BenefactorExistByID(ctx interface{}, request interface{}) *MockBenefactorSvc_BenefactorExistByID_Call { + return &MockBenefactorSvc_BenefactorExistByID_Call{Call: _e.mock.On("BenefactorExistByID", ctx, request)} +} + +func (_c *MockBenefactorSvc_BenefactorExistByID_Call) Run(run func(ctx context.Context, request benefactoreparam.BenefactorExistByIDRequest)) *MockBenefactorSvc_BenefactorExistByID_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(benefactoreparam.BenefactorExistByIDRequest)) + }) + return _c +} + +func (_c *MockBenefactorSvc_BenefactorExistByID_Call) Return(_a0 benefactoreparam.BenefactorExistByIDResponse, _a1 error) *MockBenefactorSvc_BenefactorExistByID_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *MockBenefactorSvc_BenefactorExistByID_Call) RunAndReturn(run func(context.Context, benefactoreparam.BenefactorExistByIDRequest) (benefactoreparam.BenefactorExistByIDResponse, error)) *MockBenefactorSvc_BenefactorExistByID_Call { + _c.Call.Return(run) + return _c +} + +// NewMockBenefactorSvc creates a new instance of MockBenefactorSvc. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +// The first argument is typically a *testing.T value. +func NewMockBenefactorSvc(t interface { + mock.TestingT + Cleanup(func()) +}) *MockBenefactorSvc { + mock := &MockBenefactorSvc{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/validator/benefactor/kind_box/mock_ReferTimeSvc_test.go b/validator/benefactor/kind_box/mock_ReferTimeSvc_test.go new file mode 100644 index 0000000..cb1d958 --- /dev/null +++ b/validator/benefactor/kind_box/mock_ReferTimeSvc_test.go @@ -0,0 +1,94 @@ +// Code generated by mockery v2.41.0. DO NOT EDIT. + +package benefactorkindboxvalidator + +import ( + context "context" + + param "git.gocasts.ir/ebhomengo/niki/param/benefactor/refer_time" + mock "github.com/stretchr/testify/mock" +) + +// MockReferTimeSvc is an autogenerated mock type for the ReferTimeSvc type +type MockReferTimeSvc struct { + mock.Mock +} + +type MockReferTimeSvc_Expecter struct { + mock *mock.Mock +} + +func (_m *MockReferTimeSvc) EXPECT() *MockReferTimeSvc_Expecter { + return &MockReferTimeSvc_Expecter{mock: &_m.Mock} +} + +// GetReferTimeByID provides a mock function with given fields: ctx, req +func (_m *MockReferTimeSvc) GetReferTimeByID(ctx context.Context, req param.GetReferTimeRequest) (param.GetReferTimeResponse, error) { + ret := _m.Called(ctx, req) + + if len(ret) == 0 { + panic("no return value specified for GetReferTimeByID") + } + + var r0 param.GetReferTimeResponse + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, param.GetReferTimeRequest) (param.GetReferTimeResponse, error)); ok { + return rf(ctx, req) + } + if rf, ok := ret.Get(0).(func(context.Context, param.GetReferTimeRequest) param.GetReferTimeResponse); ok { + r0 = rf(ctx, req) + } else { + r0 = ret.Get(0).(param.GetReferTimeResponse) + } + + if rf, ok := ret.Get(1).(func(context.Context, param.GetReferTimeRequest) error); ok { + r1 = rf(ctx, req) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// MockReferTimeSvc_GetReferTimeByID_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'GetReferTimeByID' +type MockReferTimeSvc_GetReferTimeByID_Call struct { + *mock.Call +} + +// GetReferTimeByID is a helper method to define mock.On call +// - ctx context.Context +// - req param.GetReferTimeRequest +func (_e *MockReferTimeSvc_Expecter) GetReferTimeByID(ctx interface{}, req interface{}) *MockReferTimeSvc_GetReferTimeByID_Call { + return &MockReferTimeSvc_GetReferTimeByID_Call{Call: _e.mock.On("GetReferTimeByID", ctx, req)} +} + +func (_c *MockReferTimeSvc_GetReferTimeByID_Call) Run(run func(ctx context.Context, req param.GetReferTimeRequest)) *MockReferTimeSvc_GetReferTimeByID_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(param.GetReferTimeRequest)) + }) + return _c +} + +func (_c *MockReferTimeSvc_GetReferTimeByID_Call) Return(_a0 param.GetReferTimeResponse, _a1 error) *MockReferTimeSvc_GetReferTimeByID_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *MockReferTimeSvc_GetReferTimeByID_Call) RunAndReturn(run func(context.Context, param.GetReferTimeRequest) (param.GetReferTimeResponse, error)) *MockReferTimeSvc_GetReferTimeByID_Call { + _c.Call.Return(run) + return _c +} + +// NewMockReferTimeSvc creates a new instance of MockReferTimeSvc. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +// The first argument is typically a *testing.T value. +func NewMockReferTimeSvc(t interface { + mock.TestingT + Cleanup(func()) +}) *MockReferTimeSvc { + mock := &MockReferTimeSvc{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/validator/benefactor/kind_box/mock_Repository_test.go b/validator/benefactor/kind_box/mock_Repository_test.go new file mode 100644 index 0000000..68970d9 --- /dev/null +++ b/validator/benefactor/kind_box/mock_Repository_test.go @@ -0,0 +1,152 @@ +// Code generated by mockery v2.41.0. DO NOT EDIT. + +package benefactorkindboxvalidator + +import ( + context "context" + + entity "git.gocasts.ir/ebhomengo/niki/entity" + mock "github.com/stretchr/testify/mock" +) + +// MockRepository is an autogenerated mock type for the Repository type +type MockRepository struct { + mock.Mock +} + +type MockRepository_Expecter struct { + mock *mock.Mock +} + +func (_m *MockRepository) EXPECT() *MockRepository_Expecter { + return &MockRepository_Expecter{mock: &_m.Mock} +} + +// BenefactorKindBoxExist provides a mock function with given fields: ctx, benefactorID, kindBoxID +func (_m *MockRepository) BenefactorKindBoxExist(ctx context.Context, benefactorID uint, kindBoxID uint) (bool, error) { + ret := _m.Called(ctx, benefactorID, kindBoxID) + + if len(ret) == 0 { + panic("no return value specified for BenefactorKindBoxExist") + } + + var r0 bool + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, uint, uint) (bool, error)); ok { + return rf(ctx, benefactorID, kindBoxID) + } + if rf, ok := ret.Get(0).(func(context.Context, uint, uint) bool); ok { + r0 = rf(ctx, benefactorID, kindBoxID) + } else { + r0 = ret.Get(0).(bool) + } + + if rf, ok := ret.Get(1).(func(context.Context, uint, uint) error); ok { + r1 = rf(ctx, benefactorID, kindBoxID) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// MockRepository_BenefactorKindBoxExist_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'BenefactorKindBoxExist' +type MockRepository_BenefactorKindBoxExist_Call struct { + *mock.Call +} + +// BenefactorKindBoxExist is a helper method to define mock.On call +// - ctx context.Context +// - benefactorID uint +// - kindBoxID uint +func (_e *MockRepository_Expecter) BenefactorKindBoxExist(ctx interface{}, benefactorID interface{}, kindBoxID interface{}) *MockRepository_BenefactorKindBoxExist_Call { + return &MockRepository_BenefactorKindBoxExist_Call{Call: _e.mock.On("BenefactorKindBoxExist", ctx, benefactorID, kindBoxID)} +} + +func (_c *MockRepository_BenefactorKindBoxExist_Call) Run(run func(ctx context.Context, benefactorID uint, kindBoxID uint)) *MockRepository_BenefactorKindBoxExist_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(uint), args[2].(uint)) + }) + return _c +} + +func (_c *MockRepository_BenefactorKindBoxExist_Call) Return(_a0 bool, _a1 error) *MockRepository_BenefactorKindBoxExist_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *MockRepository_BenefactorKindBoxExist_Call) RunAndReturn(run func(context.Context, uint, uint) (bool, error)) *MockRepository_BenefactorKindBoxExist_Call { + _c.Call.Return(run) + return _c +} + +// GetKindBox provides a mock function with given fields: ctx, kindBoxID +func (_m *MockRepository) GetKindBox(ctx context.Context, kindBoxID uint) (entity.KindBox, error) { + ret := _m.Called(ctx, kindBoxID) + + if len(ret) == 0 { + panic("no return value specified for GetKindBox") + } + + var r0 entity.KindBox + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, uint) (entity.KindBox, error)); ok { + return rf(ctx, kindBoxID) + } + if rf, ok := ret.Get(0).(func(context.Context, uint) entity.KindBox); ok { + r0 = rf(ctx, kindBoxID) + } else { + r0 = ret.Get(0).(entity.KindBox) + } + + if rf, ok := ret.Get(1).(func(context.Context, uint) error); ok { + r1 = rf(ctx, kindBoxID) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// MockRepository_GetKindBox_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'GetKindBox' +type MockRepository_GetKindBox_Call struct { + *mock.Call +} + +// GetKindBox is a helper method to define mock.On call +// - ctx context.Context +// - kindBoxID uint +func (_e *MockRepository_Expecter) GetKindBox(ctx interface{}, kindBoxID interface{}) *MockRepository_GetKindBox_Call { + return &MockRepository_GetKindBox_Call{Call: _e.mock.On("GetKindBox", ctx, kindBoxID)} +} + +func (_c *MockRepository_GetKindBox_Call) Run(run func(ctx context.Context, kindBoxID uint)) *MockRepository_GetKindBox_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(uint)) + }) + return _c +} + +func (_c *MockRepository_GetKindBox_Call) Return(_a0 entity.KindBox, _a1 error) *MockRepository_GetKindBox_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *MockRepository_GetKindBox_Call) RunAndReturn(run func(context.Context, uint) (entity.KindBox, error)) *MockRepository_GetKindBox_Call { + _c.Call.Return(run) + return _c +} + +// NewMockRepository creates a new instance of MockRepository. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +// The first argument is typically a *testing.T value. +func NewMockRepository(t interface { + mock.TestingT + Cleanup(func()) +}) *MockRepository { + mock := &MockRepository{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/validator/benefactor/kind_box/register_emptying_request.go b/validator/benefactor/kind_box/register_emptying_request.go index e15c130..7c7ecbb 100644 --- a/validator/benefactor/kind_box/register_emptying_request.go +++ b/validator/benefactor/kind_box/register_emptying_request.go @@ -48,5 +48,6 @@ func (v Validator) ValidateRegisterEmptyingRequest(ctx context.Context, req para WithErr(err) } - return map[string]string{}, nil + // nolint + return nil, nil } diff --git a/validator/benefactor/kind_box/register_emptying_request_test.go b/validator/benefactor/kind_box/register_emptying_request_test.go new file mode 100644 index 0000000..38defb0 --- /dev/null +++ b/validator/benefactor/kind_box/register_emptying_request_test.go @@ -0,0 +1,471 @@ +package benefactorkindboxvalidator + +import ( + "context" + "errors" + "testing" + "time" + + "git.gocasts.ir/ebhomengo/niki/entity" + addressparam "git.gocasts.ir/ebhomengo/niki/param/benefactor/address" + benefactorparam "git.gocasts.ir/ebhomengo/niki/param/benefactor/benefactor" + param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box" + refertimeparam "git.gocasts.ir/ebhomengo/niki/param/benefactor/refer_time" + errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" + "github.com/stretchr/testify/assert" +) + +func TestValidator_ValidateRegisterEmptyingRequest(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.KindBoxRegisterEmptyingRequest{ + KindBoxID: 1, + BenefactorID: 1, + ReturnAddressID: 1, + ReturnReferTimeID: 1, + ReturnReferDate: time.Now().Add(time.Hour * 24), + } + + 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() + mockRepository.EXPECT().GetKindBox(ctx, uint(1)).Return(entity.KindBox{Status: entity.KindBoxDeliveredStatus}, nil).Once() + mockAddressSvc.EXPECT().AddressExistByID(ctx, addressparam.GetAddressByIDRequest{ID: 1}).Return( + addressparam.GetAddressByIDResponse{Address: entity.Address{BenefactorID: 1}}, + nil, + ).Once() + mockReferTimeSvc.EXPECT().GetReferTimeByID(ctx, refertimeparam.GetReferTimeRequest{ReferTimeID: 1}).Return( + refertimeparam.GetReferTimeResponse{ReferTime: entity.ReferTime{Status: entity.ReferTimeActiveStatus}}, + nil, + ).Once() + + fieldErrors, err := validator.ValidateRegisterEmptyingRequest(ctx, req) + + assert.NoError(t, err) + assert.Nil(t, fieldErrors) + }) + + t.Run("Empty KindBox id", func(t *testing.T) { + req := param.KindBoxRegisterEmptyingRequest{ + KindBoxID: 0, + BenefactorID: 1, + ReturnAddressID: 1, + ReturnReferTimeID: 1, + ReturnReferDate: time.Now().Add(time.Hour * 24), + } + + mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorparam.BenefactorExistByIDRequest{ID: 1}). + Return(benefactorparam.BenefactorExistByIDResponse{Existed: true}, nil).Once() + mockAddressSvc.EXPECT().AddressExistByID(ctx, addressparam.GetAddressByIDRequest{ID: 1}).Return( + addressparam.GetAddressByIDResponse{Address: entity.Address{BenefactorID: 1}}, + nil, + ).Once() + mockReferTimeSvc.EXPECT().GetReferTimeByID(ctx, refertimeparam.GetReferTimeRequest{ReferTimeID: 1}).Return( + refertimeparam.GetReferTimeResponse{ReferTime: entity.ReferTime{Status: entity.ReferTimeActiveStatus}}, + nil, + ).Once() + + fieldErrors, err := validator.ValidateRegisterEmptyingRequest(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.KindBoxRegisterEmptyingRequest{ + KindBoxID: 2, + BenefactorID: 1, + ReturnAddressID: 1, + ReturnReferTimeID: 1, + ReturnReferDate: time.Now().Add(time.Hour * 24), + } + + mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorparam.BenefactorExistByIDRequest{ID: 1}). + Return(benefactorparam.BenefactorExistByIDResponse{Existed: true}, nil).Once() + mockRepository.EXPECT().BenefactorKindBoxExist(ctx, uint(1), uint(2)).Return(false, nil).Once() + mockAddressSvc.EXPECT().AddressExistByID(ctx, addressparam.GetAddressByIDRequest{ID: 1}).Return( + addressparam.GetAddressByIDResponse{Address: entity.Address{BenefactorID: 1}}, + nil, + ).Once() + mockReferTimeSvc.EXPECT().GetReferTimeByID(ctx, refertimeparam.GetReferTimeRequest{ReferTimeID: 1}).Return( + refertimeparam.GetReferTimeResponse{ReferTime: entity.ReferTime{Status: entity.ReferTimeActiveStatus}}, + nil, + ).Once() + + fieldErrors, err := validator.ValidateRegisterEmptyingRequest(ctx, req) + + assert.Error(t, err) + assert.NotNil(t, fieldErrors) + assert.Equal(t, errmsg.ErrorMsgNotFound, fieldErrors["KindBoxID"]) + }) + + t.Run("Invalid KindBox status", func(t *testing.T) { + req := param.KindBoxRegisterEmptyingRequest{ + KindBoxID: 3, + BenefactorID: 1, + ReturnAddressID: 1, + ReturnReferTimeID: 1, + ReturnReferDate: time.Now().Add(time.Hour * 24), + } + + mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorparam.BenefactorExistByIDRequest{ID: 1}). + Return(benefactorparam.BenefactorExistByIDResponse{Existed: true}, nil).Once() + mockRepository.EXPECT().BenefactorKindBoxExist(ctx, uint(1), uint(3)).Return(true, nil).Once() + mockRepository.EXPECT().GetKindBox(ctx, uint(3)).Return(entity.KindBox{Status: entity.KindBoxAssignedReceiverAgentStatus}, nil).Once() + mockAddressSvc.EXPECT().AddressExistByID(ctx, addressparam.GetAddressByIDRequest{ID: 1}).Return( + addressparam.GetAddressByIDResponse{Address: entity.Address{BenefactorID: 1}}, + nil, + ).Once() + mockReferTimeSvc.EXPECT().GetReferTimeByID(ctx, refertimeparam.GetReferTimeRequest{ReferTimeID: 1}).Return( + refertimeparam.GetReferTimeResponse{ReferTime: entity.ReferTime{Status: entity.ReferTimeActiveStatus}}, + nil, + ).Once() + + fieldErrors, err := validator.ValidateRegisterEmptyingRequest(ctx, req) + + assert.Error(t, err) + assert.NotNil(t, fieldErrors) + assert.Contains(t, fieldErrors, "KindBoxID") + }) + + t.Run("KindBox exist error", func(t *testing.T) { + req := param.KindBoxRegisterEmptyingRequest{ + KindBoxID: 1, + BenefactorID: 1, + ReturnAddressID: 1, + ReturnReferTimeID: 1, + ReturnReferDate: time.Now().Add(time.Hour * 24), + } + + 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() + mockAddressSvc.EXPECT().AddressExistByID(ctx, addressparam.GetAddressByIDRequest{ID: 1}).Return( + addressparam.GetAddressByIDResponse{Address: entity.Address{BenefactorID: 1}}, + nil, + ).Once() + mockReferTimeSvc.EXPECT().GetReferTimeByID(ctx, refertimeparam.GetReferTimeRequest{ReferTimeID: 1}).Return( + refertimeparam.GetReferTimeResponse{ReferTime: entity.ReferTime{Status: entity.ReferTimeActiveStatus}}, + nil, + ).Once() + + fieldErrors, err := validator.ValidateRegisterEmptyingRequest(ctx, req) + + assert.Error(t, err) + assert.NotNil(t, fieldErrors) + assert.Equal(t, errmsg.ErrorMsgSomethingWentWrong, fieldErrors["KindBoxID"]) + }) + + t.Run("Empty benefactor id", func(t *testing.T) { + req := param.KindBoxRegisterEmptyingRequest{ + KindBoxID: 1, + BenefactorID: 0, + ReturnAddressID: 1, + ReturnReferTimeID: 1, + ReturnReferDate: time.Now().Add(time.Hour * 24), + } + + mockRepository.EXPECT().BenefactorKindBoxExist(ctx, uint(0), uint(1)).Return(false, nil).Once() + mockAddressSvc.EXPECT().AddressExistByID(ctx, addressparam.GetAddressByIDRequest{ID: 1}).Return( + addressparam.GetAddressByIDResponse{Address: entity.Address{BenefactorID: 1}}, + nil, + ).Once() + mockReferTimeSvc.EXPECT().GetReferTimeByID(ctx, refertimeparam.GetReferTimeRequest{ReferTimeID: 1}).Return( + refertimeparam.GetReferTimeResponse{ReferTime: entity.ReferTime{Status: entity.ReferTimeActiveStatus}}, + nil, + ).Once() + + fieldErrors, err := validator.ValidateRegisterEmptyingRequest(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.KindBoxRegisterEmptyingRequest{ + KindBoxID: 1, + BenefactorID: 2, + ReturnAddressID: 1, + ReturnReferTimeID: 1, + ReturnReferDate: time.Now().Add(time.Hour * 24), + } + + mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorparam.BenefactorExistByIDRequest{ID: 2}). + Return(benefactorparam.BenefactorExistByIDResponse{Existed: false}, nil).Once() + mockRepository.EXPECT().BenefactorKindBoxExist(ctx, uint(2), uint(1)).Return(false, nil).Once() + mockAddressSvc.EXPECT().AddressExistByID(ctx, addressparam.GetAddressByIDRequest{ID: 1}).Return( + addressparam.GetAddressByIDResponse{Address: entity.Address{BenefactorID: 1}}, + nil, + ).Once() + mockReferTimeSvc.EXPECT().GetReferTimeByID(ctx, refertimeparam.GetReferTimeRequest{ReferTimeID: 1}).Return( + refertimeparam.GetReferTimeResponse{ReferTime: entity.ReferTime{Status: entity.ReferTimeActiveStatus}}, + nil, + ).Once() + + fieldErrors, err := validator.ValidateRegisterEmptyingRequest(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.KindBoxRegisterEmptyingRequest{ + KindBoxID: 1, + BenefactorID: 1, + ReturnAddressID: 1, + ReturnReferTimeID: 1, + ReturnReferDate: time.Now().Add(time.Hour * 24), + } + + 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() + mockRepository.EXPECT().GetKindBox(ctx, uint(1)).Return(entity.KindBox{Status: entity.KindBoxDeliveredStatus}, nil).Once() + mockAddressSvc.EXPECT().AddressExistByID(ctx, addressparam.GetAddressByIDRequest{ID: 1}).Return( + addressparam.GetAddressByIDResponse{Address: entity.Address{BenefactorID: 1}}, + nil, + ).Once() + mockReferTimeSvc.EXPECT().GetReferTimeByID(ctx, refertimeparam.GetReferTimeRequest{ReferTimeID: 1}).Return( + refertimeparam.GetReferTimeResponse{ReferTime: entity.ReferTime{Status: entity.ReferTimeActiveStatus}}, + nil, + ).Once() + + fieldErrors, err := validator.ValidateRegisterEmptyingRequest(ctx, req) + + assert.Error(t, err) + assert.NotNil(t, fieldErrors) + assert.Equal(t, errmsg.ErrorMsgSomethingWentWrong, fieldErrors["BenefactorID"]) + }) + + t.Run("Empty ReturnAddress id", func(t *testing.T) { + req := param.KindBoxRegisterEmptyingRequest{ + KindBoxID: 1, + BenefactorID: 1, + ReturnAddressID: 0, + ReturnReferTimeID: 1, + ReturnReferDate: time.Now().Add(time.Hour * 24), + } + + 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() + mockRepository.EXPECT().GetKindBox(ctx, uint(1)).Return(entity.KindBox{Status: entity.KindBoxDeliveredStatus}, nil).Once() + mockReferTimeSvc.EXPECT().GetReferTimeByID(ctx, refertimeparam.GetReferTimeRequest{ReferTimeID: 1}).Return( + refertimeparam.GetReferTimeResponse{ReferTime: entity.ReferTime{Status: entity.ReferTimeActiveStatus}}, + nil, + ).Once() + + fieldErrors, err := validator.ValidateRegisterEmptyingRequest(ctx, req) + + assert.Error(t, err) + assert.NotNil(t, fieldErrors) + assert.Contains(t, fieldErrors, "return_address_id") + }) + + t.Run("ReturnAddress does not exist", func(t *testing.T) { + req := param.KindBoxRegisterEmptyingRequest{ + KindBoxID: 1, + BenefactorID: 1, + ReturnAddressID: 2, + ReturnReferTimeID: 1, + ReturnReferDate: time.Now().Add(time.Hour * 24), + } + + 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() + mockRepository.EXPECT().GetKindBox(ctx, uint(1)).Return(entity.KindBox{Status: entity.KindBoxDeliveredStatus}, nil).Once() + mockAddressSvc.EXPECT().AddressExistByID(ctx, addressparam.GetAddressByIDRequest{ID: 2}).Return( + addressparam.GetAddressByIDResponse{Address: entity.Address{BenefactorID: 3}}, + nil, + ).Once() + mockReferTimeSvc.EXPECT().GetReferTimeByID(ctx, refertimeparam.GetReferTimeRequest{ReferTimeID: 1}).Return( + refertimeparam.GetReferTimeResponse{ReferTime: entity.ReferTime{Status: entity.ReferTimeActiveStatus}}, + nil, + ).Once() + + fieldErrors, err := validator.ValidateRegisterEmptyingRequest(ctx, req) + + assert.Error(t, err) + assert.NotNil(t, fieldErrors) + assert.Equal(t, errmsg.ErrorMsgNotFound, fieldErrors["return_address_id"]) + }) + + t.Run("ReturnAddress exist error", func(t *testing.T) { + req := param.KindBoxRegisterEmptyingRequest{ + KindBoxID: 1, + BenefactorID: 1, + ReturnAddressID: 1, + ReturnReferTimeID: 1, + ReturnReferDate: time.Now().Add(time.Hour * 24), + } + + 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() + mockRepository.EXPECT().GetKindBox(ctx, uint(1)).Return(entity.KindBox{Status: entity.KindBoxDeliveredStatus}, nil).Once() + mockAddressSvc.EXPECT().AddressExistByID(ctx, addressparam.GetAddressByIDRequest{ID: 1}).Return( + addressparam.GetAddressByIDResponse{}, + errors.New("service error"), + ).Once() + mockReferTimeSvc.EXPECT().GetReferTimeByID(ctx, refertimeparam.GetReferTimeRequest{ReferTimeID: 1}).Return( + refertimeparam.GetReferTimeResponse{ReferTime: entity.ReferTime{Status: entity.ReferTimeActiveStatus}}, + nil, + ).Once() + + fieldErrors, err := validator.ValidateRegisterEmptyingRequest(ctx, req) + + assert.Error(t, err) + assert.NotNil(t, fieldErrors) + assert.Equal(t, errmsg.ErrorMsgSomethingWentWrong, fieldErrors["return_address_id"]) + }) + + t.Run("Empty ReturnReferDate", func(t *testing.T) { + req := param.KindBoxRegisterEmptyingRequest{ + KindBoxID: 1, + BenefactorID: 1, + ReturnAddressID: 1, + ReturnReferTimeID: 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() + mockRepository.EXPECT().GetKindBox(ctx, uint(1)).Return(entity.KindBox{Status: entity.KindBoxDeliveredStatus}, nil).Once() + mockAddressSvc.EXPECT().AddressExistByID(ctx, addressparam.GetAddressByIDRequest{ID: 1}).Return( + addressparam.GetAddressByIDResponse{Address: entity.Address{BenefactorID: 1}}, + nil, + ).Once() + mockReferTimeSvc.EXPECT().GetReferTimeByID(ctx, refertimeparam.GetReferTimeRequest{ReferTimeID: 1}).Return( + refertimeparam.GetReferTimeResponse{ReferTime: entity.ReferTime{Status: entity.ReferTimeActiveStatus}}, + nil, + ).Once() + + fieldErrors, err := validator.ValidateRegisterEmptyingRequest(ctx, req) + + assert.Error(t, err) + assert.NotNil(t, fieldErrors) + assert.Contains(t, fieldErrors, "return_refer_date") + }) + + t.Run("Invalid ReturnReferDate", func(t *testing.T) { + req := param.KindBoxRegisterEmptyingRequest{ + KindBoxID: 1, + BenefactorID: 1, + ReturnAddressID: 1, + ReturnReferTimeID: 1, + ReturnReferDate: time.Now().Add(time.Hour * -24), + } + + 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() + mockRepository.EXPECT().GetKindBox(ctx, uint(1)).Return(entity.KindBox{Status: entity.KindBoxDeliveredStatus}, nil).Once() + mockAddressSvc.EXPECT().AddressExistByID(ctx, addressparam.GetAddressByIDRequest{ID: 1}).Return( + addressparam.GetAddressByIDResponse{Address: entity.Address{BenefactorID: 1}}, + nil, + ).Once() + mockReferTimeSvc.EXPECT().GetReferTimeByID(ctx, refertimeparam.GetReferTimeRequest{ReferTimeID: 1}).Return( + refertimeparam.GetReferTimeResponse{ReferTime: entity.ReferTime{Status: entity.ReferTimeActiveStatus}}, + nil, + ).Once() + + fieldErrors, err := validator.ValidateRegisterEmptyingRequest(ctx, req) + + assert.Error(t, err) + assert.NotNil(t, fieldErrors) + assert.Contains(t, fieldErrors, "return_refer_date") + }) + + t.Run("Empty ReturnReferTime id", func(t *testing.T) { + req := param.KindBoxRegisterEmptyingRequest{ + KindBoxID: 1, + BenefactorID: 1, + ReturnAddressID: 1, + ReturnReferTimeID: 0, + ReturnReferDate: time.Now().Add(time.Hour * 24), + } + + 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() + mockRepository.EXPECT().GetKindBox(ctx, uint(1)).Return(entity.KindBox{Status: entity.KindBoxDeliveredStatus}, nil).Once() + mockAddressSvc.EXPECT().AddressExistByID(ctx, addressparam.GetAddressByIDRequest{ID: 1}).Return( + addressparam.GetAddressByIDResponse{Address: entity.Address{BenefactorID: 1}}, + nil, + ).Once() + + fieldErrors, err := validator.ValidateRegisterEmptyingRequest(ctx, req) + + assert.Error(t, err) + assert.NotNil(t, fieldErrors) + assert.Contains(t, fieldErrors, "return_refer_time_id") + }) + + t.Run("Invalid ReturnReferTime status", func(t *testing.T) { + req := param.KindBoxRegisterEmptyingRequest{ + KindBoxID: 1, + BenefactorID: 1, + ReturnAddressID: 1, + ReturnReferTimeID: 2, + ReturnReferDate: time.Now().Add(time.Hour * 24), + } + + 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() + mockRepository.EXPECT().GetKindBox(ctx, uint(1)).Return(entity.KindBox{Status: entity.KindBoxDeliveredStatus}, nil).Once() + mockAddressSvc.EXPECT().AddressExistByID(ctx, addressparam.GetAddressByIDRequest{ID: 1}).Return( + addressparam.GetAddressByIDResponse{Address: entity.Address{BenefactorID: 1}}, + nil, + ).Once() + mockReferTimeSvc.EXPECT().GetReferTimeByID(ctx, refertimeparam.GetReferTimeRequest{ReferTimeID: 2}).Return( + refertimeparam.GetReferTimeResponse{ReferTime: entity.ReferTime{Status: entity.ReferTimeInactiveStatus}}, + nil, + ).Once() + + fieldErrors, err := validator.ValidateRegisterEmptyingRequest(ctx, req) + + assert.Error(t, err) + assert.NotNil(t, fieldErrors) + assert.Equal(t, errmsg.ErrorMsgReferTimeIsNotActive, fieldErrors["return_refer_time_id"]) + }) + + t.Run("ReturnReferTime exist error", func(t *testing.T) { + req := param.KindBoxRegisterEmptyingRequest{ + KindBoxID: 1, + BenefactorID: 1, + ReturnAddressID: 1, + ReturnReferTimeID: 1, + ReturnReferDate: time.Now().Add(time.Hour * 24), + } + + 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() + mockRepository.EXPECT().GetKindBox(ctx, uint(1)).Return(entity.KindBox{Status: entity.KindBoxDeliveredStatus}, nil).Once() + mockAddressSvc.EXPECT().AddressExistByID(ctx, addressparam.GetAddressByIDRequest{ID: 1}).Return( + addressparam.GetAddressByIDResponse{Address: entity.Address{BenefactorID: 1}}, + nil, + ).Once() + mockReferTimeSvc.EXPECT().GetReferTimeByID(ctx, refertimeparam.GetReferTimeRequest{ReferTimeID: 1}).Return( + refertimeparam.GetReferTimeResponse{}, + errors.New("service error"), + ).Once() + + fieldErrors, err := validator.ValidateRegisterEmptyingRequest(ctx, req) + + assert.Error(t, err) + assert.NotNil(t, fieldErrors) + assert.Equal(t, errmsg.ErrorMsgReferTimeNotFound, fieldErrors["return_refer_time_id"]) + }) +} diff --git a/validator/benefactor/kind_box/validator.go b/validator/benefactor/kind_box/validator.go index d8891af..a9b9d0e 100644 --- a/validator/benefactor/kind_box/validator.go +++ b/validator/benefactor/kind_box/validator.go @@ -15,19 +15,23 @@ import ( validation "github.com/go-ozzo/ozzo-validation/v4" ) +//go:generate mockery --name Repository type Repository interface { BenefactorKindBoxExist(ctx context.Context, benefactorID uint, kindBoxID uint) (bool, error) GetKindBox(ctx context.Context, kindBoxID uint) (entity.KindBox, error) } +//go:generate mockery --name BenefactorSvc type BenefactorSvc interface { BenefactorExistByID(ctx context.Context, request param.BenefactorExistByIDRequest) (param.BenefactorExistByIDResponse, error) } +//go:generate mockery --name AddressSvc type AddressSvc interface { AddressExistByID(ctx context.Context, request addressparam.GetAddressByIDRequest) (addressparam.GetAddressByIDResponse, error) } +//go:generate mockery --name ReferTimeSvc type ReferTimeSvc interface { GetReferTimeByID(ctx context.Context, req refertimeparam.GetReferTimeRequest) (refertimeparam.GetReferTimeResponse, error) } @@ -49,9 +53,12 @@ func (v Validator) doesBenefactorExist(ctx context.Context) validation.RuleFunc if !ok { return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) } - _, err := v.benefactorSvc.BenefactorExistByID(ctx, param.BenefactorExistByIDRequest{ID: benefactorID}) + res, err := v.benefactorSvc.BenefactorExistByID(ctx, param.BenefactorExistByIDRequest{ID: benefactorID}) if err != nil { - return fmt.Errorf(errmsg.ErrorMsgNotFound) + return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) + } + if !res.Existed { + return fmt.Errorf(errmsg.ErrorMsgBenefactorNotFound) } return nil diff --git a/validator/benefactor/kind_box_req/add.go b/validator/benefactor/kind_box_req/add.go index 2368db5..9fba4ba 100644 --- a/validator/benefactor/kind_box_req/add.go +++ b/validator/benefactor/kind_box_req/add.go @@ -58,5 +58,6 @@ func (v Validator) ValidateAddRequest(ctx context.Context, req param.KindBoxReqA } - return map[string]string{}, nil + // nolint + return nil, nil } diff --git a/validator/benefactor/kind_box_req/add_test.go b/validator/benefactor/kind_box_req/add_test.go new file mode 100644 index 0000000..7dcef0a --- /dev/null +++ b/validator/benefactor/kind_box_req/add_test.go @@ -0,0 +1,378 @@ +package benefactorkindboxreqvalidator + +import ( + "context" + "errors" + "testing" + "time" + + "git.gocasts.ir/ebhomengo/niki/entity" + addressparam "git.gocasts.ir/ebhomengo/niki/param/benefactor/address" + benefactorparam "git.gocasts.ir/ebhomengo/niki/param/benefactor/benefactor" + kindboxreqparam "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box_req" + refertimeparam "git.gocasts.ir/ebhomengo/niki/param/benefactor/refer_time" + errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" + "github.com/stretchr/testify/assert" +) + +func TestValidator_ValidateAddRequest(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.KindBoxReqAddRequest{ + BenefactorID: 1, + KindBoxType: entity.KindBoxOnTable, + DeliverAddressID: 1, + DeliverReferDate: time.Now().Add(time.Hour * 24), + DeliverReferTimeID: 1, + CountRequested: 1, + } + + mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorparam.BenefactorExistByIDRequest{ID: 1}). + Return(benefactorparam.BenefactorExistByIDResponse{Existed: true}, nil).Once() + mockAddressSvc.EXPECT().AddressExistByID(ctx, addressparam.GetAddressByIDRequest{ID: 1}).Return( + addressparam.GetAddressByIDResponse{Address: entity.Address{BenefactorID: 1}}, + nil, + ).Once() + mockReferTimeSvc.EXPECT().GetReferTimeByID(ctx, refertimeparam.GetReferTimeRequest{ReferTimeID: 1}).Return( + refertimeparam.GetReferTimeResponse{ReferTime: entity.ReferTime{Status: entity.ReferTimeActiveStatus}}, + nil, + ).Once() + + fieldErrors, err := validator.ValidateAddRequest(ctx, req) + + assert.NoError(t, err) + assert.Nil(t, fieldErrors) + }) + + t.Run("Invalid count requested", func(t *testing.T) { + req := kindboxreqparam.KindBoxReqAddRequest{ + BenefactorID: 1, + KindBoxType: entity.KindBoxOnTable, + DeliverAddressID: 1, + DeliverReferDate: time.Now().Add(time.Hour * 24), + DeliverReferTimeID: 1, + CountRequested: 101, + } + + mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorparam.BenefactorExistByIDRequest{ID: 1}). + Return(benefactorparam.BenefactorExistByIDResponse{Existed: true}, nil).Once() + mockAddressSvc.EXPECT().AddressExistByID(ctx, addressparam.GetAddressByIDRequest{ID: 1}).Return( + addressparam.GetAddressByIDResponse{Address: entity.Address{BenefactorID: 1}}, + nil, + ).Once() + mockReferTimeSvc.EXPECT().GetReferTimeByID(ctx, refertimeparam.GetReferTimeRequest{ReferTimeID: 1}).Return( + refertimeparam.GetReferTimeResponse{ReferTime: entity.ReferTime{Status: entity.ReferTimeActiveStatus}}, + nil, + ).Once() + + fieldErrors, err := validator.ValidateAddRequest(ctx, req) + + assert.Error(t, err) + assert.NotNil(t, fieldErrors) + assert.Contains(t, fieldErrors, "count_requested") + }) + + t.Run("Empty benefactor id", func(t *testing.T) { + req := kindboxreqparam.KindBoxReqAddRequest{ + BenefactorID: 0, + KindBoxType: entity.KindBoxOnTable, + DeliverAddressID: 1, + DeliverReferDate: time.Now().Add(time.Hour * 24), + DeliverReferTimeID: 1, + CountRequested: 1, + } + + mockAddressSvc.EXPECT().AddressExistByID(ctx, addressparam.GetAddressByIDRequest{ID: 1}).Return( + addressparam.GetAddressByIDResponse{Address: entity.Address{BenefactorID: 1}}, + nil, + ).Once() + mockReferTimeSvc.EXPECT().GetReferTimeByID(ctx, refertimeparam.GetReferTimeRequest{ReferTimeID: 1}).Return( + refertimeparam.GetReferTimeResponse{ReferTime: entity.ReferTime{Status: entity.ReferTimeActiveStatus}}, + nil, + ).Once() + + fieldErrors, err := validator.ValidateAddRequest(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.KindBoxReqAddRequest{ + BenefactorID: 2, + KindBoxType: entity.KindBoxOnTable, + DeliverAddressID: 1, + DeliverReferDate: time.Now().Add(time.Hour * 24), + DeliverReferTimeID: 1, + CountRequested: 1, + } + + mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorparam.BenefactorExistByIDRequest{ID: 2}). + Return(benefactorparam.BenefactorExistByIDResponse{Existed: false}, nil).Once() + mockAddressSvc.EXPECT().AddressExistByID(ctx, addressparam.GetAddressByIDRequest{ID: 1}).Return( + addressparam.GetAddressByIDResponse{Address: entity.Address{BenefactorID: 1}}, + nil, + ).Once() + mockReferTimeSvc.EXPECT().GetReferTimeByID(ctx, refertimeparam.GetReferTimeRequest{ReferTimeID: 1}).Return( + refertimeparam.GetReferTimeResponse{ReferTime: entity.ReferTime{Status: entity.ReferTimeActiveStatus}}, + nil, + ).Once() + + fieldErrors, err := validator.ValidateAddRequest(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 := kindboxreqparam.KindBoxReqAddRequest{ + BenefactorID: 1, + KindBoxType: entity.KindBoxOnTable, + DeliverAddressID: 1, + DeliverReferDate: time.Now().Add(time.Hour * 24), + DeliverReferTimeID: 1, + CountRequested: 1, + } + + mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorparam.BenefactorExistByIDRequest{ID: 1}). + Return(benefactorparam.BenefactorExistByIDResponse{Existed: false}, errors.New("service error")).Once() + mockAddressSvc.EXPECT().AddressExistByID(ctx, addressparam.GetAddressByIDRequest{ID: 1}).Return( + addressparam.GetAddressByIDResponse{Address: entity.Address{BenefactorID: 1}}, + nil, + ).Once() + mockReferTimeSvc.EXPECT().GetReferTimeByID(ctx, refertimeparam.GetReferTimeRequest{ReferTimeID: 1}).Return( + refertimeparam.GetReferTimeResponse{ReferTime: entity.ReferTime{Status: entity.ReferTimeActiveStatus}}, + nil, + ).Once() + + fieldErrors, err := validator.ValidateAddRequest(ctx, req) + + assert.Error(t, err) + assert.NotNil(t, fieldErrors) + assert.Equal(t, errmsg.ErrorMsgSomethingWentWrong, fieldErrors["BenefactorID"]) + }) + + t.Run("Invalid KindBox type", func(t *testing.T) { + req := kindboxreqparam.KindBoxReqAddRequest{ + BenefactorID: 1, + KindBoxType: "invalid", + DeliverAddressID: 1, + DeliverReferDate: time.Now().Add(time.Hour * 24), + DeliverReferTimeID: 1, + CountRequested: 1, + } + + mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorparam.BenefactorExistByIDRequest{ID: 1}). + Return(benefactorparam.BenefactorExistByIDResponse{Existed: true}, nil).Once() + mockAddressSvc.EXPECT().AddressExistByID(ctx, addressparam.GetAddressByIDRequest{ID: 1}).Return( + addressparam.GetAddressByIDResponse{Address: entity.Address{BenefactorID: 1}}, + nil, + ).Once() + mockReferTimeSvc.EXPECT().GetReferTimeByID(ctx, refertimeparam.GetReferTimeRequest{ReferTimeID: 1}).Return( + refertimeparam.GetReferTimeResponse{ReferTime: entity.ReferTime{Status: entity.ReferTimeActiveStatus}}, + nil, + ).Once() + + fieldErrors, err := validator.ValidateAddRequest(ctx, req) + + assert.Error(t, err) + assert.NotNil(t, fieldErrors) + assert.Contains(t, fieldErrors, "kind_box_type") + }) + + t.Run("Empty deliver address id", func(t *testing.T) { + req := kindboxreqparam.KindBoxReqAddRequest{ + BenefactorID: 1, + KindBoxType: entity.KindBoxOnTable, + DeliverAddressID: 0, + DeliverReferDate: time.Now().Add(time.Hour * 24), + DeliverReferTimeID: 1, + CountRequested: 1, + } + + mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorparam.BenefactorExistByIDRequest{ID: 1}). + Return(benefactorparam.BenefactorExistByIDResponse{Existed: true}, nil).Once() + mockReferTimeSvc.EXPECT().GetReferTimeByID(ctx, refertimeparam.GetReferTimeRequest{ReferTimeID: 1}).Return( + refertimeparam.GetReferTimeResponse{ReferTime: entity.ReferTime{Status: entity.ReferTimeActiveStatus}}, + nil, + ).Once() + + fieldErrors, err := validator.ValidateAddRequest(ctx, req) + + assert.Error(t, err) + assert.NotNil(t, fieldErrors) + assert.Contains(t, fieldErrors, "deliver_address_id") + }) + + t.Run("Deliver address does not exist", func(t *testing.T) { + req := kindboxreqparam.KindBoxReqAddRequest{ + BenefactorID: 2, + KindBoxType: entity.KindBoxOnTable, + DeliverAddressID: 2, + DeliverReferDate: time.Now().Add(time.Hour * 24), + DeliverReferTimeID: 1, + CountRequested: 1, + } + + mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorparam.BenefactorExistByIDRequest{ID: 2}). + Return(benefactorparam.BenefactorExistByIDResponse{Existed: false}, nil).Once() + mockAddressSvc.EXPECT().AddressExistByID(ctx, addressparam.GetAddressByIDRequest{ID: 2}).Return( + addressparam.GetAddressByIDResponse{Address: entity.Address{BenefactorID: 3}}, + nil, + ).Once() + mockReferTimeSvc.EXPECT().GetReferTimeByID(ctx, refertimeparam.GetReferTimeRequest{ReferTimeID: 1}).Return( + refertimeparam.GetReferTimeResponse{ReferTime: entity.ReferTime{Status: entity.ReferTimeActiveStatus}}, + nil, + ).Once() + + fieldErrors, err := validator.ValidateAddRequest(ctx, req) + + assert.Error(t, err) + assert.NotNil(t, fieldErrors) + assert.Equal(t, errmsg.ErrorMsgNotFound, fieldErrors["deliver_address_id"]) + }) + + t.Run("Deliver address exist error", func(t *testing.T) { + req := kindboxreqparam.KindBoxReqAddRequest{ + BenefactorID: 1, + KindBoxType: entity.KindBoxOnTable, + DeliverAddressID: 1, + DeliverReferDate: time.Now().Add(time.Hour * 24), + DeliverReferTimeID: 1, + CountRequested: 1, + } + + mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorparam.BenefactorExistByIDRequest{ID: 1}). + Return(benefactorparam.BenefactorExistByIDResponse{Existed: false}, errors.New("service error")).Once() + mockAddressSvc.EXPECT().AddressExistByID(ctx, addressparam.GetAddressByIDRequest{ID: 1}).Return( + addressparam.GetAddressByIDResponse{Address: entity.Address{BenefactorID: 1}}, + errors.New("service error"), + ).Once() + mockReferTimeSvc.EXPECT().GetReferTimeByID(ctx, refertimeparam.GetReferTimeRequest{ReferTimeID: 1}).Return( + refertimeparam.GetReferTimeResponse{ReferTime: entity.ReferTime{Status: entity.ReferTimeActiveStatus}}, + nil, + ).Once() + + fieldErrors, err := validator.ValidateAddRequest(ctx, req) + + assert.Error(t, err) + assert.NotNil(t, fieldErrors) + assert.Equal(t, errmsg.ErrorMsgSomethingWentWrong, fieldErrors["deliver_address_id"]) + }) + + t.Run("Invalid KindBox type", func(t *testing.T) { + req := kindboxreqparam.KindBoxReqAddRequest{ + BenefactorID: 1, + KindBoxType: entity.KindBoxOnTable, + DeliverAddressID: 1, + DeliverReferDate: time.Now().Add(time.Hour * -24), + DeliverReferTimeID: 1, + CountRequested: 1, + } + + mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorparam.BenefactorExistByIDRequest{ID: 1}). + Return(benefactorparam.BenefactorExistByIDResponse{Existed: true}, nil).Once() + mockAddressSvc.EXPECT().AddressExistByID(ctx, addressparam.GetAddressByIDRequest{ID: 1}).Return( + addressparam.GetAddressByIDResponse{Address: entity.Address{BenefactorID: 1}}, + nil, + ).Once() + mockReferTimeSvc.EXPECT().GetReferTimeByID(ctx, refertimeparam.GetReferTimeRequest{ReferTimeID: 1}).Return( + refertimeparam.GetReferTimeResponse{ReferTime: entity.ReferTime{Status: entity.ReferTimeActiveStatus}}, + nil, + ).Once() + + fieldErrors, err := validator.ValidateAddRequest(ctx, req) + + assert.Error(t, err) + assert.NotNil(t, fieldErrors) + assert.Contains(t, fieldErrors, "deliver_refer_date") + }) + + t.Run("Empty Deliver refer time id", func(t *testing.T) { + req := kindboxreqparam.KindBoxReqAddRequest{ + BenefactorID: 1, + KindBoxType: entity.KindBoxOnTable, + DeliverAddressID: 1, + DeliverReferDate: time.Now().Add(time.Hour * 24), + DeliverReferTimeID: 0, + CountRequested: 1, + } + + mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorparam.BenefactorExistByIDRequest{ID: 1}). + Return(benefactorparam.BenefactorExistByIDResponse{Existed: true}, nil).Once() + mockAddressSvc.EXPECT().AddressExistByID(ctx, addressparam.GetAddressByIDRequest{ID: 1}).Return( + addressparam.GetAddressByIDResponse{Address: entity.Address{BenefactorID: 1}}, + nil, + ).Once() + + fieldErrors, err := validator.ValidateAddRequest(ctx, req) + + assert.Error(t, err) + assert.NotNil(t, fieldErrors) + assert.Contains(t, fieldErrors, "deliver_refer_time_id") + }) + + t.Run("Invalid deliver refer time status", func(t *testing.T) { + req := kindboxreqparam.KindBoxReqAddRequest{ + BenefactorID: 1, + KindBoxType: entity.KindBoxOnTable, + DeliverAddressID: 1, + DeliverReferDate: time.Now().Add(time.Hour * 24), + DeliverReferTimeID: 2, + CountRequested: 1, + } + + mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorparam.BenefactorExistByIDRequest{ID: 1}). + Return(benefactorparam.BenefactorExistByIDResponse{Existed: true}, nil).Once() + mockAddressSvc.EXPECT().AddressExistByID(ctx, addressparam.GetAddressByIDRequest{ID: 1}).Return( + addressparam.GetAddressByIDResponse{Address: entity.Address{BenefactorID: 1}}, + nil, + ).Once() + mockReferTimeSvc.EXPECT().GetReferTimeByID(ctx, refertimeparam.GetReferTimeRequest{ReferTimeID: 2}).Return( + refertimeparam.GetReferTimeResponse{ReferTime: entity.ReferTime{Status: entity.ReferTimeInactiveStatus}}, + nil, + ).Once() + + fieldErrors, err := validator.ValidateAddRequest(ctx, req) + + assert.Error(t, err) + assert.NotNil(t, fieldErrors) + assert.Equal(t, errmsg.ErrorMsgReferTimeIsNotActive, fieldErrors["deliver_refer_time_id"]) + }) + + t.Run("Deliver refer time exist error", func(t *testing.T) { + req := kindboxreqparam.KindBoxReqAddRequest{ + BenefactorID: 1, + KindBoxType: entity.KindBoxOnTable, + DeliverAddressID: 1, + DeliverReferDate: time.Now().Add(time.Hour * 24), + DeliverReferTimeID: 1, + CountRequested: 1, + } + + mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorparam.BenefactorExistByIDRequest{ID: 1}). + Return(benefactorparam.BenefactorExistByIDResponse{Existed: true}, nil).Once() + mockAddressSvc.EXPECT().AddressExistByID(ctx, addressparam.GetAddressByIDRequest{ID: 1}).Return( + addressparam.GetAddressByIDResponse{Address: entity.Address{BenefactorID: 1}}, + nil, + ).Once() + mockReferTimeSvc.EXPECT().GetReferTimeByID(ctx, refertimeparam.GetReferTimeRequest{ReferTimeID: 1}).Return( + refertimeparam.GetReferTimeResponse{ReferTime: entity.ReferTime{Status: entity.ReferTimeActiveStatus}}, + errors.New("service error"), + ).Once() + + fieldErrors, err := validator.ValidateAddRequest(ctx, req) + + assert.Error(t, err) + assert.NotNil(t, fieldErrors) + assert.Equal(t, errmsg.ErrorMsgReferTimeNotFound, fieldErrors["deliver_refer_time_id"]) + }) +} diff --git a/validator/benefactor/kind_box_req/delete.go b/validator/benefactor/kind_box_req/delete.go index 43a32d1..cce90a3 100644 --- a/validator/benefactor/kind_box_req/delete.go +++ b/validator/benefactor/kind_box_req/delete.go @@ -40,5 +40,6 @@ func (v Validator) ValidateDeleteRequest(ctx context.Context, req param.KindBoxR WithErr(err) } - return map[string]string{}, nil + // nolint + return nil, nil } diff --git a/validator/benefactor/kind_box_req/delete_test.go b/validator/benefactor/kind_box_req/delete_test.go new file mode 100644 index 0000000..592aa37 --- /dev/null +++ b/validator/benefactor/kind_box_req/delete_test.go @@ -0,0 +1,175 @@ +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"]) + }) +} diff --git a/validator/benefactor/kind_box_req/get_all.go b/validator/benefactor/kind_box_req/get_all.go index 03e73e6..eee931e 100644 --- a/validator/benefactor/kind_box_req/get_all.go +++ b/validator/benefactor/kind_box_req/get_all.go @@ -40,5 +40,6 @@ func (v Validator) ValidateGetAll(req param.GetAllRequest) (map[string]string, e WithErr(err) } - return map[string]string{}, nil + // nolint + return nil, nil } diff --git a/validator/benefactor/kind_box_req/get_all_test.go b/validator/benefactor/kind_box_req/get_all_test.go new file mode 100644 index 0000000..b16af70 --- /dev/null +++ b/validator/benefactor/kind_box_req/get_all_test.go @@ -0,0 +1,84 @@ +package benefactorkindboxreqvalidator + +import ( + "testing" + + param "git.gocasts.ir/ebhomengo/niki/param" + kindboxreqparam "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box_req" + "github.com/stretchr/testify/assert" +) + +func TestValidator_ValidateGetAll(t *testing.T) { + mockRepository := NewMockRepository(t) + mockBenefactorSvc := NewMockBenefactorSvc(t) + mockAddressSvc := NewMockAddressSvc(t) + mockReferTimeSvc := NewMockReferTimeSvc(t) + validator := New(mockBenefactorSvc, mockAddressSvc, mockReferTimeSvc, mockRepository) + + t.Run("Successful validation", func(t *testing.T) { + req := kindboxreqparam.GetAllRequest{ + Pagination: param.PaginationRequest{}, + Sort: param.SortRequest{ + Field: "id", + Direction: "asc", + }, + Filter: map[string]any{"id": 1}, + } + + fieldErrors, err := validator.ValidateGetAll(req) + + assert.NoError(t, err) + assert.Nil(t, fieldErrors) + }) + + t.Run("Invalid sort field", func(t *testing.T) { + req := kindboxreqparam.GetAllRequest{ + Pagination: param.PaginationRequest{}, + Sort: param.SortRequest{ + Field: "invalid", + Direction: "asc", + }, + Filter: map[string]any{"id": 1}, + } + + fieldErrors, err := validator.ValidateGetAll(req) + + assert.Error(t, err) + assert.NotNil(t, fieldErrors) + assert.Contains(t, fieldErrors, "Sort") + }) + + t.Run("Invalid sort direction", func(t *testing.T) { + req := kindboxreqparam.GetAllRequest{ + Pagination: param.PaginationRequest{}, + Sort: param.SortRequest{ + Field: "id", + Direction: "invalid", + }, + Filter: map[string]any{"id": 1}, + } + + fieldErrors, err := validator.ValidateGetAll(req) + + assert.Error(t, err) + assert.NotNil(t, fieldErrors) + assert.Contains(t, fieldErrors, "Sort") + }) + + t.Run("Invalid filter", func(t *testing.T) { + req := kindboxreqparam.GetAllRequest{ + Pagination: param.PaginationRequest{}, + Sort: param.SortRequest{ + Field: "id", + Direction: "desc", + }, + Filter: map[string]any{"invalid": 1}, + } + + fieldErrors, err := validator.ValidateGetAll(req) + + assert.Error(t, err) + assert.NotNil(t, fieldErrors) + assert.Contains(t, fieldErrors, "Filter") + }) +} diff --git a/validator/benefactor/kind_box_req/get_test.go b/validator/benefactor/kind_box_req/get_test.go new file mode 100644 index 0000000..5c3673d --- /dev/null +++ b/validator/benefactor/kind_box_req/get_test.go @@ -0,0 +1 @@ +package benefactorkindboxreqvalidator diff --git a/validator/benefactor/kind_box_req/mock_AddressSvc_test.go b/validator/benefactor/kind_box_req/mock_AddressSvc_test.go new file mode 100644 index 0000000..824bbbe --- /dev/null +++ b/validator/benefactor/kind_box_req/mock_AddressSvc_test.go @@ -0,0 +1,94 @@ +// Code generated by mockery v2.41.0. DO NOT EDIT. + +package benefactorkindboxreqvalidator + +import ( + context "context" + + addressparam "git.gocasts.ir/ebhomengo/niki/param/benefactor/address" + mock "github.com/stretchr/testify/mock" +) + +// MockAddressSvc is an autogenerated mock type for the AddressSvc type +type MockAddressSvc struct { + mock.Mock +} + +type MockAddressSvc_Expecter struct { + mock *mock.Mock +} + +func (_m *MockAddressSvc) EXPECT() *MockAddressSvc_Expecter { + return &MockAddressSvc_Expecter{mock: &_m.Mock} +} + +// AddressExistByID provides a mock function with given fields: ctx, request +func (_m *MockAddressSvc) AddressExistByID(ctx context.Context, request addressparam.GetAddressByIDRequest) (addressparam.GetAddressByIDResponse, error) { + ret := _m.Called(ctx, request) + + if len(ret) == 0 { + panic("no return value specified for AddressExistByID") + } + + var r0 addressparam.GetAddressByIDResponse + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, addressparam.GetAddressByIDRequest) (addressparam.GetAddressByIDResponse, error)); ok { + return rf(ctx, request) + } + if rf, ok := ret.Get(0).(func(context.Context, addressparam.GetAddressByIDRequest) addressparam.GetAddressByIDResponse); ok { + r0 = rf(ctx, request) + } else { + r0 = ret.Get(0).(addressparam.GetAddressByIDResponse) + } + + if rf, ok := ret.Get(1).(func(context.Context, addressparam.GetAddressByIDRequest) error); ok { + r1 = rf(ctx, request) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// MockAddressSvc_AddressExistByID_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'AddressExistByID' +type MockAddressSvc_AddressExistByID_Call struct { + *mock.Call +} + +// AddressExistByID is a helper method to define mock.On call +// - ctx context.Context +// - request addressparam.GetAddressByIDRequest +func (_e *MockAddressSvc_Expecter) AddressExistByID(ctx interface{}, request interface{}) *MockAddressSvc_AddressExistByID_Call { + return &MockAddressSvc_AddressExistByID_Call{Call: _e.mock.On("AddressExistByID", ctx, request)} +} + +func (_c *MockAddressSvc_AddressExistByID_Call) Run(run func(ctx context.Context, request addressparam.GetAddressByIDRequest)) *MockAddressSvc_AddressExistByID_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(addressparam.GetAddressByIDRequest)) + }) + return _c +} + +func (_c *MockAddressSvc_AddressExistByID_Call) Return(_a0 addressparam.GetAddressByIDResponse, _a1 error) *MockAddressSvc_AddressExistByID_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *MockAddressSvc_AddressExistByID_Call) RunAndReturn(run func(context.Context, addressparam.GetAddressByIDRequest) (addressparam.GetAddressByIDResponse, error)) *MockAddressSvc_AddressExistByID_Call { + _c.Call.Return(run) + return _c +} + +// NewMockAddressSvc creates a new instance of MockAddressSvc. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +// The first argument is typically a *testing.T value. +func NewMockAddressSvc(t interface { + mock.TestingT + Cleanup(func()) +}) *MockAddressSvc { + mock := &MockAddressSvc{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/validator/benefactor/kind_box_req/mock_BenefactorSvc_test.go b/validator/benefactor/kind_box_req/mock_BenefactorSvc_test.go new file mode 100644 index 0000000..a08ca6e --- /dev/null +++ b/validator/benefactor/kind_box_req/mock_BenefactorSvc_test.go @@ -0,0 +1,94 @@ +// Code generated by mockery v2.41.0. DO NOT EDIT. + +package benefactorkindboxreqvalidator + +import ( + context "context" + + benefactoreparam "git.gocasts.ir/ebhomengo/niki/param/benefactor/benefactor" + mock "github.com/stretchr/testify/mock" +) + +// MockBenefactorSvc is an autogenerated mock type for the BenefactorSvc type +type MockBenefactorSvc struct { + mock.Mock +} + +type MockBenefactorSvc_Expecter struct { + mock *mock.Mock +} + +func (_m *MockBenefactorSvc) EXPECT() *MockBenefactorSvc_Expecter { + return &MockBenefactorSvc_Expecter{mock: &_m.Mock} +} + +// BenefactorExistByID provides a mock function with given fields: ctx, request +func (_m *MockBenefactorSvc) BenefactorExistByID(ctx context.Context, request benefactoreparam.BenefactorExistByIDRequest) (benefactoreparam.BenefactorExistByIDResponse, error) { + ret := _m.Called(ctx, request) + + if len(ret) == 0 { + panic("no return value specified for BenefactorExistByID") + } + + var r0 benefactoreparam.BenefactorExistByIDResponse + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, benefactoreparam.BenefactorExistByIDRequest) (benefactoreparam.BenefactorExistByIDResponse, error)); ok { + return rf(ctx, request) + } + if rf, ok := ret.Get(0).(func(context.Context, benefactoreparam.BenefactorExistByIDRequest) benefactoreparam.BenefactorExistByIDResponse); ok { + r0 = rf(ctx, request) + } else { + r0 = ret.Get(0).(benefactoreparam.BenefactorExistByIDResponse) + } + + if rf, ok := ret.Get(1).(func(context.Context, benefactoreparam.BenefactorExistByIDRequest) error); ok { + r1 = rf(ctx, request) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// MockBenefactorSvc_BenefactorExistByID_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'BenefactorExistByID' +type MockBenefactorSvc_BenefactorExistByID_Call struct { + *mock.Call +} + +// BenefactorExistByID is a helper method to define mock.On call +// - ctx context.Context +// - request benefactoreparam.BenefactorExistByIDRequest +func (_e *MockBenefactorSvc_Expecter) BenefactorExistByID(ctx interface{}, request interface{}) *MockBenefactorSvc_BenefactorExistByID_Call { + return &MockBenefactorSvc_BenefactorExistByID_Call{Call: _e.mock.On("BenefactorExistByID", ctx, request)} +} + +func (_c *MockBenefactorSvc_BenefactorExistByID_Call) Run(run func(ctx context.Context, request benefactoreparam.BenefactorExistByIDRequest)) *MockBenefactorSvc_BenefactorExistByID_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(benefactoreparam.BenefactorExistByIDRequest)) + }) + return _c +} + +func (_c *MockBenefactorSvc_BenefactorExistByID_Call) Return(_a0 benefactoreparam.BenefactorExistByIDResponse, _a1 error) *MockBenefactorSvc_BenefactorExistByID_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *MockBenefactorSvc_BenefactorExistByID_Call) RunAndReturn(run func(context.Context, benefactoreparam.BenefactorExistByIDRequest) (benefactoreparam.BenefactorExistByIDResponse, error)) *MockBenefactorSvc_BenefactorExistByID_Call { + _c.Call.Return(run) + return _c +} + +// NewMockBenefactorSvc creates a new instance of MockBenefactorSvc. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +// The first argument is typically a *testing.T value. +func NewMockBenefactorSvc(t interface { + mock.TestingT + Cleanup(func()) +}) *MockBenefactorSvc { + mock := &MockBenefactorSvc{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/validator/benefactor/kind_box_req/mock_ReferTimeSvc_test.go b/validator/benefactor/kind_box_req/mock_ReferTimeSvc_test.go new file mode 100644 index 0000000..1d923e9 --- /dev/null +++ b/validator/benefactor/kind_box_req/mock_ReferTimeSvc_test.go @@ -0,0 +1,94 @@ +// Code generated by mockery v2.41.0. DO NOT EDIT. + +package benefactorkindboxreqvalidator + +import ( + context "context" + + param "git.gocasts.ir/ebhomengo/niki/param/benefactor/refer_time" + mock "github.com/stretchr/testify/mock" +) + +// MockReferTimeSvc is an autogenerated mock type for the ReferTimeSvc type +type MockReferTimeSvc struct { + mock.Mock +} + +type MockReferTimeSvc_Expecter struct { + mock *mock.Mock +} + +func (_m *MockReferTimeSvc) EXPECT() *MockReferTimeSvc_Expecter { + return &MockReferTimeSvc_Expecter{mock: &_m.Mock} +} + +// GetReferTimeByID provides a mock function with given fields: ctx, req +func (_m *MockReferTimeSvc) GetReferTimeByID(ctx context.Context, req param.GetReferTimeRequest) (param.GetReferTimeResponse, error) { + ret := _m.Called(ctx, req) + + if len(ret) == 0 { + panic("no return value specified for GetReferTimeByID") + } + + var r0 param.GetReferTimeResponse + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, param.GetReferTimeRequest) (param.GetReferTimeResponse, error)); ok { + return rf(ctx, req) + } + if rf, ok := ret.Get(0).(func(context.Context, param.GetReferTimeRequest) param.GetReferTimeResponse); ok { + r0 = rf(ctx, req) + } else { + r0 = ret.Get(0).(param.GetReferTimeResponse) + } + + if rf, ok := ret.Get(1).(func(context.Context, param.GetReferTimeRequest) error); ok { + r1 = rf(ctx, req) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// MockReferTimeSvc_GetReferTimeByID_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'GetReferTimeByID' +type MockReferTimeSvc_GetReferTimeByID_Call struct { + *mock.Call +} + +// GetReferTimeByID is a helper method to define mock.On call +// - ctx context.Context +// - req param.GetReferTimeRequest +func (_e *MockReferTimeSvc_Expecter) GetReferTimeByID(ctx interface{}, req interface{}) *MockReferTimeSvc_GetReferTimeByID_Call { + return &MockReferTimeSvc_GetReferTimeByID_Call{Call: _e.mock.On("GetReferTimeByID", ctx, req)} +} + +func (_c *MockReferTimeSvc_GetReferTimeByID_Call) Run(run func(ctx context.Context, req param.GetReferTimeRequest)) *MockReferTimeSvc_GetReferTimeByID_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(param.GetReferTimeRequest)) + }) + return _c +} + +func (_c *MockReferTimeSvc_GetReferTimeByID_Call) Return(_a0 param.GetReferTimeResponse, _a1 error) *MockReferTimeSvc_GetReferTimeByID_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *MockReferTimeSvc_GetReferTimeByID_Call) RunAndReturn(run func(context.Context, param.GetReferTimeRequest) (param.GetReferTimeResponse, error)) *MockReferTimeSvc_GetReferTimeByID_Call { + _c.Call.Return(run) + return _c +} + +// NewMockReferTimeSvc creates a new instance of MockReferTimeSvc. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +// The first argument is typically a *testing.T value. +func NewMockReferTimeSvc(t interface { + mock.TestingT + Cleanup(func()) +}) *MockReferTimeSvc { + mock := &MockReferTimeSvc{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/validator/benefactor/kind_box_req/mock_Repository_test.go b/validator/benefactor/kind_box_req/mock_Repository_test.go new file mode 100644 index 0000000..d5dff1a --- /dev/null +++ b/validator/benefactor/kind_box_req/mock_Repository_test.go @@ -0,0 +1,151 @@ +// Code generated by mockery v2.41.0. DO NOT EDIT. + +package benefactorkindboxreqvalidator + +import ( + context "context" + + entity "git.gocasts.ir/ebhomengo/niki/entity" + mock "github.com/stretchr/testify/mock" +) + +// MockRepository is an autogenerated mock type for the Repository type +type MockRepository struct { + mock.Mock +} + +type MockRepository_Expecter struct { + mock *mock.Mock +} + +func (_m *MockRepository) EXPECT() *MockRepository_Expecter { + return &MockRepository_Expecter{mock: &_m.Mock} +} + +// GetKindBoxReqByID provides a mock function with given fields: ctx, kindBoxReqID +func (_m *MockRepository) GetKindBoxReqByID(ctx context.Context, kindBoxReqID uint) (entity.KindBoxReq, error) { + ret := _m.Called(ctx, kindBoxReqID) + + if len(ret) == 0 { + panic("no return value specified for GetKindBoxReqByID") + } + + var r0 entity.KindBoxReq + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, uint) (entity.KindBoxReq, error)); ok { + return rf(ctx, kindBoxReqID) + } + if rf, ok := ret.Get(0).(func(context.Context, uint) entity.KindBoxReq); ok { + r0 = rf(ctx, kindBoxReqID) + } else { + r0 = ret.Get(0).(entity.KindBoxReq) + } + + if rf, ok := ret.Get(1).(func(context.Context, uint) error); ok { + r1 = rf(ctx, kindBoxReqID) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// MockRepository_GetKindBoxReqByID_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'GetKindBoxReqByID' +type MockRepository_GetKindBoxReqByID_Call struct { + *mock.Call +} + +// GetKindBoxReqByID is a helper method to define mock.On call +// - ctx context.Context +// - kindBoxReqID uint +func (_e *MockRepository_Expecter) GetKindBoxReqByID(ctx interface{}, kindBoxReqID interface{}) *MockRepository_GetKindBoxReqByID_Call { + return &MockRepository_GetKindBoxReqByID_Call{Call: _e.mock.On("GetKindBoxReqByID", ctx, kindBoxReqID)} +} + +func (_c *MockRepository_GetKindBoxReqByID_Call) Run(run func(ctx context.Context, kindBoxReqID uint)) *MockRepository_GetKindBoxReqByID_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(uint)) + }) + return _c +} + +func (_c *MockRepository_GetKindBoxReqByID_Call) Return(_a0 entity.KindBoxReq, _a1 error) *MockRepository_GetKindBoxReqByID_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *MockRepository_GetKindBoxReqByID_Call) RunAndReturn(run func(context.Context, uint) (entity.KindBoxReq, error)) *MockRepository_GetKindBoxReqByID_Call { + _c.Call.Return(run) + return _c +} + +// KindBoxRequestExist provides a mock function with given fields: ctx, id +func (_m *MockRepository) KindBoxRequestExist(ctx context.Context, id uint) (bool, error) { + ret := _m.Called(ctx, id) + + if len(ret) == 0 { + panic("no return value specified for KindBoxRequestExist") + } + + var r0 bool + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, uint) (bool, error)); ok { + return rf(ctx, id) + } + if rf, ok := ret.Get(0).(func(context.Context, uint) bool); ok { + r0 = rf(ctx, id) + } else { + r0 = ret.Get(0).(bool) + } + + if rf, ok := ret.Get(1).(func(context.Context, uint) error); ok { + r1 = rf(ctx, id) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// MockRepository_KindBoxRequestExist_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'KindBoxRequestExist' +type MockRepository_KindBoxRequestExist_Call struct { + *mock.Call +} + +// KindBoxRequestExist is a helper method to define mock.On call +// - ctx context.Context +// - id uint +func (_e *MockRepository_Expecter) KindBoxRequestExist(ctx interface{}, id interface{}) *MockRepository_KindBoxRequestExist_Call { + return &MockRepository_KindBoxRequestExist_Call{Call: _e.mock.On("KindBoxRequestExist", ctx, id)} +} + +func (_c *MockRepository_KindBoxRequestExist_Call) Run(run func(ctx context.Context, id uint)) *MockRepository_KindBoxRequestExist_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(uint)) + }) + return _c +} + +func (_c *MockRepository_KindBoxRequestExist_Call) Return(_a0 bool, _a1 error) *MockRepository_KindBoxRequestExist_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *MockRepository_KindBoxRequestExist_Call) RunAndReturn(run func(context.Context, uint) (bool, error)) *MockRepository_KindBoxRequestExist_Call { + _c.Call.Return(run) + return _c +} + +// NewMockRepository creates a new instance of MockRepository. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +// The first argument is typically a *testing.T value. +func NewMockRepository(t interface { + mock.TestingT + Cleanup(func()) +}) *MockRepository { + mock := &MockRepository{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/validator/benefactor/kind_box_req/update.go b/validator/benefactor/kind_box_req/update.go index 32bd69c..8751525 100644 --- a/validator/benefactor/kind_box_req/update.go +++ b/validator/benefactor/kind_box_req/update.go @@ -31,7 +31,7 @@ func (v Validator) ValidateUpdateRequest(ctx context.Context, req param.KindBoxR ), validation.Field(&req.DeliverAddressID, validation.Required, - validation.By(v.doesBenefactorAddressExist(ctx, req.KindBoxReqID)), + validation.By(v.doesBenefactorAddressExist(ctx, req.BenefactorID)), ), validation.Field(&req.DeliverReferDate, validation.Required, @@ -60,5 +60,6 @@ func (v Validator) ValidateUpdateRequest(ctx context.Context, req param.KindBoxR WithErr(err) } - return map[string]string{}, nil + // nolint + return nil, nil } diff --git a/validator/benefactor/kind_box_req/update_test.go b/validator/benefactor/kind_box_req/update_test.go new file mode 100644 index 0000000..7655658 --- /dev/null +++ b/validator/benefactor/kind_box_req/update_test.go @@ -0,0 +1,54 @@ +package benefactorkindboxreqvalidator + +import ( + "context" + "testing" + "time" + + "git.gocasts.ir/ebhomengo/niki/entity" + addressparam "git.gocasts.ir/ebhomengo/niki/param/benefactor/address" + kindboxreqparam "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box_req" + refertimeparam "git.gocasts.ir/ebhomengo/niki/param/benefactor/refer_time" + "github.com/stretchr/testify/assert" +) + +func TestValidator_ValidateUpdateRequest(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.KindBoxReqUpdateRequest{ + KindBoxReqID: 1, + BenefactorID: 1, + KindBoxType: entity.KindBoxOnTable, + CountRequested: 1, + Description: "test", + DeliverReferTimeID: 1, + DeliverReferDate: time.Now().Add(time.Hour * 24), + DeliverAddressID: 1, + } + + mockRepository.EXPECT().KindBoxRequestExist(ctx, req.KindBoxReqID).Return(true, nil).Once() + mockRepository.EXPECT().GetKindBoxReqByID(ctx, req.KindBoxReqID).Return( + entity.KindBoxReq{BenefactorID: req.BenefactorID, Status: entity.KindBoxReqPendingStatus}, + nil, + ).Twice() + mockAddressSvc.EXPECT().AddressExistByID(ctx, addressparam.GetAddressByIDRequest{ID: req.DeliverAddressID}).Return( + addressparam.GetAddressByIDResponse{Address: entity.Address{BenefactorID: req.BenefactorID}}, + nil, + ).Once() + mockReferTimeSvc.EXPECT().GetReferTimeByID(ctx, refertimeparam.GetReferTimeRequest{ReferTimeID: req.DeliverReferTimeID}).Return( + refertimeparam.GetReferTimeResponse{ReferTime: entity.ReferTime{Status: entity.ReferTimeActiveStatus}}, + nil, + ).Once() + + fieldErrors, err := validator.ValidateUpdateRequest(ctx, req) + + assert.NoError(t, err) + assert.Nil(t, fieldErrors) + }) +} diff --git a/validator/benefactor/kind_box_req/validator.go b/validator/benefactor/kind_box_req/validator.go index f39be22..a2d872f 100644 --- a/validator/benefactor/kind_box_req/validator.go +++ b/validator/benefactor/kind_box_req/validator.go @@ -21,18 +21,22 @@ const ( MaxKindBoxReq uint = 100 ) +//go:generate mockery --name BenefactorSvc type BenefactorSvc interface { BenefactorExistByID(ctx context.Context, request param.BenefactorExistByIDRequest) (param.BenefactorExistByIDResponse, error) } +//go:generate mockery --name AddressSvc type AddressSvc interface { AddressExistByID(ctx context.Context, request addressparam.GetAddressByIDRequest) (addressparam.GetAddressByIDResponse, error) } +//go:generate mockery --name ReferTimeSvc type ReferTimeSvc interface { GetReferTimeByID(ctx context.Context, req refertimeparam.GetReferTimeRequest) (refertimeparam.GetReferTimeResponse, error) } +//go:generate mockery --name Repository type Repository interface { KindBoxRequestExist(ctx context.Context, id uint) (bool, error) GetKindBoxReqByID(ctx context.Context, kindBoxReqID uint) (entity.KindBoxReq, error) @@ -70,9 +74,12 @@ func (v Validator) doesBenefactorExist(ctx context.Context) validation.RuleFunc if !ok { return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) } - _, err := v.benefactorSvc.BenefactorExistByID(ctx, param.BenefactorExistByIDRequest{ID: benefactorID}) + res, err := v.benefactorSvc.BenefactorExistByID(ctx, param.BenefactorExistByIDRequest{ID: benefactorID}) if err != nil { - return fmt.Errorf(errmsg.ErrorMsgNotFound) + return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) + } + if !res.Existed { + return fmt.Errorf(errmsg.ErrorMsgBenefactorNotFound) } return nil @@ -232,23 +239,18 @@ func (v Validator) doesKindBoxRequestExist(ctx context.Context) validation.RuleF } } -func (v Validator) doesBenefactorAddressExist(ctx context.Context, kindBoxReqID uint) validation.RuleFunc { +func (v Validator) doesBenefactorAddressExist(ctx context.Context, benefactorID uint) validation.RuleFunc { return func(value interface{}) error { addressID, ok := value.(uint) if !ok { return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) } - kindBoxReq, err := v.repo.GetKindBoxReqByID(ctx, kindBoxReqID) - if err != nil { - return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) - } - address, aErr := v.addressSvc.AddressExistByID(ctx, addressparam.GetAddressByIDRequest{ID: addressID}) if aErr != nil { return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) } - if address.Address.BenefactorID != kindBoxReq.BenefactorID { + if address.Address.BenefactorID != benefactorID { return fmt.Errorf(errmsg.ErrorMsgNotFound) }