Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
Lorna Barber authored and SimbaGithub committed Jul 4, 2022
1 parent ed621d9 commit 8461138
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -299,22 +299,19 @@ public enum DBMetadataResultSetMetadata {

GET_STREAMS(
Arrays.asList(
"created_on",
"name",
"database_name",
"schema_name",
"owner",
"comment",
"table_name",
"source_type",
"base_tables",
"type",
"stale",
"mode",
"stale_after"),
"NAME",
"DATABASE_NAME",
"SCHEMA_NAME",
"OWNER",
"COMMENT",
"TABLE_NAME",
"SOURCE_TYPE",
"BASE_TABLES",
"TYPE",
"STALE",
"MODE"),
Arrays.asList(
"TEXT", "TEXT", "TEXT", "TEXT", "TEXT", "TEXT", "TEXT", "TEXT", "TEXT", "TEXT", "TEXT",
"TEXT", "TEXT"),
"TEXT", "TEXT", "TEXT", "TEXT", "TEXT", "TEXT", "TEXT", "TEXT", "TEXT", "TEXT", "TEXT"),
Arrays.asList(
Types.VARCHAR,
Types.VARCHAR,
Expand All @@ -326,8 +323,6 @@ public enum DBMetadataResultSetMetadata {
Types.VARCHAR,
Types.VARCHAR,
Types.VARCHAR,
Types.VARCHAR,
Types.VARCHAR,
Types.VARCHAR)),
;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2622,18 +2622,6 @@ public ResultSet getStreams(
String schemaUnescaped = unescapeChars(schemaPattern);
if (streamName == null || Wildcard.isWildcardPatternStr(streamName)) {
showCommand += " in schema \"" + catalogEscaped + "\".\"" + schemaUnescaped + "\"";
} else if (streamName.isEmpty()) {
return SnowflakeDatabaseMetaDataResultSet.getEmptyResultSet(GET_STREAMS, statement);
} else {
String streamNameUnescaped = unescapeChars(streamName);
showCommand +=
" in table \""
+ catalogEscaped
+ "\".\""
+ schemaUnescaped
+ "\".\""
+ streamNameUnescaped
+ "\"";
}
}
}
Expand All @@ -2654,7 +2642,6 @@ public boolean next() throws SQLException {
// iterate throw the show streams result until we find an entry
// that matches the stream name
while (showObjectResultSet.next()) {
String createdOn = showObjectResultSet.getString(1);
String name = showObjectResultSet.getString(2);
String databaseName = showObjectResultSet.getString(3);
String schemaName = showObjectResultSet.getString(4);
Expand All @@ -2666,7 +2653,6 @@ public boolean next() throws SQLException {
String type = showObjectResultSet.getString(10);
String stale = showObjectResultSet.getString(11);
String mode = showObjectResultSet.getString(12);
String staleAfter = showObjectResultSet.getString(13);

if ((compiledStreamNamePattern == null
|| compiledStreamNamePattern.matcher(streamName).matches())
Expand All @@ -2675,19 +2661,17 @@ public boolean next() throws SQLException {
&& (compiledStreamNamePattern == null
|| compiledStreamNamePattern.matcher(streamName).matches())) {
logger.debug("Found a matched column:" + tableName + "." + streamName);
nextRow[0] = createdOn;
nextRow[1] = name;
nextRow[2] = databaseName;
nextRow[3] = schemaName;
nextRow[4] = owner;
nextRow[5] = comment;
nextRow[6] = tableName;
nextRow[7] = sourceType;
nextRow[8] = baseTables;
nextRow[9] = type;
nextRow[10] = stale;
nextRow[11] = mode;
nextRow[12] = staleAfter;
nextRow[0] = name;
nextRow[1] = databaseName;
nextRow[2] = schemaName;
nextRow[3] = owner;
nextRow[4] = comment;
nextRow[5] = tableName;
nextRow[6] = sourceType;
nextRow[7] = baseTables;
nextRow[8] = type;
nextRow[9] = stale;
nextRow[10] = mode;
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
import static org.junit.Assert.*;

import java.sql.*;
import java.util.HashSet;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import net.snowflake.client.ConditionalIgnoreRule;
import net.snowflake.client.RunningOnGithubAction;
import net.snowflake.client.category.TestCategoryOthers;
Expand Down Expand Up @@ -1274,4 +1276,54 @@ public void testGetColumns() throws Throwable {
connection.createStatement().execute("drop table if exists T0");
}
}

@Test
public void testGetStreams() throws SQLException {
try (Connection con = getConnection()) {
String database = con.getCatalog();
String schema = con.getSchema();
String owner = con.unwrap(SnowflakeConnectionV1.class).getSFBaseSession().getRole();
final String targetStream = "S0";
final String targetTable = "T0";
String tableName = database + "." + schema + "." + targetTable;

Statement statement = con.createStatement();
statement.execute("create or replace table " + targetTable + "(C1 int)");
statement.execute("create or replace stream " + targetStream + " on table " + targetTable);

DatabaseMetaData metaData = con.getMetaData();

// match stream
ResultSet resultSet =
metaData.unwrap(SnowflakeDatabaseMetaData.class).getStreams(database, schema, "%");
verifyResultSetMetaDataColumns(resultSet, DBMetadataResultSetMetadata.GET_STREAMS);
Set<String> streams = new HashSet<>();
while (resultSet.next()) {
streams.add(resultSet.getString(1));
}
assertTrue(streams.contains("S0"));

// match exact stream
resultSet =
metaData
.unwrap(SnowflakeDatabaseMetaData.class)
.getStreams(database, schema, targetStream);
resultSet.next();
assertEquals(targetStream, resultSet.getString(1));
assertEquals(database, resultSet.getString(2));
assertEquals(schema, resultSet.getString(3));
assertEquals(owner, resultSet.getString(4));
assertEquals("", resultSet.getString(5));
assertEquals(tableName, resultSet.getString(6));
assertEquals("Table", resultSet.getString(7));
assertEquals(tableName, resultSet.getString(8));
assertEquals("DELTA", resultSet.getString(9));
assertEquals("false", resultSet.getString(10));
assertEquals("DEFAULT", resultSet.getString(11));

con.createStatement().execute("drop table if exists " + targetTable);
con.createStatement().execute("drop stream if exists " + targetStream);
statement.close();
}
}
}

0 comments on commit 8461138

Please sign in to comment.