forked from ebhomengo/niki
1
0
Fork 0

feat(validator): implement unit tests for benefactor validator (#126)

This commit is contained in:
Ruhollah 2024-08-10 21:17:21 +03:30
parent 3ff9d13da1
commit fe23ea181f
41 changed files with 3580 additions and 40 deletions

View File

@ -7,7 +7,7 @@ import (
) )
type KindBoxReqAddRequest struct { type KindBoxReqAddRequest struct {
BenefactorID uint `json:"benefactor_id" example:"1"` BenefactorID uint `json:"-"`
KindBoxType entity.KindBoxType `json:"kind_box_type" example:"on-table"` KindBoxType entity.KindBoxType `json:"kind_box_type" example:"on-table"`
DeliverAddressID uint `json:"deliver_address_id" example:"1"` DeliverAddressID uint `json:"deliver_address_id" example:"1"`
DeliverReferDate time.Time `json:"deliver_refer_date" example:"2025-01-02T15:04:05Z"` DeliverReferDate time.Time `json:"deliver_refer_date" example:"2025-01-02T15:04:05Z"`

View File

@ -10,7 +10,7 @@ const (
ErrorMsgEmailIsNotUnique = "email is not unique" ErrorMsgEmailIsNotUnique = "email is not unique"
ErrorMsgPhoneNumberIsNotValid = "phone number is not valid" ErrorMsgPhoneNumberIsNotValid = "phone number is not valid"
ErrorMsgUserNotAllowed = "user not allowed" ErrorMsgUserNotAllowed = "user not allowed"
ErrorMsgUserNotFound = "benefactor not found" ErrorMsgBenefactorNotFound = "benefactor not found"
ErrorMsgOtpCodeExist = "please wait a little bit" ErrorMsgOtpCodeExist = "please wait a little bit"
ErrorMsgOtpCodeIsNotValid = "verification code is not valid" ErrorMsgOtpCodeIsNotValid = "verification code is not valid"
ErrorMsgCantScanQueryResult = "can't scan query result" ErrorMsgCantScanQueryResult = "can't scan query result"

View File

@ -3,10 +3,10 @@ package adminvalidator
import ( import (
"context" "context"
"errors" "errors"
adminserviceparam "git.gocasts.ir/ebhomengo/niki/param/admin/admin"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
"testing" "testing"
adminserviceparam "git.gocasts.ir/ebhomengo/niki/param/admin/admin"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -18,7 +18,6 @@ func TestValidateLoginWithPhoneNumberRequest(t *testing.T) {
validPassword := "validpassword" validPassword := "validpassword"
t.Run("Valid request", func(t *testing.T) { t.Run("Valid request", func(t *testing.T) {
req := adminserviceparam.LoginWithPhoneNumberRequest{ req := adminserviceparam.LoginWithPhoneNumberRequest{
PhoneNumber: validPhoneNumber, PhoneNumber: validPhoneNumber,
Password: validPassword, Password: validPassword,
@ -32,7 +31,6 @@ func TestValidateLoginWithPhoneNumberRequest(t *testing.T) {
}) })
t.Run("Empty phone number", func(t *testing.T) { t.Run("Empty phone number", func(t *testing.T) {
req := adminserviceparam.LoginWithPhoneNumberRequest{ req := adminserviceparam.LoginWithPhoneNumberRequest{
PhoneNumber: "", PhoneNumber: "",
Password: validPassword, Password: validPassword,
@ -45,7 +43,6 @@ func TestValidateLoginWithPhoneNumberRequest(t *testing.T) {
}) })
t.Run("Empty password", func(t *testing.T) { t.Run("Empty password", func(t *testing.T) {
req := adminserviceparam.LoginWithPhoneNumberRequest{ req := adminserviceparam.LoginWithPhoneNumberRequest{
PhoneNumber: validPhoneNumber, PhoneNumber: validPhoneNumber,
Password: "", Password: "",

View File

@ -14,11 +14,14 @@ func (v Validator) ValidateAddAddress(ctx context.Context, req param.BenefactorA
const op = "benefactorvalidator.ValidateAddRequest" const op = "benefactorvalidator.ValidateAddRequest"
if err := validation.ValidateStruct(&req, if err := validation.ValidateStruct(&req,
validation.Field(&req.Address, validation.Required),
validation.Field(&req.BenefactorID, validation.Required, validation.Field(&req.BenefactorID, validation.Required,
validation.By(v.doesBenefactorExist(ctx))), 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.Lon, validation.Required),
validation.Field(&req.Lat, 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.Name, validation.Required),
validation.Field(&req.PostalCode, validation.Required), validation.Field(&req.PostalCode, validation.Required),
validation.Field(&req.CityID, validation.Required,
validation.By(v.doesCityExist(ctx))),
); err != nil { ); err != nil {
fieldErrors := make(map[string]string) fieldErrors := make(map[string]string)
@ -49,5 +49,6 @@ func (v Validator) ValidateAddAddress(ctx context.Context, req param.BenefactorA
WithErr(err) WithErr(err)
} }
return map[string]string{}, nil // nolint
return nil, nil
} }

View File

@ -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")
})
}

View File

@ -39,5 +39,6 @@ func (v Validator) ValidateDeleteRequest(ctx context.Context, req param.DeleteAd
WithErr(err) WithErr(err)
} }
return map[string]string{}, nil // nolint
return nil, nil
} }

View File

@ -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"])
})
}

View File

@ -39,5 +39,6 @@ func (v Validator) ValidateGetAddress(ctx context.Context, req param.GetAddressR
WithErr(err) WithErr(err)
} }
return map[string]string{}, nil // nolint
return nil, nil
} }

View File

@ -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")
})
}

View File

@ -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
}

View File

@ -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
}

View File

@ -24,6 +24,10 @@ func (v Validator) ValidateUpdateAddress(ctx context.Context, req param.UpdateAd
validation.Field(&req.Name, validation.Required), 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.PostalCode, validation.Required),
validation.Field(&req.CityID, validation.Required, validation.Field(&req.CityID, validation.Required,
@ -47,5 +51,6 @@ func (v Validator) ValidateUpdateAddress(ctx context.Context, req param.UpdateAd
WithErr(err) WithErr(err)
} }
return map[string]string{}, nil // nolint
return nil, nil
} }

View File

@ -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")
})
}

View File

@ -9,9 +9,12 @@ import (
validation "github.com/go-ozzo/ozzo-validation/v4" validation "github.com/go-ozzo/ozzo-validation/v4"
) )
//go:generate mockery --name BenefactorSvc
type BenefactorSvc interface { type BenefactorSvc interface {
BenefactorExistByID(ctx context.Context, request param.BenefactorExistByIDRequest) (param.BenefactorExistByIDResponse, error) BenefactorExistByID(ctx context.Context, request param.BenefactorExistByIDRequest) (param.BenefactorExistByIDResponse, error)
} }
//go:generate mockery --name Repository
type Repository interface { type Repository interface {
IsExistCityByID(ctx context.Context, id uint) (bool, error) IsExistCityByID(ctx context.Context, id uint) (bool, error)
IsExistAddressByID(ctx context.Context, addressID uint, benefactorID 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 { if !ok {
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) 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 { 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 return nil
} }
} }
@ -48,7 +55,7 @@ func (v Validator) doesCityExist(ctx context.Context) validation.RuleFunc {
} }
isExisted, err := v.repository.IsExistCityByID(ctx, cityID) isExisted, err := v.repository.IsExistCityByID(ctx, cityID)
if err != nil { if err != nil {
return fmt.Errorf(errmsg.ErrorMsgNotFound) return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
} }
if !isExisted { if !isExisted {
return fmt.Errorf(errmsg.ErrorMsgNotFound) 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) isExisted, err := v.repository.IsExistAddressByID(ctx, addressID, benefactorID)
if err != nil { if err != nil {
return fmt.Errorf(errmsg.ErrorMsgNotFound) return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
} }
if !isExisted { if !isExisted {
return fmt.Errorf(errmsg.ErrorMsgNotFound) return fmt.Errorf(errmsg.ErrorMsgNotFound)

View File

@ -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"])
})
}

View File

@ -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"])
})
}

View File

@ -39,5 +39,6 @@ func (v Validator) ValidateGetRequest(ctx context.Context, req param.KindBoxGetR
WithErr(err) WithErr(err)
} }
return map[string]string{}, nil // nolint
return nil, nil
} }

View File

@ -42,5 +42,6 @@ func (v Validator) ValidateGetAll(req param.KindBoxGetAllRequest) (map[string]st
WithErr(err) WithErr(err)
} }
return map[string]string{}, nil // nolint
return nil, nil
} }

View File

@ -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")
})
}

View File

@ -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"])
})
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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
}

View File

@ -48,5 +48,6 @@ func (v Validator) ValidateRegisterEmptyingRequest(ctx context.Context, req para
WithErr(err) WithErr(err)
} }
return map[string]string{}, nil // nolint
return nil, nil
} }

View File

@ -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"])
})
}

View File

@ -15,19 +15,23 @@ import (
validation "github.com/go-ozzo/ozzo-validation/v4" validation "github.com/go-ozzo/ozzo-validation/v4"
) )
//go:generate mockery --name Repository
type Repository interface { type Repository interface {
BenefactorKindBoxExist(ctx context.Context, benefactorID uint, kindBoxID uint) (bool, error) BenefactorKindBoxExist(ctx context.Context, benefactorID uint, kindBoxID uint) (bool, error)
GetKindBox(ctx context.Context, kindBoxID uint) (entity.KindBox, error) GetKindBox(ctx context.Context, kindBoxID uint) (entity.KindBox, error)
} }
//go:generate mockery --name BenefactorSvc
type BenefactorSvc interface { type BenefactorSvc interface {
BenefactorExistByID(ctx context.Context, request param.BenefactorExistByIDRequest) (param.BenefactorExistByIDResponse, error) BenefactorExistByID(ctx context.Context, request param.BenefactorExistByIDRequest) (param.BenefactorExistByIDResponse, error)
} }
//go:generate mockery --name AddressSvc
type AddressSvc interface { type AddressSvc interface {
AddressExistByID(ctx context.Context, request addressparam.GetAddressByIDRequest) (addressparam.GetAddressByIDResponse, error) AddressExistByID(ctx context.Context, request addressparam.GetAddressByIDRequest) (addressparam.GetAddressByIDResponse, error)
} }
//go:generate mockery --name ReferTimeSvc
type ReferTimeSvc interface { type ReferTimeSvc interface {
GetReferTimeByID(ctx context.Context, req refertimeparam.GetReferTimeRequest) (refertimeparam.GetReferTimeResponse, error) 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 { if !ok {
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) 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 { if err != nil {
return fmt.Errorf(errmsg.ErrorMsgNotFound) return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
}
if !res.Existed {
return fmt.Errorf(errmsg.ErrorMsgBenefactorNotFound)
} }
return nil return nil

View File

@ -58,5 +58,6 @@ func (v Validator) ValidateAddRequest(ctx context.Context, req param.KindBoxReqA
} }
return map[string]string{}, nil // nolint
return nil, nil
} }

View File

@ -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"])
})
}

View File

@ -40,5 +40,6 @@ func (v Validator) ValidateDeleteRequest(ctx context.Context, req param.KindBoxR
WithErr(err) WithErr(err)
} }
return map[string]string{}, nil // nolint
return nil, nil
} }

View File

@ -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"])
})
}

View File

@ -40,5 +40,6 @@ func (v Validator) ValidateGetAll(req param.GetAllRequest) (map[string]string, e
WithErr(err) WithErr(err)
} }
return map[string]string{}, nil // nolint
return nil, nil
} }

View File

@ -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")
})
}

View File

@ -0,0 +1 @@
package benefactorkindboxreqvalidator

View File

@ -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
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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
}

View File

@ -31,7 +31,7 @@ func (v Validator) ValidateUpdateRequest(ctx context.Context, req param.KindBoxR
), ),
validation.Field(&req.DeliverAddressID, validation.Field(&req.DeliverAddressID,
validation.Required, validation.Required,
validation.By(v.doesBenefactorAddressExist(ctx, req.KindBoxReqID)), validation.By(v.doesBenefactorAddressExist(ctx, req.BenefactorID)),
), ),
validation.Field(&req.DeliverReferDate, validation.Field(&req.DeliverReferDate,
validation.Required, validation.Required,
@ -60,5 +60,6 @@ func (v Validator) ValidateUpdateRequest(ctx context.Context, req param.KindBoxR
WithErr(err) WithErr(err)
} }
return map[string]string{}, nil // nolint
return nil, nil
} }

View File

@ -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)
})
}

View File

@ -21,18 +21,22 @@ const (
MaxKindBoxReq uint = 100 MaxKindBoxReq uint = 100
) )
//go:generate mockery --name BenefactorSvc
type BenefactorSvc interface { type BenefactorSvc interface {
BenefactorExistByID(ctx context.Context, request param.BenefactorExistByIDRequest) (param.BenefactorExistByIDResponse, error) BenefactorExistByID(ctx context.Context, request param.BenefactorExistByIDRequest) (param.BenefactorExistByIDResponse, error)
} }
//go:generate mockery --name AddressSvc
type AddressSvc interface { type AddressSvc interface {
AddressExistByID(ctx context.Context, request addressparam.GetAddressByIDRequest) (addressparam.GetAddressByIDResponse, error) AddressExistByID(ctx context.Context, request addressparam.GetAddressByIDRequest) (addressparam.GetAddressByIDResponse, error)
} }
//go:generate mockery --name ReferTimeSvc
type ReferTimeSvc interface { type ReferTimeSvc interface {
GetReferTimeByID(ctx context.Context, req refertimeparam.GetReferTimeRequest) (refertimeparam.GetReferTimeResponse, error) GetReferTimeByID(ctx context.Context, req refertimeparam.GetReferTimeRequest) (refertimeparam.GetReferTimeResponse, error)
} }
//go:generate mockery --name Repository
type Repository interface { type Repository interface {
KindBoxRequestExist(ctx context.Context, id uint) (bool, error) KindBoxRequestExist(ctx context.Context, id uint) (bool, error)
GetKindBoxReqByID(ctx context.Context, kindBoxReqID uint) (entity.KindBoxReq, 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 { if !ok {
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) 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 { if err != nil {
return fmt.Errorf(errmsg.ErrorMsgNotFound) return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
}
if !res.Existed {
return fmt.Errorf(errmsg.ErrorMsgBenefactorNotFound)
} }
return nil 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 { return func(value interface{}) error {
addressID, ok := value.(uint) addressID, ok := value.(uint)
if !ok { if !ok {
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) 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}) address, aErr := v.addressSvc.AddressExistByID(ctx, addressparam.GetAddressByIDRequest{ID: addressID})
if aErr != nil { if aErr != nil {
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
} }
if address.Address.BenefactorID != kindBoxReq.BenefactorID { if address.Address.BenefactorID != benefactorID {
return fmt.Errorf(errmsg.ErrorMsgNotFound) return fmt.Errorf(errmsg.ErrorMsgNotFound)
} }