mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-19 23:02:24 +00:00
Refactor move tests from ebean-core to ebean-test
This commit is contained in:
1 parent
0b00a14fd3
commit
eab56d323b
1650 files changed
+185
-147
No files matched your search
@@ -0,0 +1,50 @@
|
||||
package org.tests.ddl;
|
||||
|
||||
import io.ebean.annotation.ConstraintMode;
|
||||
import io.ebean.annotation.DbForeignKey;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToOne;
|
||||
|
||||
@Entity
|
||||
public class DfkCascade {
|
||||
|
||||
@Id
|
||||
long id;
|
||||
|
||||
String name;
|
||||
|
||||
@ManyToOne
|
||||
@DbForeignKey(onDelete = ConstraintMode.CASCADE)
|
||||
DfkCascadeOne one;
|
||||
|
||||
public DfkCascade(String name, DfkCascadeOne one) {
|
||||
this.name = name;
|
||||
this.one = one;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public DfkCascadeOne getOne() {
|
||||
return one;
|
||||
}
|
||||
|
||||
public void setOne(DfkCascadeOne one) {
|
||||
this.one = one;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package org.tests.ddl;
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.OneToMany;
|
||||
import java.util.List;
|
||||
|
||||
@Entity
|
||||
public class DfkCascadeOne {
|
||||
|
||||
@Id
|
||||
long id;
|
||||
|
||||
String name;
|
||||
|
||||
@OneToMany(mappedBy = "one", cascade = CascadeType.ALL)
|
||||
List<DfkCascade> details;
|
||||
|
||||
public DfkCascadeOne(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public List<DfkCascade> getDetails() {
|
||||
return details;
|
||||
}
|
||||
|
||||
public void setDetails(List<DfkCascade> details) {
|
||||
this.details = details;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package org.tests.ddl;
|
||||
|
||||
import io.ebean.annotation.DbForeignKey;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToOne;
|
||||
|
||||
@Entity
|
||||
public class DfkNone {
|
||||
|
||||
@Id
|
||||
long id;
|
||||
|
||||
String name;
|
||||
|
||||
@ManyToOne
|
||||
@DbForeignKey(noConstraint = true)
|
||||
DfkOne one;
|
||||
|
||||
public DfkNone(String name, DfkOne one) {
|
||||
this.name = name;
|
||||
this.one = one;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public DfkOne getOne() {
|
||||
return one;
|
||||
}
|
||||
|
||||
public void setOne(DfkOne one) {
|
||||
this.one = one;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package org.tests.ddl;
|
||||
|
||||
import javax.persistence.ConstraintMode;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.ForeignKey;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.ManyToOne;
|
||||
|
||||
@Entity
|
||||
public class DfkNoneViaJoin {
|
||||
|
||||
@Id
|
||||
long id;
|
||||
|
||||
String name;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "one_id", foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
|
||||
DfkOne one;
|
||||
|
||||
public DfkNoneViaJoin(String name, DfkOne one) {
|
||||
this.name = name;
|
||||
this.one = one;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public DfkOne getOne() {
|
||||
return one;
|
||||
}
|
||||
|
||||
public void setOne(DfkOne one) {
|
||||
this.one = one;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package org.tests.ddl;
|
||||
|
||||
import io.ebean.annotation.DbForeignKey;
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToMany;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Entity
|
||||
public class DfkNoneViaMtoM {
|
||||
|
||||
@Id
|
||||
long id;
|
||||
|
||||
String name;
|
||||
|
||||
/**
|
||||
* Have no Foreign key constraints on the intersection table.
|
||||
*/
|
||||
@ManyToMany(cascade = CascadeType.ALL)
|
||||
@DbForeignKey(noConstraint = true)
|
||||
List<DfkOne> ones = new ArrayList<>();
|
||||
|
||||
public DfkNoneViaMtoM(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public List<DfkOne> getOnes() {
|
||||
return ones;
|
||||
}
|
||||
|
||||
public void setOnes(List<DfkOne> ones) {
|
||||
this.ones = ones;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package org.tests.ddl;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class DfkOne {
|
||||
|
||||
@Id
|
||||
long id;
|
||||
|
||||
String name;
|
||||
|
||||
public DfkOne(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package org.tests.ddl;
|
||||
|
||||
import io.ebean.annotation.ConstraintMode;
|
||||
import io.ebean.annotation.DbForeignKey;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToOne;
|
||||
|
||||
@Entity
|
||||
public class DfkSetNull {
|
||||
|
||||
@Id
|
||||
long id;
|
||||
|
||||
String name;
|
||||
|
||||
@ManyToOne
|
||||
@DbForeignKey(onDelete = ConstraintMode.SET_NULL)
|
||||
DfkOne one;
|
||||
|
||||
public DfkSetNull(String name, DfkOne one) {
|
||||
this.name = name;
|
||||
this.one = one;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public DfkOne getOne() {
|
||||
return one;
|
||||
}
|
||||
|
||||
public void setOne(DfkOne one) {
|
||||
this.one = one;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,133 @@
|
||||
package org.tests.ddl;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.annotation.IgnorePlatform;
|
||||
import io.ebean.annotation.Platform;
|
||||
import org.ebeantest.LoggedSqlCollector;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestForeignKeyModes extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void none() {
|
||||
|
||||
DfkOne one = new DfkOne("one");
|
||||
DB.save(one);
|
||||
|
||||
DfkNone none = new DfkNone("none", one);
|
||||
DB.save(none);
|
||||
|
||||
// fails unless there is no Foreign key ...
|
||||
DB.delete(one);
|
||||
|
||||
DfkNone found = DB.find(DfkNone.class, none.getId());
|
||||
assertThat(found).isNotNull();
|
||||
// we still reference one ... even though it does not exist anymore
|
||||
assertThat(found.getOne()).isNotNull();
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void noneViaJoin() {
|
||||
|
||||
DfkOne one = new DfkOne("one2");
|
||||
DB.save(one);
|
||||
|
||||
DfkNoneViaJoin none = new DfkNoneViaJoin("none2", one);
|
||||
DB.save(none);
|
||||
|
||||
// fails unless there is no Foreign key ...
|
||||
DB.delete(one);
|
||||
|
||||
DfkNoneViaJoin found = DB.find(DfkNoneViaJoin.class, none.getId());
|
||||
assertThat(found).isNotNull();
|
||||
// we still reference one ... even though it does not exist anymore
|
||||
assertThat(found.getOne()).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void noneViaManyToMany() {
|
||||
|
||||
DfkOne one = DB.reference(DfkOne.class, 999L);
|
||||
|
||||
DfkNoneViaMtoM none = new DfkNoneViaMtoM("none2");
|
||||
none.getOnes().add(one);
|
||||
|
||||
// Would normally fail as DfkOne id:999 is not actually in Database
|
||||
// and with the foreign key constraint on the intersection table the
|
||||
// insert into the intersection table would fail
|
||||
DB.save(none);
|
||||
|
||||
DfkNoneViaMtoM found = DB.find(DfkNoneViaMtoM.class)
|
||||
.setId(none.getId())
|
||||
.fetch("ones", "id")
|
||||
.findOne();
|
||||
|
||||
assertThat(found).isNotNull();
|
||||
|
||||
// but we can't actually fetch it via ORM M2M ... as it joins across
|
||||
// to DfkOne and that doesn't exist
|
||||
assertThat(found.getOnes()).isNotNull();
|
||||
assertThat(found.getOnes()).hasSize(0);
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
DB.delete(none);
|
||||
|
||||
List<String> sql = LoggedSqlCollector.stop();
|
||||
assertThat(sql).hasSize(3);
|
||||
assertSql(sql.get(0)).contains("delete from dfk_none_via_mto_m_dfk_one where dfk_none_via_mto_m_id = ?");
|
||||
assertSqlBind(sql.get(1));
|
||||
assertSql(sql.get(2)).contains("delete from dfk_none_via_mto_m where id=?");
|
||||
}
|
||||
|
||||
@IgnorePlatform(Platform.NUODB)
|
||||
@Test
|
||||
public void setNullOnDelete() {
|
||||
|
||||
DfkOne one = new DfkOne("one2");
|
||||
DB.save(one);
|
||||
|
||||
DfkSetNull other = new DfkSetNull("none", one);
|
||||
DB.save(other);
|
||||
|
||||
// success with ... fkey value set to null
|
||||
DB.delete(one);
|
||||
|
||||
DfkSetNull found = DB.find(DfkSetNull.class, other.getId());
|
||||
assertThat(found).isNotNull();
|
||||
assertThat(found.getOne()).isNull();
|
||||
}
|
||||
|
||||
@IgnorePlatform(Platform.NUODB)
|
||||
@Test
|
||||
public void onDeleteCascade() {
|
||||
|
||||
DfkCascadeOne one = new DfkCascadeOne("one3");
|
||||
|
||||
|
||||
DfkCascade other = new DfkCascade("cascade1", one);
|
||||
one.getDetails().add(other);
|
||||
one.getDetails().add(new DfkCascade("cascade2", one));
|
||||
one.getDetails().add(new DfkCascade("cascade3", one));
|
||||
|
||||
DB.save(one);
|
||||
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
DB.delete(one);
|
||||
|
||||
List<String> sql = LoggedSqlCollector.stop();
|
||||
|
||||
assertThat(sql).hasSize(1);
|
||||
assertSql(sql.get(0)).contains("delete from dfk_cascade_one where id=?");
|
||||
|
||||
DfkCascade found = DB.find(DfkCascade.class, other.getId());
|
||||
assertThat(found).isNull();
|
||||
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user