Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:go-park-mail-ru/2023_2_Hamster i…
Browse files Browse the repository at this point in the history
…nto deploy
  • Loading branch information
CodeMaster482 committed Dec 27, 2023
2 parents d6119f2 + 5614a01 commit a68d751
Show file tree
Hide file tree
Showing 8 changed files with 1,652 additions and 1,617 deletions.
31 changes: 27 additions & 4 deletions internal/microservices/IOhub/delivery/http/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ func (h *Handler) ImportTransactions(w http.ResponseWriter, r *http.Request) {
ShowIncome: gtag.ShowIncome,
ShowOutcome: gtag.ShowOutcome,
Regular: gtag.Regular,
Image: gtag.Image,
}
tags[i] = tag
}
Expand All @@ -237,12 +238,13 @@ func (h *Handler) ImportTransactions(w http.ResponseWriter, r *http.Request) {
tagCache := sync.Map{}
if len(tags) != 0 {
for _, tag := range tags {
accountCache.Store(tag.Name, tag.ID)
tagCache.Store(tag.Name, tag.ID)
}
}

var i int
// Iterate through the records
for {
for i = 0; true; i++ {
// Read each record from csv
record, err := reader.Read()
if err == io.EOF {
Expand All @@ -253,18 +255,33 @@ func (h *Handler) ImportTransactions(w http.ResponseWriter, r *http.Request) {
return
}

if len(record) < 7 {
response.ErrorResponse(w, http.StatusBadRequest, err, "wrong format not enough args for transaction", h.logger)
return
}

if len(record) > 8 {
response.ErrorResponse(w, http.StatusBadRequest, err, "wrong format too much args for transaction", h.logger)
return
}

accountIncome := record[0]
accountOutcome := record[1]

income, err := strconv.ParseFloat(record[2], 64)
if err != nil {
response.ErrorResponse(w, http.StatusBadRequest, err, "Error converting the amount to float", h.logger)
response.ErrorResponse(w, http.StatusBadRequest, err, "Error converting the income amount to float", h.logger)
return
}

outcome, err := strconv.ParseFloat(record[3], 64)
if err != nil {
response.ErrorResponse(w, http.StatusBadRequest, err, "Error converting the amount to float", h.logger)
response.ErrorResponse(w, http.StatusBadRequest, err, "Error converting the outcome amount to float", h.logger)
return
}

if (income == 0) == (outcome == 0) {
response.ErrorResponse(w, http.StatusBadRequest, err, "transaction can be consumables or profitable", h.logger)
return
}

Expand Down Expand Up @@ -324,6 +341,7 @@ func (h *Handler) ImportTransactions(w http.ResponseWriter, r *http.Request) {
tag, err := h.tagService.CreateTag(r.Context(), &generated.CreateTagRequest{
UserId: user.ID.String(),
Name: tagName,
Image: 0,
ShowIncome: false,
Regular: false,
ShowOutcome: true,
Expand Down Expand Up @@ -362,5 +380,10 @@ func (h *Handler) ImportTransactions(w http.ResponseWriter, r *http.Request) {
}
}

if i == 0 {
response.ErrorResponse(w, http.StatusBadRequest, err, "empty file", h.logger)
return
}

response.SuccessResponse(w, http.StatusOK, "Successfully imported transactions")
}
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 a68d751

Please sign in to comment.