Skip to content

Commit

Permalink
Refactored callable statement tests to avoid running a test twice
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-astachowski committed Nov 5, 2024
1 parent 4a97907 commit 73ce0fd
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 55 deletions.
52 changes: 5 additions & 47 deletions src/test/java/net/snowflake/client/jdbc/CallableStatementIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,62 +15,21 @@
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Time;
import java.sql.Timestamp;
import java.sql.Types;
import java.util.Calendar;
import java.util.HashMap;
import net.snowflake.client.category.TestTags;
import net.snowflake.client.providers.SimpleFormatProvider;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ArgumentsSource;

// @Category(TestCategoryStatement.class)
@Tag(TestTags.STATEMENT)
public class CallableStatementIT extends BaseJDBCTest {

public static Connection getConnection() throws SQLException {
return BaseJDBCTest.getConnection();
}

public static Connection getConnection(String queryResultFormat) throws SQLException {
Connection conn = BaseJDBCTest.getConnection();
try (Statement stmt = conn.createStatement()) {
stmt.execute("alter session set jdbc_query_result_format = '" + queryResultFormat + "'");
}
return conn;
}

private final String createStoredProcedure =
"create or replace procedure square_it(num FLOAT) returns float not "
+ "null language javascript as $$ return NUM * NUM; $$";
private final String createSecondStoredProcedure =
"create or replace procedure add_nums(x DOUBLE, y DOUBLE) "
+ "returns double not null language javascript as $$ return X + Y; $$";
private final String deleteStoredProcedure = "drop procedure if exists square_it(FLOAT)";
private final String deleteSecondStoredProcedure = "drop procedure if exists add_nums(INT, INT)";

@BeforeEach
public void setUp() throws SQLException {
try (Connection con = getConnection();
Statement statement = con.createStatement()) {
statement.execute(createStoredProcedure);
statement.execute(createSecondStoredProcedure);
}
}

@AfterEach
public void tearDown() throws SQLException {
try (Connection con = getConnection();
Statement statement = con.createStatement()) {
statement.execute(deleteStoredProcedure);
statement.execute(deleteSecondStoredProcedure);
}
}
public class CallableStatementIT extends CallableStatementITBase {

@ParameterizedTest
@ArgumentsSource(SimpleFormatProvider.class)
Expand Down Expand Up @@ -106,10 +65,9 @@ public void testPrepareCall(String queryResultFormat) throws SQLException {
}
}

@ParameterizedTest
@ArgumentsSource(SimpleFormatProvider.class)
public void testFeatureNotSupportedException(String queryResultFormat) throws Throwable {
try (Connection connection = getConnection(queryResultFormat); ) {
@Test
public void testFeatureNotSupportedException() throws Throwable {
try (Connection connection = getConnection()) {
CallableStatement callableStatement = connection.prepareCall("select ?");
expectFeatureNotSupportedException(
() -> callableStatement.registerOutParameter(1, Types.INTEGER));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package net.snowflake.client.jdbc;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;

public class CallableStatementITBase extends BaseJDBCTest {
public static Connection getConnection() throws SQLException {
return BaseJDBCTest.getConnection();
}

public static Connection getConnection(String queryResultFormat) throws SQLException {
Connection conn = BaseJDBCTest.getConnection();
try (Statement stmt = conn.createStatement()) {
stmt.execute("alter session set jdbc_query_result_format = '" + queryResultFormat + "'");
}
return conn;
}

private final String createStoredProcedure =
"create or replace procedure square_it(num FLOAT) returns float not "
+ "null language javascript as $$ return NUM * NUM; $$";
private final String createSecondStoredProcedure =
"create or replace procedure add_nums(x DOUBLE, y DOUBLE) "
+ "returns double not null language javascript as $$ return X + Y; $$";
private final String deleteStoredProcedure = "drop procedure if exists square_it(FLOAT)";
private final String deleteSecondStoredProcedure = "drop procedure if exists add_nums(INT, INT)";

@BeforeEach
public void setUp() throws SQLException {
try (Connection con = getConnection();
Statement statement = con.createStatement()) {
statement.execute(createStoredProcedure);
statement.execute(createSecondStoredProcedure);
}
}

@AfterEach
public void tearDown() throws SQLException {
try (Connection con = getConnection();
Statement statement = con.createStatement()) {
statement.execute(deleteStoredProcedure);
statement.execute(deleteSecondStoredProcedure);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,15 @@
import java.sql.SQLException;
import java.sql.Types;
import net.snowflake.client.category.TestTags;
import net.snowflake.client.providers.SimpleFormatProvider;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ArgumentsSource;

// @Category(TestCategoryStatement.class)
@Tag(TestTags.STATEMENT)
public class CallableStatementLatestIT extends CallableStatementIT {

public CallableStatementLatestIT(String format) {
super();
}
public class CallableStatementLatestIT extends CallableStatementITBase {

/**
* Test that function that removes curly brackets from outside of call statements works properly
Expand All @@ -45,10 +44,11 @@ public void testParseSqlEscapeSyntaxFunction() {
*
* @throws SQLException
*/
@Test
public void testPrepareCallWithCurlyBracketSyntax() throws SQLException {
@ParameterizedTest
@ArgumentsSource(SimpleFormatProvider.class)
public void testPrepareCallWithCurlyBracketSyntax(String queryResultFormat) throws SQLException {
// test CallableStatement with no binding parameters
try (Connection connection = getConnection()) {
try (Connection connection = getConnection(queryResultFormat)) {
try (CallableStatement callableStatement = connection.prepareCall("{call square_it(5)}")) {
assertThat(callableStatement.getParameterMetaData().getParameterCount(), is(0));
}
Expand Down

0 comments on commit 73ce0fd

Please sign in to comment.