-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sets up infra to load heavy data (#1618)
* sets up infra to load heavy data * fix typo * adds more assertions
- Loading branch information
1 parent
954ed9e
commit bcf0c14
Showing
11 changed files
with
175 additions
and
17 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
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
6 changes: 6 additions & 0 deletions
6
jpa/boot-jpa-jooq-sample/src/main/java/com/example/learning/repository/PostRepository.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,6 @@ | ||
package com.example.learning.repository; | ||
|
||
import com.example.learning.entities.Post; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface PostRepository extends JpaRepository<Post, Long> {} |
6 changes: 6 additions & 0 deletions
6
jpa/boot-jpa-jooq-sample/src/main/java/com/example/learning/repository/TagRepository.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,6 @@ | ||
package com.example.learning.repository; | ||
|
||
import com.example.learning.entities.Tag; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface TagRepository extends JpaRepository<Tag, Long> {} |
6 changes: 6 additions & 0 deletions
6
jpa/boot-jpa-jooq-sample/src/main/java/com/example/learning/utils/AppConstants.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,6 @@ | ||
package com.example.learning.utils; | ||
|
||
public interface AppConstants { | ||
|
||
String PROFILE_TEST = "test"; | ||
} |
3 changes: 3 additions & 0 deletions
3
jpa/boot-jpa-jooq-sample/src/main/resources/application-local.properties
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,3 @@ | ||
spring.datasource.url=jdbc:postgresql://localhost:5432/appdb | ||
spring.datasource.username=appuser | ||
spring.datasource.password=secret |
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
75 changes: 75 additions & 0 deletions
75
jpa/boot-jpa-jooq-sample/src/test/java/com/example/learning/ApplicationIntegrationTest.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,75 @@ | ||
package com.example.learning; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.instancio.Select.all; | ||
import static org.instancio.Select.field; | ||
|
||
import com.example.learning.common.AbstractIntegrationTest; | ||
import com.example.learning.entities.Post; | ||
import com.example.learning.entities.PostComment; | ||
import com.example.learning.entities.PostDetails; | ||
import com.example.learning.entities.PostTag; | ||
import com.example.learning.entities.Tag; | ||
import java.time.LocalDateTime; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import org.instancio.Instancio; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class ApplicationIntegrationTest extends AbstractIntegrationTest { | ||
|
||
@Test | ||
void contextLoads() { | ||
// Create a finite dataset of tags with unique tag names | ||
List<Tag> finiteTags = Instancio.ofList(Tag.class) | ||
.size(100) // Limit to 100 unique tags | ||
.ignore(field(Tag::getId)) | ||
.create(); | ||
|
||
// Generate Post objects with specific fields ignored | ||
List<Post> postList = Instancio.ofList(Post.class) | ||
.size(100) | ||
.ignore(field(Post::getId)) | ||
.ignore(field(Post::getCreatedAt)) | ||
.ignore(field(Post::getModifiedAt)) | ||
.supply(all(LocalDateTime.class), () -> LocalDateTime.now()) | ||
.supply(field(Post::getDetails), () -> Instancio.of(PostDetails.class) | ||
.ignore(field(PostDetails::getId)) | ||
.ignore(field(PostDetails::getCreatedAt)) | ||
.ignore(field(PostDetails::getModifiedAt)) | ||
.ignore(field(PostDetails::getPost)) | ||
.create()) | ||
.supply(field(Post::getComments), () -> new ArrayList<PostComment>()) | ||
.supply(field(Post::getTags), () -> new ArrayList<PostTag>()) | ||
.create(); | ||
|
||
// Assign tags and comments to posts, reusing finite tags | ||
postList.forEach(post -> { | ||
post.setDetails(post.getDetails()); | ||
|
||
List<PostComment> postCommentList = Instancio.ofList(PostComment.class) | ||
.size(50) // Assuming 50 comments per post | ||
.ignore(field(PostComment::getId)) | ||
.ignore(field(PostComment::getCreatedAt)) | ||
.ignore(field(PostComment::getModifiedAt)) | ||
.ignore(field(PostComment::getPost)) | ||
.create(); | ||
postCommentList.forEach(post::addComment); | ||
|
||
// Randomly select tags from the finiteTags dataset | ||
Collections.shuffle(finiteTags); // Shuffle to randomize selection | ||
List<Tag> tags = finiteTags.stream() | ||
.limit(30) // Assign 30 tags per post | ||
.toList(); | ||
tags.forEach(post::addTag); | ||
}); | ||
|
||
// Save the modified posts | ||
List<Post> savedPostList = postRepository.saveAll(postList); | ||
|
||
// Assertions to validate the results | ||
assertThat(savedPostList).isNotEmpty().hasSize(postList.size()); | ||
assertThat(tagRepository.count()).isEqualTo(100); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...ot-jpa-jooq-sample/src/test/java/com/example/learning/common/AbstractIntegrationTest.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,37 @@ | ||
package com.example.learning.common; | ||
|
||
import static com.example.learning.utils.AppConstants.PROFILE_TEST; | ||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; | ||
|
||
import com.example.learning.repository.PostRepository; | ||
import com.example.learning.repository.TagRepository; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.instancio.junit.InstancioExtension; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.test.context.ActiveProfiles; | ||
import org.springframework.test.web.servlet.assertj.MockMvcTester; | ||
|
||
@ActiveProfiles({PROFILE_TEST}) | ||
@SpringBootTest( | ||
webEnvironment = RANDOM_PORT, | ||
classes = {SQLContainerConfig.class}, | ||
properties = {"jdbc.datasource-proxy.enabled=false"}) | ||
@AutoConfigureMockMvc | ||
@ExtendWith(InstancioExtension.class) | ||
public abstract class AbstractIntegrationTest { | ||
|
||
@Autowired | ||
protected MockMvcTester mockMvcTester; | ||
|
||
@Autowired | ||
protected ObjectMapper objectMapper; | ||
|
||
@Autowired | ||
protected PostRepository postRepository; | ||
|
||
@Autowired | ||
protected TagRepository tagRepository; | ||
} |
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