From 48a8e763b19e98aea3c1960ca05f4a68872892d2 Mon Sep 17 00:00:00 2001 From: Gear <84141000+macgeargear@users.noreply.github.com> Date: Sun, 23 Jun 2024 02:03:30 +0700 Subject: [PATCH 01/11] feat: selection dto --- internal/dto/selection.dto.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 internal/dto/selection.dto.go diff --git a/internal/dto/selection.dto.go b/internal/dto/selection.dto.go new file mode 100644 index 0000000..58140c1 --- /dev/null +++ b/internal/dto/selection.dto.go @@ -0,0 +1,32 @@ +package dto + +type Selection struct { + Id string `json:"id"` + UserId string `json:"user_id"` + BaanIds []string `json:"baan_ids"` +} + +type CreateSelectionRequest struct { + UserId string `json:"user_id" validate:"required"` + BaanIds []string `json:"baan_ids" validate:"required"` +} + +type CreateSelectionResponse struct { + Selection Selection `json:"selection"` +} + +type FindByStudentIdSelectionRequest struct { + UserId string `json:"user_id" validate:"required"` +} + +type FindByStudentIdSelectionResponse struct { + Selection Selection `json:"selection"` +} + +type UpdateSelectionRequest struct { + Selection Selection `json:"selection" validate:"required"` +} + +type UpdateSelectionResponse struct { + Success bool `json:"success"` +} From 0dc8dcfc572314a14d4ec2cc53c94088b3ffc4bf Mon Sep 17 00:00:00 2001 From: Gear <84141000+macgeargear@users.noreply.github.com> Date: Sun, 23 Jun 2024 02:21:22 +0700 Subject: [PATCH 02/11] feat: selection service --- internal/selection/selection.service.go | 113 ++++++++++++++++++++++++ internal/selection/selection.utils.go | 22 +++++ 2 files changed, 135 insertions(+) create mode 100644 internal/selection/selection.service.go create mode 100644 internal/selection/selection.utils.go diff --git a/internal/selection/selection.service.go b/internal/selection/selection.service.go new file mode 100644 index 0000000..4540089 --- /dev/null +++ b/internal/selection/selection.service.go @@ -0,0 +1,113 @@ +package selection + +import ( + "context" + "time" + + "github.com/isd-sgcu/rpkm67-gateway/apperrors" + "github.com/isd-sgcu/rpkm67-gateway/internal/dto" + selectionProto "github.com/isd-sgcu/rpkm67-go-proto/rpkm67/backend/selection/v1" + "go.uber.org/zap" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" +) + +type Service interface { + CreateSelection(req *dto.CreateSelectionRequest) (*dto.CreateSelectionResponse, *apperrors.AppError) + FindByGroupIdSelection(req *dto.FindByGroupIdSelectionRequest) (*dto.FindByGroupIdSelectionResponse, *apperrors.AppError) + UpdateSelection(req *dto.UpdateSelectionRequest) (*dto.UpdateSelectionResponse, *apperrors.AppError) +} + +type serviceImpl struct { + client selectionProto.SelectionServiceClient + log *zap.Logger +} + +func NewService(client selectionProto.SelectionServiceClient, log *zap.Logger) Service { + return &serviceImpl{ + client: client, + log: log, + } +} + +func (s *serviceImpl) CreateSelection(req *dto.CreateSelectionRequest) (*dto.CreateSelectionResponse, *apperrors.AppError) { + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + + res, err := s.client.Create(ctx, &selectionProto.CreateSelectionRequest{ + GroupId: req.GroupId, + BaanIds: req.BaanIds, + }) + if err != nil { + st, ok := status.FromError(err) + if !ok { + return nil, apperrors.InternalServer + } + switch st.Code() { + case codes.InvalidArgument: + return nil, apperrors.BadRequestError("Invalid argument") + case codes.Internal: + return nil, apperrors.InternalServerError(err.Error()) + default: + return nil, apperrors.ServiceUnavailable + } + } + + return &dto.CreateSelectionResponse{ + Selection: ProtoToDto(res.Selection), + }, nil +} + +func (s *serviceImpl) FindByGroupIdSelection(req *dto.FindByGroupIdSelectionRequest) (*dto.FindByGroupIdSelectionResponse, *apperrors.AppError) { + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + + res, err := s.client.FindByGroupId(ctx, &selectionProto.FindByGroupIdSelectionRequest{ + UserId: req.UserId, + }) + if err != nil { + st, ok := status.FromError(err) + if !ok { + return nil, apperrors.InternalServer + } + switch st.Code() { + case codes.InvalidArgument: + return nil, apperrors.BadRequestError("Invalid argument") + case codes.Internal: + return nil, apperrors.InternalServerError(err.Error()) + default: + return nil, apperrors.ServiceUnavailable + } + } + + return &dto.FindByGroupIdSelectionResponse{ + Selection: ProtoToDto(res.Selection), + }, nil +} + +func (s *serviceImpl) UpdateSelection(req *dto.UpdateSelectionRequest) (*dto.UpdateSelectionResponse, *apperrors.AppError) { + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + + res, err := s.client.Update(ctx, &selectionProto.UpdateSelectionRequest{ + Selection: DtoToProto(req.Selection), + }) + if err != nil { + st, ok := status.FromError(err) + if !ok { + return nil, apperrors.InternalServer + } + switch st.Code() { + case codes.InvalidArgument: + return nil, apperrors.BadRequestError("Invalid argument") + case codes.Internal: + return nil, apperrors.InternalServerError(err.Error()) + default: + return nil, apperrors.ServiceUnavailable + } + } + + return &dto.UpdateSelectionResponse{ + Success: res.Success, + }, nil +} diff --git a/internal/selection/selection.utils.go b/internal/selection/selection.utils.go new file mode 100644 index 0000000..f3b1179 --- /dev/null +++ b/internal/selection/selection.utils.go @@ -0,0 +1,22 @@ +package selection + +import ( + "github.com/isd-sgcu/rpkm67-gateway/internal/dto" + selectionProto "github.com/isd-sgcu/rpkm67-go-proto/rpkm67/backend/selection/v1" +) + +func ProtoToDto(selection *selectionProto.Selection) *dto.Selection { + return &dto.Selection{ + Id: selection.Id, + GroupId: selection.GroupId, + BaanIds: selection.BaanIds, + } +} + +func DtoToProto(selection *dto.Selection) *selectionProto.Selection { + return &selectionProto.Selection{ + Id: selection.Id, + GroupId: selection.GroupId, + BaanIds: selection.BaanIds, + } +} From f43313fc520fc680719c4f0dc2e33a8d3a6c453b Mon Sep 17 00:00:00 2001 From: Gear <84141000+macgeargear@users.noreply.github.com> Date: Sun, 23 Jun 2024 13:52:54 +0700 Subject: [PATCH 03/11] feat: mock selection --- Makefile | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Makefile b/Makefile index cb178d3..27c45ed 100644 --- a/Makefile +++ b/Makefile @@ -11,6 +11,8 @@ mock-gen: mockgen -source ./internal/auth/auth.service.go -destination ./mocks/auth/auth.service.go mockgen -source ./internal/baan/baan.handler.go -destination ./mocks/baan/baan.handler.go mockgen -source ./internal/baan/baan.service.go -destination ./mocks/baan/baan.service.go + mockgen -source ./internal/selection/selection.handler.go -destination ./mocks/selection/selection.handler.go + mockgen -source ./internal/selection/selection.service.go -destination ./mocks/selection/selection.service.go mockgen -source ./internal/router/context.go -destination ./mocks/router/context.mock.go mockgen -source ./internal/validator/validator.go -destination ./mocks/validator/validator.mock.go From dbbdd64dd0290801f0a656587941bb840e186746 Mon Sep 17 00:00:00 2001 From: Gear <84141000+macgeargear@users.noreply.github.com> Date: Sun, 23 Jun 2024 13:53:19 +0700 Subject: [PATCH 04/11] feat: selection handler & service --- internal/dto/selection.dto.go | 16 ++-- internal/selection/selection.handler.go | 115 ++++++++++++++++++++++++ internal/selection/selection.service.go | 40 ++++----- internal/selection/selection.utils.go | 8 ++ 4 files changed, 151 insertions(+), 28 deletions(-) create mode 100644 internal/selection/selection.handler.go diff --git a/internal/dto/selection.dto.go b/internal/dto/selection.dto.go index 58140c1..705aee0 100644 --- a/internal/dto/selection.dto.go +++ b/internal/dto/selection.dto.go @@ -2,29 +2,29 @@ package dto type Selection struct { Id string `json:"id"` - UserId string `json:"user_id"` + GroupId string `json:"group_id"` BaanIds []string `json:"baan_ids"` } type CreateSelectionRequest struct { - UserId string `json:"user_id" validate:"required"` + GroupId string `json:"group_id" validate:"required"` BaanIds []string `json:"baan_ids" validate:"required"` } type CreateSelectionResponse struct { - Selection Selection `json:"selection"` + Selection *Selection `json:"selection"` } -type FindByStudentIdSelectionRequest struct { - UserId string `json:"user_id" validate:"required"` +type FindByGroupIdSelectionRequest struct { + GroupId string `json:"group_id" validate:"required"` } -type FindByStudentIdSelectionResponse struct { - Selection Selection `json:"selection"` +type FindByGroupIdSelectionResponse struct { + Selection *Selection `json:"selection"` } type UpdateSelectionRequest struct { - Selection Selection `json:"selection" validate:"required"` + Selection *Selection `json:"selection" validate:"required"` } type UpdateSelectionResponse struct { diff --git a/internal/selection/selection.handler.go b/internal/selection/selection.handler.go new file mode 100644 index 0000000..7130b57 --- /dev/null +++ b/internal/selection/selection.handler.go @@ -0,0 +1,115 @@ +package selection + +import ( + "net/http" + "strings" + + "github.com/isd-sgcu/rpkm67-gateway/internal/dto" + "github.com/isd-sgcu/rpkm67-gateway/internal/router" + "github.com/isd-sgcu/rpkm67-gateway/internal/validator" + "go.uber.org/zap" +) + +type Handler interface { + CreateSelection(c router.Context) + FindByGroupIdSelection(c router.Context) + UpdateSelection(c router.Context) +} + +func NewHandler(svc Service, validate validator.DtoValidator, log *zap.Logger) Handler { + return &handlerImpl{ + svc: svc, + validate: validate, + log: log, + } +} + +type handlerImpl struct { + svc Service + validate validator.DtoValidator + log *zap.Logger +} + +func (h *handlerImpl) CreateSelection(c router.Context) { + body := &dto.CreateSelectionRequest{} + if err := c.Bind(body); err != nil { + h.log.Named("selection hdr").Error("failed to bind request body", zap.Error(err)) + c.BadRequestError(err.Error()) + return + } + + if errorList := h.validate.Validate(body); errorList != nil { + h.log.Named("selection hdr").Error("validation error", zap.Strings("errorList", errorList)) + c.BadRequestError(strings.Join(errorList, ", ")) + return + } + + req := &dto.CreateSelectionRequest{ + GroupId: body.GroupId, + BaanIds: body.BaanIds, + } + + res, appErr := h.svc.CreateSelection(req) + if appErr != nil { + c.ResponseError(appErr) + return + } + + c.JSON(http.StatusCreated, &dto.CreateSelectionResponse{Selection: res.Selection}) +} + +func (h *handlerImpl) FindByGroupIdSelection(c router.Context) { + groupdId := c.Param("id") + if groupdId == "" { + h.log.Named("selection hdr").Error("url parameter 'id' not found", zap.String("id", groupdId)) + c.BadRequestError("url parameter 'id' not found") + return + } + + req := &dto.FindByGroupIdSelectionRequest{ + GroupId: groupdId, + } + + if errorList := h.validate.Validate(req); errorList != nil { + h.log.Named("selection hdr").Error("validation error", zap.Strings("errorList", errorList)) + c.BadRequestError(strings.Join(errorList, ", ")) + return + } + + res, appErr := h.svc.FindByGroupIdSelection(req) + if appErr != nil { + c.ResponseError(appErr) + return + } + + c.JSON(http.StatusOK, &dto.FindByGroupIdSelectionResponse{Selection: res.Selection}) +} + +func (h *handlerImpl) UpdateSelection(c router.Context) { + body := &dto.UpdateSelectionRequest{} + if err := c.Bind(body); err != nil { + h.log.Named("selection hdr").Error("failed to bind request body", zap.Error(err)) + c.BadRequestError(err.Error()) + return + } + + if errorList := h.validate.Validate(body); errorList != nil { + h.log.Named("selection hdr").Error("validation error", zap.Strings("errorList", errorList)) + c.BadRequestError(strings.Join(errorList, ", ")) + return + } + + req := &dto.UpdateSelectionRequest{ + Selection: body.Selection, + } + + res, appErr := h.svc.UpdateSelection(req) + if appErr != nil { + c.ResponseError(appErr) + return + } + + c.JSON(http.StatusOK, &dto.UpdateSelectionResponse{ + Success: res.Success, + }) +} diff --git a/internal/selection/selection.service.go b/internal/selection/selection.service.go index 4540089..143e540 100644 --- a/internal/selection/selection.service.go +++ b/internal/selection/selection.service.go @@ -4,7 +4,7 @@ import ( "context" "time" - "github.com/isd-sgcu/rpkm67-gateway/apperrors" + "github.com/isd-sgcu/rpkm67-gateway/apperror" "github.com/isd-sgcu/rpkm67-gateway/internal/dto" selectionProto "github.com/isd-sgcu/rpkm67-go-proto/rpkm67/backend/selection/v1" "go.uber.org/zap" @@ -13,9 +13,9 @@ import ( ) type Service interface { - CreateSelection(req *dto.CreateSelectionRequest) (*dto.CreateSelectionResponse, *apperrors.AppError) - FindByGroupIdSelection(req *dto.FindByGroupIdSelectionRequest) (*dto.FindByGroupIdSelectionResponse, *apperrors.AppError) - UpdateSelection(req *dto.UpdateSelectionRequest) (*dto.UpdateSelectionResponse, *apperrors.AppError) + CreateSelection(req *dto.CreateSelectionRequest) (*dto.CreateSelectionResponse, *apperror.AppError) + FindByGroupIdSelection(req *dto.FindByGroupIdSelectionRequest) (*dto.FindByGroupIdSelectionResponse, *apperror.AppError) + UpdateSelection(req *dto.UpdateSelectionRequest) (*dto.UpdateSelectionResponse, *apperror.AppError) } type serviceImpl struct { @@ -30,7 +30,7 @@ func NewService(client selectionProto.SelectionServiceClient, log *zap.Logger) S } } -func (s *serviceImpl) CreateSelection(req *dto.CreateSelectionRequest) (*dto.CreateSelectionResponse, *apperrors.AppError) { +func (s *serviceImpl) CreateSelection(req *dto.CreateSelectionRequest) (*dto.CreateSelectionResponse, *apperror.AppError) { ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() @@ -41,15 +41,15 @@ func (s *serviceImpl) CreateSelection(req *dto.CreateSelectionRequest) (*dto.Cre if err != nil { st, ok := status.FromError(err) if !ok { - return nil, apperrors.InternalServer + return nil, apperror.InternalServer } switch st.Code() { case codes.InvalidArgument: - return nil, apperrors.BadRequestError("Invalid argument") + return nil, apperror.BadRequestError("Invalid argument") case codes.Internal: - return nil, apperrors.InternalServerError(err.Error()) + return nil, apperror.InternalServerError(err.Error()) default: - return nil, apperrors.ServiceUnavailable + return nil, apperror.ServiceUnavailable } } @@ -58,25 +58,25 @@ func (s *serviceImpl) CreateSelection(req *dto.CreateSelectionRequest) (*dto.Cre }, nil } -func (s *serviceImpl) FindByGroupIdSelection(req *dto.FindByGroupIdSelectionRequest) (*dto.FindByGroupIdSelectionResponse, *apperrors.AppError) { +func (s *serviceImpl) FindByGroupIdSelection(req *dto.FindByGroupIdSelectionRequest) (*dto.FindByGroupIdSelectionResponse, *apperror.AppError) { ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() res, err := s.client.FindByGroupId(ctx, &selectionProto.FindByGroupIdSelectionRequest{ - UserId: req.UserId, + GroupId: req.GroupId, }) if err != nil { st, ok := status.FromError(err) if !ok { - return nil, apperrors.InternalServer + return nil, apperror.InternalServer } switch st.Code() { case codes.InvalidArgument: - return nil, apperrors.BadRequestError("Invalid argument") + return nil, apperror.BadRequestError("Invalid argument") case codes.Internal: - return nil, apperrors.InternalServerError(err.Error()) + return nil, apperror.InternalServerError(err.Error()) default: - return nil, apperrors.ServiceUnavailable + return nil, apperror.ServiceUnavailable } } @@ -85,7 +85,7 @@ func (s *serviceImpl) FindByGroupIdSelection(req *dto.FindByGroupIdSelectionRequ }, nil } -func (s *serviceImpl) UpdateSelection(req *dto.UpdateSelectionRequest) (*dto.UpdateSelectionResponse, *apperrors.AppError) { +func (s *serviceImpl) UpdateSelection(req *dto.UpdateSelectionRequest) (*dto.UpdateSelectionResponse, *apperror.AppError) { ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() @@ -95,15 +95,15 @@ func (s *serviceImpl) UpdateSelection(req *dto.UpdateSelectionRequest) (*dto.Upd if err != nil { st, ok := status.FromError(err) if !ok { - return nil, apperrors.InternalServer + return nil, apperror.InternalServer } switch st.Code() { case codes.InvalidArgument: - return nil, apperrors.BadRequestError("Invalid argument") + return nil, apperror.BadRequestError("Invalid argument") case codes.Internal: - return nil, apperrors.InternalServerError(err.Error()) + return nil, apperror.InternalServerError(err.Error()) default: - return nil, apperrors.ServiceUnavailable + return nil, apperror.ServiceUnavailable } } diff --git a/internal/selection/selection.utils.go b/internal/selection/selection.utils.go index f3b1179..938c76f 100644 --- a/internal/selection/selection.utils.go +++ b/internal/selection/selection.utils.go @@ -20,3 +20,11 @@ func DtoToProto(selection *dto.Selection) *selectionProto.Selection { BaanIds: selection.BaanIds, } } + +func ProtoToDtoList(selections []*selectionProto.Selection) []*dto.Selection { + var out []*dto.Selection + for _, selection := range selections { + out = append(out, ProtoToDto(selection)) + } + return out +} From d0d32180cd7e358301e8d5b55d6189af5febc644 Mon Sep 17 00:00:00 2001 From: Gear <84141000+macgeargear@users.noreply.github.com> Date: Sun, 23 Jun 2024 13:53:34 +0700 Subject: [PATCH 05/11] feat: selection & handler tests --- .../selection/test/selection.handler_test.go | 201 +++++++++++++++ .../selection/test/selection.service_test.go | 231 ++++++++++++++++++ .../test/selection.service_test.utils.go | 19 ++ 3 files changed, 451 insertions(+) create mode 100644 internal/selection/test/selection.handler_test.go create mode 100644 internal/selection/test/selection.service_test.go create mode 100644 internal/selection/test/selection.service_test.utils.go diff --git a/internal/selection/test/selection.handler_test.go b/internal/selection/test/selection.handler_test.go new file mode 100644 index 0000000..ee50f39 --- /dev/null +++ b/internal/selection/test/selection.handler_test.go @@ -0,0 +1,201 @@ +package test + +import ( + "net/http" + "testing" + + "github.com/golang/mock/gomock" + "github.com/isd-sgcu/rpkm67-gateway/apperror" + "github.com/isd-sgcu/rpkm67-gateway/internal/dto" + "github.com/isd-sgcu/rpkm67-gateway/internal/selection" + routerMock "github.com/isd-sgcu/rpkm67-gateway/mocks/router" + selectionMock "github.com/isd-sgcu/rpkm67-gateway/mocks/selection" + validatorMock "github.com/isd-sgcu/rpkm67-gateway/mocks/validator" + "github.com/stretchr/testify/suite" + "go.uber.org/zap" +) + +type SelectionHandlerTest struct { + suite.Suite + controller *gomock.Controller + logger *zap.Logger + Selections []*dto.Selection + Selection *dto.Selection + CreateSelectionReq *dto.CreateSelectionRequest + FindByGroupIdSelectionReq *dto.FindByGroupIdSelectionRequest + UpdateSelectionReq *dto.UpdateSelectionRequest + Err *apperror.AppError + QueriesMock map[string]string +} + +func TestSelectionHandler(t *testing.T) { + suite.Run(t, new(SelectionHandlerTest)) +} + +func (t *SelectionHandlerTest) SetupTest() { + t.controller = gomock.NewController(t.T()) + t.logger = zap.NewNop() + + selectionsProto := MockSelectionsProto() + selectionProto := selectionsProto[0] + + t.Selections = selection.ProtoToDtoList(selectionsProto) + t.Selection = selection.ProtoToDto(selectionProto) + // updatedSelection := selection.ProtoToDto(selectionsProto[2]) + + t.CreateSelectionReq = &dto.CreateSelectionRequest{} + t.FindByGroupIdSelectionReq = &dto.FindByGroupIdSelectionRequest{ + GroupId: t.Selection.GroupId, + } + t.UpdateSelectionReq = &dto.UpdateSelectionRequest{} +} + +func (t *SelectionHandlerTest) TestCreateSelectionSuccess() { + expectedResp := &dto.CreateSelectionResponse{ + Selection: t.Selection, + } + + controller := gomock.NewController(t.T()) + + selectionSvc := selectionMock.NewMockService(controller) + validator := validatorMock.NewMockDtoValidator(controller) + context := routerMock.NewMockContext(controller) + + context.EXPECT().Bind(t.CreateSelectionReq).Return(nil) + validator.EXPECT().Validate(t.CreateSelectionReq).Return(nil) + selectionSvc.EXPECT().CreateSelection(t.CreateSelectionReq).Return(expectedResp, nil) + context.EXPECT().JSON(http.StatusCreated, expectedResp) + + handler := selection.NewHandler(selectionSvc, validator, t.logger) + handler.CreateSelection(context) +} + +func (t *SelectionHandlerTest) TestCreateSelectionInvalidArgument() { + expectedErr := apperror.BadRequestError("invalid argument") + + controller := gomock.NewController(t.T()) + + selectionSvc := selectionMock.NewMockService(controller) + validator := validatorMock.NewMockDtoValidator(controller) + context := routerMock.NewMockContext(controller) + + context.EXPECT().Bind(t.CreateSelectionReq).Return(expectedErr) + context.EXPECT().BadRequestError(expectedErr.Error()) + + handler := selection.NewHandler(selectionSvc, validator, t.logger) + handler.CreateSelection(context) +} + +func (t *SelectionHandlerTest) TestCreateSelectionInternalError() { + expectedErr := apperror.InternalServerError("internal error") + + controller := gomock.NewController(t.T()) + + selectionSvc := selectionMock.NewMockService(controller) + validator := validatorMock.NewMockDtoValidator(controller) + context := routerMock.NewMockContext(controller) + + context.EXPECT().Bind(t.CreateSelectionReq).Return(nil) + validator.EXPECT().Validate(t.CreateSelectionReq).Return(nil) + + selectionSvc.EXPECT().CreateSelection(t.CreateSelectionReq).Return(nil, expectedErr) + context.EXPECT().ResponseError(expectedErr) + + handler := selection.NewHandler(selectionSvc, validator, t.logger) + handler.CreateSelection(context) +} + +func (t *SelectionHandlerTest) TestFindByStudentIdSelectionSuccess() { + expectedResp := &dto.FindByGroupIdSelectionResponse{ + Selection: t.Selection, + } + + controller := gomock.NewController(t.T()) + + selectionSvc := selectionMock.NewMockService(controller) + validator := validatorMock.NewMockDtoValidator(controller) + context := routerMock.NewMockContext(controller) + + context.EXPECT().Param("id").Return(t.Selection.GroupId) + validator.EXPECT().Validate(t.FindByGroupIdSelectionReq).Return(nil) + selectionSvc.EXPECT().FindByGroupIdSelection(t.FindByGroupIdSelectionReq).Return(expectedResp, nil) + context.EXPECT().JSON(http.StatusOK, expectedResp) + + handler := selection.NewHandler(selectionSvc, validator, t.logger) + handler.FindByGroupIdSelection(context) + +} + +func (t *SelectionHandlerTest) TestFindByStudentIdSelectionInvalidArgument() { + expectedErr := apperror.BadRequestError("url parameter 'id' not found") + + controller := gomock.NewController(t.T()) + + selectionSvc := selectionMock.NewMockService(controller) + validator := validatorMock.NewMockDtoValidator(controller) + context := routerMock.NewMockContext(controller) + + context.EXPECT().Param("id").Return("") + context.EXPECT().BadRequestError(expectedErr.Error()) + + handler := selection.NewHandler(selectionSvc, validator, t.logger) + handler.FindByGroupIdSelection(context) +} + +func (t *SelectionHandlerTest) TestFindByStudentIdSelectionInternalError() { + expectedErr := apperror.InternalServerError("internal error") + + controller := gomock.NewController(t.T()) + + selectionSvc := selectionMock.NewMockService(controller) + validator := validatorMock.NewMockDtoValidator(controller) + context := routerMock.NewMockContext(controller) + + context.EXPECT().Param("id").Return(t.Selection.GroupId) + validator.EXPECT().Validate(t.FindByGroupIdSelectionReq).Return(nil) + + selectionSvc.EXPECT().FindByGroupIdSelection(t.FindByGroupIdSelectionReq).Return(nil, expectedErr) + context.EXPECT().ResponseError(expectedErr) + + handler := selection.NewHandler(selectionSvc, validator, t.logger) + handler.FindByGroupIdSelection(context) +} + +func (t *SelectionHandlerTest) TestUpdateSelectionSuccess() { + expectedResp := &dto.UpdateSelectionResponse{ + Success: true, + } + + controller := gomock.NewController(t.T()) + + selectionSvc := selectionMock.NewMockService(controller) + validator := validatorMock.NewMockDtoValidator(controller) + context := routerMock.NewMockContext(controller) + + context.EXPECT().Bind(t.UpdateSelectionReq).Return(nil) + validator.EXPECT().Validate(t.UpdateSelectionReq).Return(nil) + selectionSvc.EXPECT().UpdateSelection(t.UpdateSelectionReq).Return(expectedResp, nil) + context.EXPECT().JSON(http.StatusOK, expectedResp) + + handler := selection.NewHandler(selectionSvc, validator, t.logger) + handler.UpdateSelection(context) +} + +func (t *SelectionHandlerTest) TestUpdateSelectionInvalidArgument() { + expectedErr := apperror.BadRequestError("invalid argument") + + controller := gomock.NewController(t.T()) + + selectionSvc := selectionMock.NewMockService(controller) + validator := validatorMock.NewMockDtoValidator(controller) + context := routerMock.NewMockContext(controller) + + context.EXPECT().Bind(t.UpdateSelectionReq).Return(expectedErr) + context.EXPECT().BadRequestError(expectedErr.Error()) + + handler := selection.NewHandler(selectionSvc, validator, t.logger) + handler.UpdateSelection(context) +} + +func (t *SelectionHandlerTest) TestUpdateSelectionInternalError() { +} diff --git a/internal/selection/test/selection.service_test.go b/internal/selection/test/selection.service_test.go new file mode 100644 index 0000000..11f2b8f --- /dev/null +++ b/internal/selection/test/selection.service_test.go @@ -0,0 +1,231 @@ +package test + +import ( + "testing" + + "github.com/golang/mock/gomock" + "github.com/isd-sgcu/rpkm67-gateway/apperror" + "github.com/isd-sgcu/rpkm67-gateway/internal/dto" + "github.com/isd-sgcu/rpkm67-gateway/internal/selection" + selectionMock "github.com/isd-sgcu/rpkm67-gateway/mocks/client/selection" + selectionProto "github.com/isd-sgcu/rpkm67-go-proto/rpkm67/backend/selection/v1" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/suite" + "go.uber.org/zap" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" +) + +type SelectionServiceTest struct { + suite.Suite + controller *gomock.Controller + logger *zap.Logger + SelectionsProto []*selectionProto.Selection + SelectionsDto []*dto.Selection + SelectionProto *selectionProto.Selection + SelectionDto *dto.Selection + CreateSelectionProtoRequest *selectionProto.CreateSelectionRequest + CreateSelectionDtoRequest *dto.CreateSelectionRequest + FindByGroupIdSelectionProtoRequest *selectionProto.FindByGroupIdSelectionRequest + FindByGroupIdSelectionDtoRequest *dto.FindByGroupIdSelectionRequest + UpdateSelectionProtoRequest *selectionProto.UpdateSelectionRequest + UpdateSelectionDtoRequest *dto.UpdateSelectionRequest + Err apperror.AppError +} + +func TestSelectionService(t *testing.T) { + suite.Run(t, new(SelectionServiceTest)) +} + +func (t *SelectionServiceTest) SetupTest() { + t.controller = gomock.NewController(t.T()) + t.logger = zap.NewNop() + + t.SelectionsProto = MockSelectionsProto() + t.SelectionProto = t.SelectionsProto[0] + t.SelectionsDto = selection.ProtoToDtoList(t.SelectionsProto) + t.SelectionDto = selection.ProtoToDto(t.SelectionProto) + + t.CreateSelectionProtoRequest = &selectionProto.CreateSelectionRequest{ + GroupId: t.SelectionProto.GroupId, + BaanIds: t.SelectionProto.BaanIds, + } + t.CreateSelectionDtoRequest = &dto.CreateSelectionRequest{ + GroupId: t.SelectionDto.GroupId, + BaanIds: t.SelectionDto.BaanIds, + } + t.FindByGroupIdSelectionProtoRequest = &selectionProto.FindByGroupIdSelectionRequest{ + GroupId: t.SelectionProto.GroupId, + } + t.FindByGroupIdSelectionDtoRequest = &dto.FindByGroupIdSelectionRequest{ + GroupId: t.SelectionDto.GroupId, + } + t.UpdateSelectionProtoRequest = &selectionProto.UpdateSelectionRequest{ + Selection: t.SelectionProto, + } + t.UpdateSelectionDtoRequest = &dto.UpdateSelectionRequest{ + Selection: t.SelectionDto, + } +} + +func (t *SelectionServiceTest) TestCreateSelectionSuccess() { + protoResp := &selectionProto.CreateSelectionResponse{ + Selection: t.SelectionProto, + } + + createSelectionDto := selection.ProtoToDto(protoResp.Selection) + + expected := &dto.CreateSelectionResponse{ + Selection: createSelectionDto, + } + + client := selectionMock.SelectionClientMock{} + client.On("Create", t.CreateSelectionProtoRequest).Return(protoResp, nil) + + svc := selection.NewService(&client, t.logger) + actual, err := svc.CreateSelection(t.CreateSelectionDtoRequest) + + assert.Nil(t.T(), err) + assert.Equal(t.T(), expected, actual) +} + +func (t *SelectionServiceTest) TestCreateSelectionInvalidArgument() { + protoReq := t.CreateSelectionProtoRequest + + expected := apperror.BadRequestError("Invalid argument") + + clientErr := status.Error(codes.InvalidArgument, apperror.BadRequest.Error()) + + client := selectionMock.SelectionClientMock{} + client.On("Create", protoReq).Return(nil, clientErr) + + svc := selection.NewService(&client, t.logger) + actual, err := svc.CreateSelection(t.CreateSelectionDtoRequest) + + assert.Nil(t.T(), actual) + assert.Equal(t.T(), expected, err) +} + +func (t *SelectionServiceTest) TestCreateSelectionInternalError() { + protoReq := t.CreateSelectionProtoRequest + + expected := apperror.InternalServerError("rpc error: code = Internal desc = Internal error") + + clientErr := status.Error(codes.Internal, apperror.InternalServer.Error()) + + client := selectionMock.SelectionClientMock{} + client.On("Create", protoReq).Return(nil, clientErr) + + svc := selection.NewService(&client, t.logger) + actual, err := svc.CreateSelection(t.CreateSelectionDtoRequest) + + assert.Nil(t.T(), actual) + assert.Equal(t.T(), expected, err) +} + +func (t *SelectionServiceTest) TestFindByGroupIdSelectionSuccess() { + protoResp := &selectionProto.FindByGroupIdSelectionResponse{ + Selection: t.SelectionProto, + } + + expected := &dto.FindByGroupIdSelectionResponse{ + Selection: t.SelectionDto, + } + + client := selectionMock.SelectionClientMock{} + client.On("FindByGroupId", t.FindByGroupIdSelectionProtoRequest).Return(protoResp, nil) + + svc := selection.NewService(&client, t.logger) + actual, err := svc.FindByGroupIdSelection(t.FindByGroupIdSelectionDtoRequest) + + assert.Nil(t.T(), err) + assert.Equal(t.T(), expected, actual) +} + +func (t *SelectionServiceTest) TestFindByGroupIdSelectionInvalidArgument() { + protoReq := t.FindByGroupIdSelectionProtoRequest + + expected := apperror.BadRequestError("Invalid argument") + + clientErr := status.Error(codes.InvalidArgument, apperror.BadRequest.Error()) + + client := selectionMock.SelectionClientMock{} + client.On("FindByGroupId", protoReq).Return(nil, clientErr) + + svc := selection.NewService(&client, t.logger) + actual, err := svc.FindByGroupIdSelection(t.FindByGroupIdSelectionDtoRequest) + + assert.Nil(t.T(), actual) + assert.Equal(t.T(), expected, err) +} + +func (t *SelectionServiceTest) TestFindByGroupIdSelectionInternalError() { + protoReq := t.FindByGroupIdSelectionProtoRequest + + expected := apperror.InternalServerError("rpc error: code = Internal desc = Internal error") + + clientErr := status.Error(codes.Internal, apperror.InternalServer.Error()) + + client := selectionMock.SelectionClientMock{} + client.On("FindByGroupId", protoReq).Return(nil, clientErr) + + svc := selection.NewService(&client, t.logger) + actual, err := svc.FindByGroupIdSelection(t.FindByGroupIdSelectionDtoRequest) + + assert.Nil(t.T(), actual) + assert.Equal(t.T(), expected, err) + +} + +func (t *SelectionServiceTest) TestUpdateSelectionSuccess() { + protoResp := &selectionProto.UpdateSelectionResponse{ + Success: true, + } + + expected := &dto.UpdateSelectionResponse{ + Success: true, + } + + client := selectionMock.SelectionClientMock{} + client.On("Update", t.UpdateSelectionProtoRequest).Return(protoResp, nil) + + svc := selection.NewService(&client, t.logger) + actual, err := svc.UpdateSelection(t.UpdateSelectionDtoRequest) + + assert.Nil(t.T(), err) + assert.Equal(t.T(), expected, actual) +} + +func (t *SelectionServiceTest) TestUpdateSelectionInvalidArgument() { + protoReq := t.UpdateSelectionProtoRequest + + expected := apperror.BadRequestError("Invalid argument") + + clientErr := status.Error(codes.InvalidArgument, apperror.BadRequest.Error()) + + client := selectionMock.SelectionClientMock{} + client.On("Update", protoReq).Return(nil, clientErr) + + svc := selection.NewService(&client, t.logger) + actual, err := svc.UpdateSelection(t.UpdateSelectionDtoRequest) + + assert.Nil(t.T(), actual) + assert.Equal(t.T(), expected, err) +} + +func (t *SelectionServiceTest) TestUpdateSelectionInternalError() { + protoReq := t.UpdateSelectionProtoRequest + + expected := apperror.InternalServerError("rpc error: code = Internal desc = Internal error") + + clientErr := status.Error(codes.Internal, apperror.InternalServer.Error()) + + client := selectionMock.SelectionClientMock{} + client.On("Update", protoReq).Return(nil, clientErr) + + svc := selection.NewService(&client, t.logger) + actual, err := svc.UpdateSelection(t.UpdateSelectionDtoRequest) + + assert.Nil(t.T(), actual) + assert.Equal(t.T(), expected, err) +} diff --git a/internal/selection/test/selection.service_test.utils.go b/internal/selection/test/selection.service_test.utils.go new file mode 100644 index 0000000..929f5d1 --- /dev/null +++ b/internal/selection/test/selection.service_test.utils.go @@ -0,0 +1,19 @@ +package test + +import ( + "github.com/bxcodec/faker/v4" + selectionProto "github.com/isd-sgcu/rpkm67-go-proto/rpkm67/backend/selection/v1" +) + +func MockSelectionsProto() []*selectionProto.Selection { + var selections []*selectionProto.Selection + for i := 0; i < 10; i++ { + selection := &selectionProto.Selection{ + Id: faker.UUIDDigit(), + GroupId: faker.UUIDDigit(), + BaanIds: []string{faker.UUIDDigit(), faker.UUIDDigit(), faker.UUIDDigit()}, + } + selections = append(selections, selection) + } + return selections +} From c79a5a08fbbddff2d1f88abff0df4d67ba10d553 Mon Sep 17 00:00:00 2001 From: Gear <84141000+macgeargear@users.noreply.github.com> Date: Sun, 23 Jun 2024 13:53:44 +0700 Subject: [PATCH 06/11] feat: handler & service mock --- mocks/client/selection/selection.mock.go | 43 +++++++++++++ mocks/selection/selection.handler.go | 71 +++++++++++++++++++++ mocks/selection/selection.service.go | 81 ++++++++++++++++++++++++ 3 files changed, 195 insertions(+) create mode 100644 mocks/client/selection/selection.mock.go create mode 100644 mocks/selection/selection.handler.go create mode 100644 mocks/selection/selection.service.go diff --git a/mocks/client/selection/selection.mock.go b/mocks/client/selection/selection.mock.go new file mode 100644 index 0000000..26be500 --- /dev/null +++ b/mocks/client/selection/selection.mock.go @@ -0,0 +1,43 @@ +package selection + +import ( + "context" + + selectionProto "github.com/isd-sgcu/rpkm67-go-proto/rpkm67/backend/selection/v1" + "github.com/stretchr/testify/mock" + "google.golang.org/grpc" +) + +type SelectionClientMock struct { + mock.Mock +} + +func (c *SelectionClientMock) Create(_ context.Context, req *selectionProto.CreateSelectionRequest, _ ...grpc.CallOption) (res *selectionProto.CreateSelectionResponse, err error) { + args := c.Called(req) + + if args.Get(0) != nil { + res = args.Get(0).(*selectionProto.CreateSelectionResponse) + } + + return res, args.Error(1) +} + +func (c *SelectionClientMock) FindByGroupId(_ context.Context, req *selectionProto.FindByGroupIdSelectionRequest, _ ...grpc.CallOption) (res *selectionProto.FindByGroupIdSelectionResponse, err error) { + args := c.Called(req) + + if args.Get(0) != nil { + res = args.Get(0).(*selectionProto.FindByGroupIdSelectionResponse) + } + + return res, args.Error(1) +} + +func (c *SelectionClientMock) Update(_ context.Context, req *selectionProto.UpdateSelectionRequest, _ ...grpc.CallOption) (res *selectionProto.UpdateSelectionResponse, err error) { + args := c.Called(req) + + if args.Get(0) != nil { + res = args.Get(0).(*selectionProto.UpdateSelectionResponse) + } + + return res, args.Error(1) +} diff --git a/mocks/selection/selection.handler.go b/mocks/selection/selection.handler.go new file mode 100644 index 0000000..bac652c --- /dev/null +++ b/mocks/selection/selection.handler.go @@ -0,0 +1,71 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: ./internal/selection/selection.handler.go + +// Package mock_selection is a generated GoMock package. +package mock_selection + +import ( + reflect "reflect" + + gomock "github.com/golang/mock/gomock" + router "github.com/isd-sgcu/rpkm67-gateway/internal/router" +) + +// MockHandler is a mock of Handler interface. +type MockHandler struct { + ctrl *gomock.Controller + recorder *MockHandlerMockRecorder +} + +// MockHandlerMockRecorder is the mock recorder for MockHandler. +type MockHandlerMockRecorder struct { + mock *MockHandler +} + +// NewMockHandler creates a new mock instance. +func NewMockHandler(ctrl *gomock.Controller) *MockHandler { + mock := &MockHandler{ctrl: ctrl} + mock.recorder = &MockHandlerMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockHandler) EXPECT() *MockHandlerMockRecorder { + return m.recorder +} + +// CreateSelection mocks base method. +func (m *MockHandler) CreateSelection(c router.Context) { + m.ctrl.T.Helper() + m.ctrl.Call(m, "CreateSelection", c) +} + +// CreateSelection indicates an expected call of CreateSelection. +func (mr *MockHandlerMockRecorder) CreateSelection(c interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CreateSelection", reflect.TypeOf((*MockHandler)(nil).CreateSelection), c) +} + +// FindByStudentIdSelection mocks base method. +func (m *MockHandler) FindByStudentIdSelection(c router.Context) { + m.ctrl.T.Helper() + m.ctrl.Call(m, "FindByStudentIdSelection", c) +} + +// FindByStudentIdSelection indicates an expected call of FindByStudentIdSelection. +func (mr *MockHandlerMockRecorder) FindByStudentIdSelection(c interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "FindByStudentIdSelection", reflect.TypeOf((*MockHandler)(nil).FindByStudentIdSelection), c) +} + +// UpdateSelection mocks base method. +func (m *MockHandler) UpdateSelection(c router.Context) { + m.ctrl.T.Helper() + m.ctrl.Call(m, "UpdateSelection", c) +} + +// UpdateSelection indicates an expected call of UpdateSelection. +func (mr *MockHandlerMockRecorder) UpdateSelection(c interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UpdateSelection", reflect.TypeOf((*MockHandler)(nil).UpdateSelection), c) +} diff --git a/mocks/selection/selection.service.go b/mocks/selection/selection.service.go new file mode 100644 index 0000000..85817c2 --- /dev/null +++ b/mocks/selection/selection.service.go @@ -0,0 +1,81 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: ./internal/selection/selection.service.go + +// Package mock_selection is a generated GoMock package. +package mock_selection + +import ( + reflect "reflect" + + gomock "github.com/golang/mock/gomock" + apperror "github.com/isd-sgcu/rpkm67-gateway/apperror" + dto "github.com/isd-sgcu/rpkm67-gateway/internal/dto" +) + +// MockService is a mock of Service interface. +type MockService struct { + ctrl *gomock.Controller + recorder *MockServiceMockRecorder +} + +// MockServiceMockRecorder is the mock recorder for MockService. +type MockServiceMockRecorder struct { + mock *MockService +} + +// NewMockService creates a new mock instance. +func NewMockService(ctrl *gomock.Controller) *MockService { + mock := &MockService{ctrl: ctrl} + mock.recorder = &MockServiceMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockService) EXPECT() *MockServiceMockRecorder { + return m.recorder +} + +// CreateSelection mocks base method. +func (m *MockService) CreateSelection(req *dto.CreateSelectionRequest) (*dto.CreateSelectionResponse, *apperror.AppError) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "CreateSelection", req) + ret0, _ := ret[0].(*dto.CreateSelectionResponse) + ret1, _ := ret[1].(*apperror.AppError) + return ret0, ret1 +} + +// CreateSelection indicates an expected call of CreateSelection. +func (mr *MockServiceMockRecorder) CreateSelection(req interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CreateSelection", reflect.TypeOf((*MockService)(nil).CreateSelection), req) +} + +// FindByGroupIdSelection mocks base method. +func (m *MockService) FindByGroupIdSelection(req *dto.FindByGroupIdSelectionRequest) (*dto.FindByGroupIdSelectionResponse, *apperror.AppError) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "FindByGroupIdSelection", req) + ret0, _ := ret[0].(*dto.FindByGroupIdSelectionResponse) + ret1, _ := ret[1].(*apperror.AppError) + return ret0, ret1 +} + +// FindByGroupIdSelection indicates an expected call of FindByGroupIdSelection. +func (mr *MockServiceMockRecorder) FindByGroupIdSelection(req interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "FindByGroupIdSelection", reflect.TypeOf((*MockService)(nil).FindByGroupIdSelection), req) +} + +// UpdateSelection mocks base method. +func (m *MockService) UpdateSelection(req *dto.UpdateSelectionRequest) (*dto.UpdateSelectionResponse, *apperror.AppError) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "UpdateSelection", req) + ret0, _ := ret[0].(*dto.UpdateSelectionResponse) + ret1, _ := ret[1].(*apperror.AppError) + return ret0, ret1 +} + +// UpdateSelection indicates an expected call of UpdateSelection. +func (mr *MockServiceMockRecorder) UpdateSelection(req interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UpdateSelection", reflect.TypeOf((*MockService)(nil).UpdateSelection), req) +} From 2dd0c92646f0fadf9c4e589ec20bcb126635fe77 Mon Sep 17 00:00:00 2001 From: Gear <84141000+macgeargear@users.noreply.github.com> Date: Sun, 23 Jun 2024 14:13:17 +0700 Subject: [PATCH 07/11] fix(auth): signup return type service --- go.mod | 2 +- go.sum | 6 ++++++ internal/auth/auth.service.go | 14 +++++++------- internal/selection/test/selection.handler_test.go | 1 - 4 files changed, 14 insertions(+), 9 deletions(-) diff --git a/go.mod b/go.mod index 95730d2..304e79f 100644 --- a/go.mod +++ b/go.mod @@ -12,7 +12,7 @@ require ( github.com/go-playground/validator/v10 v10.20.0 github.com/golang/mock v1.6.0 github.com/google/uuid v1.6.0 - github.com/isd-sgcu/rpkm67-go-proto v0.0.4 + github.com/isd-sgcu/rpkm67-go-proto v0.0.7 github.com/joho/godotenv v1.5.1 github.com/stretchr/testify v1.9.0 github.com/swaggo/files v1.0.1 diff --git a/go.sum b/go.sum index e62b602..d3c9f1b 100644 --- a/go.sum +++ b/go.sum @@ -57,6 +57,12 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/isd-sgcu/rpkm67-go-proto v0.0.4 h1:ZO1m0EfTtjQ2IcRy6mUHj+mAb4loM3ckeisKefKqKmY= github.com/isd-sgcu/rpkm67-go-proto v0.0.4/go.mod h1:Z5SYz5kEe4W+MdqPouF0zEOiaqvg+s9I1S5d0q6e+Jw= +github.com/isd-sgcu/rpkm67-go-proto v0.0.5 h1:8zTvNQ0NPokoVRK/NQJYxWjcBFMZNK0avotPs9RSPh0= +github.com/isd-sgcu/rpkm67-go-proto v0.0.5/go.mod h1:Z5SYz5kEe4W+MdqPouF0zEOiaqvg+s9I1S5d0q6e+Jw= +github.com/isd-sgcu/rpkm67-go-proto v0.0.6 h1:alECc0pLyJmbJ2cLBukNDplka+ucWutR6yLXqAn0lJM= +github.com/isd-sgcu/rpkm67-go-proto v0.0.6/go.mod h1:Z5SYz5kEe4W+MdqPouF0zEOiaqvg+s9I1S5d0q6e+Jw= +github.com/isd-sgcu/rpkm67-go-proto v0.0.7 h1:BrHjp7hDFmXTMuMKuA4mK3bjtHTvzozO0vYC1hV1a+w= +github.com/isd-sgcu/rpkm67-go-proto v0.0.7/go.mod h1:Z5SYz5kEe4W+MdqPouF0zEOiaqvg+s9I1S5d0q6e+Jw= github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY= diff --git a/internal/auth/auth.service.go b/internal/auth/auth.service.go index 6141ad9..1d61b38 100644 --- a/internal/auth/auth.service.go +++ b/internal/auth/auth.service.go @@ -15,7 +15,7 @@ import ( type Service interface { Validate() RefreshToken() - SignUp(req *dto.SignUpRequest) (*dto.Credential, *apperror.AppError) + SignUp(req *dto.SignUpRequest) (*dto.SignupResponse, *apperror.AppError) SignIn(req *dto.SignInRequest) (*dto.Credential, *apperror.AppError) SignOut(req *dto.TokenPayloadAuth) (*dto.SignOutResponse, *apperror.AppError) ForgotPassword(req *dto.ForgotPasswordRequest) (*dto.ForgotPasswordResponse, *apperror.AppError) @@ -39,7 +39,7 @@ func (s *serviceImpl) Validate() { func (s *serviceImpl) RefreshToken() { } -func (s *serviceImpl) SignUp(req *dto.SignUpRequest) (*dto.Credential, *apperror.AppError) { +func (s *serviceImpl) SignUp(req *dto.SignUpRequest) (*dto.SignupResponse, *apperror.AppError) { ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() @@ -48,7 +48,6 @@ func (s *serviceImpl) SignUp(req *dto.SignUpRequest) (*dto.Credential, *apperror Password: req.Password, Firstname: req.Firstname, Lastname: req.Lastname, - Role: "student", }) if err != nil { st, ok := status.FromError(err) @@ -65,10 +64,11 @@ func (s *serviceImpl) SignUp(req *dto.SignUpRequest) (*dto.Credential, *apperror } } - return &dto.Credential{ - AccessToken: res.Credential.AccessToken, - RefreshToken: res.Credential.RefreshToken, - ExpiresIn: int(res.Credential.ExpiresIn), + return &dto.SignupResponse{ + Id: res.Id, + Email: res.Email, + Firstname: res.Firstname, + Lastname: res.Lastname, }, nil } diff --git a/internal/selection/test/selection.handler_test.go b/internal/selection/test/selection.handler_test.go index ee50f39..1a57a64 100644 --- a/internal/selection/test/selection.handler_test.go +++ b/internal/selection/test/selection.handler_test.go @@ -41,7 +41,6 @@ func (t *SelectionHandlerTest) SetupTest() { t.Selections = selection.ProtoToDtoList(selectionsProto) t.Selection = selection.ProtoToDto(selectionProto) - // updatedSelection := selection.ProtoToDto(selectionsProto[2]) t.CreateSelectionReq = &dto.CreateSelectionRequest{} t.FindByGroupIdSelectionReq = &dto.FindByGroupIdSelectionRequest{ From b4076462728a05010658284c30a60b328494f005 Mon Sep 17 00:00:00 2001 From: Gear <84141000+macgeargear@users.noreply.github.com> Date: Sun, 23 Jun 2024 14:57:58 +0700 Subject: [PATCH 08/11] chore --- internal/selection/selection.handler.go | 15 ++++++---- internal/selection/selection.service.go | 12 ++++++++ .../selection/test/selection.handler_test.go | 28 +++++++++++++------ 3 files changed, 40 insertions(+), 15 deletions(-) diff --git a/internal/selection/selection.handler.go b/internal/selection/selection.handler.go index 7130b57..4b6d87b 100644 --- a/internal/selection/selection.handler.go +++ b/internal/selection/selection.handler.go @@ -33,13 +33,13 @@ type handlerImpl struct { func (h *handlerImpl) CreateSelection(c router.Context) { body := &dto.CreateSelectionRequest{} if err := c.Bind(body); err != nil { - h.log.Named("selection hdr").Error("failed to bind request body", zap.Error(err)) + h.log.Named("CreateSelection").Error("Bind: failed to bind request body", zap.Error(err)) c.BadRequestError(err.Error()) return } if errorList := h.validate.Validate(body); errorList != nil { - h.log.Named("selection hdr").Error("validation error", zap.Strings("errorList", errorList)) + h.log.Named("CreateSelection").Error("Validate: ", zap.Strings("errorList", errorList)) c.BadRequestError(strings.Join(errorList, ", ")) return } @@ -51,6 +51,7 @@ func (h *handlerImpl) CreateSelection(c router.Context) { res, appErr := h.svc.CreateSelection(req) if appErr != nil { + h.log.Named("CreateSelection").Error("CreateSelection: ", zap.Error(appErr)) c.ResponseError(appErr) return } @@ -61,7 +62,7 @@ func (h *handlerImpl) CreateSelection(c router.Context) { func (h *handlerImpl) FindByGroupIdSelection(c router.Context) { groupdId := c.Param("id") if groupdId == "" { - h.log.Named("selection hdr").Error("url parameter 'id' not found", zap.String("id", groupdId)) + h.log.Named("FindByGroupIdSelection").Error("Param: id not found") c.BadRequestError("url parameter 'id' not found") return } @@ -71,13 +72,14 @@ func (h *handlerImpl) FindByGroupIdSelection(c router.Context) { } if errorList := h.validate.Validate(req); errorList != nil { - h.log.Named("selection hdr").Error("validation error", zap.Strings("errorList", errorList)) + h.log.Named("FindByGroupIdSelection").Error("Validate: ", zap.Strings("errorList", errorList)) c.BadRequestError(strings.Join(errorList, ", ")) return } res, appErr := h.svc.FindByGroupIdSelection(req) if appErr != nil { + h.log.Named("FindByGroupIdSelection").Error("FindByGroupIdSelection: ", zap.Error(appErr)) c.ResponseError(appErr) return } @@ -88,13 +90,13 @@ func (h *handlerImpl) FindByGroupIdSelection(c router.Context) { func (h *handlerImpl) UpdateSelection(c router.Context) { body := &dto.UpdateSelectionRequest{} if err := c.Bind(body); err != nil { - h.log.Named("selection hdr").Error("failed to bind request body", zap.Error(err)) + h.log.Named("UpdateSelection").Error("Bind: ", zap.Error(err)) c.BadRequestError(err.Error()) return } if errorList := h.validate.Validate(body); errorList != nil { - h.log.Named("selection hdr").Error("validation error", zap.Strings("errorList", errorList)) + h.log.Named("UpdateSelection").Error("Validate: ", zap.Strings("errorList", errorList)) c.BadRequestError(strings.Join(errorList, ", ")) return } @@ -105,6 +107,7 @@ func (h *handlerImpl) UpdateSelection(c router.Context) { res, appErr := h.svc.UpdateSelection(req) if appErr != nil { + h.log.Named("UpdateSelection").Error("UpdateSelection: ", zap.Error(appErr)) c.ResponseError(appErr) return } diff --git a/internal/selection/selection.service.go b/internal/selection/selection.service.go index 143e540..a049e81 100644 --- a/internal/selection/selection.service.go +++ b/internal/selection/selection.service.go @@ -41,14 +41,18 @@ func (s *serviceImpl) CreateSelection(req *dto.CreateSelectionRequest) (*dto.Cre if err != nil { st, ok := status.FromError(err) if !ok { + s.log.Named("CreateSelection").Error("FromeError: ", zap.Error(err)) return nil, apperror.InternalServer } switch st.Code() { case codes.InvalidArgument: + s.log.Named("CreateSelection").Error("Create: ", zap.Error(err)) return nil, apperror.BadRequestError("Invalid argument") case codes.Internal: + s.log.Named("CreateSelection").Error("Create: ", zap.Error(err)) return nil, apperror.InternalServerError(err.Error()) default: + s.log.Named("CreateSelection").Error("Create: ", zap.Error(err)) return nil, apperror.ServiceUnavailable } } @@ -68,14 +72,18 @@ func (s *serviceImpl) FindByGroupIdSelection(req *dto.FindByGroupIdSelectionRequ if err != nil { st, ok := status.FromError(err) if !ok { + s.log.Named("FindByGroupIdSelection").Error("FromeError: ", zap.Error(err)) return nil, apperror.InternalServer } switch st.Code() { case codes.InvalidArgument: + s.log.Named("FindByGroupIdSelection").Error("FindByGroupId: ", zap.Error(err)) return nil, apperror.BadRequestError("Invalid argument") case codes.Internal: + s.log.Named("FindByGroupIdSelection").Error("FindByGroupId: ", zap.Error(err)) return nil, apperror.InternalServerError(err.Error()) default: + s.log.Named("FindByGroupIdSelection").Error("FindByGroupId: ", zap.Error(err)) return nil, apperror.ServiceUnavailable } } @@ -95,14 +103,18 @@ func (s *serviceImpl) UpdateSelection(req *dto.UpdateSelectionRequest) (*dto.Upd if err != nil { st, ok := status.FromError(err) if !ok { + s.log.Named("UpdateSelection").Error("FromeError: ", zap.Error(err)) return nil, apperror.InternalServer } switch st.Code() { case codes.InvalidArgument: + s.log.Named("UpdateSelection").Error("Update: ", zap.Error(err)) return nil, apperror.BadRequestError("Invalid argument") case codes.Internal: + s.log.Named("UpdateSelection").Error("Update: ", zap.Error(err)) return nil, apperror.InternalServerError(err.Error()) default: + s.log.Named("UpdateSelection").Error("Update: ", zap.Error(err)) return nil, apperror.ServiceUnavailable } } diff --git a/internal/selection/test/selection.handler_test.go b/internal/selection/test/selection.handler_test.go index 1a57a64..532b84f 100644 --- a/internal/selection/test/selection.handler_test.go +++ b/internal/selection/test/selection.handler_test.go @@ -74,14 +74,12 @@ func (t *SelectionHandlerTest) TestCreateSelectionInvalidArgument() { controller := gomock.NewController(t.T()) - selectionSvc := selectionMock.NewMockService(controller) - validator := validatorMock.NewMockDtoValidator(controller) context := routerMock.NewMockContext(controller) context.EXPECT().Bind(t.CreateSelectionReq).Return(expectedErr) context.EXPECT().BadRequestError(expectedErr.Error()) - handler := selection.NewHandler(selectionSvc, validator, t.logger) + handler := selection.NewHandler(nil, nil, t.logger) handler.CreateSelection(context) } @@ -130,14 +128,12 @@ func (t *SelectionHandlerTest) TestFindByStudentIdSelectionInvalidArgument() { controller := gomock.NewController(t.T()) - selectionSvc := selectionMock.NewMockService(controller) - validator := validatorMock.NewMockDtoValidator(controller) context := routerMock.NewMockContext(controller) context.EXPECT().Param("id").Return("") context.EXPECT().BadRequestError(expectedErr.Error()) - handler := selection.NewHandler(selectionSvc, validator, t.logger) + handler := selection.NewHandler(nil, nil, t.logger) handler.FindByGroupIdSelection(context) } @@ -185,16 +181,30 @@ func (t *SelectionHandlerTest) TestUpdateSelectionInvalidArgument() { controller := gomock.NewController(t.T()) - selectionSvc := selectionMock.NewMockService(controller) - validator := validatorMock.NewMockDtoValidator(controller) context := routerMock.NewMockContext(controller) context.EXPECT().Bind(t.UpdateSelectionReq).Return(expectedErr) context.EXPECT().BadRequestError(expectedErr.Error()) - handler := selection.NewHandler(selectionSvc, validator, t.logger) + handler := selection.NewHandler(nil, nil, t.logger) handler.UpdateSelection(context) } func (t *SelectionHandlerTest) TestUpdateSelectionInternalError() { + expectedErr := apperror.InternalServerError("internal error") + + controller := gomock.NewController(t.T()) + + selectionSvc := selectionMock.NewMockService(controller) + validator := validatorMock.NewMockDtoValidator(controller) + context := routerMock.NewMockContext(controller) + + context.EXPECT().Bind(t.UpdateSelectionReq).Return(nil) + validator.EXPECT().Validate(t.UpdateSelectionReq).Return(nil) + + selectionSvc.EXPECT().UpdateSelection(t.UpdateSelectionReq).Return(nil, expectedErr) + context.EXPECT().ResponseError(expectedErr) + + handler := selection.NewHandler(selectionSvc, validator, t.logger) + handler.UpdateSelection(context) } From b10412ef93fb61f8f70ac3458524aefa04ce1e07 Mon Sep 17 00:00:00 2001 From: Gear <84141000+macgeargear@users.noreply.github.com> Date: Sun, 23 Jun 2024 15:37:31 +0700 Subject: [PATCH 09/11] chore --- internal/selection/selection.service.go | 2 +- .../selection/test/selection.handler_test.go | 60 +++++++------------ .../selection/test/selection.service_test.go | 37 ++++++------ 3 files changed, 40 insertions(+), 59 deletions(-) diff --git a/internal/selection/selection.service.go b/internal/selection/selection.service.go index a049e81..583509d 100644 --- a/internal/selection/selection.service.go +++ b/internal/selection/selection.service.go @@ -103,7 +103,7 @@ func (s *serviceImpl) UpdateSelection(req *dto.UpdateSelectionRequest) (*dto.Upd if err != nil { st, ok := status.FromError(err) if !ok { - s.log.Named("UpdateSelection").Error("FromeError: ", zap.Error(err)) + s.log.Named("UpdateSelection").Error("FromError: ", zap.Error(err)) return nil, apperror.InternalServer } switch st.Code() { diff --git a/internal/selection/test/selection.handler_test.go b/internal/selection/test/selection.handler_test.go index 532b84f..daad7b6 100644 --- a/internal/selection/test/selection.handler_test.go +++ b/internal/selection/test/selection.handler_test.go @@ -54,11 +54,9 @@ func (t *SelectionHandlerTest) TestCreateSelectionSuccess() { Selection: t.Selection, } - controller := gomock.NewController(t.T()) - - selectionSvc := selectionMock.NewMockService(controller) - validator := validatorMock.NewMockDtoValidator(controller) - context := routerMock.NewMockContext(controller) + selectionSvc := selectionMock.NewMockService(t.controller) + validator := validatorMock.NewMockDtoValidator(t.controller) + context := routerMock.NewMockContext(t.controller) context.EXPECT().Bind(t.CreateSelectionReq).Return(nil) validator.EXPECT().Validate(t.CreateSelectionReq).Return(nil) @@ -72,9 +70,7 @@ func (t *SelectionHandlerTest) TestCreateSelectionSuccess() { func (t *SelectionHandlerTest) TestCreateSelectionInvalidArgument() { expectedErr := apperror.BadRequestError("invalid argument") - controller := gomock.NewController(t.T()) - - context := routerMock.NewMockContext(controller) + context := routerMock.NewMockContext(t.controller) context.EXPECT().Bind(t.CreateSelectionReq).Return(expectedErr) context.EXPECT().BadRequestError(expectedErr.Error()) @@ -86,11 +82,9 @@ func (t *SelectionHandlerTest) TestCreateSelectionInvalidArgument() { func (t *SelectionHandlerTest) TestCreateSelectionInternalError() { expectedErr := apperror.InternalServerError("internal error") - controller := gomock.NewController(t.T()) - - selectionSvc := selectionMock.NewMockService(controller) - validator := validatorMock.NewMockDtoValidator(controller) - context := routerMock.NewMockContext(controller) + selectionSvc := selectionMock.NewMockService(t.controller) + validator := validatorMock.NewMockDtoValidator(t.controller) + context := routerMock.NewMockContext(t.controller) context.EXPECT().Bind(t.CreateSelectionReq).Return(nil) validator.EXPECT().Validate(t.CreateSelectionReq).Return(nil) @@ -107,11 +101,9 @@ func (t *SelectionHandlerTest) TestFindByStudentIdSelectionSuccess() { Selection: t.Selection, } - controller := gomock.NewController(t.T()) - - selectionSvc := selectionMock.NewMockService(controller) - validator := validatorMock.NewMockDtoValidator(controller) - context := routerMock.NewMockContext(controller) + selectionSvc := selectionMock.NewMockService(t.controller) + validator := validatorMock.NewMockDtoValidator(t.controller) + context := routerMock.NewMockContext(t.controller) context.EXPECT().Param("id").Return(t.Selection.GroupId) validator.EXPECT().Validate(t.FindByGroupIdSelectionReq).Return(nil) @@ -126,9 +118,7 @@ func (t *SelectionHandlerTest) TestFindByStudentIdSelectionSuccess() { func (t *SelectionHandlerTest) TestFindByStudentIdSelectionInvalidArgument() { expectedErr := apperror.BadRequestError("url parameter 'id' not found") - controller := gomock.NewController(t.T()) - - context := routerMock.NewMockContext(controller) + context := routerMock.NewMockContext(t.controller) context.EXPECT().Param("id").Return("") context.EXPECT().BadRequestError(expectedErr.Error()) @@ -140,11 +130,9 @@ func (t *SelectionHandlerTest) TestFindByStudentIdSelectionInvalidArgument() { func (t *SelectionHandlerTest) TestFindByStudentIdSelectionInternalError() { expectedErr := apperror.InternalServerError("internal error") - controller := gomock.NewController(t.T()) - - selectionSvc := selectionMock.NewMockService(controller) - validator := validatorMock.NewMockDtoValidator(controller) - context := routerMock.NewMockContext(controller) + selectionSvc := selectionMock.NewMockService(t.controller) + validator := validatorMock.NewMockDtoValidator(t.controller) + context := routerMock.NewMockContext(t.controller) context.EXPECT().Param("id").Return(t.Selection.GroupId) validator.EXPECT().Validate(t.FindByGroupIdSelectionReq).Return(nil) @@ -161,11 +149,9 @@ func (t *SelectionHandlerTest) TestUpdateSelectionSuccess() { Success: true, } - controller := gomock.NewController(t.T()) - - selectionSvc := selectionMock.NewMockService(controller) - validator := validatorMock.NewMockDtoValidator(controller) - context := routerMock.NewMockContext(controller) + selectionSvc := selectionMock.NewMockService(t.controller) + validator := validatorMock.NewMockDtoValidator(t.controller) + context := routerMock.NewMockContext(t.controller) context.EXPECT().Bind(t.UpdateSelectionReq).Return(nil) validator.EXPECT().Validate(t.UpdateSelectionReq).Return(nil) @@ -179,9 +165,7 @@ func (t *SelectionHandlerTest) TestUpdateSelectionSuccess() { func (t *SelectionHandlerTest) TestUpdateSelectionInvalidArgument() { expectedErr := apperror.BadRequestError("invalid argument") - controller := gomock.NewController(t.T()) - - context := routerMock.NewMockContext(controller) + context := routerMock.NewMockContext(t.controller) context.EXPECT().Bind(t.UpdateSelectionReq).Return(expectedErr) context.EXPECT().BadRequestError(expectedErr.Error()) @@ -193,11 +177,9 @@ func (t *SelectionHandlerTest) TestUpdateSelectionInvalidArgument() { func (t *SelectionHandlerTest) TestUpdateSelectionInternalError() { expectedErr := apperror.InternalServerError("internal error") - controller := gomock.NewController(t.T()) - - selectionSvc := selectionMock.NewMockService(controller) - validator := validatorMock.NewMockDtoValidator(controller) - context := routerMock.NewMockContext(controller) + selectionSvc := selectionMock.NewMockService(t.controller) + validator := validatorMock.NewMockDtoValidator(t.controller) + context := routerMock.NewMockContext(t.controller) context.EXPECT().Bind(t.UpdateSelectionReq).Return(nil) validator.EXPECT().Validate(t.UpdateSelectionReq).Return(nil) diff --git a/internal/selection/test/selection.service_test.go b/internal/selection/test/selection.service_test.go index 11f2b8f..4fbf3d7 100644 --- a/internal/selection/test/selection.service_test.go +++ b/internal/selection/test/selection.service_test.go @@ -9,7 +9,6 @@ import ( "github.com/isd-sgcu/rpkm67-gateway/internal/selection" selectionMock "github.com/isd-sgcu/rpkm67-gateway/mocks/client/selection" selectionProto "github.com/isd-sgcu/rpkm67-go-proto/rpkm67/backend/selection/v1" - "github.com/stretchr/testify/assert" "github.com/stretchr/testify/suite" "go.uber.org/zap" "google.golang.org/grpc/codes" @@ -85,8 +84,8 @@ func (t *SelectionServiceTest) TestCreateSelectionSuccess() { svc := selection.NewService(&client, t.logger) actual, err := svc.CreateSelection(t.CreateSelectionDtoRequest) - assert.Nil(t.T(), err) - assert.Equal(t.T(), expected, actual) + t.Nil(err) + t.Equal(expected, actual) } func (t *SelectionServiceTest) TestCreateSelectionInvalidArgument() { @@ -102,8 +101,8 @@ func (t *SelectionServiceTest) TestCreateSelectionInvalidArgument() { svc := selection.NewService(&client, t.logger) actual, err := svc.CreateSelection(t.CreateSelectionDtoRequest) - assert.Nil(t.T(), actual) - assert.Equal(t.T(), expected, err) + t.Nil(actual) + t.Equal(expected, err) } func (t *SelectionServiceTest) TestCreateSelectionInternalError() { @@ -119,8 +118,8 @@ func (t *SelectionServiceTest) TestCreateSelectionInternalError() { svc := selection.NewService(&client, t.logger) actual, err := svc.CreateSelection(t.CreateSelectionDtoRequest) - assert.Nil(t.T(), actual) - assert.Equal(t.T(), expected, err) + t.Nil(actual) + t.Equal(expected, err) } func (t *SelectionServiceTest) TestFindByGroupIdSelectionSuccess() { @@ -138,8 +137,8 @@ func (t *SelectionServiceTest) TestFindByGroupIdSelectionSuccess() { svc := selection.NewService(&client, t.logger) actual, err := svc.FindByGroupIdSelection(t.FindByGroupIdSelectionDtoRequest) - assert.Nil(t.T(), err) - assert.Equal(t.T(), expected, actual) + t.Nil(err) + t.Equal(expected, actual) } func (t *SelectionServiceTest) TestFindByGroupIdSelectionInvalidArgument() { @@ -155,8 +154,8 @@ func (t *SelectionServiceTest) TestFindByGroupIdSelectionInvalidArgument() { svc := selection.NewService(&client, t.logger) actual, err := svc.FindByGroupIdSelection(t.FindByGroupIdSelectionDtoRequest) - assert.Nil(t.T(), actual) - assert.Equal(t.T(), expected, err) + t.Nil(actual) + t.Equal(expected, err) } func (t *SelectionServiceTest) TestFindByGroupIdSelectionInternalError() { @@ -172,8 +171,8 @@ func (t *SelectionServiceTest) TestFindByGroupIdSelectionInternalError() { svc := selection.NewService(&client, t.logger) actual, err := svc.FindByGroupIdSelection(t.FindByGroupIdSelectionDtoRequest) - assert.Nil(t.T(), actual) - assert.Equal(t.T(), expected, err) + t.Nil(actual) + t.Equal(expected, err) } @@ -192,8 +191,8 @@ func (t *SelectionServiceTest) TestUpdateSelectionSuccess() { svc := selection.NewService(&client, t.logger) actual, err := svc.UpdateSelection(t.UpdateSelectionDtoRequest) - assert.Nil(t.T(), err) - assert.Equal(t.T(), expected, actual) + t.Nil(err) + t.Equal(expected, actual) } func (t *SelectionServiceTest) TestUpdateSelectionInvalidArgument() { @@ -209,8 +208,8 @@ func (t *SelectionServiceTest) TestUpdateSelectionInvalidArgument() { svc := selection.NewService(&client, t.logger) actual, err := svc.UpdateSelection(t.UpdateSelectionDtoRequest) - assert.Nil(t.T(), actual) - assert.Equal(t.T(), expected, err) + t.Nil(actual) + t.Equal(expected, err) } func (t *SelectionServiceTest) TestUpdateSelectionInternalError() { @@ -226,6 +225,6 @@ func (t *SelectionServiceTest) TestUpdateSelectionInternalError() { svc := selection.NewService(&client, t.logger) actual, err := svc.UpdateSelection(t.UpdateSelectionDtoRequest) - assert.Nil(t.T(), actual) - assert.Equal(t.T(), expected, err) + t.Nil(actual) + t.Equal(expected, err) } From adc1149d1c7962c1e098f8adfbb775af5695516a Mon Sep 17 00:00:00 2001 From: Gear <84141000+macgeargear@users.noreply.github.com> Date: Sun, 23 Jun 2024 15:53:18 +0700 Subject: [PATCH 10/11] chore chore --- .../selection/test/selection.handler_test.go | 70 +++++++++---------- .../selection/test/selection.service_test.go | 69 +++++++----------- 2 files changed, 60 insertions(+), 79 deletions(-) diff --git a/internal/selection/test/selection.handler_test.go b/internal/selection/test/selection.handler_test.go index daad7b6..f1a62b2 100644 --- a/internal/selection/test/selection.handler_test.go +++ b/internal/selection/test/selection.handler_test.go @@ -50,143 +50,139 @@ func (t *SelectionHandlerTest) SetupTest() { } func (t *SelectionHandlerTest) TestCreateSelectionSuccess() { - expectedResp := &dto.CreateSelectionResponse{ - Selection: t.Selection, - } - selectionSvc := selectionMock.NewMockService(t.controller) validator := validatorMock.NewMockDtoValidator(t.controller) context := routerMock.NewMockContext(t.controller) + handler := selection.NewHandler(selectionSvc, validator, t.logger) + + expectedResp := &dto.CreateSelectionResponse{ + Selection: t.Selection, + } context.EXPECT().Bind(t.CreateSelectionReq).Return(nil) validator.EXPECT().Validate(t.CreateSelectionReq).Return(nil) selectionSvc.EXPECT().CreateSelection(t.CreateSelectionReq).Return(expectedResp, nil) context.EXPECT().JSON(http.StatusCreated, expectedResp) - handler := selection.NewHandler(selectionSvc, validator, t.logger) handler.CreateSelection(context) } func (t *SelectionHandlerTest) TestCreateSelectionInvalidArgument() { - expectedErr := apperror.BadRequestError("invalid argument") - context := routerMock.NewMockContext(t.controller) + handler := selection.NewHandler(nil, nil, t.logger) + + expectedErr := apperror.BadRequestError("invalid argument") context.EXPECT().Bind(t.CreateSelectionReq).Return(expectedErr) context.EXPECT().BadRequestError(expectedErr.Error()) - handler := selection.NewHandler(nil, nil, t.logger) handler.CreateSelection(context) } func (t *SelectionHandlerTest) TestCreateSelectionInternalError() { - expectedErr := apperror.InternalServerError("internal error") - selectionSvc := selectionMock.NewMockService(t.controller) validator := validatorMock.NewMockDtoValidator(t.controller) context := routerMock.NewMockContext(t.controller) + handler := selection.NewHandler(selectionSvc, validator, t.logger) + + expectedErr := apperror.InternalServerError("internal error") context.EXPECT().Bind(t.CreateSelectionReq).Return(nil) validator.EXPECT().Validate(t.CreateSelectionReq).Return(nil) - selectionSvc.EXPECT().CreateSelection(t.CreateSelectionReq).Return(nil, expectedErr) context.EXPECT().ResponseError(expectedErr) - handler := selection.NewHandler(selectionSvc, validator, t.logger) handler.CreateSelection(context) } func (t *SelectionHandlerTest) TestFindByStudentIdSelectionSuccess() { - expectedResp := &dto.FindByGroupIdSelectionResponse{ - Selection: t.Selection, - } - selectionSvc := selectionMock.NewMockService(t.controller) validator := validatorMock.NewMockDtoValidator(t.controller) context := routerMock.NewMockContext(t.controller) + handler := selection.NewHandler(selectionSvc, validator, t.logger) + + expectedResp := &dto.FindByGroupIdSelectionResponse{ + Selection: t.Selection, + } context.EXPECT().Param("id").Return(t.Selection.GroupId) validator.EXPECT().Validate(t.FindByGroupIdSelectionReq).Return(nil) selectionSvc.EXPECT().FindByGroupIdSelection(t.FindByGroupIdSelectionReq).Return(expectedResp, nil) context.EXPECT().JSON(http.StatusOK, expectedResp) - handler := selection.NewHandler(selectionSvc, validator, t.logger) handler.FindByGroupIdSelection(context) - } func (t *SelectionHandlerTest) TestFindByStudentIdSelectionInvalidArgument() { - expectedErr := apperror.BadRequestError("url parameter 'id' not found") - context := routerMock.NewMockContext(t.controller) + handler := selection.NewHandler(nil, nil, t.logger) + + expectedErr := apperror.BadRequestError("url parameter 'id' not found") context.EXPECT().Param("id").Return("") context.EXPECT().BadRequestError(expectedErr.Error()) - handler := selection.NewHandler(nil, nil, t.logger) handler.FindByGroupIdSelection(context) } func (t *SelectionHandlerTest) TestFindByStudentIdSelectionInternalError() { - expectedErr := apperror.InternalServerError("internal error") - selectionSvc := selectionMock.NewMockService(t.controller) validator := validatorMock.NewMockDtoValidator(t.controller) context := routerMock.NewMockContext(t.controller) + handler := selection.NewHandler(selectionSvc, validator, t.logger) + + expectedErr := apperror.InternalServerError("internal error") context.EXPECT().Param("id").Return(t.Selection.GroupId) validator.EXPECT().Validate(t.FindByGroupIdSelectionReq).Return(nil) - selectionSvc.EXPECT().FindByGroupIdSelection(t.FindByGroupIdSelectionReq).Return(nil, expectedErr) context.EXPECT().ResponseError(expectedErr) - handler := selection.NewHandler(selectionSvc, validator, t.logger) handler.FindByGroupIdSelection(context) } func (t *SelectionHandlerTest) TestUpdateSelectionSuccess() { - expectedResp := &dto.UpdateSelectionResponse{ - Success: true, - } - selectionSvc := selectionMock.NewMockService(t.controller) validator := validatorMock.NewMockDtoValidator(t.controller) context := routerMock.NewMockContext(t.controller) + handler := selection.NewHandler(selectionSvc, validator, t.logger) + + expectedResp := &dto.UpdateSelectionResponse{ + Success: true, + } context.EXPECT().Bind(t.UpdateSelectionReq).Return(nil) validator.EXPECT().Validate(t.UpdateSelectionReq).Return(nil) selectionSvc.EXPECT().UpdateSelection(t.UpdateSelectionReq).Return(expectedResp, nil) context.EXPECT().JSON(http.StatusOK, expectedResp) - handler := selection.NewHandler(selectionSvc, validator, t.logger) handler.UpdateSelection(context) } func (t *SelectionHandlerTest) TestUpdateSelectionInvalidArgument() { - expectedErr := apperror.BadRequestError("invalid argument") - context := routerMock.NewMockContext(t.controller) + handler := selection.NewHandler(nil, nil, t.logger) + + expectedErr := apperror.BadRequestError("invalid argument") context.EXPECT().Bind(t.UpdateSelectionReq).Return(expectedErr) context.EXPECT().BadRequestError(expectedErr.Error()) - handler := selection.NewHandler(nil, nil, t.logger) handler.UpdateSelection(context) } func (t *SelectionHandlerTest) TestUpdateSelectionInternalError() { - expectedErr := apperror.InternalServerError("internal error") - selectionSvc := selectionMock.NewMockService(t.controller) validator := validatorMock.NewMockDtoValidator(t.controller) context := routerMock.NewMockContext(t.controller) + handler := selection.NewHandler(selectionSvc, validator, t.logger) + + expectedErr := apperror.InternalServerError("internal error") context.EXPECT().Bind(t.UpdateSelectionReq).Return(nil) validator.EXPECT().Validate(t.UpdateSelectionReq).Return(nil) - selectionSvc.EXPECT().UpdateSelection(t.UpdateSelectionReq).Return(nil, expectedErr) context.EXPECT().ResponseError(expectedErr) - handler := selection.NewHandler(selectionSvc, validator, t.logger) handler.UpdateSelection(context) } diff --git a/internal/selection/test/selection.service_test.go b/internal/selection/test/selection.service_test.go index 4fbf3d7..24a73a2 100644 --- a/internal/selection/test/selection.service_test.go +++ b/internal/selection/test/selection.service_test.go @@ -68,20 +68,19 @@ func (t *SelectionServiceTest) SetupTest() { } func (t *SelectionServiceTest) TestCreateSelectionSuccess() { + client := selectionMock.SelectionClientMock{} + svc := selection.NewService(&client, t.logger) + protoResp := &selectionProto.CreateSelectionResponse{ Selection: t.SelectionProto, } createSelectionDto := selection.ProtoToDto(protoResp.Selection) - expected := &dto.CreateSelectionResponse{ Selection: createSelectionDto, } - client := selectionMock.SelectionClientMock{} client.On("Create", t.CreateSelectionProtoRequest).Return(protoResp, nil) - - svc := selection.NewService(&client, t.logger) actual, err := svc.CreateSelection(t.CreateSelectionDtoRequest) t.Nil(err) @@ -89,16 +88,14 @@ func (t *SelectionServiceTest) TestCreateSelectionSuccess() { } func (t *SelectionServiceTest) TestCreateSelectionInvalidArgument() { - protoReq := t.CreateSelectionProtoRequest + client := selectionMock.SelectionClientMock{} + svc := selection.NewService(&client, t.logger) + protoReq := t.CreateSelectionProtoRequest expected := apperror.BadRequestError("Invalid argument") - clientErr := status.Error(codes.InvalidArgument, apperror.BadRequest.Error()) - client := selectionMock.SelectionClientMock{} client.On("Create", protoReq).Return(nil, clientErr) - - svc := selection.NewService(&client, t.logger) actual, err := svc.CreateSelection(t.CreateSelectionDtoRequest) t.Nil(actual) @@ -106,16 +103,14 @@ func (t *SelectionServiceTest) TestCreateSelectionInvalidArgument() { } func (t *SelectionServiceTest) TestCreateSelectionInternalError() { - protoReq := t.CreateSelectionProtoRequest + client := selectionMock.SelectionClientMock{} + svc := selection.NewService(&client, t.logger) + protoReq := t.CreateSelectionProtoRequest expected := apperror.InternalServerError("rpc error: code = Internal desc = Internal error") - clientErr := status.Error(codes.Internal, apperror.InternalServer.Error()) - client := selectionMock.SelectionClientMock{} client.On("Create", protoReq).Return(nil, clientErr) - - svc := selection.NewService(&client, t.logger) actual, err := svc.CreateSelection(t.CreateSelectionDtoRequest) t.Nil(actual) @@ -123,18 +118,17 @@ func (t *SelectionServiceTest) TestCreateSelectionInternalError() { } func (t *SelectionServiceTest) TestFindByGroupIdSelectionSuccess() { + client := selectionMock.SelectionClientMock{} + svc := selection.NewService(&client, t.logger) + protoResp := &selectionProto.FindByGroupIdSelectionResponse{ Selection: t.SelectionProto, } - expected := &dto.FindByGroupIdSelectionResponse{ Selection: t.SelectionDto, } - client := selectionMock.SelectionClientMock{} client.On("FindByGroupId", t.FindByGroupIdSelectionProtoRequest).Return(protoResp, nil) - - svc := selection.NewService(&client, t.logger) actual, err := svc.FindByGroupIdSelection(t.FindByGroupIdSelectionDtoRequest) t.Nil(err) @@ -142,16 +136,14 @@ func (t *SelectionServiceTest) TestFindByGroupIdSelectionSuccess() { } func (t *SelectionServiceTest) TestFindByGroupIdSelectionInvalidArgument() { - protoReq := t.FindByGroupIdSelectionProtoRequest + client := selectionMock.SelectionClientMock{} + svc := selection.NewService(&client, t.logger) + protoReq := t.FindByGroupIdSelectionProtoRequest expected := apperror.BadRequestError("Invalid argument") - clientErr := status.Error(codes.InvalidArgument, apperror.BadRequest.Error()) - client := selectionMock.SelectionClientMock{} client.On("FindByGroupId", protoReq).Return(nil, clientErr) - - svc := selection.NewService(&client, t.logger) actual, err := svc.FindByGroupIdSelection(t.FindByGroupIdSelectionDtoRequest) t.Nil(actual) @@ -159,16 +151,14 @@ func (t *SelectionServiceTest) TestFindByGroupIdSelectionInvalidArgument() { } func (t *SelectionServiceTest) TestFindByGroupIdSelectionInternalError() { - protoReq := t.FindByGroupIdSelectionProtoRequest + client := selectionMock.SelectionClientMock{} + svc := selection.NewService(&client, t.logger) + protoReq := t.FindByGroupIdSelectionProtoRequest expected := apperror.InternalServerError("rpc error: code = Internal desc = Internal error") - clientErr := status.Error(codes.Internal, apperror.InternalServer.Error()) - client := selectionMock.SelectionClientMock{} client.On("FindByGroupId", protoReq).Return(nil, clientErr) - - svc := selection.NewService(&client, t.logger) actual, err := svc.FindByGroupIdSelection(t.FindByGroupIdSelectionDtoRequest) t.Nil(actual) @@ -177,18 +167,17 @@ func (t *SelectionServiceTest) TestFindByGroupIdSelectionInternalError() { } func (t *SelectionServiceTest) TestUpdateSelectionSuccess() { + client := selectionMock.SelectionClientMock{} + svc := selection.NewService(&client, t.logger) + protoResp := &selectionProto.UpdateSelectionResponse{ Success: true, } - expected := &dto.UpdateSelectionResponse{ Success: true, } - client := selectionMock.SelectionClientMock{} client.On("Update", t.UpdateSelectionProtoRequest).Return(protoResp, nil) - - svc := selection.NewService(&client, t.logger) actual, err := svc.UpdateSelection(t.UpdateSelectionDtoRequest) t.Nil(err) @@ -196,16 +185,14 @@ func (t *SelectionServiceTest) TestUpdateSelectionSuccess() { } func (t *SelectionServiceTest) TestUpdateSelectionInvalidArgument() { - protoReq := t.UpdateSelectionProtoRequest + client := selectionMock.SelectionClientMock{} + svc := selection.NewService(&client, t.logger) + protoReq := t.UpdateSelectionProtoRequest expected := apperror.BadRequestError("Invalid argument") - clientErr := status.Error(codes.InvalidArgument, apperror.BadRequest.Error()) - client := selectionMock.SelectionClientMock{} client.On("Update", protoReq).Return(nil, clientErr) - - svc := selection.NewService(&client, t.logger) actual, err := svc.UpdateSelection(t.UpdateSelectionDtoRequest) t.Nil(actual) @@ -213,16 +200,14 @@ func (t *SelectionServiceTest) TestUpdateSelectionInvalidArgument() { } func (t *SelectionServiceTest) TestUpdateSelectionInternalError() { - protoReq := t.UpdateSelectionProtoRequest + client := selectionMock.SelectionClientMock{} + svc := selection.NewService(&client, t.logger) + protoReq := t.UpdateSelectionProtoRequest expected := apperror.InternalServerError("rpc error: code = Internal desc = Internal error") - clientErr := status.Error(codes.Internal, apperror.InternalServer.Error()) - client := selectionMock.SelectionClientMock{} client.On("Update", protoReq).Return(nil, clientErr) - - svc := selection.NewService(&client, t.logger) actual, err := svc.UpdateSelection(t.UpdateSelectionDtoRequest) t.Nil(actual) From 50ed8c400ede679066a123484af0160833999423 Mon Sep 17 00:00:00 2001 From: Gear <84141000+macgeargear@users.noreply.github.com> Date: Sun, 23 Jun 2024 19:48:04 +0700 Subject: [PATCH 11/11] fix: test --- internal/selection/selection.handler.go | 6 +- internal/selection/selection.service.go | 15 +---- .../selection/test/selection.handler_test.go | 61 ++++++++----------- 3 files changed, 33 insertions(+), 49 deletions(-) diff --git a/internal/selection/selection.handler.go b/internal/selection/selection.handler.go index 4b6d87b..6ed79f6 100644 --- a/internal/selection/selection.handler.go +++ b/internal/selection/selection.handler.go @@ -60,15 +60,15 @@ func (h *handlerImpl) CreateSelection(c router.Context) { } func (h *handlerImpl) FindByGroupIdSelection(c router.Context) { - groupdId := c.Param("id") - if groupdId == "" { + groupId := c.Param("id") + if groupId == "" { h.log.Named("FindByGroupIdSelection").Error("Param: id not found") c.BadRequestError("url parameter 'id' not found") return } req := &dto.FindByGroupIdSelectionRequest{ - GroupId: groupdId, + GroupId: groupId, } if errorList := h.validate.Validate(req); errorList != nil { diff --git a/internal/selection/selection.service.go b/internal/selection/selection.service.go index 583509d..9041ed1 100644 --- a/internal/selection/selection.service.go +++ b/internal/selection/selection.service.go @@ -39,20 +39,17 @@ func (s *serviceImpl) CreateSelection(req *dto.CreateSelectionRequest) (*dto.Cre BaanIds: req.BaanIds, }) if err != nil { + s.log.Named("CreateSelection").Error("Create: ", zap.Error(err)) st, ok := status.FromError(err) if !ok { - s.log.Named("CreateSelection").Error("FromeError: ", zap.Error(err)) return nil, apperror.InternalServer } switch st.Code() { case codes.InvalidArgument: - s.log.Named("CreateSelection").Error("Create: ", zap.Error(err)) return nil, apperror.BadRequestError("Invalid argument") case codes.Internal: - s.log.Named("CreateSelection").Error("Create: ", zap.Error(err)) return nil, apperror.InternalServerError(err.Error()) default: - s.log.Named("CreateSelection").Error("Create: ", zap.Error(err)) return nil, apperror.ServiceUnavailable } } @@ -70,20 +67,17 @@ func (s *serviceImpl) FindByGroupIdSelection(req *dto.FindByGroupIdSelectionRequ GroupId: req.GroupId, }) if err != nil { + s.log.Named("FindByGroupIdSelection").Error("FindByGroupId: ", zap.Error(err)) st, ok := status.FromError(err) if !ok { - s.log.Named("FindByGroupIdSelection").Error("FromeError: ", zap.Error(err)) return nil, apperror.InternalServer } switch st.Code() { case codes.InvalidArgument: - s.log.Named("FindByGroupIdSelection").Error("FindByGroupId: ", zap.Error(err)) return nil, apperror.BadRequestError("Invalid argument") case codes.Internal: - s.log.Named("FindByGroupIdSelection").Error("FindByGroupId: ", zap.Error(err)) return nil, apperror.InternalServerError(err.Error()) default: - s.log.Named("FindByGroupIdSelection").Error("FindByGroupId: ", zap.Error(err)) return nil, apperror.ServiceUnavailable } } @@ -101,20 +95,17 @@ func (s *serviceImpl) UpdateSelection(req *dto.UpdateSelectionRequest) (*dto.Upd Selection: DtoToProto(req.Selection), }) if err != nil { + s.log.Named("UpdateSelection").Error("Update: ", zap.Error(err)) st, ok := status.FromError(err) if !ok { - s.log.Named("UpdateSelection").Error("FromError: ", zap.Error(err)) return nil, apperror.InternalServer } switch st.Code() { case codes.InvalidArgument: - s.log.Named("UpdateSelection").Error("Update: ", zap.Error(err)) return nil, apperror.BadRequestError("Invalid argument") case codes.Internal: - s.log.Named("UpdateSelection").Error("Update: ", zap.Error(err)) return nil, apperror.InternalServerError(err.Error()) default: - s.log.Named("UpdateSelection").Error("Update: ", zap.Error(err)) return nil, apperror.ServiceUnavailable } } diff --git a/internal/selection/test/selection.handler_test.go b/internal/selection/test/selection.handler_test.go index f1a62b2..06096ee 100644 --- a/internal/selection/test/selection.handler_test.go +++ b/internal/selection/test/selection.handler_test.go @@ -24,8 +24,6 @@ type SelectionHandlerTest struct { CreateSelectionReq *dto.CreateSelectionRequest FindByGroupIdSelectionReq *dto.FindByGroupIdSelectionRequest UpdateSelectionReq *dto.UpdateSelectionRequest - Err *apperror.AppError - QueriesMock map[string]string } func TestSelectionHandler(t *testing.T) { @@ -42,11 +40,16 @@ func (t *SelectionHandlerTest) SetupTest() { t.Selections = selection.ProtoToDtoList(selectionsProto) t.Selection = selection.ProtoToDto(selectionProto) - t.CreateSelectionReq = &dto.CreateSelectionRequest{} + t.CreateSelectionReq = &dto.CreateSelectionRequest{ + GroupId: t.Selection.GroupId, + BaanIds: t.Selection.BaanIds, + } t.FindByGroupIdSelectionReq = &dto.FindByGroupIdSelectionRequest{ GroupId: t.Selection.GroupId, } - t.UpdateSelectionReq = &dto.UpdateSelectionRequest{} + t.UpdateSelectionReq = &dto.UpdateSelectionRequest{ + Selection: selection.ProtoToDto(selectionsProto[1]), + } } func (t *SelectionHandlerTest) TestCreateSelectionSuccess() { @@ -59,7 +62,7 @@ func (t *SelectionHandlerTest) TestCreateSelectionSuccess() { Selection: t.Selection, } - context.EXPECT().Bind(t.CreateSelectionReq).Return(nil) + context.EXPECT().Bind(&dto.CreateSelectionRequest{}).SetArg(0, *t.CreateSelectionReq) validator.EXPECT().Validate(t.CreateSelectionReq).Return(nil) selectionSvc.EXPECT().CreateSelection(t.CreateSelectionReq).Return(expectedResp, nil) context.EXPECT().JSON(http.StatusCreated, expectedResp) @@ -67,30 +70,26 @@ func (t *SelectionHandlerTest) TestCreateSelectionSuccess() { handler.CreateSelection(context) } -func (t *SelectionHandlerTest) TestCreateSelectionInvalidArgument() { +func (t *SelectionHandlerTest) TestCreateSelectionBindError() { context := routerMock.NewMockContext(t.controller) handler := selection.NewHandler(nil, nil, t.logger) - expectedErr := apperror.BadRequestError("invalid argument") - - context.EXPECT().Bind(t.CreateSelectionReq).Return(expectedErr) - context.EXPECT().BadRequestError(expectedErr.Error()) + context.EXPECT().Bind(&dto.CreateSelectionRequest{}).Return(apperror.BadRequest) + context.EXPECT().BadRequestError(apperror.BadRequest.Error()) handler.CreateSelection(context) } -func (t *SelectionHandlerTest) TestCreateSelectionInternalError() { +func (t *SelectionHandlerTest) TestCreateSelectionServiceError() { selectionSvc := selectionMock.NewMockService(t.controller) validator := validatorMock.NewMockDtoValidator(t.controller) context := routerMock.NewMockContext(t.controller) handler := selection.NewHandler(selectionSvc, validator, t.logger) - expectedErr := apperror.InternalServerError("internal error") - - context.EXPECT().Bind(t.CreateSelectionReq).Return(nil) + context.EXPECT().Bind(&dto.CreateSelectionRequest{}).SetArg(0, *t.CreateSelectionReq) validator.EXPECT().Validate(t.CreateSelectionReq).Return(nil) - selectionSvc.EXPECT().CreateSelection(t.CreateSelectionReq).Return(nil, expectedErr) - context.EXPECT().ResponseError(expectedErr) + selectionSvc.EXPECT().CreateSelection(t.CreateSelectionReq).Return(nil, apperror.InternalServer) + context.EXPECT().ResponseError(apperror.InternalServer) handler.CreateSelection(context) } @@ -113,7 +112,7 @@ func (t *SelectionHandlerTest) TestFindByStudentIdSelectionSuccess() { handler.FindByGroupIdSelection(context) } -func (t *SelectionHandlerTest) TestFindByStudentIdSelectionInvalidArgument() { +func (t *SelectionHandlerTest) TestFindByStudentIdSelectionUrlParamEmpty() { context := routerMock.NewMockContext(t.controller) handler := selection.NewHandler(nil, nil, t.logger) @@ -125,18 +124,16 @@ func (t *SelectionHandlerTest) TestFindByStudentIdSelectionInvalidArgument() { handler.FindByGroupIdSelection(context) } -func (t *SelectionHandlerTest) TestFindByStudentIdSelectionInternalError() { +func (t *SelectionHandlerTest) TestFindByStudentIdSelectionServiceError() { selectionSvc := selectionMock.NewMockService(t.controller) validator := validatorMock.NewMockDtoValidator(t.controller) context := routerMock.NewMockContext(t.controller) handler := selection.NewHandler(selectionSvc, validator, t.logger) - expectedErr := apperror.InternalServerError("internal error") - context.EXPECT().Param("id").Return(t.Selection.GroupId) validator.EXPECT().Validate(t.FindByGroupIdSelectionReq).Return(nil) - selectionSvc.EXPECT().FindByGroupIdSelection(t.FindByGroupIdSelectionReq).Return(nil, expectedErr) - context.EXPECT().ResponseError(expectedErr) + selectionSvc.EXPECT().FindByGroupIdSelection(t.FindByGroupIdSelectionReq).Return(nil, apperror.InternalServer) + context.EXPECT().ResponseError(apperror.InternalServer) handler.FindByGroupIdSelection(context) } @@ -151,7 +148,7 @@ func (t *SelectionHandlerTest) TestUpdateSelectionSuccess() { Success: true, } - context.EXPECT().Bind(t.UpdateSelectionReq).Return(nil) + context.EXPECT().Bind(&dto.UpdateSelectionRequest{}).SetArg(0, *t.UpdateSelectionReq) validator.EXPECT().Validate(t.UpdateSelectionReq).Return(nil) selectionSvc.EXPECT().UpdateSelection(t.UpdateSelectionReq).Return(expectedResp, nil) context.EXPECT().JSON(http.StatusOK, expectedResp) @@ -159,30 +156,26 @@ func (t *SelectionHandlerTest) TestUpdateSelectionSuccess() { handler.UpdateSelection(context) } -func (t *SelectionHandlerTest) TestUpdateSelectionInvalidArgument() { +func (t *SelectionHandlerTest) TestUpdateSelectionBindError() { context := routerMock.NewMockContext(t.controller) handler := selection.NewHandler(nil, nil, t.logger) - expectedErr := apperror.BadRequestError("invalid argument") - - context.EXPECT().Bind(t.UpdateSelectionReq).Return(expectedErr) - context.EXPECT().BadRequestError(expectedErr.Error()) + context.EXPECT().Bind(&dto.UpdateSelectionRequest{}).Return(apperror.BadRequest) + context.EXPECT().BadRequestError(apperror.BadRequest.Error()) handler.UpdateSelection(context) } -func (t *SelectionHandlerTest) TestUpdateSelectionInternalError() { +func (t *SelectionHandlerTest) TestUpdateSelectionServiceError() { selectionSvc := selectionMock.NewMockService(t.controller) validator := validatorMock.NewMockDtoValidator(t.controller) context := routerMock.NewMockContext(t.controller) handler := selection.NewHandler(selectionSvc, validator, t.logger) - expectedErr := apperror.InternalServerError("internal error") - - context.EXPECT().Bind(t.UpdateSelectionReq).Return(nil) + context.EXPECT().Bind(&dto.UpdateSelectionRequest{}).SetArg(0, *t.UpdateSelectionReq) validator.EXPECT().Validate(t.UpdateSelectionReq).Return(nil) - selectionSvc.EXPECT().UpdateSelection(t.UpdateSelectionReq).Return(nil, expectedErr) - context.EXPECT().ResponseError(expectedErr) + selectionSvc.EXPECT().UpdateSelection(t.UpdateSelectionReq).Return(nil, apperror.InternalServer) + context.EXPECT().ResponseError(apperror.InternalServer) handler.UpdateSelection(context) }