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

403 lines
13 KiB
Go
Raw Permalink Normal View History

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