Skip to content

Commit

Permalink
[#1627] Minor refactoring of the test
Browse files Browse the repository at this point in the history
I think we can remove the association of embeddable for the
sake of making the test simpler
  • Loading branch information
DavideD committed Jul 25, 2023
1 parent e509b87 commit 903c34e
Showing 1 changed file with 26 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
*/
package org.hibernate.reactive;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import java.util.Objects;

import org.hibernate.annotations.SQLSelect;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
Expand All @@ -22,23 +20,21 @@
import org.junit.jupiter.api.extension.RegisterExtension;

import io.vertx.junit5.VertxTestContext;
import jakarta.persistence.ElementCollection;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.Id;

import static org.assertj.core.api.Assertions.assertThat;
import static org.hibernate.reactive.SQLSelectTest.Person.SELECT_QUERY;
import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.POSTGRESQL;
import static org.hibernate.reactive.testing.DBSelectionExtension.runOnlyFor;
import static org.junit.jupiter.api.Assertions.assertNotNull;

public class SQLSelectTest extends BaseReactiveTest {
@RegisterExtension // We use native queries, which may be different for other DBs
public DBSelectionExtension dbSelection = runOnlyFor( POSTGRESQL );

private SqlStatementTracker sqlTracker;

private Person thePerson;
public static String SQL = "xxxxx";

@Override
protected Collection<Class<?>> annotatedEntities() {
Expand All @@ -63,33 +59,31 @@ protected void addServices(StandardServiceRegistryBuilder builder) {

@BeforeEach
public void populateDb(VertxTestContext context) {
List<String> phones = Arrays.asList( "999-999-9999", "111-111-1111", "123-456-7890" );
thePerson = new Person();
thePerson.id = 724200;
thePerson.name = "Claude";
thePerson.phones = phones;

test( context, getMutinySessionFactory().withTransaction( (s, t) -> s.persist( thePerson ) ) );
test( context, getMutinySessionFactory().withTransaction( s -> s.persist( thePerson ) ) );
}

@Test
public void findEntity(VertxTestContext context) {
test( context, openSession()
.thenCompose( session -> session.find( Person.class, thePerson.getId() ) )
.thenAccept( found -> {
assertPhones( found, "999-999-9999", "111-111-1111", "123-456-7890" );
assertThat( sqlTracker.getLoggedQueries() ).contains( Person.SELECT_QUERY.replace( "?", "$1" ) );
assertThat( found ).isEqualTo( thePerson );
assertThat( sqlTracker.getLoggedQueries() )
.containsExactly(
"select version()",
SELECT_QUERY.replace( "?", "$1" )
);
} )
);
}

private static void assertPhones(Person person, String... expectedPhones) {
assertNotNull( person );
assertThat( person.getPhones() ).containsExactlyInAnyOrder( expectedPhones );
}

@Entity(name = "Person")
@SQLSelect(sql = Person.SELECT_QUERY)
@SQLSelect(sql = SELECT_QUERY)
static class Person {
// Query containing simple where check to distinguish from a possible generated query
public static final String SELECT_QUERY = "SELECT id, name FROM person WHERE id = ? and 'hreact' = 'hreact'";
Expand All @@ -99,9 +93,6 @@ static class Person {

private String name;

@ElementCollection(fetch = FetchType.EAGER)
private List<String> phones = new ArrayList<>();

public int getId() {
return id;
}
Expand All @@ -118,8 +109,21 @@ public void setName(String name) {
this.name = name;
}

public List<String> getPhones() {
return phones;
@Override
public boolean equals(Object o) {
if ( this == o ) {
return true;
}
if ( o == null || getClass() != o.getClass() ) {
return false;
}
Person person = (Person) o;
return Objects.equals( name, person.name );
}

@Override
public int hashCode() {
return Objects.hash( name );
}
}
}

0 comments on commit 903c34e

Please sign in to comment.