-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: reduce transfering extra data from database
- Loading branch information
1 parent
0c6dd99
commit b539556
Showing
3 changed files
with
74 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
order-service/src/test/java/com/example/orderservice/repositories/OrderRepositoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/*** | ||
<p> | ||
Licensed under MIT License Copyright (c) 2023 Raja Kolli. | ||
</p> | ||
***/ | ||
|
||
package com.example.orderservice.repositories; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import com.example.orderservice.common.PostGreSQLContainer; | ||
import com.example.orderservice.entities.Order; | ||
import com.example.orderservice.util.TestData; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; | ||
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; | ||
import org.springframework.boot.testcontainers.context.ImportTestcontainers; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.PageRequest; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.domain.Sort; | ||
|
||
@ImportTestcontainers(PostGreSQLContainer.class) | ||
@DataJpaTest(properties = "application.catalogServiceUrl=http://dummy") | ||
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) | ||
class OrderRepositoryTest { | ||
|
||
@Autowired private OrderRepository orderRepository; | ||
@Autowired private OrderItemRepository orderItemRepository; | ||
|
||
@AfterEach | ||
void tearDown() { | ||
this.orderItemRepository.deleteAllInBatch(); | ||
this.orderRepository.deleteAllInBatch(); | ||
} | ||
|
||
@Test | ||
void findAllOrders() { | ||
|
||
List<Order> orderList = new ArrayList<>(); | ||
for (int i = 0; i <= 15; i++) { | ||
orderList.add(TestData.getOrder()); | ||
} | ||
this.orderRepository.saveAll(orderList); | ||
|
||
// create Pageable instance | ||
Pageable pageable = PageRequest.of(1, 5, Sort.by("id").ascending()); | ||
|
||
Page<Long> page = this.orderRepository.findAllOrders(pageable); | ||
|
||
assertThat(page).isNotNull(); | ||
assertThat(page.getTotalElements()).isEqualTo(16); | ||
assertThat(page.getTotalPages()).isEqualTo(4); | ||
assertThat(page.getNumber()).isEqualTo(1); | ||
assertThat(page.isFirst()).isEqualTo(false); | ||
assertThat(page.isLast()).isEqualTo(false); | ||
assertThat(page.hasNext()).isEqualTo(true); | ||
assertThat(page.hasPrevious()).isEqualTo(true); | ||
assertThat(page.getNumberOfElements()).isEqualTo(5); | ||
assertThat(page.getContent()).isNotEmpty().hasSize(5); | ||
} | ||
} |