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,74 @@
package org.tests.query.embedded;
import io.ebean.BaseTestCase;
import io.ebean.DB;
import io.ebean.ExpressionList;
import org.junit.jupiter.api.Test;
import org.tests.model.embedded.EEmbDatePeriod;
import org.tests.model.embedded.EEmbInner;
import org.tests.model.embedded.EEmbOuter;
import java.util.Date;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Test that selecting A and filtering on 'B.C=some_value' will correctly create join on table for B.
* Model:
* - Bean A has relation to bean B
* - Bean B has an embedded property C
*/
public class TestFilteringByEmbeddedInJoinedTable extends BaseTestCase {
private EEmbOuter createOuter(String nomeOuter, Date date1, Date date2) {
EEmbOuter outer1 = new EEmbOuter();
outer1.setNomeOuter(nomeOuter);
EEmbDatePeriod eEmbDatePeriod = new EEmbDatePeriod();
eEmbDatePeriod.setDate1(date1);
eEmbDatePeriod.setDate2(date2);
outer1.setDatePeriod(eEmbDatePeriod);
DB.save(outer1);
return outer1;
}
@Test
public void testOuterTableJoined() {
EEmbOuter outer1 = createOuter("outer1", new Date(11111), new Date(12222));
// Unused outer2 just to populate the DB
EEmbOuter outer2 = createOuter("outer2", new Date(21111), new Date(22222));
assertThat(outer2).isNotNull();
EEmbOuter outer3 = createOuter("outer3", new Date(31111), new Date(32222));
EEmbInner inner1 = new EEmbInner();
inner1.setOuter(outer1);
inner1.setNomeInner("inner1-1");
DB.save(inner1);
EEmbInner inner2 = new EEmbInner();
// Setting outer1 also here
inner2.setOuter(outer1);
inner2.setNomeInner("inner2-1");
DB.save(inner2);
EEmbInner inner3 = new EEmbInner();
inner3.setOuter(outer3);
inner3.setNomeInner("inner3-3");
DB.save(inner3);
ExpressionList<EEmbInner> el = DB.find(EEmbInner.class).where()
.eq("outer.datePeriod.date1", new Date(11111));
List<EEmbInner> list = el.findList();
assertThat(list).hasSize(2);
}
}
@@ -0,0 +1,111 @@
package org.tests.query.embedded;
import io.ebean.BaseTestCase;
import io.ebean.DB;
import io.ebean.Database;
import io.ebean.cache.ServerCache;
import io.ebean.cache.ServerCacheManager;
import io.ebean.cache.ServerCacheStatistics;
import org.tests.model.embedded.EAddress;
import org.tests.model.embedded.EInvoice;
import org.tests.model.embedded.EInvoice.State;
import org.junit.jupiter.api.Test;
import java.util.Date;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
public class TestMultipleEmbeddedLoading extends BaseTestCase {
@Test
public void testSimpleCase() {
// prepare test
EAddress ship = new EAddress();
ship.setStreet("1 Banana St");
ship.setSuburb("Suburb");
ship.setCity("Auckland");
EAddress bill = new EAddress();
bill.setStreet("2 Apple St");
bill.setSuburb("Suburb");
bill.setCity("Auckland");
EInvoice invoice = new EInvoice();
invoice.setInvoiceDate(new Date(System.currentTimeMillis()));
invoice.setState(State.New);
invoice.setShipAddress(ship);
invoice.setBillAddress(bill);
// act: save and fetch
DB.save(invoice);
EInvoice invoice2 = DB.find(EInvoice.class, invoice.getId());
// assert fetched bean populated as expected
assertEquals(invoice.getId(), invoice2.getId());
assertEquals(invoice.getState(), invoice2.getState());
assertThat(invoice2.getInvoiceDate()).isEqualToIgnoringMillis(invoice.getInvoiceDate());
assertEquals("2 Apple St", invoice.getBillAddress().getStreet());
assertEquals("2 Apple St", invoice2.getBillAddress().getStreet());
// act: only update one of the embedded fields
invoice2.getBillAddress().setStreet("3 Pineapple St");
// bean should be dirty
DB.save(invoice2);
awaitL2Cache();
EInvoice invoice3 = DB.find(EInvoice.class, invoice.getId());
// assert field has updated value
assertEquals("3 Pineapple St", invoice3.getBillAddress().getStreet());
// fetch a partial
EInvoice invoicePartial = DB.find(EInvoice.class)
.select("state, date")
.where().idEq(invoice.getId())
.findOne();
// lazy load of embedded bean
EAddress billAddress = invoicePartial.getBillAddress();
assertNotNull(billAddress);
assertEquals("3 Pineapple St", billAddress.getStreet());
Database server = DB.getDefault();
ServerCacheManager serverCacheManager = server.cacheManager();
// get cache, clear the cache and statistics
ServerCache beanCache = serverCacheManager.beanCache(EInvoice.class);
beanCache.clear();
beanCache.statistics(true);
// fetch and load the cache
EInvoice invoice4 = DB.find(EInvoice.class, invoice.getId());
assertNotNull(invoice4);
ServerCacheStatistics statistics = beanCache.statistics(false);
assertEquals(1, statistics.getSize());
assertEquals(0, statistics.getHitCount());
// fetch out of the cache this time
EInvoice invoice5 = DB.find(EInvoice.class, invoice.getId());
assertNotNull(invoice5);
statistics = beanCache.statistics(false);
assertEquals(1, statistics.getSize());
assertEquals(1, statistics.getHitCount());
billAddress = invoice5.getBillAddress();
assertNotNull(billAddress);
assertEquals("3 Pineapple St", billAddress.getStreet());
}
}