Skip to content
Merged
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
29 changes: 15 additions & 14 deletions java-bigquery-jdbc/.agents/skills/bigquery-jdbc-testing/SKILL.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
name: bigquery-jdbc-testing
description: Conventions for writing and refactoring unit and integration tests in the BigQuery JDBC driver (java-bigquery-jdbc). Use this skill when adding, reviewing, or cleaning up tests under java-bigquery-jdbc/src/test, including tests for connection/credential setup, Storage Read/Write API clients, TPC/universe-domain behavior, and ITBase-derived integration tests.
description: Conventions for writing and refactoring unit and integration tests in the BigQuery JDBC driver (java-bigquery-jdbc). Use this skill when adding, reviewing, or cleaning up tests under java-bigquery-jdbc/src/test, including tests for connection/credential setup, Storage Read/Write API clients, universe-domain behavior, and ITBase-derived integration tests.
---

# BigQuery JDBC Driver: Writing Tests
Expand Down Expand Up @@ -39,7 +39,7 @@ Example:
String url =
getUriOAuthServiceAccount()
.append("ProjectId", "project")
.append("universeDomain", "apis-tpczero.goog")
.append("universeDomain", "example.com")
.toString();
```

Expand All @@ -54,20 +54,20 @@ the assertions into one `private static` helper that takes the varying parts
(credentials, endpoint URI, expected scopes, expected universe) as parameters.
Each test then makes one call per object.

This is how `BigQueryConnectionTest` does it. `TPC_UNIVERSE`, `storageUri` and
`assertStorageCredentialsScoped` are private members of that class, not shared
fixtures:
This is how `BigQueryConnectionTest` does it. `CUSTOM_UNIVERSE`, `storageUri`
and `assertStorageCredentialsScoped` are private members of that class, not
shared fixtures:

```java
assertStorageCredentialsScoped(
readClient.getSettings().getCredentialsProvider().getCredentials(),
TPC_UNIVERSE,
storageUri(TPC_UNIVERSE, "BigQueryRead"),
CUSTOM_UNIVERSE,
storageUri(CUSTOM_UNIVERSE, "BigQueryRead"),
BigQueryReadSettings.getDefaultServiceScopes());
assertStorageCredentialsScoped(
writeClient.getSettings().getCredentialsProvider().getCredentials(),
TPC_UNIVERSE,
storageUri(TPC_UNIVERSE, "BigQueryWrite"),
CUSTOM_UNIVERSE,
storageUri(CUSTOM_UNIVERSE, "BigQueryWrite"),
BigQueryWriteSettings.getDefaultServiceScopes());
```

Expand All @@ -83,8 +83,8 @@ takes each side. In particular:
`GoogleCredentials`, also test a credential that is *not* a service account,
such as `getUriOAuthToken()` / `GoogleCredentials.create(AccessToken)`. Check
that it passes through unchanged (`assertSame` where possible).
- **Universe domain.** If the code behaves differently for TPC
(`universeDomain=apis-tpczero.goog`), also test the **default** universe (no
- **Universe domain.** If the code behaves differently for a custom universe
domain (`universeDomain=example.com`), also test the **default** universe (no
`universeDomain` property, so `googleapis.com`).
- **Already configured input.** For example, credentials that already have
explicit scopes must keep them.
Expand All @@ -111,9 +111,10 @@ cases or weaken assertions (for example, changing `assertEquals` to
misleads the reader.
- Use try-with-resources for `Connection`, `Statement`, and `ResultSet`, or close
them explicitly. IT suites share quotas.
- `@Tag("disable_tpc")` excludes a test from TPC runs. Remove it only when the
feature is known to work against TPC, and in that case add an assertion that
proves the intended path was used (for example `assertArrowResultSet`).
- `@Tag("disable_universe_domain")` excludes a test from custom universe domain
runs. Remove it only when the feature is known to work against a custom
universe domain, and in that case add an assertion that proves the intended
path was used (for example `assertArrowResultSet`).

## 5. Style and verification

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -377,8 +377,8 @@ protected boolean removeEldestEntry(Map.Entry<String, Map<String, String>> eldes
BigQueryConnectionProperty.newBuilder()
.setName(UNIVERSE_DOMAIN_OVERRIDE_PROPERTY_NAME)
.setDescription(
"The name of the partner-operated cloud which is a new instance of Google"
+ " production, known as a Trusted Partner Cloud universe.")
"The universe domain for the target cloud environment. Defaults to"
+ " googleapis.com.")
.setDefaultValue(DEFAULT_UNIVERSE_DOMAIN_VALUE)
.build(),
BigQueryConnectionProperty.newBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public class BigQueryConnectionTest extends BigQueryJdbcLoggingBaseTest {

private static final String DEFAULT_VERSION = "0.0.0";
private static final String DEFAULT_JDBC_TOKEN_VALUE = "Google-BigQuery-JDBC-Driver";
private static final String TPC_UNIVERSE = "apis-tpczero.goog";
private static final String CUSTOM_UNIVERSE = "example.com";
private static final String DEFAULT_UNIVERSE = "googleapis.com";
private static final String BASE_URL =
"jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;"
Expand Down Expand Up @@ -974,28 +974,29 @@ public void testDuplicateSessionIdKeysAreRejected() {
}

@Test
public void testReadAndWriteClientsScopeServiceAccountCredentialsForTpc() throws Exception {
public void testReadAndWriteClientsScopeServiceAccountCredentialsForCustomUniverse()
throws Exception {
String url =
getUriOAuthServiceAccount()
.append("ProjectId", "project")
.append("universeDomain", TPC_UNIVERSE)
.append("universeDomain", CUSTOM_UNIVERSE)
.toString();

try (BigQueryConnection connection = new BigQueryConnection(url)) {
BigQueryReadClient readClient = connection.getBigQueryReadClient();
assertEquals(TPC_UNIVERSE, readClient.getSettings().getUniverseDomain());
assertEquals(CUSTOM_UNIVERSE, readClient.getSettings().getUniverseDomain());
assertStorageCredentialsScoped(
readClient.getSettings().getCredentialsProvider().getCredentials(),
TPC_UNIVERSE,
storageUri(TPC_UNIVERSE, "BigQueryRead"),
CUSTOM_UNIVERSE,
storageUri(CUSTOM_UNIVERSE, "BigQueryRead"),
BigQueryReadSettings.getDefaultServiceScopes());

BigQueryWriteClient writeClient = connection.getBigQueryWriteClient();
assertEquals(TPC_UNIVERSE, writeClient.getSettings().getUniverseDomain());
assertEquals(CUSTOM_UNIVERSE, writeClient.getSettings().getUniverseDomain());
assertStorageCredentialsScoped(
writeClient.getSettings().getCredentialsProvider().getCredentials(),
TPC_UNIVERSE,
storageUri(TPC_UNIVERSE, "BigQueryWrite"),
CUSTOM_UNIVERSE,
storageUri(CUSTOM_UNIVERSE, "BigQueryWrite"),
BigQueryWriteSettings.getDefaultServiceScopes());
}
}
Expand Down Expand Up @@ -1064,7 +1065,7 @@ public void testScopeStorageCredentialsPreservesExplicitScopes() throws Exceptio
ServiceAccountCredentials.newBuilder()
.setClientEmail("service@account")
.setPrivateKeyString(fake_pkcs8_key)
.setUniverseDomain(TPC_UNIVERSE)
.setUniverseDomain(CUSTOM_UNIVERSE)
.build()
.createScoped(explicitScopes);

Expand All @@ -1073,7 +1074,7 @@ public void testScopeStorageCredentialsPreservesExplicitScopes() throws Exceptio
alreadyScoped, BigQueryReadSettings.getDefaultServiceScopes());

assertStorageCredentialsScoped(
result, TPC_UNIVERSE, storageUri(TPC_UNIVERSE, "BigQueryRead"), explicitScopes);
result, CUSTOM_UNIVERSE, storageUri(CUSTOM_UNIVERSE, "BigQueryRead"), explicitScopes);
}

private static URI storageUri(String universeDomain, String service) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3567,7 +3567,7 @@ private void assertExportedKeysPreparedQuery(String catalog, String schema, Stri

static Stream<Arguments> exportedKeysFlexibleTableAndCatalogProvider() {
return Stream.of(
Arguments.of("tpczero-system:bigquery-drivers", "_dataset_1", "my-table 01"),
Arguments.of("custom-universe:my-project", "_dataset_1", "my-table 01"),
Arguments.of("domain.com:project-1", "dataset_p", "00_お客様"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ protected static BigQuery getBigQuery(String connectionUrl) {
return (BigQuery) method.invoke(unwrapped);
}
} catch (Throwable e) {
// ignore for some set of tests; Proxy/TPC tests will fail if it doesn't work.
// ignore for some set of tests; Proxy/universe-domain tests will fail if it doesn't work.
}
return BigQueryOptions.getDefaultInstance().getService();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public void testGetCredentialsForPreGeneratedToken() {
}

@Test
public void testGetCredentialsForPreGeneratedTokenTPC() throws IOException {
public void testGetCredentialsForPreGeneratedTokenCustomUniverseDomain() throws IOException {
Map<String, String> authProperties =
BigQueryJdbcOAuthUtility.parseOAuthProperties(
DataSource.fromUrl(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,20 +318,22 @@ public void testParseDefaultDataset_threeTierPcntNamespace() {
}

@Test
public void testParseDefaultDataset_tpcProject() {
DatasetId datasetId = BigQueryJdbcUrlUtility.parseDefaultDataset("tpc:my-project:my_dataset");
public void testParseDefaultDataset_universeDomainProject() {
DatasetId datasetId =
BigQueryJdbcUrlUtility.parseDefaultDataset("custom-universe:my-project:my_dataset");
assertNotNull(datasetId);
assertEquals("my_dataset", datasetId.getDataset());
assertEquals("tpc:my-project", datasetId.getProject());
assertEquals("custom-universe:my-project", datasetId.getProject());
}

@Test
public void testParseDefaultDataset_tpcProject_threeTierPcntNamespace() {
public void testParseDefaultDataset_universeDomainProject_threeTierPcntNamespace() {
DatasetId datasetId =
BigQueryJdbcUrlUtility.parseDefaultDataset("tpc:my-project:my-warehouse.my_namespace");
BigQueryJdbcUrlUtility.parseDefaultDataset(
"custom-universe:my-project:my-warehouse.my_namespace");
assertNotNull(datasetId);
assertEquals("my-warehouse.my_namespace", datasetId.getDataset());
assertEquals("tpc:my-project", datasetId.getProject());
assertEquals("custom-universe:my-project", datasetId.getProject());
}

@Test
Expand All @@ -344,7 +346,7 @@ public void testParseDefaultDataset_invalidFormats() {
() -> BigQueryJdbcUrlUtility.parseDefaultDataset(":my_dataset"));
assertThrows(
BigQueryJdbcRuntimeException.class,
() -> BigQueryJdbcUrlUtility.parseDefaultDataset("tpc:my-project:"));
() -> BigQueryJdbcUrlUtility.parseDefaultDataset("custom-universe:my-project:"));
assertThrows(
BigQueryJdbcRuntimeException.class,
() -> BigQueryJdbcUrlUtility.parseDefaultDataset(" : "));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ public void testValidExternalAccountAuthenticationRawJson() throws SQLException
"https://www.googleapis.com/auth/bigquery, false"
})
@Tag("advanced")
@Tag("disable_tpc")
@Tag("disable_universe_domain")
public void testValidPreGeneratedAccessTokenAuthentication(String scope, boolean isReadOnly)
throws Exception {
final JsonObject authJson = getAuthJson();
Expand Down
Loading
Loading