Refactor move tests from ebean-core to ebean-test

This commit is contained in:
rbygrave committed 2021-09-08 22:49:23 +12:00
1 parent 0b00a14fd3
commit eab56d323b
1650 files changed
+185 -147

No files matched your search

@@ -0,0 +1,19 @@
package org.tests.iud;
import javax.persistence.Entity;
import javax.persistence.OneToMany;
import java.util.ArrayList;
import java.util.List;
import static javax.persistence.CascadeType.ALL;
@Entity
public class PcfCalendar extends PcfModel {
@OneToMany(cascade = ALL, orphanRemoval = true)
private List<PcfEvent> events = new ArrayList<>();
public void addEvent(PcfEvent event) {
events.add(event);
}
}
@@ -0,0 +1,24 @@
package org.tests.iud;
import javax.persistence.Entity;
import javax.persistence.OneToOne;
import static javax.persistence.CascadeType.ALL;
@Entity
public class PcfCity extends PcfModel {
private final String name;
@OneToOne(cascade = ALL, optional = false)
private PcfPerson mayor;
@OneToOne(cascade = ALL, optional = false)
private PcfPerson viceMayor;
public PcfCity(String name, PcfPerson mayor, PcfPerson viceMayor) {
this.name = name;
this.mayor = mayor;
this.viceMayor = viceMayor;
}
}
@@ -0,0 +1,20 @@
package org.tests.iud;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.OneToMany;
import java.util.ArrayList;
import java.util.List;
@Entity
public class PcfCountry extends PcfModel {
@OneToMany(cascade = CascadeType.ALL)
private List<PcfCity> cities = new ArrayList<>();
public void addCity(PcfCity city) {
cities.add(city);
}
}
@@ -0,0 +1,13 @@
package org.tests.iud;
import javax.persistence.Entity;
@Entity
public class PcfEvent extends PcfModel {
final String name;
public PcfEvent(String name) {
this.name = name;
}
}
@@ -0,0 +1,18 @@
package org.tests.iud;
import io.ebean.Model;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.persistence.Version;
@MappedSuperclass
public class PcfModel extends Model {
@Id
long id;
@Version
long version;
}
@@ -0,0 +1,26 @@
package org.tests.iud;
import javax.persistence.Entity;
import javax.persistence.OneToMany;
import java.util.ArrayList;
import java.util.List;
import static javax.persistence.CascadeType.ALL;
@Entity
public class PcfPerson extends PcfModel {
private final String name;
@OneToMany(cascade = ALL, orphanRemoval = true)
private List<PcfCalendar> calendars = new ArrayList<>();
public PcfPerson(String name) {
this.name = name;
}
public void addCalendar(PcfCalendar calendar) {
calendars.add(calendar);
}
}
@@ -0,0 +1,102 @@
package org.tests.iud;
import io.ebean.BaseTestCase;
import io.ebean.DB;
import io.ebean.DB;
import io.ebean.PagedList;
import org.ebeantest.LoggedSqlCollector;
import org.junit.jupiter.api.Test;
import org.tests.model.carwheel.Car;
import org.tests.model.carwheel.Tire;
import org.tests.model.carwheel.Wheel;
import java.util.ArrayList;
import java.util.List;
import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;
public class TestCarWheelIud extends BaseTestCase {
@Test
public void test() {
Car car = new Car();
Tire t1 = new Tire();
Wheel w1 = new Wheel();
w1.setCar(car);
w1.setTire(t1);
Tire t2 = new Tire();
Wheel w2 = new Wheel();
w2.setCar(car);
w2.setTire(t2);
Tire t3 = new Tire();
Wheel w3 = new Wheel();
w3.setCar(car);
w3.setTire(t3);
Tire t4 = new Tire();
Wheel w4 = new Wheel();
w4.setCar(car);
w4.setTire(t4);
List<Wheel> wheels = new ArrayList<>();
wheels.add(w1);
wheels.add(w2);
wheels.add(w3);
wheels.add(w4);
car.setWheels(wheels);
DB.save(car);
// And I'm trying to delete this entry with code:
Car car2 = DB.find(Car.class, car.getId());
DB.delete(car2);
}
@Test
public void aggregatePaging() {
DB.find(Car.class).delete();
final Car car0 = createCar("Ford", 40);
final Car car1 = createCar("Ford", 50);
final Car car2 = createCar("Mazda", 12);
DB.saveAll(asList(car0, car1, car2));
LoggedSqlCollector.start();
final PagedList<Car> pagedList = DB.find(Car.class)
.select("brand, totalSold")
.setMaxRows(10)
.findPagedList();
final List<Car> list = pagedList.getList();
final int count = pagedList.getTotalCount();
assertThat(list).hasSize(2);
assertThat(count).isEqualTo(2);
final List<String> sql = LoggedSqlCollector.stop();
assertThat(sql).hasSize(2);
if (isH2() || isPostgres()) {
assertSql(sql.get(0)).contains("select t0.brand, sum(t0.sold) from sa_car t0 group by t0.brand limit 10");
assertSql(sql.get(1)).contains("select count(*) from ( select t0.brand, sum(t0.sold) from sa_car t0 group by t0.brand)");
}
}
private Car createCar(String brand, int sold) {
Car car0 = new Car();
car0.setBrand(brand);
car0.setSold(sold);
return car0;
}
}
@@ -0,0 +1,63 @@
package org.tests.iud;
import io.ebean.BaseTestCase;
import io.ebean.DB;
import org.tests.model.info.InfoCompany;
import org.tests.model.info.InfoCustomer;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
public class TestInfoOneToOne extends BaseTestCase {
@Test
public void test_cascade_oneToOne() {
InfoCompany company = new InfoCompany();
company.setName("info company");
InfoCustomer customer = new InfoCustomer();
customer.setName("first info cust");
customer.setCompany(company);
customer.save();
// assert JsonIgnore working as expected
String asJson = DB.json().toJson(company);
assertThat(asJson).doesNotContain("contacts");
// assert both are inserted
assertNotNull(customer.getId());
assertNotNull(company.getId());
// both can be fetched
assertNotNull(InfoCustomer.find.byId(customer.getId()));
assertNotNull(InfoCompany.find.byId(company.getId()));
// just update the customer
customer.setName("first mod");
customer.update();
// update the customer and company
customer.getCompany().setName("2nd mod company");
customer.setName("2nd mod customer");
customer.update();
// fetch and then update both
InfoCustomer fetchedCustomer = InfoCustomer.find.byId(customer.getId());
fetchedCustomer.getCompany().setName("3rd mod company");
fetchedCustomer.setName("3rd mod customer");
fetchedCustomer.update();
// delete both customer and company
fetchedCustomer.delete();
assertNull(InfoCustomer.find.byId(customer.getId()));
assertNull(InfoCompany.find.byId(company.getId()));
}
}
@@ -0,0 +1,40 @@
package org.tests.iud;
import io.ebean.BaseTestCase;
import io.ebean.BeanState;
import io.ebean.DB;
import org.tests.model.basic.EBasicVer;
import org.junit.jupiter.api.Test;
import java.util.Set;
import static org.junit.jupiter.api.Assertions.assertFalse;
public class TestInsertQueryUpdate extends BaseTestCase {
@Test
public void test() {
EBasicVer e0 = new EBasicVer("name0");
e0.setDescription("desc0");
DB.save(e0);
EBasicVer e1 = DB.find(EBasicVer.class)
.select("name")
.setId(e0.getId())
.findOne();
BeanState beanState = DB.beanState(e1);
Set<String> loadedProps = beanState.loadedProps();
assertFalse(loadedProps.contains("description"));
//lastUpdate
e1.setName("name1");
DB.save(e1);
e1.setDescription("desc1");
DB.save(e1);
}
}
@@ -0,0 +1,42 @@
package org.tests.iud;
import io.ebean.BaseTestCase;
import io.ebean.DB;
import io.ebean.Transaction;
import org.junit.jupiter.api.Test;
import org.tests.model.basic.EBasicVer;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
public class TestInsertUpdateTrans extends BaseTestCase {
@Test
public void test() {
try (Transaction txn = DB.beginTransaction()) {
EBasicVer e0 = new EBasicVer("onInsert");
e0.setDescription("something");
DB.save(e0);
assertNotNull(e0.getId());
assertNotNull(e0.getLastUpdate());
assertEquals("onInsert", e0.getName());
e0.setName("onUpdate");
e0.setDescription("differentFromInsert");
DB.save(e0);
EBasicVer e1 = DB.find(EBasicVer.class, e0.getId());
// we should fetch back the updated data (not inserted)
assertEquals(e0.getId(), e1.getId());
assertEquals("onUpdate", e1.getName());
assertEquals("differentFromInsert", e1.getDescription());
txn.commit();
}
}
}
@@ -0,0 +1,60 @@
package org.tests.iud;
import io.avaje.moduuid.ModUUID;
import io.ebean.BaseTestCase;
import org.ebeantest.LoggedSqlCollector;
import org.junit.jupiter.api.Test;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
public class TestPersistCascade extends BaseTestCase {
@Test
public void insert() {
PcfCountry country = new PcfCountry();
for (int a = 0; a < 3; a++) {
PcfPerson mayor = createPerson();
PcfPerson viceMayor = createPerson();
country.addCity(new PcfCity("city_" + ModUUID.newShortId(), mayor, viceMayor));
}
LoggedSqlCollector.start();
country.save();
final List<String> sql = LoggedSqlCollector.stop();
if (isPersistBatchOnCascade()) {
assertSql(sql.get(0)).contains("insert into pcf_country");
assertSql(sql.get(1)).contains("insert into pcf_person");
assertSqlBind(sql, 2, 7);
assertThat(sql.get(8)).contains("insert into pcf_calendar");
assertSqlBind(sql, 9, 20);
assertThat(sql.get(21)).contains("insert into pcf_city");
assertSqlBind(sql, 22, 24);
assertThat(sql.get(25)).contains("insert into pcf_event");
assertSqlBind(sql, 26, 45);
assertThat(sql.get(46)).contains("insert into pcf_event");
assertSqlBind(sql, 47, 66);
assertThat(sql.get(67)).contains("insert into pcf_event");
assertSqlBind(sql, 68, 87);
}
country.deletePermanent();
}
private static PcfPerson createPerson() {
PcfPerson person = new PcfPerson("person_" + ModUUID.newShortId());
for (int a = 0; a < 2; a++) {
PcfCalendar calendar = new PcfCalendar();
for (int b = 0; b < 10; b++) {
calendar.addEvent(new PcfEvent("evt_" + ModUUID.newShortId()));
}
person.addCalendar(calendar);
}
return person;
}
}