#1934 - ENH: Add methods to Model that take explicit transaction - save(transaction), delete(transaction) ...

This commit is contained in:
rob bygrave
2020-02-07 13:00:59 +13:00
parent 4ed3a18b9d
commit 8b4eae2f91
2 changed files with 79 additions and 0 deletions
@@ -1,6 +1,7 @@
package org.tests.transaction;
import io.ebean.BaseTestCase;
import io.ebean.DB;
import io.ebean.Database;
import io.ebean.DatabaseFactory;
import io.ebean.Query;
@@ -131,4 +132,47 @@ public class TestExplicitTransactionMode extends BaseTestCase {
super(JsonConfig.Date.ISO8601);
}
}
@Test
public void modelSaveWithTransaction() {
try (Transaction txn = DB.getDefault().createTransaction()) {
UTMaster bean1 = new UTMaster("otherSave");
bean1.save(txn);
assertThat(DB.find(UTMaster.class).usingTransaction(txn)
.where().eq("name", "otherSave").findOne()).isNotNull();
bean1.deletePermanent(txn);
assertThat(DB.find(UTMaster.class).usingTransaction(txn)
.where().eq("name", "otherSave").findOne())
.isNull();
UTMaster bean2 = new UTMaster("otherInsert");
bean2.insert(txn);
assertTrue(DB.find(UTMaster.class).usingTransaction(txn)
.where().eq("name", "otherInsert").exists());
bean2.setDescription("changed description");
bean2.update(txn);
assertThat(DB.find(UTMaster.class).usingTransaction(txn)
.where().eq("name", "otherInsert").findOne())
.isNotNull()
.extracting(UTMaster::getDescription).contains("changed description");
UTMaster bean3 = new UTMaster("otherThree");
bean3.save(txn);
assertThat(DB.find(UTMaster.class).usingTransaction(txn)
.where().eq("name", "otherThree").findOne())
.isNotNull();
bean3.delete(txn);
assertThat(DB.find(UTMaster.class).usingTransaction(txn)
.where().eq("name", "otherThree").findOne())
.isNull();
}
}
}