diff --git a/internal/service/middleware/middleware_test.go b/internal/service/middleware/middleware_test.go index 97b7d43..a080c53 100644 --- a/internal/service/middleware/middleware_test.go +++ b/internal/service/middleware/middleware_test.go @@ -92,7 +92,7 @@ func TestJwtToken_Validate(t *testing.T) { // Валидация истёкшего токена expiredTime := time.Now().Add(-1 * time.Hour).Unix() - expiredToken, err := jwtService.Create(session, expiredTime) + expiredToken, _ := jwtService.Create(session, expiredTime) _, err = jwtService.Validate(expiredToken, session) assert.Error(t, err) diff --git a/microservices/ads_service/mocks/mocks.go b/microservices/ads_service/mocks/mocks.go index c6db7d8..277e1a9 100644 --- a/microservices/ads_service/mocks/mocks.go +++ b/microservices/ads_service/mocks/mocks.go @@ -99,26 +99,32 @@ func (m *MockAdUseCase) GetUserPlaces(ctx context.Context, userId string) ([]dom } type MockAdRepository struct { - MockGetAllPlaces func(ctx context.Context, filter domain.AdFilter) ([]domain.GetAllAdsResponse, error) - MockGetPlaceById func(ctx context.Context, adId string) (domain.GetAllAdsResponse, error) - MockUpdateViewsCount func(ctx context.Context, ad domain.GetAllAdsResponse) (domain.GetAllAdsResponse, error) - MockCreatePlace func(ctx context.Context, ad *domain.Ad, newAd domain.CreateAdRequest, userId string) error - MockSavePlace func(ctx context.Context, ad *domain.Ad) error - MockUpdatePlace func(ctx context.Context, ad *domain.Ad, adId string, userId string, updatedAd domain.UpdateAdRequest) error - MockDeletePlace func(ctx context.Context, adId string, userId string) error - MockGetPlacesPerCity func(ctx context.Context, city string) ([]domain.GetAllAdsResponse, error) - MockSaveImages func(ctx context.Context, adUUID string, imagePaths []string) error - MockGetAdImages func(ctx context.Context, adId string) ([]string, error) - MockGetUserPlaces func(ctx context.Context, userId string) ([]domain.GetAllAdsResponse, error) - MockDeleteAdImage func(ctx context.Context, adId string, imageId int, userId string) (string, error) + MockGetAllPlaces func(ctx context.Context, filter domain.AdFilter, userId string) ([]domain.GetAllAdsResponse, error) + MockGetPlaceById func(ctx context.Context, adId string) (domain.GetAllAdsResponse, error) + MockUpdateViewsCount func(ctx context.Context, ad domain.GetAllAdsResponse) (domain.GetAllAdsResponse, error) + MockCreatePlace func(ctx context.Context, ad *domain.Ad, newAd domain.CreateAdRequest, userId string) error + MockSavePlace func(ctx context.Context, ad *domain.Ad) error + MockUpdatePlace func(ctx context.Context, ad *domain.Ad, adId string, userId string, updatedAd domain.UpdateAdRequest) error + MockDeletePlace func(ctx context.Context, adId string, userId string) error + MockGetPlacesPerCity func(ctx context.Context, city string) ([]domain.GetAllAdsResponse, error) + MockSaveImages func(ctx context.Context, adUUID string, imagePaths []string) error + MockGetAdImages func(ctx context.Context, adId string) ([]string, error) + MockGetUserPlaces func(ctx context.Context, userId string) ([]domain.GetAllAdsResponse, error) + MockDeleteAdImage func(ctx context.Context, adId string, imageId int, userId string) (string, error) + MockAddToFavorites func(ctx context.Context, adId string, userId string) error + MockDeleteFromFavorites func(ctx context.Context, adId string, userId string) error + MockGetUserFavorites func(ctx context.Context, userId string) ([]domain.GetAllAdsResponse, error) + MockUpdateFavoritesCount func(ctx context.Context, adId string) error + MockUpdatePriority func(ctx context.Context, adId string, userId string, amount int) error + MockResetExpiredPriorities func(ctx context.Context) error } func (m *MockAdRepository) DeleteAdImage(ctx context.Context, adId string, imageId int, userId string) (string, error) { return m.MockDeleteAdImage(ctx, adId, imageId, userId) } -func (m *MockAdRepository) GetAllPlaces(ctx context.Context, filter domain.AdFilter) ([]domain.GetAllAdsResponse, error) { - return m.MockGetAllPlaces(ctx, filter) +func (m *MockAdRepository) GetAllPlaces(ctx context.Context, filter domain.AdFilter, userId string) ([]domain.GetAllAdsResponse, error) { + return m.MockGetAllPlaces(ctx, filter, userId) } func (m *MockAdRepository) GetPlaceById(ctx context.Context, adId string) (domain.GetAllAdsResponse, error) { @@ -161,6 +167,30 @@ func (m *MockAdRepository) GetUserPlaces(ctx context.Context, userId string) ([] return m.MockGetUserPlaces(ctx, userId) } +func (m *MockAdRepository) AddToFavorites(ctx context.Context, adId string, userId string) error { + return m.MockAddToFavorites(ctx, adId, userId) +} + +func (m *MockAdRepository) DeleteFromFavorites(ctx context.Context, adId string, userId string) error { + return m.MockDeleteFromFavorites(ctx, adId, userId) +} + +func (m *MockAdRepository) GetUserFavorites(ctx context.Context, userId string) ([]domain.GetAllAdsResponse, error) { + return m.MockGetUserFavorites(ctx, userId) +} + +func (m *MockAdRepository) UpdateFavoritesCount(ctx context.Context, adId string) error { + return m.MockUpdateFavoritesCount(ctx, adId) +} + +func (m *MockAdRepository) UpdatePriority(ctx context.Context, adId string, userId string, amount int) error { + return m.MockUpdatePriority(ctx, adId, userId, amount) +} + +func (m *MockAdRepository) ResetExpiredPriorities(ctx context.Context) error { + return m.MockResetExpiredPriorities(ctx) +} + type MockMinioService struct { UploadFileFunc func(file []byte, contentType, id string) (string, error) DeleteFileFunc func(filePath string) error diff --git a/microservices/ads_service/repository/ads_repository_test.go b/microservices/ads_service/repository/ads_repository_test.go index 0432efe..4eca2ed 100644 --- a/microservices/ads_service/repository/ads_repository_test.go +++ b/microservices/ads_service/repository/ads_repository_test.go @@ -31,7 +31,12 @@ func TestGetAllPlaces(t *testing.T) { if err := logger.InitLoggers(); err != nil { log.Fatalf("Failed to initialize loggers: %v", err) } - defer logger.SyncLoggers() + defer func() { + err := logger.SyncLoggers() + if err != nil { + return + } + }() db, mock, err := setupDBMock() require.NoError(t, err) @@ -46,6 +51,8 @@ func TestGetAllPlaces(t *testing.T) { GuestCount: "", } + userId := "12345" + // Фиксированная дата для теста fixedDate := time.Date(1, time.January, 1, 0, 0, 0, 0, time.UTC) @@ -75,7 +82,7 @@ func TestGetAllPlaces(t *testing.T) { }).AddRow("author-uuid", "test_username", "some_password", "test@example.com", "Test User", 4.5, "avatar_url", "M", 2, fixedDate) mock.ExpectQuery(regexp.QuoteMeta(userQuery)).WithArgs("author-uuid").WillReturnRows(userRows) - ads, err := repo.GetAllPlaces(context.Background(), filter) + ads, err := repo.GetAllPlaces(context.Background(), filter, userId) require.NoError(t, err) assert.Len(t, ads, 1) @@ -113,10 +120,17 @@ func TestGetAllPlaces_Failure(t *testing.T) { if err := logger.InitLoggers(); err != nil { log.Fatalf("Failed to initialize loggers: %v", err) } - defer logger.SyncLoggers() + defer func() { + err := logger.SyncLoggers() + if err != nil { + return + } + }() db, mock, err := setupDBMock() assert.Nil(t, err) + userId := "12345" + repo := NewAdRepository(db) filter := domain.AdFilter{} query := ` @@ -129,7 +143,7 @@ func TestGetAllPlaces_Failure(t *testing.T) { mock.ExpectQuery(query). WillReturnError(errors.New("db error")) - ads, err := repo.GetAllPlaces(context.Background(), filter) + ads, err := repo.GetAllPlaces(context.Background(), filter, userId) assert.Error(t, err) assert.Nil(t, ads) } @@ -138,7 +152,12 @@ func TestGetPlaceById(t *testing.T) { if err := logger.InitLoggers(); err != nil { log.Fatalf("Failed to initialize loggers: %v", err) } - defer logger.SyncLoggers() + defer func() { + err := logger.SyncLoggers() + if err != nil { + return + } + }() db, mock, err := setupDBMock() assert.Nil(t, err) @@ -209,7 +228,12 @@ func TestGetPlaceById_Failure(t *testing.T) { if err := logger.InitLoggers(); err != nil { log.Fatalf("Failed to initialize loggers: %v", err) } - defer logger.SyncLoggers() + defer func() { + err := logger.SyncLoggers() + if err != nil { + return + } + }() db, mock, err := setupDBMock() assert.Nil(t, err) @@ -228,7 +252,12 @@ func TestUpdatePlace(t *testing.T) { if err := logger.InitLoggers(); err != nil { log.Fatalf("Failed to initialize loggers: %v", err) } - defer logger.SyncLoggers() + defer func() { + err := logger.SyncLoggers() + if err != nil { + return + } + }() db, mock, err := setupDBMock() assert.Nil(t, err) @@ -282,7 +311,12 @@ func TestUpdatePlace_AdNotFound(t *testing.T) { if err := logger.InitLoggers(); err != nil { log.Fatalf("Failed to initialize loggers: %v", err) } - defer logger.SyncLoggers() + defer func() { + err := logger.SyncLoggers() + if err != nil { + return + } + }() db, mock, err := setupDBMock() assert.Nil(t, err) @@ -313,7 +347,12 @@ func TestUpdatePlace_AdDateNotFound(t *testing.T) { if err := logger.InitLoggers(); err != nil { log.Fatalf("Failed to initialize loggers: %v", err) } - defer logger.SyncLoggers() + defer func() { + err := logger.SyncLoggers() + if err != nil { + return + } + }() db, mock, err := setupDBMock() assert.Nil(t, err) @@ -346,7 +385,12 @@ func TestUpdatePlace_UserNotAuthorized(t *testing.T) { if err := logger.InitLoggers(); err != nil { log.Fatalf("Failed to initialize loggers: %v", err) } - defer logger.SyncLoggers() + defer func() { + err := logger.SyncLoggers() + if err != nil { + return + } + }() db, mock, err := setupDBMock() assert.Nil(t, err) @@ -383,7 +427,12 @@ func TestDeletePlace(t *testing.T) { if err := logger.InitLoggers(); err != nil { log.Fatalf("Failed to initialize loggers: %v", err) } - defer logger.SyncLoggers() + defer func() { + err := logger.SyncLoggers() + if err != nil { + return + } + }() db, mock, err := setupDBMock() assert.Nil(t, err) @@ -439,7 +488,12 @@ func TestDeletePlace_Failure(t *testing.T) { if err := logger.InitLoggers(); err != nil { log.Fatalf("Failed to initialize loggers: %v", err) } - defer logger.SyncLoggers() + defer func() { + err := logger.SyncLoggers() + if err != nil { + return + } + }() db, mock, err := setupDBMock() assert.Nil(t, err) @@ -466,7 +520,12 @@ func TestCreatePlace(t *testing.T) { if err := logger.InitLoggers(); err != nil { log.Fatalf("Failed to initialize loggers: %v", err) } - defer logger.SyncLoggers() + defer func() { + err := logger.SyncLoggers() + if err != nil { + return + } + }() db, mock, err := setupDBMock() require.NoError(t, err) @@ -556,7 +615,12 @@ func TestCreatePlace_CityNotFound(t *testing.T) { if err := logger.InitLoggers(); err != nil { log.Fatalf("Failed to initialize loggers: %v", err) } - defer logger.SyncLoggers() + defer func() { + err := logger.SyncLoggers() + if err != nil { + return + } + }() db, mock, err := setupDBMock() assert.Nil(t, err) @@ -606,7 +670,12 @@ func TestCreatePlace_UserNotHost(t *testing.T) { if err := logger.InitLoggers(); err != nil { log.Fatalf("Failed to initialize loggers: %v", err) } - defer logger.SyncLoggers() + defer func() { + err := logger.SyncLoggers() + if err != nil { + return + } + }() db, mock, err := setupDBMock() assert.Nil(t, err) @@ -653,7 +722,12 @@ func TestSavePlace_Success(t *testing.T) { if err := logger.InitLoggers(); err != nil { log.Fatalf("Failed to initialize loggers: %v", err) } - defer logger.SyncLoggers() + defer func() { + err := logger.SyncLoggers() + if err != nil { + return + } + }() db, mock, err := setupDBMock() assert.Nil(t, err) @@ -694,7 +768,12 @@ func TestSavePlace_Failure(t *testing.T) { if err := logger.InitLoggers(); err != nil { log.Fatalf("Failed to initialize loggers: %v", err) } - defer logger.SyncLoggers() + defer func() { + err := logger.SyncLoggers() + if err != nil { + return + } + }() db, mock, err := setupDBMock() assert.Nil(t, err) @@ -735,7 +814,12 @@ func TestGetPlacesPerCity_Success(t *testing.T) { if err := logger.InitLoggers(); err != nil { log.Fatalf("Failed to initialize loggers: %v", err) } - defer logger.SyncLoggers() + defer func() { + err := logger.SyncLoggers() + if err != nil { + return + } + }() db, mock, err := setupDBMock() assert.Nil(t, err) @@ -786,7 +870,12 @@ func TestGetPlacesPerCity_Failure(t *testing.T) { if err := logger.InitLoggers(); err != nil { log.Fatalf("Failed to initialize loggers: %v", err) } - defer logger.SyncLoggers() + defer func() { + err := logger.SyncLoggers() + if err != nil { + return + } + }() db, mock, err := setupDBMock() assert.Nil(t, err) @@ -820,7 +909,12 @@ func TestSaveImages_Success(t *testing.T) { if err := logger.InitLoggers(); err != nil { log.Fatalf("Failed to initialize loggers: %v", err) } - defer logger.SyncLoggers() + defer func() { + err := logger.SyncLoggers() + if err != nil { + return + } + }() db, mock, err := setupDBMock() assert.Nil(t, err) @@ -854,7 +948,12 @@ func TestSaveImages_Failure(t *testing.T) { if err := logger.InitLoggers(); err != nil { log.Fatalf("Failed to initialize loggers: %v", err) } - defer logger.SyncLoggers() + defer func() { + err := logger.SyncLoggers() + if err != nil { + return + } + }() db, mock, err := setupDBMock() assert.Nil(t, err) @@ -893,7 +992,12 @@ func TestGetAdImages_Success(t *testing.T) { if err := logger.InitLoggers(); err != nil { log.Fatalf("Failed to initialize loggers: %v", err) } - defer logger.SyncLoggers() + defer func() { + err := logger.SyncLoggers() + if err != nil { + return + } + }() db, mock, err := setupDBMock() assert.Nil(t, err) @@ -931,7 +1035,12 @@ func TestGetAdImages_Failure(t *testing.T) { if err := logger.InitLoggers(); err != nil { log.Fatalf("Failed to initialize loggers: %v", err) } - defer logger.SyncLoggers() + defer func() { + err := logger.SyncLoggers() + if err != nil { + return + } + }() db, mock, err := setupDBMock() assert.Nil(t, err) @@ -965,7 +1074,12 @@ func TestGetUserPlaces_Success(t *testing.T) { if err := logger.InitLoggers(); err != nil { log.Fatalf("Failed to initialize loggers: %v", err) } - defer logger.SyncLoggers() + defer func() { + err := logger.SyncLoggers() + if err != nil { + return + } + }() db, mock, err := setupDBMock() assert.Nil(t, err) @@ -1020,7 +1134,12 @@ func TestGetUserPlaces_Failure(t *testing.T) { if err := logger.InitLoggers(); err != nil { log.Fatalf("Failed to initialize loggers: %v", err) } - defer logger.SyncLoggers() + defer func() { + err := logger.SyncLoggers() + if err != nil { + return + } + }() db, mock, err := setupDBMock() assert.Nil(t, err) @@ -1067,7 +1186,12 @@ func TestDeleteAdImage(t *testing.T) { if err := logger.InitLoggers(); err != nil { log.Fatalf("Failed to initialize loggers: %v", err) } - defer logger.SyncLoggers() + defer func() { + err := logger.SyncLoggers() + if err != nil { + return + } + }() db, mock, err := setupDBMock() assert.Nil(t, err) diff --git a/microservices/ads_service/usecase/ads_usecase_test.go b/microservices/ads_service/usecase/ads_usecase_test.go index 5e7f961..f8a3864 100644 --- a/microservices/ads_service/usecase/ads_usecase_test.go +++ b/microservices/ads_service/usecase/ads_usecase_test.go @@ -22,13 +22,14 @@ func TestAdUseCase_GetAllPlaces(t *testing.T) { expectedAds := []domain.GetAllAdsResponse{ {UUID: "1234", CityID: 1, AuthorUUID: "user123"}, } - mockRepo.MockGetAllPlaces = func(ctx context.Context, filter domain.AdFilter) ([]domain.GetAllAdsResponse, error) { + mockRepo.MockGetAllPlaces = func(ctx context.Context, filter domain.AdFilter, userId string) ([]domain.GetAllAdsResponse, error) { return expectedAds, nil } ctx := context.Background() filter := domain.AdFilter{Location: "New York"} - ads, err := useCase.GetAllPlaces(ctx, filter) + userId := "user123" + ads, err := useCase.GetAllPlaces(ctx, filter, userId) assert.NoError(t, err) assert.Equal(t, expectedAds, ads) @@ -242,13 +243,14 @@ func TestAdUseCase_GetAllPlaces_Error(t *testing.T) { mockMinioService := &mocks.MockMinioService{} useCase := NewAdUseCase(mockRepo, mockMinioService) - mockRepo.MockGetAllPlaces = func(ctx context.Context, filter domain.AdFilter) ([]domain.GetAllAdsResponse, error) { + mockRepo.MockGetAllPlaces = func(ctx context.Context, filter domain.AdFilter, userId string) ([]domain.GetAllAdsResponse, error) { return nil, errors.New("database error") } ctx := context.Background() filter := domain.AdFilter{Location: "New York"} - ads, err := useCase.GetAllPlaces(ctx, filter) + userId := "user123" + ads, err := useCase.GetAllPlaces(ctx, filter, userId) assert.Error(t, err) assert.Nil(t, ads) @@ -369,6 +371,9 @@ func TestAdUseCase_UpdatePlace_ErrorOnUploadImage(t *testing.T) { mockMinioService := &mocks.MockMinioService{} useCase := NewAdUseCase(mockRepo, mockMinioService) fileHeaders, err := createValidFileHeaders(3) + if err != nil { + return + } adID := "invalid_ad_id" userID := "user456" newAd := domain.Ad{} diff --git a/microservices/auth_service/repository/auth_repository_test.go b/microservices/auth_service/repository/auth_repository_test.go index eda0902..e882142 100644 --- a/microservices/auth_service/repository/auth_repository_test.go +++ b/microservices/auth_service/repository/auth_repository_test.go @@ -33,7 +33,12 @@ func TestCreateUser(t *testing.T) { if err := logger.InitLoggers(); err != nil { log.Fatalf("Failed to initialize loggers: %v", err) } - defer logger.SyncLoggers() + defer func() { + err := logger.SyncLoggers() + if err != nil { + return + } + }() gormDB, mock := setupTestDB(t) defer func() { @@ -120,10 +125,20 @@ func TestCreateUser_Failure(t *testing.T) { if err := logger.InitLoggers(); err != nil { log.Fatalf("Failed to initialize loggers: %v", err) } - defer logger.SyncLoggers() + defer func() { + err := logger.SyncLoggers() + if err != nil { + return + } + }() db, mock := setupTestDB(t) - defer db.DB() + defer func(db *gorm.DB) { + _, err := db.DB() + if err != nil { + return + } + }(db) repo := NewAuthRepository(db) @@ -173,11 +188,21 @@ func TestSaveUser_Success(t *testing.T) { if err := logger.InitLoggers(); err != nil { log.Fatalf("Failed to initialize loggers: %v", err) } - defer logger.SyncLoggers() + defer func() { + err := logger.SyncLoggers() + if err != nil { + return + } + }() // Настройка тестовой базы данных и sqlmock db, mock := setupTestDB(t) - defer db.DB() + defer func(db *gorm.DB) { + _, err := db.DB() + if err != nil { + return + } + }(db) // Создание репозитория repo := NewAuthRepository(db) @@ -229,9 +254,19 @@ func TestSaveUser_Failure(t *testing.T) { if err := logger.InitLoggers(); err != nil { log.Fatalf("Failed to initialize loggers: %v", err) } - defer logger.SyncLoggers() + defer func() { + err := logger.SyncLoggers() + if err != nil { + return + } + }() db, mock := setupTestDB(t) - defer db.DB() + defer func(db *gorm.DB) { + _, err := db.DB() + if err != nil { + return + } + }(db) repo := NewAuthRepository(db) @@ -274,9 +309,19 @@ func TestGetUserById_Success(t *testing.T) { if err := logger.InitLoggers(); err != nil { log.Fatalf("Failed to initialize loggers: %v", err) } - defer logger.SyncLoggers() + defer func() { + err := logger.SyncLoggers() + if err != nil { + return + } + }() db, mock := setupTestDB(t) - defer db.DB() + defer func(db *gorm.DB) { + _, err := db.DB() + if err != nil { + return + } + }(db) repo := NewAuthRepository(db) @@ -297,9 +342,19 @@ func TestGetUserById_NotFound(t *testing.T) { if err := logger.InitLoggers(); err != nil { log.Fatalf("Failed to initialize loggers: %v", err) } - defer logger.SyncLoggers() + defer func() { + err := logger.SyncLoggers() + if err != nil { + return + } + }() db, mock := setupTestDB(t) - defer db.DB() + defer func(db *gorm.DB) { + _, err := db.DB() + if err != nil { + return + } + }(db) repo := NewAuthRepository(db) @@ -316,9 +371,19 @@ func TestGetAllUser_Success(t *testing.T) { if err := logger.InitLoggers(); err != nil { log.Fatalf("Failed to initialize loggers: %v", err) } - defer logger.SyncLoggers() + defer func() { + err := logger.SyncLoggers() + if err != nil { + return + } + }() db, mock := setupTestDB(t) - defer db.DB() + defer func(db *gorm.DB) { + _, err := db.DB() + if err != nil { + return + } + }(db) repo := NewAuthRepository(db) @@ -337,9 +402,19 @@ func TestGetAllUser_Failure(t *testing.T) { if err := logger.InitLoggers(); err != nil { log.Fatalf("Failed to initialize loggers: %v", err) } - defer logger.SyncLoggers() + defer func() { + err := logger.SyncLoggers() + if err != nil { + return + } + }() db, mock := setupTestDB(t) - defer db.DB() + defer func(db *gorm.DB) { + _, err := db.DB() + if err != nil { + return + } + }(db) repo := NewAuthRepository(db) @@ -353,9 +428,19 @@ func TestAuthRepository_GetUserByName(t *testing.T) { if err := logger.InitLoggers(); err != nil { log.Fatalf("Failed to initialize loggers: %v", err) } - defer logger.SyncLoggers() + defer func() { + err := logger.SyncLoggers() + if err != nil { + return + } + }() db, mock := setupTestDB(t) - defer db.DB() + defer func(db *gorm.DB) { + _, err := db.DB() + if err != nil { + return + } + }(db) repo := NewAuthRepository(db) @@ -409,11 +494,21 @@ func TestAuthRepository_PutUser(t *testing.T) { if err := logger.InitLoggers(); err != nil { log.Fatalf("Failed to initialize loggers: %v", err) } - defer logger.SyncLoggers() + defer func() { + err := logger.SyncLoggers() + if err != nil { + return + } + }() // Настраиваем тестовую базу данных и sqlmock db, mock := setupTestDB(t) - defer db.DB() + defer func(db *gorm.DB) { + _, err := db.DB() + if err != nil { + return + } + }(db) repo := NewAuthRepository(db) @@ -500,12 +595,20 @@ func TestGetUserByEmail(t *testing.T) { if err := logger.InitLoggers(); err != nil { log.Fatalf("Failed to initialize loggers: %v", err) } - defer logger.SyncLoggers() + defer func() { + err := logger.SyncLoggers() + if err != nil { + return + } + }() gormDB, mock := setupTestDB(t) defer func() { db, _ := gormDB.DB() - db.Close() + err := db.Close() + if err != nil { + return + } }() authRepo := NewAuthRepository(gormDB) diff --git a/microservices/auth_service/usecase/auth_usecase_test.go b/microservices/auth_service/usecase/auth_usecase_test.go index c73a731..6a3c880 100644 --- a/microservices/auth_service/usecase/auth_usecase_test.go +++ b/microservices/auth_service/usecase/auth_usecase_test.go @@ -52,7 +52,12 @@ func TestRegisterUser(t *testing.T) { if err := logger.InitLoggers(); err != nil { log.Fatalf("Failed to initialize loggers: %v", err) } - defer logger.SyncLoggers() + defer func() { + err := logger.SyncLoggers() + if err != nil { + return + } + }() mockAuthRepo := &mocks.MockAuthRepository{} mockMinioService := &mocks.MockMinioService{} @@ -253,7 +258,12 @@ func TestLoginUser(t *testing.T) { if err := logger.InitLoggers(); err != nil { log.Fatalf("Failed to initialize loggers: %v", err) } - defer logger.SyncLoggers() + defer func() { + err := logger.SyncLoggers() + if err != nil { + return + } + }() mockAuthRepo := &mocks.MockAuthRepository{} uc := NewAuthUseCase(mockAuthRepo, nil) @@ -439,7 +449,12 @@ func TestPutUser(t *testing.T) { if err := logger.InitLoggers(); err != nil { log.Fatalf("Failed to initialize loggers: %v", err) } - defer logger.SyncLoggers() + defer func() { + err := logger.SyncLoggers() + if err != nil { + return + } + }() mockAuthRepo := &mocks.MockAuthRepository{} mockMinioService := &mocks.MockMinioService{} @@ -633,7 +648,12 @@ func TestGetUserById(t *testing.T) { if err := logger.InitLoggers(); err != nil { log.Fatalf("Failed to initialize loggers: %v", err) } - defer logger.SyncLoggers() + defer func() { + err := logger.SyncLoggers() + if err != nil { + return + } + }() mockAuthRepo := &mocks.MockAuthRepository{} uc := NewAuthUseCase(mockAuthRepo, nil) diff --git a/microservices/city_service/repository/cities_repository_test.go b/microservices/city_service/repository/cities_repository_test.go index 31183ba..0e0e3a6 100644 --- a/microservices/city_service/repository/cities_repository_test.go +++ b/microservices/city_service/repository/cities_repository_test.go @@ -4,6 +4,7 @@ import ( "2024_2_FIGHT-CLUB/domain" "2024_2_FIGHT-CLUB/internal/service/logger" "context" + "database/sql" "errors" "github.com/DATA-DOG/go-sqlmock" "github.com/stretchr/testify/assert" @@ -19,12 +20,22 @@ func TestGetCitiesSuccess(t *testing.T) { if err := logger.InitLoggers(); err != nil { log.Fatalf("Failed to initialize loggers: %v", err) } - defer logger.SyncLoggers() + defer func() { + err := logger.SyncLoggers() + if err != nil { + return + } + }() db, mock, err := sqlmock.New() if err != nil { t.Fatalf("an error '%s' was encountered while creating SQLMock database connection", err) } - defer db.Close() + defer func(db *sql.DB) { + err := db.Close() + if err != nil { + return + } + }(db) gormDB, err := gorm.Open(postgres.New(postgres.Config{ Conn: db, @@ -58,12 +69,22 @@ func TestGetCitiesFailure(t *testing.T) { if err := logger.InitLoggers(); err != nil { log.Fatalf("Failed to initialize loggers: %v", err) } - defer logger.SyncLoggers() + defer func() { + err := logger.SyncLoggers() + if err != nil { + return + } + }() db, mock, err := sqlmock.New() if err != nil { t.Fatalf("an error '%s' was encountered while creating SQLMock database connection", err) } - defer db.Close() + defer func(db *sql.DB) { + err := db.Close() + if err != nil { + return + } + }(db) gormDB, err := gorm.Open(postgres.New(postgres.Config{ Conn: db, @@ -93,12 +114,22 @@ func TestGetCityByEnName(t *testing.T) { if err := logger.InitLoggers(); err != nil { log.Fatalf("Failed to initialize loggers: %v", err) } - defer logger.SyncLoggers() + defer func() { + err := logger.SyncLoggers() + if err != nil { + return + } + }() db, mock, err := sqlmock.New() if err != nil { t.Fatalf("an error '%s' was encountered while creating SQLMock database connection", err) } - defer db.Close() + defer func(db *sql.DB) { + err := db.Close() + if err != nil { + return + } + }(db) gormDB, err := gorm.Open(postgres.New(postgres.Config{ Conn: db, diff --git a/microservices/city_service/usecase/cities_usecase_test.go b/microservices/city_service/usecase/cities_usecase_test.go index d32dbb5..242dfdf 100644 --- a/microservices/city_service/usecase/cities_usecase_test.go +++ b/microservices/city_service/usecase/cities_usecase_test.go @@ -17,7 +17,12 @@ func TestGetCitiesSuccess(t *testing.T) { if err := logger.InitLoggers(); err != nil { log.Fatalf("Failed to initialize loggers: %v", err) } - defer logger.SyncLoggers() + defer func() { + err := logger.SyncLoggers() + if err != nil { + return + } + }() mockRepo := &mocks.MockCitiesRepository{ MockGetCities: func(ctx context.Context) ([]domain.City, error) { return []domain.City{ @@ -46,7 +51,12 @@ func TestGetCitiesFailure(t *testing.T) { if err := logger.InitLoggers(); err != nil { log.Fatalf("Failed to initialize loggers: %v", err) } - defer logger.SyncLoggers() + defer func() { + err := logger.SyncLoggers() + if err != nil { + return + } + }() mockRepo := &mocks.MockCitiesRepository{ MockGetCities: func(ctx context.Context) ([]domain.City, error) { return nil, errors.New("failed to retrieve cities")