Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ public class ITBase extends BigQueryJdbcBaseTest {
"DECLARE DELAY_TIME DATETIME; SET DELAY_TIME = DATETIME_ADD(CURRENT_DATETIME, INTERVAL 300"
+ " SECOND); WHILE CURRENT_DATETIME < DELAY_TIME DO END WHILE;";

public static final String PCNT_SCHEMA =
System.getenv()
.getOrDefault("PCNT_SCHEMA", "bq-drivers-test-warehouse.jdbc_pcnt_test_namespace");

private static String sharedDataset;
private static String sharedDataset2;

Expand Down Expand Up @@ -372,6 +376,27 @@ public static void setUpTable(String dataset, String table) throws InterruptedEx
QueryJobConfiguration.of(String.format(insertQuery2, DEFAULT_CATALOG, dataset, table)));
}

public static final String createPcntTableQuery =
"CREATE OR REPLACE TABLE `%s.%s.%s` (id INT64, name STRING);";
public static final String insertPcntTableQuery =
"INSERT INTO `%s.%s.%s` (id, name) VALUES (1, 'Alice'), (2, 'Bob');";
public static final String dropPcntTableQuery = "DROP TABLE IF EXISTS `%s.%s.%s`;";

public static void setUpPcntTable(String schema, String table) throws InterruptedException {
bigQuery.query(
QueryJobConfiguration.of(
String.format(createPcntTableQuery, DEFAULT_CATALOG, schema, table)));
bigQuery.query(
QueryJobConfiguration.of(
String.format(insertPcntTableQuery, DEFAULT_CATALOG, schema, table)));
}

public static void cleanUpPcntTable(String schema, String table) throws InterruptedException {
bigQuery.query(
QueryJobConfiguration.of(
String.format(dropPcntTableQuery, DEFAULT_CATALOG, schema, table)));
}

public static void cleanUp(String dataset) throws InterruptedException {
bigQuery.query(QueryJobConfiguration.of(String.format(dropSchema, DEFAULT_CATALOG, dataset)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,21 @@ public class ITConnectionTest {
static Random random = new Random();
static int randomNumber = random.nextInt(999);
private static final String TABLE_NAME = "JDBC_CONNECTION_TEST_TABLE" + randomNumber;
private static final String PCNT_TABLE_NAME = "PCNT_CONN_TEST_TABLE_" + randomNumber;

@BeforeAll
public static void beforeClass() throws InterruptedException {
DATASET = ITBase.getSharedDataset();
ITBase.setUpTable(DATASET, TABLE_NAME);
ITBase.setUpProcedure(DATASET, TABLE_NAME);
ITBase.setUpPcntTable(ITBase.PCNT_SCHEMA, PCNT_TABLE_NAME);
}

@AfterAll
public static void afterClass() throws InterruptedException {
// Shared dataset cleanup is handled by shutdown hook
// Shared dataset cleanup is handled by ITBase shutdown hook.
// Clean up dynamic table created in the shared PCNT namespace.
ITBase.cleanUpPcntTable(ITBase.PCNT_SCHEMA, PCNT_TABLE_NAME);
}

@Test
Expand Down Expand Up @@ -436,4 +440,55 @@ public void testIsValid() throws SQLException {
assertTrue(connection.isValid(0)); // 0 seconds timeout
connection.close();
}

@Test
public void testDefaultDatasetColonDelimiter() throws SQLException {
String urlWithColon =
ITBase.connectionUrl + ";DefaultDataset=" + DEFAULT_CATALOG + ":" + DATASET + ";";
try (Connection connection = DriverManager.getConnection(urlWithColon)) {
assertNotNull(connection);
assertFalse(connection.isClosed());
try (Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM " + TABLE_NAME)) {
assertTrue(rs.next());
assertTrue(rs.getMetaData().getColumnCount() > 0);
}
}
}

@Test
public void testPcntDefaultDataset2TierNamespace() throws SQLException {
String urlWithPcnt = ITBase.connectionUrl + ";DefaultDataset=" + ITBase.PCNT_SCHEMA + ";";
try (Connection connection = DriverManager.getConnection(urlWithPcnt)) {
assertNotNull(connection);
assertFalse(connection.isClosed());
try (Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM " + PCNT_TABLE_NAME)) {
assertTrue(rs.next());
assertEquals(1, rs.getInt("id"));
assertEquals("Alice", rs.getString("name"));
}
}
}

@Test
public void testPcntDefaultDataset3TierNamespace() throws SQLException {
String urlWithPcnt =
ITBase.connectionUrl
+ ";DefaultDataset="
+ DEFAULT_CATALOG
+ ":"
+ ITBase.PCNT_SCHEMA
+ ";";
try (Connection connection = DriverManager.getConnection(urlWithPcnt)) {
assertNotNull(connection);
assertFalse(connection.isClosed());
try (Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM " + PCNT_TABLE_NAME)) {
assertTrue(rs.next());
assertEquals(1, rs.getInt("id"));
assertEquals("Alice", rs.getString("name"));
}
}
}
Comment thread
keshavdandeva marked this conversation as resolved.
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,11 @@ public class ITDatabaseMetadataTest extends ITBase {
private static final String CONSTRAINTS_TABLE_NAME = "JDBC_CONSTRAINTS_TEST_TABLE";
private static final String CONSTRAINTS_TABLE_NAME2 = "JDBC_CONSTRAINTS_TEST_TABLE2";
private static final String CONSTRAINTS_TABLE_NAME3 = "JDBC_CONSTRAINTS_TEST_TABLE3";
private static final String PCNT_SCHEMA = "bq-drivers-test-warehouse.jdbc_pcnt_test_namespace";
private static final String PCNT_TABLE_NAME = "PCNT_TEST_TABLE";
private static final Pattern VERSION_PATTERN =
Pattern.compile("^(\\d+)\\.(\\d+)(?:\\.\\d+)+\\s*.*");
private static final String DEFAULT_CATALOG = ServiceOptions.getDefaultProjectId();
private static final String TABLE_NAME = "JDBC_DBMETADATA_TEST_TABLE" + randomNumber;
private static final String PCNT_TABLE_NAME = "PCNT_METADATA_TEST_TABLE_" + randomNumber;

@BeforeAll
public static void beforeClass() throws InterruptedException, SQLException {
Expand All @@ -72,10 +71,15 @@ public static void beforeClass() throws InterruptedException, SQLException {
CONSTRAINTS_DATASET = ITBase.getSharedDataset();
// Set up Dataset
ITBase.setUpTable(DATASET, TABLE_NAME);
ITBase.setUpPcntTable(ITBase.PCNT_SCHEMA, PCNT_TABLE_NAME);
}

@AfterAll
public static void afterClass() throws SQLException {}
public static void afterClass() throws SQLException, InterruptedException {
// Shared dataset cleanup is handled by ITBase shutdown hook.
// Clean up dynamic table created in the shared PCNT namespace.
ITBase.cleanUpPcntTable(ITBase.PCNT_SCHEMA, PCNT_TABLE_NAME);
}

@Disabled
@Test
Expand Down Expand Up @@ -1817,4 +1821,73 @@ protected void verifyGetColumns(Connection connection, DatabaseMetaData metaData
assertTrue(count > 0);
connection.createStatement().execute("drop table if exists " + dataset + "." + jdbctesttable);
}

@Test
public void testDatabaseMetadataGetSchemasPcnt() throws SQLException {
try (Connection connection = DriverManager.getConnection(ITBase.connectionUrl)) {
DatabaseMetaData metaData = connection.getMetaData();
try (ResultSet rs = metaData.getSchemas(DEFAULT_CATALOG, PCNT_SCHEMA)) {
assertNotNull(rs, "ResultSet from getSchemas() should not be null");
assertTrue(
rs.next(), "Expected PCNT schema " + PCNT_SCHEMA + " in catalog " + DEFAULT_CATALOG);
Comment thread
keshavdandeva marked this conversation as resolved.
assertEquals(PCNT_SCHEMA, rs.getString("TABLE_SCHEM"));
assertEquals(DEFAULT_CATALOG, rs.getString("TABLE_CATALOG"));
}
}
}

@Test
public void testDatabaseMetadataGetTablesPcnt() throws SQLException {
try (Connection connection = DriverManager.getConnection(ITBase.connectionUrl)) {
DatabaseMetaData metaData = connection.getMetaData();
try (ResultSet rs = metaData.getTables(DEFAULT_CATALOG, PCNT_SCHEMA, PCNT_TABLE_NAME, null)) {
assertNotNull(rs, "ResultSet from getTables() should not be null");
assertTrue(
rs.next(), "Expected PCNT table " + PCNT_TABLE_NAME + " under schema " + PCNT_SCHEMA);
Comment thread
keshavdandeva marked this conversation as resolved.
assertEquals(DEFAULT_CATALOG, rs.getString("TABLE_CAT"));
assertEquals(PCNT_SCHEMA, rs.getString("TABLE_SCHEM"));
assertEquals(PCNT_TABLE_NAME, rs.getString("TABLE_NAME"));
assertEquals("TABLE", rs.getString("TABLE_TYPE"));
}
}
}

@Test
public void testDatabaseMetadataGetColumnsPcnt() throws SQLException {
try (Connection connection = DriverManager.getConnection(ITBase.connectionUrl)) {
DatabaseMetaData metaData = connection.getMetaData();
try (ResultSet rs = metaData.getColumns(DEFAULT_CATALOG, PCNT_SCHEMA, PCNT_TABLE_NAME, "%")) {
assertNotNull(rs, "ResultSet from getColumns() should not be null");
Comment thread
keshavdandeva marked this conversation as resolved.
int columnCount = 0;
boolean foundId = false;
boolean foundName = false;
while (rs.next()) {
columnCount++;
assertEquals(DEFAULT_CATALOG, rs.getString("TABLE_CAT"));
assertEquals(PCNT_SCHEMA, rs.getString("TABLE_SCHEM"));
assertEquals(PCNT_TABLE_NAME, rs.getString("TABLE_NAME"));
String colName = rs.getString("COLUMN_NAME");
String typeName = rs.getString("TYPE_NAME");
if ("id".equals(colName)) {
foundId = true;
assertEquals(1, rs.getInt("ORDINAL_POSITION"));
assertTrue(
typeName.equalsIgnoreCase("INT64")
|| typeName.equalsIgnoreCase("INTEGER")
|| typeName.equalsIgnoreCase("BIGINT"));
} else if ("name".equals(colName)) {
foundName = true;
assertEquals(2, rs.getInt("ORDINAL_POSITION"));
assertTrue(
typeName.equalsIgnoreCase("STRING")
|| typeName.equalsIgnoreCase("NVARCHAR")
|| typeName.equalsIgnoreCase("VARCHAR"));
}
}
assertEquals(2, columnCount, "Expected 2 columns in PCNT table " + PCNT_TABLE_NAME);
assertTrue(foundId, "Expected column 'id' in PCNT table");
assertTrue(foundName, "Expected column 'name' in PCNT table");
Comment thread
keshavdandeva marked this conversation as resolved.
}
}
}
}
Loading