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