Skip to content

Commit

Permalink
feat : adds post validation test (#1632)
Browse files Browse the repository at this point in the history
* feat : adds post validation test

* enhance test

* fix : issue with inserting of initial data

* fix issue with setting status

* adds mising import

* adds not blank constraint

* adds more tests
  • Loading branch information
rajadilipkolli authored Jan 10, 2025
1 parent 713dc89 commit 5e9c78e
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import static com.example.jooq.r2dbc.testcontainersflyway.db.tables.Tags.TAGS;

import com.example.jooq.r2dbc.config.logging.Loggable;
import com.example.jooq.r2dbc.model.Status;
import com.example.jooq.r2dbc.repository.PostRepository;
import com.example.jooq.r2dbc.testcontainersflyway.db.tables.records.PostCommentsRecord;
import com.example.jooq.r2dbc.testcontainersflyway.db.tables.records.PostsRecord;
Expand Down Expand Up @@ -43,15 +44,22 @@ public void run(String... args) {
DeleteUsingStep<PostsRecord> postsRecordDeleteUsingStep = dslContext.deleteFrom(POSTS);

Mono.from(postsTagsRecordDeleteUsingStep)
.doOnError(e -> log.error("Failed to delete posts_tags", e))
.then(Mono.from(tagsRecordDeleteUsingStep))
.doOnError(e -> log.error("Failed to delete tags", e))
.then(Mono.from(postCommentsRecordDeleteUsingStep))
.doOnError(e -> log.error("Failed to delete post_comments", e))
.then(Mono.from(postsRecordDeleteUsingStep))
.doOnError(e -> log.error("Failed to delete posts", e))
.then(
Mono.from(
dslContext
.insertInto(POSTS)
.columns(POSTS.TITLE, POSTS.CONTENT)
.values("jooq test", "content of Jooq test")
.columns(POSTS.TITLE, POSTS.CONTENT, POSTS.STATUS)
.values(
"jooq test",
"content of Jooq test",
Status.PUBLISHED.name())
.returningResult(POSTS.ID)))
.flatMap(
postId ->
Expand Down Expand Up @@ -94,7 +102,7 @@ public void run(String... args) {
.subscribe(
data -> log.debug("Retrieved data: {}", data),
error ->
log.debug("Failed to retrieve posts with comments and tags", error),
log.error("Failed to retrieve posts with comments and tags", error),
() -> log.debug("done"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import static org.jooq.impl.DSL.select;

import com.example.jooq.r2dbc.entities.Post;
import com.example.jooq.r2dbc.model.Status;
import com.example.jooq.r2dbc.model.request.CreatePostCommand;
import com.example.jooq.r2dbc.model.request.CreatePostComment;
import com.example.jooq.r2dbc.model.response.PaginatedResult;
Expand Down Expand Up @@ -70,8 +71,11 @@ public Mono<UUID> create(CreatePostCommand createPostCommand) {
var createPostSQL =
dslContext
.insertInto(POSTS)
.columns(POSTS.TITLE, POSTS.CONTENT)
.values(createPostCommand.title(), createPostCommand.content())
.columns(POSTS.TITLE, POSTS.CONTENT, POSTS.STATUS)
.values(
createPostCommand.title(),
createPostCommand.content(),
Status.DRAFT.name())
.returningResult(POSTS.ID);

return Flux.fromIterable(createPostCommand.tagName())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ CREATE
TABLE
posts(
id uuid NOT NULL DEFAULT uuid_generate_v4(),
title text,
content text,
status text,
title text NOT NULL,
content text NOT NULL,
status text NOT NULL,
created_at timestamptz DEFAULT NOW(),
created_by text,
updated_at timestamptz,
Expand Down Expand Up @@ -47,3 +47,20 @@ CREATE
tag_id
)
);

ALTER TABLE
posts ADD CONSTRAINT check_title_not_blank CHECK(
LENGTH(
TRIM( title )
)> 0
),
ADD CONSTRAINT check_content_not_blank CHECK(
LENGTH(
TRIM( content )
)> 0
),
ADD CONSTRAINT check_status_not_blank CHECK(
LENGTH(
TRIM( status )
)> 0
);
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.UUID;
import java.util.stream.Stream;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.data.r2dbc.DataR2dbcTest;
import org.springframework.context.annotation.Import;
Expand Down Expand Up @@ -204,6 +208,31 @@ void testOptimisticLockingOnConcurrentPostUpdates() {
.verify();
}

@ParameterizedTest
@MethodSource("invalidPostProvider")
void testInsertPostWithInvalidDataShouldFail(
String title, String content, Status status, String expectedError) {
StepVerifier.create(
postRepository.save(
new Post().setTitle(title).setContent(content).setStatus(status)))
.expectErrorMatches(
throwable ->
throwable instanceof DataIntegrityViolationException
&& throwable.getMessage().contains(expectedError))
.verify();
}

private static Stream<Arguments> invalidPostProvider() {
return Stream.of(
Arguments.of(null, "content", Status.DRAFT, "title"),
Arguments.of("", "content", Status.DRAFT, "title"),
Arguments.of(" ", "content", Status.DRAFT, "title"),
Arguments.of("title", "content", null, "status"),
Arguments.of("title", "", Status.DRAFT, "content"),
Arguments.of("title", " ", Status.DRAFT, "content"),
Arguments.of("title", null, Status.DRAFT, "content"));
}

private Mono<Post> createPost() {
return postRepository.save(
new Post().setTitle("jooq test").setContent("content of Jooq test"));
Expand Down

0 comments on commit 5e9c78e

Please sign in to comment.