Skip to content

Commit

Permalink
Merge pull request #1 from isd-sgcu/feature/delete-pet
Browse files Browse the repository at this point in the history
feat: add delete pet
  • Loading branch information
bookpanda authored Dec 22, 2023
2 parents d8457c9 + a373089 commit fb7179c
Show file tree
Hide file tree
Showing 5 changed files with 243 additions and 10 deletions.
26 changes: 17 additions & 9 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,46 +5,54 @@ go 1.21.3
toolchain go1.21.5

require (
github.com/bxcodec/faker/v3 v3.8.1
github.com/google/uuid v1.5.0
github.com/pkg/errors v0.9.1
github.com/rs/zerolog v1.31.0
github.com/spf13/viper v1.18.2
google.golang.org/grpc v1.60.1
gorm.io/driver/postgres v1.5.4
gorm.io/gorm v1.25.5
)

require (
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/stretchr/objx v0.5.0 // indirect
golang.org/x/net v0.19.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231120223509-83a465c0220f // indirect
google.golang.org/protobuf v1.31.0 // indirect
)

require (
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/isd-sgcu/johnjud-go-proto v0.0.8 // indirect
github.com/isd-sgcu/johnjud-go-proto v0.0.8
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgx/v5 v5.4.3 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/rs/zerolog v1.31.0 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/testify v1.8.4
github.com/subosito/gotenv v1.6.0 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/crypto v0.16.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231120223509-83a465c0220f // indirect
google.golang.org/grpc v1.60.1 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
4 changes: 3 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/bxcodec/faker/v3 v3.8.1 h1:qO/Xq19V6uHt2xujwpaetgKhraGCapqY2CRWGD/SqcM=
github.com/bxcodec/faker/v3 v3.8.1/go.mod h1:DdSDccxF5msjFo5aO4vrobRQ8nIApg8kq3QWPEQD6+o=
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down Expand Up @@ -71,6 +73,7 @@ github.com/spf13/viper v1.18.2 h1:LUXCnvUvSM6FXAsj6nnfc8Q2tp1dIgUfY9Kc8GsSOiQ=
github.com/spf13/viper v1.18.2/go.mod h1:EKmWIqdnk5lOcmR72yw6hS+8OPYcwD0jteitLMVB+yk=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
Expand Down Expand Up @@ -98,7 +101,6 @@ golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/genproto v0.0.0-20231106174013-bbf56f31fb17 h1:wpZ8pe2x1Q3f2KyT5f8oP/fa9rHAKgFPr/HZdNuS+PQ=
google.golang.org/genproto/googleapis/rpc v0.0.0-20231120223509-83a465c0220f h1:ultW7fxlIvee4HYrtnaRPon9HpEgFk5zYpmfMgtKB5I=
google.golang.org/genproto/googleapis/rpc v0.0.0-20231120223509-83a465c0220f/go.mod h1:L9KNLi232K1/xB6f7AlSX692koaRnKaWSR0stBki0Yc=
google.golang.org/grpc v1.60.1 h1:26+wFr+cNqSGFcOXcabYC0lUVJVRa2Sb2ortSK7VrEU=
Expand Down
55 changes: 55 additions & 0 deletions src/app/service/pet/pet.service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package pet

import (
"context"
"errors"

"github.com/isd-sgcu/johnjud-backend/src/app/model/pet"
proto "github.com/isd-sgcu/johnjud-go-proto/johnjud/backend/pet/v1"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"gorm.io/gorm"
)

type Service struct {
repository IRepository
}

type IRepository interface {
FindAll(result *[]*pet.Pet) error
FindOne(id string, result *[]*pet.Pet) error
Create(in *pet.Pet) error
Update(id string, result *pet.Pet) error
Delete(id string) error
}

func NewService(repository IRepository) *Service {
return &Service{repository: repository}
}

func (s *Service) Delete(ctx context.Context, req *proto.DeletePetRequest) (*proto.DeletePetResponse, error) {
err := s.repository.Delete(req.Id)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, status.Error(codes.NotFound, "pet not found")
}
return nil, status.Error(codes.Internal, "internal error")
}
return &proto.DeletePetResponse{Success: true}, nil
}

func (*Service) Create(context.Context, *proto.CreatePetRequest) (*proto.CreatePetResponse, error) {
panic("unimplemented")
}

func (*Service) FindAll(context.Context, *proto.FindAllPetRequest) (*proto.FindAllPetResponse, error) {
panic("unimplemented")
}

func (*Service) FindOne(context.Context, *proto.FindOnePetRequest) (*proto.FindOnePetResponse, error) {
panic("unimplemented")
}

func (*Service) Update(context.Context, *proto.UpdatePetRequest) (*proto.UpdatePetResponse, error) {
panic("unimplemented")
}
133 changes: 133 additions & 0 deletions src/app/service/pet/pet.service_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package pet

import (
"context"
"errors"
"math/rand"
"os/user"
"testing"
"time"

"github.com/bxcodec/faker/v3"
"github.com/google/uuid"
mock "github.com/isd-sgcu/johnjud-backend/src/mocks/pet"
"gorm.io/gorm"

"github.com/isd-sgcu/johnjud-backend/src/app/model"
"github.com/isd-sgcu/johnjud-backend/src/app/model/pet"
proto "github.com/isd-sgcu/johnjud-go-proto/johnjud/backend/pet/v1"

petConst "github.com/isd-sgcu/johnjud-backend/src/constant/pet"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

type PetServiceTest struct {
suite.Suite
Pet *pet.Pet
UpdatePet *user.User
PetDto *proto.Pet
CreatePetReqMock *proto.CreatePetRequest
UpdatePetReqMock *proto.UpdatePetRequest
}

func TestUserService(t *testing.T) {
suite.Run(t, new(PetServiceTest))
}

func (t *PetServiceTest) SetupTest() {
t.Pet = &pet.Pet{
Base: model.Base{
ID: uuid.New(),
CreatedAt: time.Time{},
UpdatedAt: time.Time{},
DeletedAt: gorm.DeletedAt{},
},
Type: faker.Word(),
Species: faker.Word(),
Name: faker.Name(),
Birthdate: faker.Word(),
Gender: petConst.Gender(rand.Intn(2) + 1),
Habit: faker.Paragraph(),
Caption: faker.Paragraph(),
Status: petConst.Status(rand.Intn(2) + 1),
IsSterile: true,
IsVaccinated: true,
IsVisible: true,
IsClubPet: true,
Background: faker.Paragraph(),
Address: faker.Paragraph(),
Contact: faker.Paragraph(),
}

t.PetDto = &proto.Pet{
Type: t.Pet.Type,
Species: t.Pet.Species,
Name: t.Pet.Name,
Birthdate: t.Pet.Birthdate,
Gender: proto.Gender(t.Pet.Gender),
Habit: t.Pet.Habit,
Caption: t.Pet.Caption,
Status: proto.PetStatus(t.Pet.Status),
IsSterile: t.Pet.IsSterile,
IsVaccinated: t.Pet.IsVaccinated,
IsVisible: t.Pet.IsVisible,
IsClubPet: t.Pet.IsClubPet,
Background: t.Pet.Background,
Address: t.Pet.Address,
Contact: t.Pet.Contact,
}
}
func (t *PetServiceTest) TestDeleteSuccess() {
want := &proto.DeletePetResponse{Success: true}

repo := new(mock.RepositoryMock)
repo.On("Delete", t.Pet.ID.String()).Return(nil)

srv := NewService(repo)
actual, err := srv.Delete(context.Background(), &proto.DeletePetRequest{Id: t.Pet.ID.String()})

assert.Nil(t.T(), err)
assert.Equal(t.T(), want, actual)
repo.AssertExpectations(t.T())
}

func (t *PetServiceTest) TestDeleteNotFound() {
repo := new(mock.RepositoryMock)
repo.On("Delete", t.Pet.ID.String()).Return(gorm.ErrRecordNotFound)

srv := NewService(repo)
_, err := srv.Delete(context.Background(), &proto.DeletePetRequest{Id: t.Pet.ID.String()})

st, ok := status.FromError(err)
assert.True(t.T(), ok)
assert.Equal(t.T(), codes.NotFound, st.Code())
repo.AssertExpectations(t.T())
}

func (t *PetServiceTest) TestDeleteWithDatabaseError() {
repo := new(mock.RepositoryMock)
repo.On("Delete", t.Pet.ID.String()).Return(errors.New("internal server error"))

srv := NewService(repo)
_, err := srv.Delete(context.Background(), &proto.DeletePetRequest{Id: t.Pet.ID.String()})

st, ok := status.FromError(err)
assert.True(t.T(), ok)
assert.Equal(t.T(), codes.Internal, st.Code())
repo.AssertExpectations(t.T())
}

func (t *PetServiceTest) TestDeleteWithUnexpectedError() {
repo := new(mock.RepositoryMock)
repo.On("Delete", t.Pet.ID.String()).Return(errors.New("unexpected error"))

srv := NewService(repo)
_, err := srv.Delete(context.Background(), &proto.DeletePetRequest{Id: t.Pet.ID.String()})

assert.Error(t.T(), err)
repo.AssertExpectations(t.T())
}
35 changes: 35 additions & 0 deletions src/mocks/pet/pet.mock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package pet

import (
"github.com/isd-sgcu/johnjud-backend/src/app/model/pet"
"github.com/stretchr/testify/mock"
)

type RepositoryMock struct {
mock.Mock
}

func (r *RepositoryMock) FindAll(result *[]*pet.Pet) error {
// unimplemented for now
return nil
}

func (r *RepositoryMock) FindOne(id string, result *[]*pet.Pet) error {
// unimplemented for now
return nil
}

func (r *RepositoryMock) Create(in *pet.Pet) error {
// unimplemented for now
return nil
}

func (r *RepositoryMock) Update(id string, result *pet.Pet) error {
// unimplemented for now
return nil
}

func (r *RepositoryMock) Delete(id string) error {
args := r.Called(id)
return args.Error(0)
}

0 comments on commit fb7179c

Please sign in to comment.