From 545101bd39f2a66c24ea8ae65fb855ec2269b0c5 Mon Sep 17 00:00:00 2001 From: Yehyeok Bang Date: Mon, 29 Jan 2024 20:30:42 +0900 Subject: [PATCH] =?UTF-8?q?[#30]=20test:=20fcm=20=ED=86=A0=ED=81=B0=20?= =?UTF-8?q?=EC=88=98=EC=A0=95=20=EA=B8=B0=EB=8A=A5=20=EA=B4=80=EB=A0=A8=20?= =?UTF-8?q?=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BD=94=EB=93=9C=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/user/services/user_service_test.go | 117 +++++++++++++++++++++++++ 1 file changed, 117 insertions(+) diff --git a/app/user/services/user_service_test.go b/app/user/services/user_service_test.go index 29cf7bb..eea2353 100644 --- a/app/user/services/user_service_test.go +++ b/app/user/services/user_service_test.go @@ -97,6 +97,123 @@ func TestUserService_Login(t *testing.T) { assert.NotEmpty(t, responseToken.Token) } +func TestUserService_UpdateFcmToken(t *testing.T) { + // Mock UserRepository and UserUtil + mockRepo := new(MockUserRepository) + mockUtil := new(MockUserUtil) + + // Set up sample user for the test + input := types.RequestUpdateFcmToken{ + FcmToken: "new_token", + } + + // Set up expectations + currentUser := &models.User{ + ID: 1, + Name: "test", + Nickname: "test", + Email: "test@gmail.com", + Age: 25, + Gender: "male", + FcmToken: "old_token", + } + + updatedUser := &models.User{ + ID: 1, + Name: "test", + Nickname: "test", + Email: "test@gmail.com", + Age: 25, + Gender: "male", + FcmToken: "new_token", + } + + // Set up expectations for the mock util + mockUtil.On("FindCurrentUser", mock.AnythingOfType("*gin.Context")).Return(currentUser, nil) + + // Set up expectations for the mock repository + mockRepo.On("Update", mock.AnythingOfType("*models.User")).Return(*updatedUser, nil) + + // Create UserService with the mock repository and util + userService := services.NewUserService(mockRepo, mockUtil) + + // Create a test context + ctx, _ := gin.CreateTestContext(nil) + + // Call the method under test + err := userService.UpdateFcmToken(ctx, input) + + // Assert that the expectations were met + mockUtil.AssertExpectations(t) + mockRepo.AssertExpectations(t) + + // Check the results + assert.Nil(t, err) +} + +func TestUserService_UpdateFcmToken_NoUser(t *testing.T) { + // Mock UserRepository + mockRepo := new(MockUserRepository) + mockUtil := new(MockUserUtil) + + // Set up expectations for the mock util + mockUtil.On("FindCurrentUser", mock.AnythingOfType("*gin.Context")).Return((*models.User)(nil), errors.New("user not found")) + + // Create UserService with the mock repository and util + userService := services.NewUserService(mockRepo, mockUtil) + + // Create a test context + ctx, _ := gin.CreateTestContext(nil) + + // Set up sample user for the test + input := types.RequestUpdateFcmToken{ + FcmToken: "new_token", + } + + // Call the method under test + err := userService.UpdateFcmToken(ctx, input) + + // Assert that the expectations were met + mockUtil.AssertExpectations(t) + + // Check the results + assert.NotNil(t, err) + assert.EqualError(t, err, "user not found") +} + +func TestUserService_UpdateFcmToken_Error(t *testing.T) { + // Mock UserRepository + mockRepo := new(MockUserRepository) + mockUtil := new(MockUserUtil) + + // Set up expectations for the mock util + mockUtil.On("FindCurrentUser", mock.AnythingOfType("*gin.Context")).Return(&models.User{}, nil) + + // Set up sample user for the test + input := types.RequestUpdateFcmToken{ + FcmToken: "new_token", + } + + // Set up expectations for the mock repository + mockRepo.On("Update", mock.AnythingOfType("*models.User")).Return(models.User{}, errors.New("record not found")) + + // Create UserService with the mock repository and util + userService := services.NewUserService(mockRepo, mockUtil) + + // Create a test context + ctx, _ := gin.CreateTestContext(nil) + + // Call the method under test + err := userService.UpdateFcmToken(ctx, input) + + // Assert that the expectations were met + mockUtil.AssertExpectations(t) + mockRepo.AssertExpectations(t) + + // Check the results + assert.NotNil(t, err) +} + func TestUserService_GetUserInfo(t *testing.T) { // Mock UserRepository mockRepo := new(MockUserRepository)