Skip to content

Commit

Permalink
Add toOne delete and update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dreab8 committed Dec 3, 2018
1 parent 41d2f96 commit a7aa2c6
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 13 deletions.
Expand Up @@ -13,9 +13,11 @@
import org.hibernate.orm.test.support.domains.gambit.EntityWithManyToOneJoinTable;
import org.hibernate.orm.test.support.domains.gambit.SimpleEntity;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;

/**
Expand All @@ -35,8 +37,19 @@ protected boolean exportSchema() {
return true;
}

@AfterEach
public void tearDown() {
sessionFactoryScope().inTransaction(
session -> {
final EntityWithManyToOneJoinTable loaded = session.get( EntityWithManyToOneJoinTable.class, 1 );
session.delete( loaded );
session.delete( loaded.getOther() );
}
);
}

@Test
public void testOperations() {
public void testSave() {
EntityWithManyToOneJoinTable entity = new EntityWithManyToOneJoinTable( 1, "first", Integer.MAX_VALUE );

SimpleEntity other = new SimpleEntity(
Expand Down Expand Up @@ -70,6 +83,27 @@ public void testOperations() {
assertThat( loaded.getSomeInteger(), equalTo( Integer.MAX_VALUE ) );
}
);
}

@Test
public void testHqlSelect() {
EntityWithManyToOneJoinTable entity = new EntityWithManyToOneJoinTable( 1, "first", Integer.MAX_VALUE );

SimpleEntity other = new SimpleEntity(
2,
Calendar.getInstance().getTime(),
null,
Integer.MAX_VALUE,
Long.MAX_VALUE,
null
);

entity.setOther( other );

sessionFactoryScope().inTransaction( session -> {
session.save( other );
session.save( entity );
} );

sessionFactoryScope().inTransaction(
session -> {
Expand All @@ -80,7 +114,53 @@ public void testOperations() {
assertThat( value, equalTo( "first" ) );
}
);
}

@Test
public void testUpdate() {
EntityWithManyToOneJoinTable entity = new EntityWithManyToOneJoinTable( 1, "first", Integer.MAX_VALUE );

SimpleEntity other = new SimpleEntity(
2,
Calendar.getInstance().getTime(),
null,
Integer.MAX_VALUE,
Long.MAX_VALUE,
null
);

entity.setOther( other );

sessionFactoryScope().inTransaction( session -> {
session.save( other );
session.save( entity );
} );

SimpleEntity anOther = new SimpleEntity(
3,
Calendar.getInstance().getTime(),
null,
Integer.MIN_VALUE,
Long.MIN_VALUE,
null
);

sessionFactoryScope().inTransaction(
session -> {
final EntityWithManyToOneJoinTable loaded = session.get( EntityWithManyToOneJoinTable.class, 1 );
assert loaded != null;
session.save( anOther );
loaded.setOther( anOther );
}
);

sessionFactoryScope().inTransaction(
session -> {
final EntityWithManyToOneJoinTable loaded = session.get( EntityWithManyToOneJoinTable.class, 1 );

assertThat( loaded.getOther(), notNullValue() );
assertThat( loaded.getOther().getId(), equalTo( 3 ) );
}
);
}
}
Expand Up @@ -101,6 +101,7 @@ public void testSave() {
assertThat( othersById.get( 3 ).getSomeInteger(), is( Integer.MIN_VALUE ) );
assertThat( othersById.get( 3 ).getSomeLong(), is( Long.MIN_VALUE ) );

} );
}
);
}
}
Expand Up @@ -13,9 +13,11 @@
import org.hibernate.orm.test.support.domains.gambit.EntityWithOneToOneJoinTable;
import org.hibernate.orm.test.support.domains.gambit.SimpleEntity;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;

/**
Expand All @@ -34,6 +36,17 @@ protected boolean exportSchema() {
return true;
}

@AfterEach
public void tearDown() {
sessionFactoryScope().inTransaction(
session -> {
final EntityWithOneToOneJoinTable loaded = session.get( EntityWithOneToOneJoinTable.class, 1 );
session.delete( loaded );
session.delete( loaded.getOther() );
}
);
}

@Test
public void testOperations() {
EntityWithOneToOneJoinTable entity = new EntityWithOneToOneJoinTable( 1, "first", Integer.MAX_VALUE );
Expand Down Expand Up @@ -80,4 +93,53 @@ public void testOperations() {
}
);
}

@Test
public void testUpdate() {
EntityWithOneToOneJoinTable entity = new EntityWithOneToOneJoinTable( 1, "first", Integer.MAX_VALUE );

SimpleEntity other = new SimpleEntity(
2,
Calendar.getInstance().getTime(),
null,
Integer.MAX_VALUE,
Long.MAX_VALUE,
null
);

entity.setOther( other );

sessionFactoryScope().inTransaction(
session -> {
session.save( other );
session.save( entity );
}
);

SimpleEntity anOther = new SimpleEntity(
3,
Calendar.getInstance().getTime(),
null,
Integer.MIN_VALUE,
Long.MAX_VALUE,
null
);

sessionFactoryScope().inTransaction(
session -> {
EntityWithOneToOneJoinTable loaded = session.get( EntityWithOneToOneJoinTable.class, 1 );
session.save( anOther );
loaded.setOther( anOther );
}
);

sessionFactoryScope().inTransaction(
session -> {
EntityWithOneToOneJoinTable loaded = session.get( EntityWithOneToOneJoinTable.class, 1 );
SimpleEntity loadedOther = loaded.getOther();
assertThat( loadedOther, notNullValue() );
assertThat( loadedOther.getId(), equalTo( 3 ) );
}
);
}
}
Expand Up @@ -8,8 +8,6 @@

import java.util.Calendar;

import org.hamcrest.CoreMatchers;

import org.hibernate.boot.MetadataSources;
import org.hibernate.orm.test.SessionFactoryBasedFunctionalTest;
import org.hibernate.orm.test.support.domains.gambit.EntityWithOneToOne;
Expand All @@ -19,8 +17,9 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import org.hamcrest.CoreMatchers;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;

/**
Expand Down Expand Up @@ -88,6 +87,42 @@ public void testGet() {
);
}

@Test
public void testUpdate(){
SimpleEntity other = new SimpleEntity(
3,
Calendar.getInstance().getTime(),
null,
Integer.MIN_VALUE,
Long.MIN_VALUE,
null
);

sessionFactoryScope().inTransaction(
session -> {
final EntityWithOneToOne loaded = session.get( EntityWithOneToOne.class, 1 );
assert loaded != null;
assertThat( loaded.getName(), equalTo( "first" ) );
assert loaded.getOther() != null;
assertThat( loaded.getOther().getId(), equalTo( 2 ) );
session.delete( loaded.getOther() );
loaded.setOther( other );
session.save( other );
}
);

sessionFactoryScope().inTransaction(
session -> {
final EntityWithOneToOne loaded = session.get( EntityWithOneToOne.class, 1 );
assert loaded != null;
assertThat( loaded.getName(), equalTo( "first" ) );
assert loaded.getOther() != null;
assertThat( loaded.getOther().getId(), equalTo( 3 ) );
}
);

}

@Test
public void testQueryParentAttribute2() {
sessionFactoryScope().inTransaction(
Expand Down Expand Up @@ -147,6 +182,7 @@ private void deleteAll() {
assert loaded != null;
assert loaded.getOther() != null;
session.remove( loaded );
session.remove( loaded.getOther() );
}
);

Expand All @@ -156,13 +192,5 @@ private void deleteAll() {
assertThat( notfound, CoreMatchers.nullValue() );
}
);

sessionFactoryScope().inTransaction(
session -> {
final SimpleEntity simpleEntity = session.find( SimpleEntity.class, 2 );
assertThat( simpleEntity, notNullValue() );
session.remove( simpleEntity );
}
);
}
}

0 comments on commit a7aa2c6

Please sign in to comment.