Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat : adds post validation test #1632

Merged
merged 7 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -50,8 +51,11 @@ public void run(String... args) {
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
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,
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@coderabbitai, how to handle NOT BLANK scenario? if use input value as "" it is allowing so I want to not allow it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rajadilipkolli To prevent empty strings, you can add CHECK constraints to ensure the columns contain at least one non-whitespace character. Here's how you can modify your table:

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);

This will:

  1. Ensure title is not blank or just whitespace
  2. Ensure content is not blank or just whitespace
  3. Ensure status is not blank or just whitespace

You can add these constraints to your migration file after the table creation.

⟪ʕ •ᴥ•ʔ⟫ thoughtfully taking notes about PostgreSQL constraints


✏️ Learnings added
Learnt from: rajadilipkolli
PR: rajadilipkolli/my-spring-boot-experiments#1632
File: r2dbc/boot-jooq-r2dbc-sample/src/main/resources/db/migration/postgresql/V1__01_init.sql:8-8
Timestamp: 2025-01-10T06:17:49.785Z
Learning: In PostgreSQL, to prevent empty strings or whitespace-only values in text columns, use CHECK constraints with LENGTH(TRIM(column_name)) > 0. This is more robust than just NOT NULL constraint which allows empty strings.

Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.

content text NOT NULL,
status text NOT NULL,
created_at timestamptz DEFAULT NOW(),
created_by text,
updated_at timestamptz,
Expand Down
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,27 @@ 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("title", "content", null, "status"),
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
Loading