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)