Skip to content

Commit

Permalink
start test controller
Browse files Browse the repository at this point in the history
  • Loading branch information
Qaleka committed Dec 17, 2024
1 parent a07e302 commit 6d1b47a
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 2 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ require (
github.com/prometheus/procfs v0.15.1 // indirect
github.com/rogpeppe/go-internal v1.13.1 // indirect
github.com/rs/xid v1.6.0 // indirect
github.com/stretchr/objx v0.5.2 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/net v0.30.0 // indirect
golang.org/x/sync v0.8.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWN
github.com/rs/xid v1.6.0 h1:fV591PaemRlL6JfRxGDEPl69wICngIQ3shQtzfy2gxU=
github.com/rs/xid v1.6.0/go.mod h1:7XoLgs4eV+QndskICGsho+ADou8ySMSjJKDIan90Nz0=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
Expand Down
67 changes: 67 additions & 0 deletions internal/ads/controller/ads_controlle_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package controller

//import (
// "2024_2_FIGHT-CLUB/domain"
// "2024_2_FIGHT-CLUB/microservices/ads_service/controller/gen"
// "2024_2_FIGHT-CLUB/microservices/ads_service/mocks"
// "github.com/stretchr/testify/assert"
// "github.com/stretchr/testify/mock"
// "net/http"
// "net/http/httptest"
// "testing"
//)
//
//func TestAdHandler_GetAllPlaces_Success(t *testing.T) {
// // Мокаем зависимые сервисы
// mockGrpcClient := &mocks.MockGrpcClient{}
// mockSessionService := &mocks.MockServiceSession{}
//
// // Создаем экземпляр AdHandler
// handler := &AdHandler{
// client: mockGrpcClient,
// sessionService: mockSessionService,
// }
//
// // Создаем фейковый запрос
// req, err := http.NewRequest("GET", "/ads?location=NewYork&rating=5", nil)
// if err != nil {
// t.Fatalf("could not create request: %v", err)
// }
//
// // Мокаем поведение для GetSessionId
// mockSessionService.On("GetSessionId", req).Return("mockSessionID", nil)
//
// // Мокаем запрос к gRPC API
// expectedResponse := &gen.GetAllAdsResponseList{
// Housing: []*gen.GetAllAdsResponse{
// {
// Id: "1234", CityId: 1, AuthorUUID: "user123", Address: "Test Address", PublicationDate: "2024-12-17",
// },
// },
// }
//
// mockGrpcClient.On("GetAllPlaces", mock.Anything, mock.AnythingOfType("*gen.AdFilterRequest")).Return(expectedResponse, nil)
//
// // Мокаем функцию конвертации Proto в Go
// mockConvertService.On("ConvertGetAllAdsResponseProtoToGo", expectedResponse).Return([]domain.GetAllAdsResponse{
// {UUID: "1234", CityID: 1, AuthorUUID: "user123", Address: "Test Address", PublicationDate: "2024-12-17"},
// }, nil)
//
// // Создаем тестовый HTTP-респондер
// rr := httptest.NewRecorder()
//
// // Вызываем метод
// handler.GetAllPlaces(rr, req)
//
// // Проверяем статус код ответа
// assert.Equal(t, http.StatusOK, rr.Code)
//
// // Проверяем, что тело ответа соответствует ожидаемому
// expectedBody := `[{"UUID":"1234","CityID":1,"AuthorUUID":"user123","Address":"Test Address","PublicationDate":"2024-12-17"}]`
// assert.JSONEq(t, expectedBody, rr.Body.String())
//
// // Проверяем, что все ожидаемые вызовы были выполнены
// mockGrpcClient.AssertExpectations(t)
// mockSessionService.AssertExpectations(t)
// mockConvertService.AssertExpectations(t)
//}
71 changes: 69 additions & 2 deletions microservices/ads_service/mocks/mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ package mocks
import (
"2024_2_FIGHT-CLUB/domain"
"2024_2_FIGHT-CLUB/internal/service/middleware"
"2024_2_FIGHT-CLUB/microservices/ads_service/controller/gen"
"context"
"github.com/golang-jwt/jwt"
"github.com/gorilla/sessions"
"github.com/stretchr/testify/mock"
"google.golang.org/grpc"
"net/http"
)

Expand All @@ -31,7 +34,7 @@ type MockServiceSession struct {
MockGetUserID func(ctx context.Context, sessionID string) (string, error)
MockLogoutSession func(ctx context.Context, sessionID string) error
MockCreateSession func(ctx context.Context, user *domain.User) (string, error)
MockGetSessionData func(ctx context.Context, sessionID string) (*map[string]interface{}, error)
MockGetSessionData func(ctx context.Context, sessionID string) (*domain.SessionData, error)
MockGetSession func(r *http.Request) (*sessions.Session, error)
}

Expand All @@ -47,7 +50,7 @@ func (m *MockServiceSession) CreateSession(ctx context.Context, user *domain.Use
return m.MockCreateSession(ctx, user)
}

func (m *MockServiceSession) GetSessionData(ctx context.Context, sessionID string) (*map[string]interface{}, error) {
func (m *MockServiceSession) GetSessionData(ctx context.Context, sessionID string) (*domain.SessionData, error) {
return m.MockGetSessionData(ctx, sessionID)
}

Expand Down Expand Up @@ -228,3 +231,67 @@ func (m *MockMinioService) UploadFile(file []byte, contentType, id string) (stri
func (m *MockMinioService) DeleteFile(filePath string) error {
return m.DeleteFileFunc(filePath)
}

type MockGrpcClient struct {
mock.Mock
}

func (m *MockGrpcClient) GetAllPlaces(ctx context.Context, in *gen.AdFilterRequest, opts ...grpc.CallOption) (*gen.GetAllAdsResponseList, error) {
args := m.Called(ctx, in, opts)
return args.Get(0).(*gen.GetAllAdsResponseList), args.Error(1)
}

func (m *MockGrpcClient) GetOnePlace(ctx context.Context, in *gen.GetPlaceByIdRequest, opts ...grpc.CallOption) (*gen.GetAllAdsResponse, error) {
args := m.Called(ctx, in, opts)
return args.Get(0).(*gen.GetAllAdsResponse), args.Error(1)
}

func (m *MockGrpcClient) CreatePlace(ctx context.Context, in *gen.CreateAdRequest, opts ...grpc.CallOption) (*gen.Ad, error) {
args := m.Called(ctx, in, opts)
return args.Get(0).(*gen.Ad), args.Error(1)
}

func (m *MockGrpcClient) UpdatePlace(ctx context.Context, in *gen.UpdateAdRequest, opts ...grpc.CallOption) (*gen.AdResponse, error) {
args := m.Called(ctx, in, opts)
return args.Get(0).(*gen.AdResponse), args.Error(1)
}

func (m *MockGrpcClient) DeletePlace(ctx context.Context, in *gen.DeletePlaceRequest, opts ...grpc.CallOption) (*gen.DeleteResponse, error) {
args := m.Called(ctx, in, opts)
return args.Get(0).(*gen.DeleteResponse), args.Error(1)
}

func (m *MockGrpcClient) GetPlacesPerCity(ctx context.Context, in *gen.GetPlacesPerCityRequest, opts ...grpc.CallOption) (*gen.GetAllAdsResponseList, error) {
args := m.Called(ctx, in, opts)
return args.Get(0).(*gen.GetAllAdsResponseList), args.Error(1)
}

func (m *MockGrpcClient) GetUserPlaces(ctx context.Context, in *gen.GetUserPlacesRequest, opts ...grpc.CallOption) (*gen.GetAllAdsResponseList, error) {
args := m.Called(ctx, in, opts)
return args.Get(0).(*gen.GetAllAdsResponseList), args.Error(1)
}

func (m *MockGrpcClient) DeleteAdImage(ctx context.Context, in *gen.DeleteAdImageRequest, opts ...grpc.CallOption) (*gen.DeleteResponse, error) {
args := m.Called(ctx, in, opts)
return args.Get(0).(*gen.DeleteResponse), args.Error(1)
}

func (m *MockGrpcClient) AddToFavorites(ctx context.Context, in *gen.AddToFavoritesRequest, opts ...grpc.CallOption) (*gen.AdResponse, error) {
args := m.Called(ctx, in, opts)
return args.Get(0).(*gen.AdResponse), args.Error(1)
}

func (m *MockGrpcClient) DeleteFromFavorites(ctx context.Context, in *gen.DeleteFromFavoritesRequest, opts ...grpc.CallOption) (*gen.AdResponse, error) {
args := m.Called(ctx, in, opts)
return args.Get(0).(*gen.AdResponse), args.Error(1)
}

func (m *MockGrpcClient) GetUserFavorites(ctx context.Context, in *gen.GetUserFavoritesRequest, opts ...grpc.CallOption) (*gen.GetAllAdsResponseList, error) {
args := m.Called(ctx, in, opts)
return args.Get(0).(*gen.GetAllAdsResponseList), args.Error(1)
}

func (m *MockGrpcClient) UpdatePriority(ctx context.Context, in *gen.UpdatePriorityRequest, opts ...grpc.CallOption) (*gen.AdResponse, error) {
args := m.Called(ctx, in, opts)
return args.Get(0).(*gen.AdResponse), args.Error(1)
}

0 comments on commit 6d1b47a

Please sign in to comment.