Skip to content

Commit

Permalink
SNOW-1018737: Fix testSessionVariables (#1616)
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-dprzybysz authored Jan 25, 2024
1 parent 86b6528 commit ab5e561
Showing 1 changed file with 21 additions and 57 deletions.
78 changes: 21 additions & 57 deletions src/test/java/net/snowflake/client/jdbc/SessionVariablesIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,77 +3,41 @@
*/
package net.snowflake.client.jdbc;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.logging.Logger;
import net.snowflake.client.AbstractDriverIT;
import net.snowflake.client.ConditionalIgnoreRule;
import net.snowflake.client.RunningOnGithubAction;
import net.snowflake.client.category.TestCategoryOthers;
import org.junit.Test;
import org.junit.experimental.categories.Category;

/** Created by tcruanes on 4/24/17. */
@Category(TestCategoryOthers.class)
public final class SessionVariablesIT extends AbstractDriverIT {
private static Logger logger = Logger.getLogger(BaseJDBCTest.class.getName());

private static void sql(final Connection connection, String sqlText) throws SQLException {
// Create a warehouse for the test
Statement stmt = connection.createStatement();
stmt.setMaxRows(1);
boolean hasResultSet = stmt.execute(sqlText);
if (hasResultSet) {
assertTrue(stmt.getResultSet().next());
}
stmt.close();
}

@Test
@ConditionalIgnoreRule.ConditionalIgnore(condition = RunningOnGithubAction.class)
public void testSessionVariables() throws SQLException, IOException {
// build connection properties
public void testSettingSessionVariablesInConnectionProperties() throws SQLException {
Properties properties = new Properties();
properties.put("var1", "some example");
properties.put("var2", "1");
properties.put("$var3", "some example");
properties.put("$var4", "1");

// Open a connection under the snowflake account and enable variable support
Connection con = getSnowflakeAdminConnection(properties);
sql(
con,
"alter system set"
+ " enable_assignment_scalar=true,"
+ " enable_assignment_statement=true");
con.close();

// Create a new connection passing along some variables.
properties.put("$var1", "some example");
properties.put("$var2", 10L);
properties.put("var1", "ignored");
properties.put("var2", "ignored");
con = getConnection(properties);

// Check that the variables are set in the session
Statement statement = con.createStatement();
ResultSet resultSet = statement.executeQuery("show variables");

// expecting at least two rows... up to 4 variables
assertTrue(resultSet.next());
assertTrue(resultSet.next());

statement.close();
con.close();

con = getSnowflakeAdminConnection(properties);
sql(con, "alter system unset enable_assignment_scalar, enable_assignment_statement");
con.close();
properties.put("$var1", "some example 1");
properties.put("$var2", "2");
properties.put("var3", "some example 3");

try (Connection con = getConnection(properties);
Statement statement = con.createStatement();
ResultSet resultSet = statement.executeQuery("show variables")) {
Map<String, String> variablesInSession = new HashMap<>();
while (resultSet.next()) {
variablesInSession.put(resultSet.getString("name"), resultSet.getString("value"));
}

assertEquals("some example 1", variablesInSession.get("VAR1"));
assertEquals("2", variablesInSession.get("VAR2"));
assertNull(variablesInSession.get("VAR3"));
}
}
}

0 comments on commit ab5e561

Please sign in to comment.