Skip to content

Test ConstraintViolationException SQLState code #1807

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

Merged
merged 1 commit into from
Nov 29, 2023
Merged
Changes from all 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 @@ -5,6 +5,7 @@
*/
package org.hibernate.reactive;

import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

Expand All @@ -21,8 +22,11 @@
import jakarta.persistence.Table;

import static java.util.concurrent.TimeUnit.MINUTES;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
import static org.assertj.core.api.Assertions.assertThat;
import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.DB2;
import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.SQLSERVER;
import static org.hibernate.reactive.containers.DatabaseConfiguration.dbType;
import static org.hibernate.reactive.testing.ReactiveAssertions.assertThrown;

@Timeout(value = 10, timeUnit = MINUTES)

Expand All @@ -33,25 +37,41 @@ protected Collection<Class<?>> annotatedEntities() {
return List.of( Person.class );
}

Class<?> getExpectedException() {
return ConstraintViolationException.class;
}

@Test
public void testDuplicateKeyException(VertxTestContext context) {
test( context, openMutinySession()
test( context, assertThrown( ConstraintViolationException.class, openMutinySession()
.call( session -> session.persist( new Person( "testFLush1", "unique" ) ) )
.call( Mutiny.Session::flush )
.call( session -> session.persist( new Person( "testFlush2", "unique" ) ) )
.call( Mutiny.Session::flush )
.invoke( ignore -> fail( "Expected exception not thrown" ) )
.onFailure().recoverWithItem( err -> {
assertEquals( getExpectedException(), err.getClass() );
return null;
} )
.call( Mutiny.Session::flush ) )
.invoke( MutinyExceptionsTest::assertSqlStateCode )
.invoke( MutinyExceptionsTest::assertConstraintName )
);
}

private static void assertSqlStateCode(ConstraintViolationException exception) {
if ( dbType() == SQLSERVER ) {
// The SQL state code is always null in Sql Server (see https://github.com/eclipse-vertx/vertx-sql-client/issues/1385)
// We test the vendor code for now
SQLException sqlException = (SQLException) exception.getCause();
assertThat( sqlException.getErrorCode() ).isEqualTo( 2601 );
}
else {
assertThat( exception.getSQLState() )
.as( "Constraint violation SQL state code should start with 23" )
.matches( "23\\d{3}" );
}
}

private static void assertConstraintName(ConstraintViolationException exception) {
// DB2 does not return the constraint name
if ( dbType() != DB2 ) {
assertThat( exception.getConstraintName() )
.as( "Failed constraint name should not be null" )
.isNotNull();
}
}

@Entity(name = "Person")
@Table(name = "PersonForExceptionWithMutiny")
public static class Person {
Expand Down