Skip to content

Commit

Permalink
Add Nested nullable embedded test
Browse files Browse the repository at this point in the history
  • Loading branch information
IAM20 committed Jan 31, 2024
1 parent 8e20430 commit cfe0680
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@

package com.navercorp.spring.data.jdbc.plus.sql.guide.order;

import javax.annotation.Nullable;

import org.springframework.data.annotation.Id;
import org.springframework.data.relational.core.mapping.Embedded;
import org.springframework.data.relational.core.mapping.Table;

import lombok.Builder;
Expand All @@ -43,7 +46,27 @@ public class Order {

private String purchaserNo;

@Nullable
@Embedded.Nullable(prefix = "discount_")
private Discount discount;

public void complete() {
this.status = OrderStatus.COMPLETED;
}

@Builder
public record Discount(
@Nullable
Long amount,

@Embedded.Nullable
DiscountType type
) {
}

@Builder
public record DiscountType(
String type
) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,20 @@
package com.navercorp.spring.data.jdbc.plus.sql.guide.order;

import static java.util.Comparator.*;
import static java.util.function.Function.*;
import static java.util.stream.Collectors.*;
import static org.assertj.core.api.Assertions.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.util.Streamable;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -51,12 +56,43 @@ class OrderRepositoryTest {
.price(5000L)
.status(OrderStatus.PLACE)
.purchaserNo("navercorp")
.discount(Order.Discount.builder()
.type(new Order.DiscountType("coupon"))
.build())
.build(),
Order.builder()
.price(3000L)
.status(OrderStatus.COMPLETED)
.purchaserNo("navercorp")
.build());
.discount(Order.Discount.builder()
.amount(1000L)
.type(new Order.DiscountType("coupon"))
.build()
).build(),
Order.builder()
.price(9000L)
.status(OrderStatus.COMPLETED)
.purchaserNo("navercorp")
.discount(Order.Discount.builder()
.amount(1000L)
.build()
).build()
);

@Test
void findById() {
// given
var insertedOrder = sut.save(orders.get(0));

// when
Optional<Order> actual = sut.findById(insertedOrder.getId());

// then
assertThat(actual).hasValueSatisfying(it -> {
assertThat(it.getId()).isEqualTo(insertedOrder.getId());
assertThat(it.getDiscount()).isNull();
});
}

@Test
void findByPurchaserNo() {
Expand All @@ -68,7 +104,7 @@ void findByPurchaserNo() {

// then
actual.sort(comparing(Order::getPrice));
Assertions.assertThat(actual).hasSize(3);
assertThat(actual).hasSize(4);
assertThat(actual.get(0).getPrice()).isEqualTo(1000L);
assertThat(actual.get(0).getStatus()).isEqualTo(OrderStatus.PLACE);
assertThat(actual.get(1).getPrice()).isEqualTo(3000L);
Expand All @@ -77,6 +113,27 @@ void findByPurchaserNo() {
assertThat(actual.get(2).getStatus()).isEqualTo(OrderStatus.PLACE);
}

@Test
void findByIds() {
// given
var ids = Streamable.of(sut.saveAll(orders)).stream()
.map(Order::getId)
.toList();

Map<Long, Order> orderByPrice = orders.stream()
.collect(toMap(Order::getPrice, identity()));

// when
List<Order> actual = Streamable.of(sut.findAllById(ids)).toList();

// then
assertThat(actual).allSatisfy(it ->
assertThat(it).usingRecursiveComparison()
.ignoringFields("id")
.isEqualTo(orderByPrice.get(it.getPrice()))
);
}

@Test
void search() {
// given
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ CREATE TABLE IF NOT EXISTS n_order (
price INT,
status VARCHAR(20),
purchaser_no VARCHAR(36),
discount_amount INT,
discount_type VARCHAR(20),
PRIMARY KEY(id));

CREATE TABLE IF NOT EXISTS n_board (
Expand Down

0 comments on commit cfe0680

Please sign in to comment.