Skip to content

Commit

Permalink
Merge pull request #400 from softeerbootcamp-2nd/feat/create-test-graph
Browse files Browse the repository at this point in the history
Feat/create test graph
  • Loading branch information
dydwo0740 authored Aug 22, 2023
2 parents 71266a9 + b5d185f commit d43f9c6
Show file tree
Hide file tree
Showing 10 changed files with 334 additions and 45 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package autoever2.cartag.domain.option;

import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@NoArgsConstructor
public class SubOptionIdAndPriceDto {
private int optionId;
private Long optionPrice;
@Builder
public SubOptionIdAndPriceDto(int optionId, Long optionPrice) {
this.optionId = optionId;
this.optionPrice = optionPrice;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ public List<CarPriceDto> findCarPriceAndCount() {
String sql = "select SalesHistory.sold_options_id, (car_default_price + sum(model_price)) as sum from Model inner join HistoryModelMapper " +
"on Model.model_id = HistoryModelMapper.model_id inner join SalesHistory " +
"on SalesHistory.history_id = HistoryModelMapper.history_id inner join Car " +
"on Car.car_id = SalesHistory.car_id inner join SubOptionData " +
"on SubOptionData.car_id = Car.car_id group by SalesHistory.history_id";
"on Car.car_id = SalesHistory.car_id group by SalesHistory.history_id";

return template.query(sql, carPriceRowMapper());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import autoever2.cartag.domain.option.OptionDetailMappedDto;
import autoever2.cartag.domain.option.OptionShortMappedDto;
import autoever2.cartag.domain.option.QuoteSubOptionDto;
import autoever2.cartag.domain.option.SubOptionIdAndPriceDto;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.support.DataAccessUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
Expand All @@ -30,10 +31,10 @@ public List<OptionShortMappedDto> findOptionList(int carId, boolean isDefault) {
StringBuilder query = new StringBuilder();
query.append("select o.option_id, o.option_name, oc.option_category_name, o.option_image, o.option_used_count ");

if(!isDefault) {
if (!isDefault) {
query.append(", od.option_bought_count, od.option_price from SubOptionData od ");
}
if(isDefault) {
if (isDefault) {
query.append("from DefaultOptionData od ");
}

Expand Down Expand Up @@ -82,11 +83,11 @@ public Optional<OptionDetailMappedDto> findOptionDetail(int carId, int optionId,
StringBuilder query = new StringBuilder();
query.append("select oc.option_category_name as category_name, o.option_name, o.option_description, o.option_image, o.option_used_count ");

if(isDefault) {
if (isDefault) {
query.append("from DefaultOptionData od ");
}

if(!isDefault) {
if (!isDefault) {
query.append(", od.option_bought_count from SubOptionData od ");
}

Expand Down Expand Up @@ -121,19 +122,7 @@ public List<OptionDetailMappedDto> findPackageSubOptions(int optionId) {
return template.query(sql, param, optionDetailRowMapper());
}

public Optional<Long> findOptionPriceByOptionId(int optionId) {
String sql = "select option_price from SubOptionData where option_id = :optionId";

try {
SqlParameterSource param = new MapSqlParameterSource()
.addValue("optionId", optionId);
return Optional.of(template.queryForObject(sql, param, Long.class));
} catch (DataAccessException e) {
return Optional.empty();
}
}

public Optional<QuoteSubOptionDto> findSubOptionByOptionId(int optionId){
public Optional<QuoteSubOptionDto> findSubOptionByOptionId(int optionId) {
String sql = "select CarOption.option_id, option_name, option_image, option_price, option_category_name as optionTitle from CarOption inner join SubOptionData " +
"on CarOption.option_id = SubOptionData.option_id inner join OptionCategory on " +
"OptionCategory.option_category_id = CarOption.option_category_id where CarOption.option_id = :optionId";
Expand All @@ -146,6 +135,15 @@ public Optional<QuoteSubOptionDto> findSubOptionByOptionId(int optionId){
}
}

public List<SubOptionIdAndPriceDto> findAllSubOptionInfo() {
String sql = "select option_id, option_price from SubOptionData";
return template.query(sql, subOptionIdAndPriceRowMapper());
}

private RowMapper<SubOptionIdAndPriceDto> subOptionIdAndPriceRowMapper() {
return BeanPropertyRowMapper.newInstance(SubOptionIdAndPriceDto.class);
}

private RowMapper<QuoteSubOptionDto> shareSubOptionRowMapper() {
return BeanPropertyRowMapper.newInstance(QuoteSubOptionDto.class);
}
Expand Down
43 changes: 21 additions & 22 deletions backend/src/main/java/autoever2/cartag/service/CarService.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import autoever2.cartag.domain.color.OuterColorDto;
import autoever2.cartag.domain.model.ModelDefaultDto;
import autoever2.cartag.domain.option.QuoteSubOptionDto;
import autoever2.cartag.domain.option.SubOptionIdAndPriceDto;
import autoever2.cartag.domain.quote.QuoteDataDto;
import autoever2.cartag.domain.quote.QuoteInfoDto;
import autoever2.cartag.exception.EmptyDataException;
Expand Down Expand Up @@ -69,28 +70,26 @@ public CarDefaultDto findCarDefaultDtoByCarId(int carId) {

public List<BoughtCarDto> findAllBoughInfos() {
List<CarPriceDto> carPriceAndCount = carRepository.findCarPriceAndCount();

return carPriceAndCount.stream()
.map(carPriceDto -> {
String optionList = carPriceDto.getOptionList();
Long key = 0L;
if (optionList.isEmpty()) {
key = carPriceDto.getPrice() / 100000 * 100000;
} else {
String[] optionIdList = optionList.split(",");

Long sum = Arrays.stream(optionIdList)
.mapToLong(s -> optionRepository.findOptionPriceByOptionId(Integer.parseInt(s)).get())
.sum();

key = ((carPriceDto.getPrice() + sum) / 100000) * 1000000;
}

return Map.entry(key, 1);
})
.collect(Collectors.groupingByConcurrent(Map.Entry::getKey, Collectors.summingInt(Map.Entry::getValue)))
.entrySet().stream()
.map(entry -> BoughtCarDto.toBoughtCarDto(entry.getKey(), entry.getValue()))
List<SubOptionIdAndPriceDto> allSubOptionInfo = optionRepository.findAllSubOptionInfo();
Map<Integer, Long> subOptionPrice = allSubOptionInfo.stream()
.collect(Collectors.toMap(SubOptionIdAndPriceDto::getOptionId, SubOptionIdAndPriceDto::getOptionPrice));
Map<Long, Integer> map = new HashMap<>();
for (int i = 0; i < carPriceAndCount.size(); i++) {
Long sum = 0L;
if(!carPriceAndCount.get(i).getOptionList().isEmpty()){
String[] split = carPriceAndCount.get(i).getOptionList().split(",");
for (String s : split) {
sum += subOptionPrice.get(Integer.parseInt(s));
}
}
long key = ((carPriceAndCount.get(i).getPrice() + sum) / 100000) * 100000;
map.put(key, map.getOrDefault(key, 0) + 1);
}
return map.entrySet().stream()
.map(entry -> BoughtCarDto.builder()
.totalPrice(entry.getKey())
.count(entry.getValue())
.build())
.collect(Collectors.toList());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package autoever2.cartag.controller;

import autoever2.cartag.domain.car.BoughtCarDto;
import autoever2.cartag.domain.option.QuoteSubOptionDto;
import autoever2.cartag.domain.quote.HistoryShortDto;
import autoever2.cartag.domain.quote.QuoteDataDto;
Expand Down Expand Up @@ -90,6 +91,7 @@ void getRecommendedList() throws Exception {
.andExpect(jsonPath("$.histories[2].soldCount").value(162))
.andExpect(jsonPath("$.histories[3].historyId").value(169));
}

@Test
@DisplayName("id에 따른 공유 데이터 반환")
void getShareInfo() throws Exception {
Expand Down Expand Up @@ -196,4 +198,46 @@ void getShareInfo() throws Exception {
.andExpect(jsonPath("$.colorCarInnerImage").value("inner_red.jpg"))
.andExpect(jsonPath("$.optionList.size()").value(3));
}

@Test
@DisplayName("차량 구매 정보 반환 api")
void getBoughtInfos() throws Exception {
List<BoughtCarDto> boughtCarDtoList = new ArrayList<>();
boughtCarDtoList.add(BoughtCarDto
.builder()
.totalPrice(4900000L)
.count(1900)
.build());
boughtCarDtoList.add(BoughtCarDto
.builder()
.totalPrice(5100000L)
.count(2200)
.build());
boughtCarDtoList.add(BoughtCarDto
.builder()
.totalPrice(6000000L)
.count(4300)
.build());
boughtCarDtoList.add(BoughtCarDto
.builder()
.totalPrice(6700000L)
.count(1400)
.build());
boughtCarDtoList.add(BoughtCarDto
.builder()
.totalPrice(7000000L)
.count(1200)
.build());

given(carService.findAllBoughInfos()).willReturn(boughtCarDtoList);

ResultActions resultActions = mockMvc.perform(MockMvcRequestBuilders.get("/api/quote/bought/infos"));

//then
resultActions.andExpect(status().isOk())
.andExpect(jsonPath("$[0].totalPrice").value(4900000L))
.andExpect(jsonPath("$[1].count").value(2200))
.andExpect(jsonPath("$[2].totalPrice").value(6000000L))
.andExpect(jsonPath("$[3].count").value(1400));
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package autoever2.cartag.integration;

import autoever2.cartag.controller.QuoteController;
import autoever2.cartag.domain.car.BoughtCarDto;
import autoever2.cartag.domain.quote.QuoteDataDto;
import autoever2.cartag.domain.quote.QuoteInfoDto;
import autoever2.cartag.recommend.RecommendConnector;
Expand All @@ -24,8 +25,8 @@
import static org.junit.jupiter.api.Assertions.assertEquals;

@SpringBootTest
@Transactional
@ActiveProfiles("test")
@Transactional
public class QuoteTest {

@Autowired
Expand Down Expand Up @@ -70,4 +71,15 @@ void testShare() {
assertEquals("/images/options/sub/warmer.jpg", optionList.get(1).getOptionImage());
assertEquals("악세사리", optionList.get(2).getOptionTitle());
}

@Test
@DisplayName("/api/quote/bought/infos")
@Sql({"classpath:insert/insert-boughtinfo-h2.sql"})
void testBoughtInfo(){
List<BoughtCarDto> allHistorySum = quoteController.getAllHistorySum();

assertEquals(6, allHistorySum.size());
assertEquals(2, allHistorySum.get(0).getCount());
assertEquals(42300000L, allHistorySum.get(1).getTotalPrice());
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package autoever2.cartag.repository;

import autoever2.cartag.domain.car.CarInfoDto;
import autoever2.cartag.domain.car.CarPriceDto;
import autoever2.cartag.domain.car.CarTypeDto;
import autoever2.cartag.domain.car.TrimInfoDto;
import org.assertj.core.api.SoftAssertions;
Expand All @@ -24,7 +25,7 @@

@JdbcTest
@ActiveProfiles("test")
@Sql(scripts = {"classpath:/insert/insertCar-h2.sql"})
@Sql(scripts = {"classpath:/insert/insert-boughtinfo-h2.sql"})
@ExtendWith(SoftAssertionsExtension.class)
class CarRepositoryTest {

Expand Down Expand Up @@ -90,7 +91,7 @@ void findAllCarTypeList() {

@Test
@DisplayName("차량 트림 정보를 반환")
void getTrimInfo(){
void getTrimInfo() {
Optional<TrimInfoDto> trimInfo = carRepository.findTrimInfoByCarId(1);
assertTrue(trimInfo.isPresent());

Expand All @@ -101,4 +102,20 @@ void getTrimInfo(){
Optional<TrimInfoDto> trimNoFoundInfo = carRepository.findTrimInfoByCarId(7);
assertTrue(trimNoFoundInfo.isEmpty());
}

@Test
@DisplayName("차량 가격 정보와 optionIdList를 반환하는 로직")
void getPriceAndOptionList(){
List<CarPriceDto> totalInfo = carRepository.findCarPriceAndCount();

assertEquals(11, totalInfo.size());
assertEquals(41480000L, totalInfo.get(0).getPrice());

String emptyOptionList = totalInfo.get(0).getOptionList();
assertTrue(emptyOptionList.isEmpty());

String optionList = totalInfo.get(10).getOptionList();
assertTrue(!optionList.isEmpty());
assertEquals(69, Integer.parseInt(optionList));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import autoever2.cartag.domain.option.OptionDetailMappedDto;
import autoever2.cartag.domain.option.OptionShortMappedDto;
import autoever2.cartag.domain.option.QuoteSubOptionDto;
import autoever2.cartag.domain.option.SubOptionIdAndPriceDto;
import org.assertj.core.api.SoftAssertions;
import org.assertj.core.api.junit.jupiter.InjectSoftAssertions;
import org.assertj.core.api.junit.jupiter.SoftAssertionsExtension;
Expand Down Expand Up @@ -234,7 +235,7 @@ void findDefaultOptionList() {

@Test
@DisplayName("공유를 위한 optionInfo 추출")
void getOptionInfo(){
void getOptionInfo() {
Optional<QuoteSubOptionDto> subOptionInfoV1 = optionRepository.findSubOptionByOptionId(1);
assertTrue(subOptionInfoV1.isPresent());

Expand All @@ -256,4 +257,15 @@ void countExistOptions() {

assertEquals(1, optionRepository.countExistOptions(carId, optionIds));
}

@Test
@DisplayName("모든 subOptionData를 추출")
void findAllSubOptionInfos(){
List<SubOptionIdAndPriceDto> allSubOptionInfo = optionRepository.findAllSubOptionInfo();
assertEquals(6, allSubOptionInfo.size());

SubOptionIdAndPriceDto subOptionIdAndPriceDto = allSubOptionInfo.get(0);
assertEquals(1, subOptionIdAndPriceDto.getOptionId());
assertEquals(100000L, subOptionIdAndPriceDto.getOptionPrice());
}
}
Loading

0 comments on commit d43f9c6

Please sign in to comment.