Skip to content

Commit

Permalink
chore chore
Browse files Browse the repository at this point in the history
  • Loading branch information
macgeargear committed Jun 23, 2024
1 parent b10412e commit adc1149
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 79 deletions.
70 changes: 33 additions & 37 deletions internal/selection/test/selection.handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
69 changes: 27 additions & 42 deletions internal/selection/test/selection.service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,107 +68,97 @@ 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)
t.Equal(expected, actual)
}

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)
t.Equal(expected, err)
}

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)
t.Equal(expected, err)
}

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)
t.Equal(expected, actual)
}

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)
t.Equal(expected, err)
}

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)
Expand All @@ -177,52 +167,47 @@ 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)
t.Equal(expected, actual)
}

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)
t.Equal(expected, err)
}

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)
Expand Down

0 comments on commit adc1149

Please sign in to comment.