Skip to content

Commit

Permalink
test/#470 : DailyMeal을 DailyMealFoods로 변경으로 인한 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
hwangdaesun committed Aug 18, 2024
1 parent ae7ec5d commit 6e42ad9
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.time.LocalDateTime;
import java.util.List;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

import com.gaebaljip.exceed.application.domain.meal.Meal;
import com.gaebaljip.exceed.application.domain.meal.DailyMealFoods;
import com.gaebaljip.exceed.application.domain.nutritionist.MonthlyMeal;
import com.gaebaljip.exceed.common.DatabaseTest;
import com.gaebaljip.exceed.common.dto.DailyMealDTO;
Expand All @@ -22,11 +21,9 @@ class MealPersistenceAdapterTest extends DatabaseTest {
@Test
void when_queryForDaily_expected_success() {
LocalDateTime dateTime = LocalDateTime.of(2024, 6, 10, 11, 11);
List<Meal> meals = mealPersistenceAdapter.query(new DailyMealDTO(1L, dateTime));
assertAll(
() -> assertEquals(2, meals.size()),
() -> assertEquals(1, meals.get(0).getConsumedFoods().size()),
() -> assertEquals(1, meals.get(1).getConsumedFoods().size()));
DailyMealFoods dailyMealFoods =
mealPersistenceAdapter.queryDailyMealFoods(new DailyMealDTO(1L, dateTime));
assertAll(() -> assertEquals(2, dailyMealFoods.getMealFoods().size()));
}

@Test
Expand All @@ -51,15 +48,15 @@ void when_queryForMonth_expected_success() {
monthlyMeal
.getMonthlyMeal()
.get(dateTime.toLocalDate())
.getMeals()
.getMealFoods()
.size()),
() ->
assertEquals(
2,
monthlyMeal
.getMonthlyMeal()
.get(plusDateTime.toLocalDate())
.getMeals()
.getMealFoods()
.size()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.gaebaljip.exceed.application.domain.meal;

import static org.junit.jupiter.api.Assertions.*;

import java.time.LocalDate;

import org.junit.jupiter.api.Test;

import com.gaebaljip.exceed.common.exception.meal.NotSameDateException;
import com.gaebaljip.exceed.common.factory.DailyMealFoodsFixtureFactory;

class DailyMealFoodsTest {

@Test
void when_mealSize_0_expected_return0() {
DailyMealFoods dailyMealFoods =
DailyMealFoodsFixtureFactory.create(LocalDate.now(), LocalDate.now(), 0);
double calorie = dailyMealFoods.calculateCurrentCalorie();
assertEquals(calorie, 0.0);
}

@Test
void when_mealSize_over0_and_sameDate_expected_return_success() {
DailyMealFoods dailyMealFoods =
DailyMealFoodsFixtureFactory.create(LocalDate.now(), LocalDate.now(), 3);
assertDoesNotThrow(() -> dailyMealFoods);
}

@Test
void when_mealSize_over0_and_NotSameDate_expected_return_exception() {
assertThrows(
NotSameDateException.class,
() ->
DailyMealFoodsFixtureFactory.create(
LocalDate.now(), LocalDate.now().plusDays(2), 3));
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,24 @@
import static org.junit.jupiter.api.Assertions.*;

import java.time.LocalDate;
import java.util.List;

import org.junit.jupiter.api.Test;

import com.gaebaljip.exceed.application.domain.meal.DailyMeal;
import com.gaebaljip.exceed.application.domain.meal.Meal;
import com.gaebaljip.exceed.application.domain.meal.DailyMealFoods;
import com.gaebaljip.exceed.application.domain.member.Member;
import com.gaebaljip.exceed.common.factory.MealsFixtureFactory;
import com.gaebaljip.exceed.common.factory.DailyMealFoodsFixtureFactory;
import com.gaebaljip.exceed.common.factory.MemberFixtureFactory;

class CalorieDailyMealAnalyzerTest {
class CalorieDailyMealFoodsAnalyzerTest {
@Test
void when_meals_size_0_expected_false() {
List<Meal> meals = MealsFixtureFactory.create(LocalDate.now(), LocalDate.now(), 0);
DailyMealFoods dailyMealFoods =
DailyMealFoodsFixtureFactory.create(LocalDate.now(), LocalDate.now(), 0);
Member member = MemberFixtureFactory.create(1);
DailyCalorieAnalyzerFactory dailyCalorieAnalyzerFactory =
DailyCalorieAnalyzerFactory.getInstance();
DailyCalorieAnalyzer dailyCalorieAnalyzer =
dailyCalorieAnalyzerFactory.createAnalyzer(new DailyMeal(meals), member);
dailyCalorieAnalyzerFactory.createAnalyzer(dailyMealFoods, member);
boolean analyze = dailyCalorieAnalyzer.analyze();
assertEquals(analyze, false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import static org.mockito.BDDMockito.given;

import java.time.LocalDateTime;
import java.util.List;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand All @@ -13,13 +12,13 @@
import org.mockito.junit.jupiter.MockitoExtension;

import com.gaebaljip.exceed.adapter.in.nutritionist.request.GetAllAnalysisRequest;
import com.gaebaljip.exceed.application.domain.meal.Meal;
import com.gaebaljip.exceed.application.domain.meal.DailyMealFoods;
import com.gaebaljip.exceed.application.domain.member.Member;
import com.gaebaljip.exceed.application.port.out.meal.DailyMealPort;
import com.gaebaljip.exceed.application.port.out.member.MemberPort;
import com.gaebaljip.exceed.common.dto.AllAnalysisDTO;
import com.gaebaljip.exceed.common.dto.DailyMealDTO;
import com.gaebaljip.exceed.common.factory.MealsFixtureFactory;
import com.gaebaljip.exceed.common.factory.DailyMealFoodsFixtureFactory;
import com.gaebaljip.exceed.common.factory.MemberFixtureFactory;

@ExtendWith(MockitoExtension.class)
Expand All @@ -31,14 +30,18 @@ class GetAllCalorieAnalysisServiceTest {
@InjectMocks private GetAllCalorieAnalysisService getAllCalorieAnalysisService;

@Test
void when_meal_is_0_expected_isVisited_false_and_allAchieved_false() {
void when_mealFood_is_0_expected_isVisited_false_and_allAchieved_false() {

// given
LocalDateTime now = LocalDateTime.now();
Member member = MemberFixtureFactory.create(1);
GetAllAnalysisRequest request = getGetAllAnalysisRequest(now);
given(dailyMealPort.query(new DailyMealDTO(request.memberId(), request.dateTime())))
.willReturn(List.of());
DailyMealFoods dailyMealFoods =
DailyMealFoodsFixtureFactory.create(now.toLocalDate(), now.toLocalDate(), 0);
given(
dailyMealPort.queryDailyMealFoods(
new DailyMealDTO(request.memberId(), request.dateTime())))
.willReturn(dailyMealFoods);
given(memberPort.query(request.memberId(), request.dateTime())).willReturn(member);

// when
Expand All @@ -55,15 +58,18 @@ void when_meal_is_0_expected_isVisited_false_and_allAchieved_false() {
}

@Test
void when_meal_is_3_expected_isVisited_false_and_allAchieved_false() {
void when_mealFood_is_3_expected_isVisited_false_and_allAchieved_false() {

// given
LocalDateTime now = LocalDateTime.now();
Member member = MemberFixtureFactory.create(1);
GetAllAnalysisRequest request = getGetAllAnalysisRequest(now);
List<Meal> meals = MealsFixtureFactory.create(now.toLocalDate(), now.toLocalDate(), 3);
given(dailyMealPort.query(new DailyMealDTO(request.memberId(), request.dateTime())))
.willReturn(meals);
DailyMealFoods dailyMealFoods =
DailyMealFoodsFixtureFactory.create(now.toLocalDate(), now.toLocalDate(), 3);
given(
dailyMealPort.queryDailyMealFoods(
new DailyMealDTO(request.memberId(), request.dateTime())))
.willReturn(dailyMealFoods);
given(memberPort.query(request.memberId(), request.dateTime())).willReturn(member);

// when
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,39 @@
package com.gaebaljip.exceed.common.factory;

import static org.jeasy.random.FieldPredicates.named;

import java.time.LocalDate;
import java.util.List;

import org.jeasy.random.EasyRandom;
import org.jeasy.random.EasyRandomParameters;
import org.jeasy.random.api.Randomizer;

import com.gaebaljip.exceed.application.domain.meal.Meal;
import com.gaebaljip.exceed.application.domain.meal.DailyMealFoods;
import com.gaebaljip.exceed.application.domain.meal.MealFoodEntity;
import com.gaebaljip.exceed.application.domain.meal.Unit;

public class MealsFixtureFactory {
public static List<Meal> create(LocalDate startDate, LocalDate endDate, int mealSize) {
public class DailyMealFoodsFixtureFactory {

/**
* startDate ~ endDate(양 끝 포함)한 MealFoodEntity를 사용해 DailyMealFoods 생성
*
* @param startDate
* @param endDate
* @param size
* @return
*/
public static DailyMealFoods create(LocalDate startDate, LocalDate endDate, int size) {
EasyRandomParameters easyRandomParameters =
new EasyRandomParameters()
.scanClasspathForConcreteTypes(true)
.dateRange(startDate, endDate)
.excludeField(named("updatedDate"))
.randomize(Unit.class, new UnitRandomizer());
EasyRandom easyRandom = new EasyRandom(easyRandomParameters);
return easyRandom.objects(Meal.class, mealSize).toList();
List<MealFoodEntity> mealFoodEntities =
easyRandom.objects(MealFoodEntity.class, size).toList();
return new DailyMealFoods(mealFoodEntities);
}

private static class UnitRandomizer implements Randomizer<Unit> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,43 @@

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.gaebaljip.exceed.application.domain.meal.DailyMeal;
import com.gaebaljip.exceed.application.domain.meal.Meal;
import com.gaebaljip.exceed.application.domain.meal.DailyMealFoods;
import com.gaebaljip.exceed.application.domain.nutritionist.MonthlyMeal;

public class MonthlyMealFixtureFactory {
public static MonthlyMeal create(LocalDateTime startDateTime, LocalDateTime endDateTime) {
List<Meal> meals =
MealsFixtureFactory.create(
startDateTime.toLocalDate(), endDateTime.toLocalDate(), 40);
Map<LocalDate, DailyMeal> dailyMealMap =
meals.stream()
.collect(
Collectors.groupingBy(
meal -> meal.getMealDateTime().toLocalDate(),
Collectors.collectingAndThen(
Collectors.toList(), DailyMeal::new)));

List<Meal> emptyMeal = new ArrayList<>();
startDateTime
.toLocalDate()
.datesUntil(endDateTime.toLocalDate())
.forEach(day -> dailyMealMap.putIfAbsent(day, new DailyMeal(emptyMeal)));
/**
* startDate ~ endDate (양 끝 포함)를 가진 DailyMealFoods를 사용해 MonthlyMeal 생성
*
* @param startDateTime
* @param endDateTime
* @return
*/
public static MonthlyMeal create(LocalDateTime startDateTime, LocalDateTime endDateTime) {

MonthlyMeal monthlyMeal = new MonthlyMeal(dailyMealMap);
LocalDate startDate = startDateTime.toLocalDate();
LocalDate endDate = endDateTime.toLocalDate();
List<DailyMealFoods> dailyMealFoodsList =
startDate
.datesUntil(endDate.plusDays(1))
.map(date -> DailyMealFoodsFixtureFactory.create(date, date, 3))
.toList();
Map<LocalDate, DailyMealFoods> dateDailyMealFoodsMap =
dailyMealFoodsList.stream()
.collect(
Collectors.toMap(
dailyMealFoods ->
dailyMealFoods
.getMealFoods()
.get(0)
.getCreatedDate()
.toLocalDate(),
dailyMealFoods -> dailyMealFoods));
MonthlyMeal monthlyMeal = new MonthlyMeal(dateDailyMealFoodsMap);
return monthlyMeal;
}
}

0 comments on commit 6e42ad9

Please sign in to comment.