Skip to content

Commit

Permalink
✅ Test: 버그 수정 및 랭킹 테스트코드 작성 (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
win-luck committed Oct 30, 2023
1 parent 8c48314 commit 113074b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/main/java/com/diareat/diareat/food/service/FoodService.java
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,10 @@ public List<ResponseRankUserDto> getUserRankByWeek(Long userId) {
}

// 팔로우한 유저들의 점수를 계산하여 rankUserDtos에 추가
rankUserDtos.forEach(rankUserDto ->
users.forEach(user ->
rankUserDtos.add(calculateUserScoreThisWeek(user, LocalDate.now().with(DayOfWeek.MONDAY), LocalDate.now()))));
for (User user : users) {
ResponseRankUserDto userDto = calculateUserScoreThisWeek(user, LocalDate.now().with(DayOfWeek.MONDAY), LocalDate.now());
rankUserDtos.add(userDto);
}

// 식습관 총점 기준 내림차순 정렬
rankUserDtos.sort(Comparator.comparing(ResponseRankUserDto::getTotalScore).reversed());
Expand Down Expand Up @@ -297,12 +298,12 @@ private HashMap<LocalDate, List<BaseNutrition>> getNutritionSumByDateMap(Long us
private double calculateNutriRatioAndScore(double total, double standard, int type) {
double ratio = Math.round(((total / standard) * 100.0) * 10.0) / 10.0;
if (type == 0) { // 단백질
if (ratio <= 100.0) return ratio;
if (ratio < 100.0) return ratio;
else if (ratio <= 150) return 100;
else return (-2 * ratio + 400 < 0) ? 0 : (-2 * ratio + 400);
} else { // 칼탄지
double gradient = 1.11; // (9분의 10)
if (ratio <= 90.0) return ratio * gradient;
if (ratio < 90.0) return ratio * gradient;
else if (ratio <= 110) return 100;
else return (-gradient * (ratio - 200) < 0) ? 0 : (-gradient * (ratio - 200));
}
Expand Down
46 changes: 46 additions & 0 deletions src/test/java/com/diareat/diareat/service/FoodServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import com.diareat.diareat.food.service.FoodService;
import com.diareat.diareat.user.domain.BaseNutrition;
import com.diareat.diareat.user.domain.User;
import com.diareat.diareat.user.dto.ResponseRankUserDto;
import com.diareat.diareat.user.repository.FollowRepository;
import com.diareat.diareat.user.repository.UserRepository;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand All @@ -23,6 +25,7 @@

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
Expand All @@ -43,6 +46,9 @@ class FoodServiceTest {
@Mock
UserRepository userRepository;

@Mock
FollowRepository followRepository;

@DisplayName("음식 정보 저장")
@Test
void testSaveAndGetFood() { // 음식 정보 저장 및 해당 날짜 음식 리스트 불러오기
Expand Down Expand Up @@ -309,4 +315,44 @@ void getWorst3FoodsTest() {
assertEquals("Food4", top3Foods.get(1).getName());
assertEquals("Food5", top3Foods.get(2).getName());
}

@Test
void getUserRankByWeek() {
// given
User user1 = User.createUser("testUser1", "testImage","testPassword", 180, 80, 0, 18, BaseNutrition.createNutrition(1000,100,100,100));
User user2 = User.createUser("testUser2", "testImage","testPassword", 180, 80, 0, 18, BaseNutrition.createNutrition(1000,100,100,100));
user1.setId(1L);
user2.setId(2L);

Food food1 = Food.createFood( "Food1", user1, BaseNutrition.createNutrition(1000, 100 ,100, 100));
Food food2 = Food.createFood( "Food2", user2, BaseNutrition.createNutrition(2000, 110 ,50, 90));

given(userRepository.findById(user1.getId())).willReturn(Optional.of(user1));
given(followRepository.findAllByFromUser(user1.getId())).willReturn(List.of(user2));
given(foodRepository.findAllByUserIdAndDateBetween(eq(1L), any(LocalDate.class), any(LocalDate.class))).willReturn(List.of(food1));
given(foodRepository.findAllByUserIdAndDateBetween(eq(2L), any(LocalDate.class), any(LocalDate.class))).willReturn(List.of(food2));

// when
List<ResponseRankUserDto> response = foodService.getUserRankByWeek(user1.getId());

// then
assertEquals(2, response.size());
assertEquals("testUser1", response.get(0).getName());
assertEquals("testUser2", response.get(1).getName());
assertEquals(100, response.get(0).getCalorieScore());
assertEquals(100, response.get(0).getCarbohydrateScore());
assertEquals(100, response.get(0).getProteinScore());
assertEquals(100, response.get(0).getFatScore());
assertEquals(400, response.get(0).getTotalScore());

assertEquals(0, response.get(1).getCalorieScore());
assertEquals(100, response.get(1).getCarbohydrateScore());
assertEquals(50, response.get(1).getProteinScore());
assertEquals(100, response.get(1).getFatScore());
assertEquals(250, response.get(1).getTotalScore());
verify(userRepository, times(1)).findById(user1.getId());
verify(followRepository, times(1)).findAllByFromUser(user1.getId());
verify(foodRepository, times(1)).findAllByUserIdAndDateBetween(eq(1L), any(LocalDate.class), any(LocalDate.class));
verify(foodRepository, times(1)).findAllByUserIdAndDateBetween(eq(2L), any(LocalDate.class), any(LocalDate.class));
}
}

0 comments on commit 113074b

Please sign in to comment.