Skip to content

Commit

Permalink
Refactor AdditionalDMLE2EIT test logic
Browse files Browse the repository at this point in the history
  • Loading branch information
strongduanmu committed Sep 20, 2023
1 parent 41d9f0c commit aec21b0
Showing 1 changed file with 14 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void assertExecuteUpdateWithAutoGeneratedKeys(final AssertionTestParameter testP
if (null == testParam.getTestCaseContext()) {
return;
}
if (isPostgreSQLOrOpenGauss(testParam.getDatabaseType().getType())) {
if (isPostgreSQLOrOpenGauss(testParam.getDatabaseType().getType()) || isOracleInsertStatement(testParam.getDatabaseType().getType(), testParam.getTestCaseContext().getTestCase().getSql())) {
return;
}
SingleE2EContainerComposer containerComposer = new SingleE2EContainerComposer(testParam);
Expand All @@ -65,14 +65,19 @@ void assertExecuteUpdateWithAutoGeneratedKeys(final AssertionTestParameter testP
assertDataSet(testParam, containerComposer, actualUpdateCount);
}

// TODO support oracle insert statement return auto generated keys
private boolean isOracleInsertStatement(final String databaseType, final String sql) {
return "Oracle".equals(databaseType) && sql.toUpperCase().startsWith("INSERT");
}

private boolean isPostgreSQLOrOpenGauss(final String databaseType) {
return "PostgreSQL".equals(databaseType) || "openGauss".equals(databaseType);
}

private int executeUpdateForStatementWithAutoGeneratedKeys(final AssertionTestParameter testParam,
final SingleE2EContainerComposer containerComposer, final Connection connection) throws SQLException {
try (Statement statement = connection.createStatement()) {
int result = statement.executeUpdate(String.format(containerComposer.getSQL(), containerComposer.getAssertion().getSQLValues().toArray()), Statement.RETURN_GENERATED_KEYS);
int result = statement.executeUpdate(containerComposer.getSQL(), Statement.RETURN_GENERATED_KEYS);
assertGeneratedKeys(testParam, containerComposer, statement.getGeneratedKeys());
return result;
}
Expand Down Expand Up @@ -112,7 +117,7 @@ void assertExecuteUpdateWithColumnIndexes(final AssertionTestParameter testParam

private int executeUpdateForStatementWithColumnIndexes(final SingleE2EContainerComposer containerComposer, final Connection connection) throws SQLException {
try (Statement statement = connection.createStatement()) {
return statement.executeUpdate(String.format(containerComposer.getSQL(), containerComposer.getAssertion().getSQLValues().toArray()), new int[]{1});
return statement.executeUpdate(containerComposer.getSQL(), new int[]{1});
}
}

Expand Down Expand Up @@ -147,7 +152,7 @@ void assertExecuteUpdateWithColumnNames(final AssertionTestParameter testParam)

private int executeUpdateForStatementWithColumnNames(final SingleE2EContainerComposer containerComposer, final Connection connection) throws SQLException {
try (Statement statement = connection.createStatement()) {
return statement.executeUpdate(String.format(containerComposer.getSQL(), containerComposer.getAssertion().getSQLValues().toArray()));
return statement.executeUpdate(containerComposer.getSQL());
}
}

Expand Down Expand Up @@ -184,7 +189,7 @@ void assertExecuteWithoutAutoGeneratedKeys(final AssertionTestParameter testPara

private int executeForStatementWithoutAutoGeneratedKeys(final SingleE2EContainerComposer containerComposer, final Connection connection) throws SQLException {
try (Statement statement = connection.createStatement()) {
assertFalse(statement.execute(String.format(containerComposer.getSQL(), containerComposer.getAssertion().getSQLValues().toArray()), Statement.NO_GENERATED_KEYS), "Not a DML statement.");
assertFalse(statement.execute(containerComposer.getSQL(), Statement.NO_GENERATED_KEYS), "Not a DML statement.");
return statement.getUpdateCount();
}
}
Expand All @@ -207,7 +212,7 @@ void assertExecuteWithAutoGeneratedKeys(final AssertionTestParameter testParam)
if (null == testParam.getTestCaseContext()) {
return;
}
if (isPostgreSQLOrOpenGauss(testParam.getDatabaseType().getType())) {
if (isPostgreSQLOrOpenGauss(testParam.getDatabaseType().getType()) || isOracleInsertStatement(testParam.getDatabaseType().getType(), testParam.getTestCaseContext().getTestCase().getSql())) {
return;
}
SingleE2EContainerComposer containerComposer = new SingleE2EContainerComposer(testParam);
Expand All @@ -224,8 +229,7 @@ void assertExecuteWithAutoGeneratedKeys(final AssertionTestParameter testParam)
private int executeForStatementWithAutoGeneratedKeys(final AssertionTestParameter testParam,
final SingleE2EContainerComposer containerComposer, final Connection connection) throws SQLException {
try (Statement statement = connection.createStatement()) {
assertFalse(statement.execute(
String.format(containerComposer.getSQL(), containerComposer.getAssertion().getSQLValues().toArray()), Statement.RETURN_GENERATED_KEYS), "Not a DML statement.");
assertFalse(statement.execute(containerComposer.getSQL(), Statement.RETURN_GENERATED_KEYS), "Not a DML statement.");
assertGeneratedKeys(testParam, containerComposer, statement.getGeneratedKeys());
return statement.getUpdateCount();
}
Expand Down Expand Up @@ -265,7 +269,7 @@ void assertExecuteWithColumnIndexes(final AssertionTestParameter testParam) thro

private int executeForStatementWithColumnIndexes(final SingleE2EContainerComposer containerComposer, final Connection connection) throws SQLException {
try (Statement statement = connection.createStatement()) {
assertFalse(statement.execute(String.format(containerComposer.getSQL(), containerComposer.getAssertion().getSQLValues().toArray()), new int[]{1}), "Not a DML statement.");
assertFalse(statement.execute(containerComposer.getSQL(), new int[]{1}), "Not a DML statement.");
return statement.getUpdateCount();
}
}
Expand Down Expand Up @@ -302,7 +306,7 @@ void assertExecuteWithColumnNames(final AssertionTestParameter testParam) throws

private int executeForStatementWithColumnNames(final SingleE2EContainerComposer containerComposer, final Connection connection) throws SQLException {
try (Statement statement = connection.createStatement()) {
assertFalse(statement.execute(String.format(containerComposer.getSQL(), containerComposer.getAssertion().getSQLValues().toArray()), new String[]{"TODO"}), "Not a DML statement.");
assertFalse(statement.execute(containerComposer.getSQL(), new String[]{"TODO"}), "Not a DML statement.");
return statement.getUpdateCount();
}
}
Expand Down

0 comments on commit aec21b0

Please sign in to comment.