Skip to content

Commit

Permalink
feat: image svc add assignpet
Browse files Browse the repository at this point in the history
  • Loading branch information
bookpanda committed Jan 27, 2024
1 parent 1160b70 commit a56c4aa
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
17 changes: 17 additions & 0 deletions src/app/service/image/image.service.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,20 @@ func (s *Service) FindByPetId(petId string) ([]*proto.Image, error) {
return res.Images, nil

}

func (s *Service) AssignPet(petId string, imageIds []string) error {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

_, err := s.client.AssignPet(ctx, &proto.AssignPetRequest{PetId: petId, Ids: imageIds})
if err != nil {
log.Error().
Err(err).
Str("service", "image").
Str("module", "assign pet").
Msg("Error while connecting to service")
return err
}

return nil
}
32 changes: 29 additions & 3 deletions src/app/service/image/image.service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ import (

type ImageServiceTest struct {
suite.Suite
petId string
images []*proto.Image
petId string
images []*proto.Image
imageIds []string
}

func TestImageService(t *testing.T) {
Expand All @@ -37,6 +38,7 @@ func (t *ImageServiceTest) SetupTest() {
ImageUrl: faker.URL(),
},
}
t.imageIds = []string{t.images[0].Id, t.images[1].Id}
}

func (t *ImageServiceTest) TestFindByPetIdSuccess() {
Expand All @@ -56,7 +58,7 @@ func (t *ImageServiceTest) TestFindByPetIdSuccess() {
func (t *ImageServiceTest) TestFindByPetIdError() {
c := mock.ClientMock{}
c.On("FindByPetId", &proto.FindImageByPetIdRequest{PetId: t.petId}).
Return(&proto.FindImageByPetIdResponse{Images: t.images}, status.Error(codes.Unavailable, "Connection Timeout"))
Return(nil, status.Error(codes.Unavailable, "Connection Timeout"))

srv := NewService(&c)
actual, err := srv.FindByPetId(t.petId)
Expand All @@ -66,3 +68,27 @@ func (t *ImageServiceTest) TestFindByPetIdError() {
assert.Nil(t.T(), actual)
assert.Equal(t.T(), codes.Unavailable, st.Code())
}

func (t *ImageServiceTest) TestAssignPetSuccess() {
c := mock.ClientMock{}
c.On("AssignPet", &proto.AssignPetRequest{PetId: t.petId, Ids: t.imageIds}).
Return(&proto.AssignPetResponse{Success: true}, nil)

srv := NewService(&c)
err := srv.AssignPet(t.petId, t.imageIds)

assert.Nil(t.T(), err)
}

func (t *ImageServiceTest) TestAssignPetError() {
c := mock.ClientMock{}
c.On("AssignPet", &proto.AssignPetRequest{PetId: t.petId, Ids: t.imageIds}).
Return(nil, status.Error(codes.Unavailable, "Connection Timeout"))

srv := NewService(&c)
err := srv.AssignPet(t.petId, t.imageIds)

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

0 comments on commit a56c4aa

Please sign in to comment.