Skip to content

Commit

Permalink
SNOW-1001928: Fix flaky test DatabaseMetaDataLatestIT.testHandlingSpe…
Browse files Browse the repository at this point in the history
…cialChars (#1599)

* SNOW-1001928: Fix flaky test DatabaseMetaDataLatestIT.testHandlingSpecialChars
* SNOW-1001928: Create and drop schema in Metadata tests inside lambda
  • Loading branch information
sfc-gh-dprzybysz authored Jan 9, 2024
1 parent e1ed9e6 commit 42f377a
Show file tree
Hide file tree
Showing 4 changed files with 265 additions and 213 deletions.
38 changes: 38 additions & 0 deletions src/test/java/net/snowflake/client/TestUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.sql.Statement;
import java.util.regex.Pattern;
import net.snowflake.client.core.SFException;
import net.snowflake.client.jdbc.SnowflakeUtil;
import net.snowflake.client.log.SFLogger;
import net.snowflake.client.log.SFLoggerFactory;
import org.junit.Assert;
Expand Down Expand Up @@ -69,4 +71,40 @@ public static void assertValidQueryId(String queryId) {
assertTrue(
"Expecting " + queryId + " is a valid UUID", QUERY_ID_REGEX.matcher(queryId).matches());
}

/**
* Creates schema and deletes it at the end of the passed function execution
*
* @param statement statement
* @param schemaName name of schema to create and delete after lambda execution
* @param action action to execute when schema was created
* @throws Exception when any error occurred
*/
public static void withSchema(Statement statement, String schemaName, ThrowingRunnable action)
throws Exception {
try {
statement.execute("CREATE OR REPLACE SCHEMA " + schemaName);
action.run();
} finally {
statement.execute("DROP SCHEMA " + schemaName);
}
}

/**
* Creates schema and deletes it at the end of the passed function execution
*
* @param statement statement
* @param action action to execute when schema was created
* @throws Exception when any error occurred
*/
public static void withRandomSchema(Statement statement, ThrowingConsumer<String> action)
throws Exception {
String customSchema = "TEST_SCHEMA_" + SnowflakeUtil.randomAlphaNumeric(5);
try {
statement.execute("CREATE OR REPLACE SCHEMA " + customSchema);
action.call(customSchema);
} finally {
statement.execute("DROP SCHEMA " + customSchema);
}
}
}
6 changes: 6 additions & 0 deletions src/test/java/net/snowflake/client/ThrowingConsumer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package net.snowflake.client;

@FunctionalInterface
public interface ThrowingConsumer<T> {
void call(T parameter) throws Exception;
}
6 changes: 6 additions & 0 deletions src/test/java/net/snowflake/client/ThrowingRunnable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package net.snowflake.client;

@FunctionalInterface
public interface ThrowingRunnable {
void run() throws Exception;
}
Loading

0 comments on commit 42f377a

Please sign in to comment.