HHH-17766 Additional tests on converted attributes
This commit is contained in:
parent
73221d0090
commit
c6e41c6f21
|
@ -32,13 +32,14 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||||
@DomainModel( annotatedClasses = ConvertedAttributesTypecheckTest.TestEntity.class )
|
@DomainModel( annotatedClasses = ConvertedAttributesTypecheckTest.TestEntity.class )
|
||||||
@SessionFactory
|
@SessionFactory
|
||||||
@Jira( "https://hibernate.atlassian.net/browse/HHH-17693" )
|
@Jira( "https://hibernate.atlassian.net/browse/HHH-17693" )
|
||||||
|
@Jira( "https://hibernate.atlassian.net/browse/HHH-17766" )
|
||||||
public class ConvertedAttributesTypecheckTest {
|
public class ConvertedAttributesTypecheckTest {
|
||||||
@BeforeAll
|
@BeforeAll
|
||||||
public void setUp(SessionFactoryScope scope) {
|
public void setUp(SessionFactoryScope scope) {
|
||||||
scope.inTransaction( session -> session.persist( new TestEntity(
|
scope.inTransaction( session -> session.persist( new TestEntity(
|
||||||
Set.of( "one", "two" ),
|
Set.of( "one", "two" ),
|
||||||
"123",
|
"123",
|
||||||
String.valueOf( Duration.ofDays( 3 ).toMillis() )
|
"3"
|
||||||
) ) );
|
) ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -58,25 +59,45 @@ public class ConvertedAttributesTypecheckTest {
|
||||||
} );
|
} );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBinaryArithmeticOnConvertedNumber(SessionFactoryScope scope) {
|
||||||
|
scope.inTransaction( session -> {
|
||||||
|
assertThat( session.createQuery(
|
||||||
|
"select convertedNumber - 123 from TestEntity",
|
||||||
|
Integer.class
|
||||||
|
).getSingleResult() ).isEqualTo( 0 );
|
||||||
|
assertThat( session.createQuery(
|
||||||
|
"select 123 + convertedNumber from TestEntity",
|
||||||
|
Integer.class
|
||||||
|
).getSingleResult() ).isEqualTo( 246 );
|
||||||
|
} );
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testUnaryExpressionOnConvertedNumber(SessionFactoryScope scope) {
|
public void testUnaryExpressionOnConvertedNumber(SessionFactoryScope scope) {
|
||||||
scope.inTransaction( session -> {
|
scope.inTransaction( session -> {
|
||||||
session.createQuery(
|
assertThat( session.createQuery(
|
||||||
|
"select -convertedNumber from TestEntity",
|
||||||
|
Integer.class
|
||||||
|
).getSingleResult() ).isEqualTo( -123 );
|
||||||
|
assertThat( session.createQuery(
|
||||||
"from TestEntity where -convertedNumber = -123",
|
"from TestEntity where -convertedNumber = -123",
|
||||||
TestEntity.class
|
TestEntity.class
|
||||||
).getSingleResult();
|
).getSingleResult().getConvertedNumber() ).isEqualTo( "123" );
|
||||||
} );
|
} );
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testFromDurationExpressionOnConvertedDuration(SessionFactoryScope scope) {
|
public void testFromDurationExpressionOnConvertedDuration(SessionFactoryScope scope) {
|
||||||
scope.inTransaction( session -> {
|
scope.inTransaction( session -> {
|
||||||
session.createQuery(
|
assertThat( session.createQuery(
|
||||||
"from TestEntity where convertedDuration by day = ?1",
|
"select convertedDuration by day from TestEntity",
|
||||||
TestEntity.class
|
Long.class
|
||||||
)
|
).getSingleResult() ).isEqualTo( 3L );
|
||||||
.setParameter( 1, Duration.ofDays( 3 ).toNanos() )
|
assertThat( session.createQuery(
|
||||||
.getSingleResult();
|
"from TestEntity where convertedDuration by day = 3",
|
||||||
|
TestEntity.class
|
||||||
|
).getSingleResult().getConvertedDuration() ).isEqualTo( "3" );
|
||||||
} );
|
} );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -147,12 +168,12 @@ public class ConvertedAttributesTypecheckTest {
|
||||||
public static class DurationConverter implements AttributeConverter<String, Duration> {
|
public static class DurationConverter implements AttributeConverter<String, Duration> {
|
||||||
@Override
|
@Override
|
||||||
public Duration convertToDatabaseColumn(final String attribute) {
|
public Duration convertToDatabaseColumn(final String attribute) {
|
||||||
return attribute == null ? null : Duration.ofMillis( Long.parseLong( attribute ) );
|
return attribute == null ? null : Duration.ofDays( Long.parseLong( attribute ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String convertToEntityAttribute(final Duration dbData) {
|
public String convertToEntityAttribute(final Duration dbData) {
|
||||||
return dbData == null ? null : String.valueOf( dbData.toMillis() );
|
return dbData == null ? null : String.valueOf( dbData.toDays() );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue