Skip to content

Commit

Permalink
tags test fixx
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeMaster482 committed Dec 27, 2023
1 parent f2b1347 commit 280e9fb
Show file tree
Hide file tree
Showing 4 changed files with 601 additions and 609 deletions.
263 changes: 132 additions & 131 deletions internal/microservices/category/delivery/grpc/handlers_test.go
Original file line number Diff line number Diff line change
@@ -1,165 +1,166 @@
package grpc

// import (
// "context"
// "errors"
// "github.com/go-park-mail-ru/2023_2_Hamster/internal/common/logger"
// proto "github.com/go-park-mail-ru/2023_2_Hamster/internal/microservices/category/delivery/grpc/generated"
// mocks "github.com/go-park-mail-ru/2023_2_Hamster/internal/microservices/category/mocks"
// "github.com/go-park-mail-ru/2023_2_Hamster/internal/models"
// "github.com/golang/mock/gomock"
// "github.com/golang/protobuf/ptypes/empty"
// "github.com/google/uuid"
// "github.com/stretchr/testify/assert"
// "testing"
// )
import (
"context"
"errors"
"testing"

// func TestCreateTag(t *testing.T) {
// ctrl := gomock.NewController(t)
// defer ctrl.Finish()

// expectedTagID := uuid.New()

// request := &proto.CreateTagRequest{
// UserId: expectedTagID.String(),
// ParentId: expectedTagID.String(),
// Name: "TestTag",
// ShowIncome: true,
// ShowOutcome: true,
// Regular: false,
// }
"github.com/go-park-mail-ru/2023_2_Hamster/internal/common/logger"
proto "github.com/go-park-mail-ru/2023_2_Hamster/internal/microservices/category/delivery/grpc/generated"
mocks "github.com/go-park-mail-ru/2023_2_Hamster/internal/microservices/category/mocks"
"github.com/go-park-mail-ru/2023_2_Hamster/internal/models"
"github.com/golang/mock/gomock"
"github.com/golang/protobuf/ptypes/empty"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
)

// mockCategoryServices := mocks.NewMockUsecase(ctrl)
// mockCategoryServices.EXPECT().
// CreateTag(gomock.Any(), gomock.Any()).
// Return(expectedTagID, nil)

// categoryGRPC := NewCategoryGRPC(mockCategoryServices, *logger.NewLogger(context.TODO()))

// response, err := categoryGRPC.CreateTag(context.Background(), request)

// assert.NoError(t, err)
// assert.NotNil(t, response)
// assert.Equal(t, expectedTagID.String(), response.TagId)
// }
func TestCreateTag(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

// func TestGetTags(t *testing.T) {
// ctrl := gomock.NewController(t)
// defer ctrl.Finish()

// expectedUserID := uuid.New()

// request := &proto.UserIdRequest{
// UserId: expectedUserID.String(),
// }

// expectedTags := []models.Category{{}, {}}
expectedTagID := uuid.New()

// mockCategoryServices := mocks.NewMockUsecase(ctrl)
// mockCategoryServices.EXPECT().
// GetTags(gomock.Any(), gomock.Any()).
// Return(expectedTags, nil)
request := &proto.CreateTagRequest{
UserId: expectedTagID.String(),
ParentId: expectedTagID.String(),
Name: "TestTag",
ShowIncome: true,
ShowOutcome: true,
Regular: false,
}

// categoryGRPC := NewCategoryGRPC(mockCategoryServices, *logger.NewLogger(context.TODO()))
mockCategoryServices := mocks.NewMockUsecase(ctrl)
mockCategoryServices.EXPECT().
CreateTag(gomock.Any(), gomock.Any()).
Return(expectedTagID, nil)

// response, err := categoryGRPC.GetTags(context.Background(), request)
categoryGRPC := NewCategoryGRPC(mockCategoryServices, *logger.NewLogger(context.TODO()))

// assert.NoError(t, err)
// assert.NotNil(t, response)
// assert.Len(t, response.Categories, len(expectedTags))
response, err := categoryGRPC.CreateTag(context.Background(), request)

// }
assert.NoError(t, err)
assert.NotNil(t, response)
assert.Equal(t, expectedTagID.String(), response.TagId)
}

// func TestGetTags2(t *testing.T) {
// ctrl := gomock.NewController(t)
// defer ctrl.Finish()
func TestGetTags(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

expectedUserID := uuid.New()

// expectedUserID := uuid.New()
request := &proto.UserIdRequest{
UserId: expectedUserID.String(),
}

// request := &proto.UserIdRequest{
// UserId: expectedUserID.String(),
// }
expectedTags := []models.Category{{}, {}}

// expectedError := errors.New("some error message")
mockCategoryServices := mocks.NewMockUsecase(ctrl)
mockCategoryServices.EXPECT().
GetTags(gomock.Any(), gomock.Any()).
Return(expectedTags, nil)

// mockCategoryServices := mocks.NewMockUsecase(ctrl)
// mockCategoryServices.EXPECT().
// GetTags(gomock.Any(), gomock.Any()).
// Return(nil, expectedError)
categoryGRPC := NewCategoryGRPC(mockCategoryServices, *logger.NewLogger(context.TODO()))

// categoryGRPC := NewCategoryGRPC(mockCategoryServices, *logger.NewLogger(context.TODO()))
response, err := categoryGRPC.GetTags(context.Background(), request)

// response, err := categoryGRPC.GetTags(context.Background(), request)
assert.NoError(t, err)
assert.NotNil(t, response)
assert.Len(t, response.Categories, len(expectedTags))

// assert.Error(t, err)
// assert.Nil(t, response)
// assert.Equal(t, expectedError, err)
// }
}

// func TestUpdateTag(t *testing.T) {
// ctrl := gomock.NewController(t)
// defer ctrl.Finish()
func TestGetTags2(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

// expectedID := uuid.New()
// expectedUserID := uuid.New()
// expectedParentID := uuid.New()
expectedUserID := uuid.New()

// request := &proto.Category{
// Id: expectedID.String(),
// UserId: expectedUserID.String(),
// ParentId: expectedParentID.String(),
// Name: "UpdatedTag",
// ShowIncome: true,
// ShowOutcome: false,
// Regular: true,
// }
request := &proto.UserIdRequest{
UserId: expectedUserID.String(),
}

// mockCategoryServices := mocks.NewMockUsecase(ctrl)
// mockCategoryServices.EXPECT().
// UpdateTag(gomock.Any(), gomock.Any()).
// Return(nil) // Предполагаем, что обновление прошло успешно.
expectedError := errors.New("some error message")

// categoryGRPC := NewCategoryGRPC(mockCategoryServices, *logger.NewLogger(context.TODO()))
mockCategoryServices := mocks.NewMockUsecase(ctrl)
mockCategoryServices.EXPECT().
GetTags(gomock.Any(), gomock.Any()).
Return(nil, expectedError)

// response, err := categoryGRPC.UpdateTag(context.Background(), request)
categoryGRPC := NewCategoryGRPC(mockCategoryServices, *logger.NewLogger(context.TODO()))

// assert.NoError(t, err)
// assert.NotNil(t, response)
response, err := categoryGRPC.GetTags(context.Background(), request)

// // Проверим, что возвращенные данные соответствуют ожидаемым.
// assert.Equal(t, expectedID.String(), response.Id)
// assert.Equal(t, expectedUserID.String(), response.UserId)
// assert.Equal(t, expectedParentID.String(), response.ParentId)
// assert.Equal(t, request.Name, response.Name)
// assert.Equal(t, request.ShowIncome, response.ShowIncome)
// assert.Equal(t, request.ShowOutcome, response.ShowOutcome)
// assert.Equal(t, request.Regular, response.Regular)
// }
assert.Error(t, err)
assert.Nil(t, response)
assert.Equal(t, expectedError, err)
}

// func TestDeleteTag(t *testing.T) {
// ctrl := gomock.NewController(t)
// defer ctrl.Finish()
func TestUpdateTag(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

// expectedTagID := uuid.New()
// expectedUserID := uuid.New()
expectedID := uuid.New()
expectedUserID := uuid.New()
expectedParentID := uuid.New()

// request := &proto.DeleteRequest{
// TagId: expectedTagID.String(),
// UserId: expectedUserID.String(),
// }
request := &proto.Category{
Id: expectedID.String(),
UserId: expectedUserID.String(),
ParentId: expectedParentID.String(),
Name: "UpdatedTag",
ShowIncome: true,
ShowOutcome: false,
Regular: true,
}

// mockCategoryServices := mocks.NewMockUsecase(ctrl)
// mockCategoryServices.EXPECT().
// DeleteTag(gomock.Any(), expectedTagID, expectedUserID).
// Return(nil) // Предполагаем, что удаление прошло успешно.
mockCategoryServices := mocks.NewMockUsecase(ctrl)
mockCategoryServices.EXPECT().
UpdateTag(gomock.Any(), gomock.Any()).
Return(nil) // Предполагаем, что обновление прошло успешно.

// categoryGRPC := NewCategoryGRPC(mockCategoryServices, *logger.NewLogger(context.TODO()))
categoryGRPC := NewCategoryGRPC(mockCategoryServices, *logger.NewLogger(context.TODO()))

// response, err := categoryGRPC.DeleteTag(context.Background(), request)
response, err := categoryGRPC.UpdateTag(context.Background(), request)

// assert.NoError(t, err)
// assert.NotNil(t, response)
assert.NoError(t, err)
assert.NotNil(t, response)

// // Проверим, что возвращенные данные соответствуют ожидаемым.
// assert.Equal(t, &empty.Empty{}, response)
// }
// Проверим, что возвращенные данные соответствуют ожидаемым.
assert.Equal(t, expectedID.String(), response.Id)
assert.Equal(t, expectedUserID.String(), response.UserId)
assert.Equal(t, expectedParentID.String(), response.ParentId)
assert.Equal(t, request.Name, response.Name)
assert.Equal(t, request.ShowIncome, response.ShowIncome)
assert.Equal(t, request.ShowOutcome, response.ShowOutcome)
assert.Equal(t, request.Regular, response.Regular)
}

func TestDeleteTag(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

expectedTagID := uuid.New()
expectedUserID := uuid.New()

request := &proto.DeleteRequest{
TagId: expectedTagID.String(),
UserId: expectedUserID.String(),
}

mockCategoryServices := mocks.NewMockUsecase(ctrl)
mockCategoryServices.EXPECT().
DeleteTag(gomock.Any(), expectedTagID, expectedUserID).
Return(nil) // Предполагаем, что удаление прошло успешно.

categoryGRPC := NewCategoryGRPC(mockCategoryServices, *logger.NewLogger(context.TODO()))

response, err := categoryGRPC.DeleteTag(context.Background(), request)

assert.NoError(t, err)
assert.NotNil(t, response)

// Проверим, что возвращенные данные соответствуют ожидаемым.
assert.Equal(t, &empty.Empty{}, response)
}
Loading

0 comments on commit 280e9fb

Please sign in to comment.