Skip to content

Commit

Permalink
repo-sqale tests: shortcuts for new JDBC sessions/transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
virgo47 committed Sep 9, 2021
1 parent f2b27d6 commit fea8ee8
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public class SqaleRepoBaseTest extends AbstractSpringTest

@BeforeClass
public void clearDatabase() {
try (JdbcSession jdbcSession = sqlRepoContext.newJdbcSession().startTransaction()) {
try (JdbcSession jdbcSession = startTransaction()) {
// object delete cascades to sub-rows of the "object aggregate"

jdbcSession.executeStatement("TRUNCATE m_object CASCADE;");
Expand All @@ -78,7 +78,7 @@ public void clearDatabase() {

// this is "suite" scope code, but @BeforeSuite can't use injected fields
if (!cacheTablesCleared) {
try (JdbcSession jdbcSession = sqlRepoContext.newJdbcSession().startTransaction()) {
try (JdbcSession jdbcSession = startTransaction()) {
// We could skip default relation ID with .where(u.id.gt(0)), but it must work.
jdbcSession.newDelete(QUri.DEFAULT).execute();
jdbcSession.newDelete(QExtItem.DEFAULT).execute();
Expand All @@ -97,7 +97,7 @@ public void clearDatabase() {

// Called on demand
public void clearAudit() {
try (JdbcSession jdbcSession = sqlRepoContext.newJdbcSession().startTransaction()) {
try (JdbcSession jdbcSession = startTransaction()) {
jdbcSession.executeStatement("TRUNCATE ma_audit_event CASCADE;");
jdbcSession.commit();
display("AUDIT tables cleared");
Expand Down Expand Up @@ -141,7 +141,7 @@ protected <R, Q extends FlexibleRelationalPathBase<R>> long count(

protected <R, Q extends FlexibleRelationalPathBase<R>> long count(
Q path, Predicate... conditions) {
try (JdbcSession jdbcSession = sqlRepoContext.newJdbcSession().startReadOnlyTransaction()) {
try (JdbcSession jdbcSession = startReadOnlyTransaction()) {
SQLQuery<?> query = jdbcSession.newQuery()
.from(path)
.where(conditions);
Expand All @@ -156,7 +156,7 @@ protected <R, Q extends FlexibleRelationalPathBase<R>> List<R> select(

protected <R, Q extends FlexibleRelationalPathBase<R>> List<R> select(
Q path, Predicate... conditions) {
try (JdbcSession jdbcSession = sqlRepoContext.newJdbcSession().startReadOnlyTransaction()) {
try (JdbcSession jdbcSession = startReadOnlyTransaction()) {
return jdbcSession.newQuery()
.from(path)
.where(conditions)
Expand All @@ -174,7 +174,7 @@ protected <R, Q extends FlexibleRelationalPathBase<R>> List<R> select(

protected <R, Q extends FlexibleRelationalPathBase<R>> @Nullable R selectOneNullable(
Q path, Predicate... conditions) {
try (JdbcSession jdbcSession = sqlRepoContext.newJdbcSession().startReadOnlyTransaction()) {
try (JdbcSession jdbcSession = startReadOnlyTransaction()) {
return jdbcSession.newQuery()
.from(path)
.where(conditions)
Expand Down Expand Up @@ -335,7 +335,7 @@ private String extKey(Containerable extContainer, String itemName, MExtItemHolde
PrismContainerValue<?> pcv = extContainer.asPrismContainerValue();
ItemDefinition<?> def = pcv.getDefinition().findItemDefinition(new ItemName(itemName));
MExtItem.Key key = MExtItem.keyFrom(def, holder);
try (JdbcSession jdbcSession = sqlRepoContext.newJdbcSession().startReadOnlyTransaction()) {
try (JdbcSession jdbcSession = startReadOnlyTransaction()) {
QExtItem ei = QExtItem.DEFAULT;
return jdbcSession.newQuery()
.from(ei)
Expand Down Expand Up @@ -418,4 +418,12 @@ public ItemDefinition<?> getDefinition(ItemName attributeName) {
}
}
// endregion

protected JdbcSession startTransaction() {
return sqlRepoContext.newJdbcSession().startTransaction();
}

protected JdbcSession startReadOnlyTransaction() {
return sqlRepoContext.newJdbcSession().startReadOnlyTransaction();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public void test200CleanupByAge() throws SchemaException {
}

private long selectMinMaxId(QAuditEventRecord qae, NumberExpression<Long> minMaxPath) {
try (JdbcSession jdbcSession = sqlRepoContext.newJdbcSession().startReadOnlyTransaction()) {
try (JdbcSession jdbcSession = startReadOnlyTransaction()) {
return jdbcSession.newQuery()
.select(minMaxPath)
.from(qae)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ public void test125SearchIterativeWithCustomOrdering() throws Exception {

and("all objects were processed in proper order");
QUser u = aliasFor(QUser.class);
try (JdbcSession jdbcSession = sqlRepoContext.newJdbcSession().startReadOnlyTransaction()) {
try (JdbcSession jdbcSession = startReadOnlyTransaction()) {
List<String> result = jdbcSession.newQuery()
.from(u)
.orderBy(u.costCenter.asc(), u.oid.asc())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public void test020TestOrgClosureConsistency() throws Exception {
OperationResult result = createOperationResult();

given("reset closure");
try (JdbcSession jdbcSession = sqlRepoContext.newJdbcSession().startTransaction()) {
try (JdbcSession jdbcSession = startTransaction()) {
jdbcSession.executeStatement("CALL m_refresh_org_closure(true)");
jdbcSession.commit();
}
Expand Down Expand Up @@ -126,7 +126,7 @@ public void test021OrgClosureIsRefreshedBeforeOrgFilterQuery() throws Exception
OperationResult result = createOperationResult();

given("reset closure");
try (JdbcSession jdbcSession = sqlRepoContext.newJdbcSession().startTransaction()) {
try (JdbcSession jdbcSession = startTransaction()) {
jdbcSession.executeStatement("CALL m_refresh_org_closure(true)");
jdbcSession.commit();
}
Expand Down Expand Up @@ -365,7 +365,7 @@ public void test900WorkingWithPgArraysJsonbAndBytea() {
user.subtypes = new String[] { "subtype1", "subtype2" };
user.ext = new Jsonb("{\"key\" : \"value\",\n\"number\": 47} "); // more whitespaces/lines
user.photo = new byte[] { 0, 1, 0, 1 };
try (JdbcSession jdbcSession = sqlRepoContext.newJdbcSession().startTransaction()) {
try (JdbcSession jdbcSession = startTransaction()) {
jdbcSession.newInsert(u).populate(user).execute();
jdbcSession.commit();
}
Expand All @@ -378,7 +378,7 @@ public void test900WorkingWithPgArraysJsonbAndBytea() {
assertThat(row.photo).hasSize(4);

// setting NULLs
try (JdbcSession jdbcSession = sqlRepoContext.newJdbcSession().startTransaction()) {
try (JdbcSession jdbcSession = startTransaction()) {
jdbcSession.newUpdate(u)
.setNull(u.policySituations)
.set(u.subtypes, (String[]) null) // this should do the same
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class OrgHierarchyPerfTest extends SqaleRepoBaseTest {
public void initObjects() throws Exception {
OperationResult result = createOperationResult();

try (JdbcSession jdbcSession = sqlRepoContext.newJdbcSession().startTransaction()) {
try (JdbcSession jdbcSession = startTransaction()) {
jdbcSession.executeStatement("CALL m_refresh_org_closure(true)");
jdbcSession.commit();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public void test210TenThreadsReturning() throws Exception {
private void concurrencyUniversal(
long duration, WorkerThread[] workerThreads, boolean alwaysOrder) throws Exception {

try (JdbcSession jdbcSession = sqlRepoContext.newJdbcSession().startTransaction()) {
try (JdbcSession jdbcSession = startTransaction()) {
System.out.println(">>>>" + jdbcSession.connection().getTransactionIsolation());
}

Expand Down

0 comments on commit fea8ee8

Please sign in to comment.