HHH-16125 make tests work with postgres enums

This commit is contained in:
Gavin 2023-04-28 15:29:15 +02:00 committed by Gavin King
parent c91cfdf2f1
commit 15d20113e7
5 changed files with 28 additions and 14 deletions

View File

@ -48,7 +48,7 @@ public class Boy {
private Map<String, Integer> scorePerPreferredName = new HashMap<>(); private Map<String, Integer> scorePerPreferredName = new HashMap<>();
private int[] favoriteNumbers; private int[] favoriteNumbers;
private Set<Toy> favoriteToys = new HashSet<>(); private Set<Toy> favoriteToys = new HashSet<>();
private Set<Character> characters = new HashSet<>(); private Set<CharacterTrait> characters = new HashSet<>();
private Map<String, FavoriteFood> foods = new HashMap<>(); private Map<String, FavoriteFood> foods = new HashMap<>();
private Set<CountryAttitude> countryAttitudes = new HashSet<>(); private Set<CountryAttitude> countryAttitudes = new HashSet<>();
@ -155,11 +155,11 @@ public class Boy {
@ElementCollection @ElementCollection
@Enumerated(EnumType.STRING) @Enumerated(EnumType.STRING)
@Column(name = "`characters`") @Column(name = "`characters`")
public Set<Character> getCharacters() { public Set<CharacterTrait> getCharacters() {
return characters; return characters;
} }
public void setCharacters(Set<Character> characters) { public void setCharacters(Set<CharacterTrait> characters) {
this.characters = characters; this.characters = characters;
} }

View File

@ -10,7 +10,7 @@ package org.hibernate.orm.test.annotations.collectionelement;
/** /**
* @author Emmanuel Bernard * @author Emmanuel Bernard
*/ */
public enum Character { public enum CharacterTrait {
GENTLE, GENTLE,
NORMAL, NORMAL,
AGGRESSIVE, AGGRESSIVE,

View File

@ -80,8 +80,8 @@ public class DefaultNamingCollectionElementTest {
favNbrs[index] = index * 3; favNbrs[index] = index * 3;
} }
boy.setFavoriteNumbers( favNbrs ); boy.setFavoriteNumbers( favNbrs );
boy.getCharacters().add( Character.GENTLE ); boy.getCharacters().add( CharacterTrait.GENTLE );
boy.getCharacters().add( Character.CRAFTY ); boy.getCharacters().add( CharacterTrait.CRAFTY );
HashMap<String, FavoriteFood> foods = new HashMap<>(); HashMap<String, FavoriteFood> foods = new HashMap<>();
foods.put( "breakfast", FavoriteFood.PIZZA ); foods.put( "breakfast", FavoriteFood.PIZZA );
@ -103,7 +103,7 @@ public class DefaultNamingCollectionElementTest {
assertEquals( Integer.valueOf( 5 ), boy.getScorePerNickName().get( "Thing" ) ); assertEquals( Integer.valueOf( 5 ), boy.getScorePerNickName().get( "Thing" ) );
assertNotNull( boy.getFavoriteNumbers() ); assertNotNull( boy.getFavoriteNumbers() );
assertEquals( 3, boy.getFavoriteNumbers()[1] ); assertEquals( 3, boy.getFavoriteNumbers()[1] );
assertTrue( boy.getCharacters().contains( Character.CRAFTY ) ); assertTrue( boy.getCharacters().contains( CharacterTrait.CRAFTY ) );
assertTrue( boy.getFavoriteFood().get( "dinner" ).equals( FavoriteFood.SUSHI ) ); assertTrue( boy.getFavoriteFood().get( "dinner" ).equals( FavoriteFood.SUSHI ) );
assertTrue( boy.getFavoriteFood().get( "lunch" ).equals( FavoriteFood.KUNGPAOCHICKEN ) ); assertTrue( boy.getFavoriteFood().get( "lunch" ).equals( FavoriteFood.KUNGPAOCHICKEN ) );
assertTrue( boy.getFavoriteFood().get( "breakfast" ).equals( FavoriteFood.PIZZA ) ); assertTrue( boy.getFavoriteFood().get( "breakfast" ).equals( FavoriteFood.PIZZA ) );
@ -199,8 +199,8 @@ public class DefaultNamingCollectionElementTest {
favNbrs[index] = index * 3; favNbrs[index] = index * 3;
} }
boy.setFavoriteNumbers( favNbrs ); boy.setFavoriteNumbers( favNbrs );
boy.getCharacters().add( Character.GENTLE ); boy.getCharacters().add( CharacterTrait.GENTLE );
boy.getCharacters().add( Character.CRAFTY ); boy.getCharacters().add( CharacterTrait.CRAFTY );
session.persist( boy ); session.persist( boy );
session.getTransaction().commit(); session.getTransaction().commit();
@ -215,7 +215,7 @@ public class DefaultNamingCollectionElementTest {
assertEquals( new Integer( 5 ), boy.getScorePerNickName().get( "Thing" ) ); assertEquals( new Integer( 5 ), boy.getScorePerNickName().get( "Thing" ) );
assertNotNull( boy.getFavoriteNumbers() ); assertNotNull( boy.getFavoriteNumbers() );
assertEquals( 3, boy.getFavoriteNumbers()[1] ); assertEquals( 3, boy.getFavoriteNumbers()[1] );
assertTrue( boy.getCharacters().contains( Character.CRAFTY ) ); assertTrue( boy.getCharacters().contains( CharacterTrait.CRAFTY ) );
List result = session.createQuery( List result = session.createQuery(
"select boy from Boy boy join boy.nickNames names where names = :name" ) "select boy from Boy boy join boy.nickNames names where names = :name" )
.setParameter( "name", "Thing" ).list(); .setParameter( "name", "Thing" ).list();

View File

@ -54,6 +54,7 @@ import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.isOneOf; import static org.hamcrest.Matchers.isOneOf;
import static org.hibernate.testing.orm.domain.gambit.EntityOfBasics.Gender.FEMALE;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail; import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
@ -409,9 +410,15 @@ public class FunctionTests {
public void testCoalesceFunction(SessionFactoryScope scope) { public void testCoalesceFunction(SessionFactoryScope scope) {
scope.inTransaction( scope.inTransaction(
session -> { session -> {
session.createQuery("select coalesce(nullif('',''), e.gender, e.convertedGender) from EntityOfBasics e", EntityOfBasics.Gender.class) //Derby does not like literal nulls :-/
// session.createQuery("select coalesce(null, e.gender, org.hibernate.testing.orm.domain.gambit.EntityOfBasics$Gender.MALE) from EntityOfBasics e", EntityOfBasics.Gender.class)
// .list();
session.createQuery("select coalesce(nullif(e.gender,org.hibernate.testing.orm.domain.gambit.EntityOfBasics$Gender.FEMALE), e.gender) from EntityOfBasics e", EntityOfBasics.Gender.class)
.list(); .list();
session.createQuery("select ifnull(e.gender, e.convertedGender) from EntityOfBasics e", EntityOfBasics.Gender.class) session.createQuery("select coalesce(nullif(e.gender,?1), e.gender) from EntityOfBasics e", EntityOfBasics.Gender.class)
.setParameter(1, FEMALE)
.list();
session.createQuery("select ifnull(e.gender, org.hibernate.testing.orm.domain.gambit.EntityOfBasics$Gender.FEMALE) from EntityOfBasics e", EntityOfBasics.Gender.class)
.list(); .list();
assertThat( session.createQuery("select coalesce(nullif('',''), nullif('bye','bye'), 'hello', 'oops')", String.class).getSingleResult(), is("hello") ); assertThat( session.createQuery("select coalesce(nullif('',''), nullif('bye','bye'), 'hello', 'oops')", String.class).getSingleResult(), is("hello") );
assertThat( session.createQuery("select ifnull(nullif('bye','bye'), 'hello')", String.class).getSingleResult(), is("hello") ); assertThat( session.createQuery("select ifnull(nullif('bye','bye'), 'hello')", String.class).getSingleResult(), is("hello") );

View File

@ -36,6 +36,7 @@ import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.nullValue; import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.isOneOf; import static org.hamcrest.Matchers.isOneOf;
import static org.hibernate.testing.orm.domain.gambit.EntityOfBasics.Gender.FEMALE;
/** /**
* @author Steve Ebersole * @author Steve Ebersole
@ -181,9 +182,15 @@ public class StandardFunctionTests {
public void testCoalesceFunction(SessionFactoryScope scope) { public void testCoalesceFunction(SessionFactoryScope scope) {
scope.inTransaction( scope.inTransaction(
session -> { session -> {
session.createQuery("select coalesce(nullif('',''), e.gender, e.convertedGender) from EntityOfBasics e") //Derby does not like literal nulls :-/
// session.createQuery("select coalesce(null, e.gender, org.hibernate.testing.orm.domain.gambit.EntityOfBasics$Gender.MALE) from EntityOfBasics e")
// .list();
session.createQuery("select coalesce(nullif(e.gender,org.hibernate.testing.orm.domain.gambit.EntityOfBasics$Gender.FEMALE), e.gender) from EntityOfBasics e")
.list(); .list();
session.createQuery("select ifnull(e.gender, e.convertedGender) from EntityOfBasics e") session.createQuery("select coalesce(nullif(e.gender,?1), e.gender) from EntityOfBasics e")
.setParameter(1, FEMALE)
.list();
session.createQuery("select ifnull(e.gender, org.hibernate.testing.orm.domain.gambit.EntityOfBasics$Gender.MALE) from EntityOfBasics e")
.list(); .list();
} }
); );