Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(bigquery): add helper classes for cloud-client testing (1/3) #10034

Conversation

alarconesparza
Copy link
Contributor

@alarconesparza alarconesparza commented Feb 27, 2025

Description

Part of internal b/394478489

First part of bigquery cloud-client (1/3).

(#10034, #10035, #10036)

Create[Dataset,Table,View] and Delete[Dataset,Table] are required for testing the following samples (samples will be added in part 2 and 3).

Samples

  1. bigquery_view_dataset_access policy
  2. bigquery_view_table_or_view_access_policy
  3. bigquery_grant_access_to_dataset
  4. bigquery_grant_access_to_table_or_view
  5. bigquery_revoke_dataset_access
  6. bigquery_revoke_access_to_table_or_view

Checklist

  • I have followed Sample Format Guide
  • pom.xml parent set to latest shared-configuration
  • Appropriate changes to README are included in PR
  • These samples need a new API enabled in testing projects to pass (let us know which ones)
  • These samples need a new/updated env vars in testing projects set to pass (let us know which ones)
  • Tests pass: mvn clean verify required
  • Lint passes: mvn -P lint checkstyle:check required
  • Static Analysis: mvn -P lint clean compile pmd:cpd-check spotbugs:check advisory only
  • This sample adds a new sample directory, and I updated the CODEOWNERS file with the codeowners for this sample
  • This sample adds a new Product API, and I updated the Blunderbuss issue/PR auto-assigner with the codeowners for this sample
  • Please merge this PR for me once it is approved

@product-auto-label product-auto-label bot added api: bigquery Issues related to the BigQuery API. samples Issues that are directly related to samples. labels Feb 27, 2025
@alarconesparza alarconesparza marked this pull request as ready for review February 27, 2025 21:04
@alarconesparza alarconesparza changed the title feat(bigquery): add helper classes for cloud-client testing feat(bigquery): add helper classes for cloud-client testing (1/3) Feb 28, 2025
@OremGLG OremGLG self-requested a review March 3, 2025 17:22
Copy link
Contributor

@OremGLG OremGLG left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@iennae
Copy link
Contributor

iennae commented Mar 3, 2025

/gemini review

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

The pull request introduces helper classes for cloud-client testing in BigQuery, specifically focusing on creating and deleting datasets, tables, and views. The code includes the necessary classes and integration tests to validate the functionality. Overall, the code is well-structured and addresses the requirements outlined in the pull request description. However, there are a few areas that could be improved for better clarity and maintainability.

Summary of Findings

  • TODO comments: The code contains TODO comments that should be addressed before merging. These comments indicate areas where the developer needs to replace placeholder values with actual values.
  • Redundant cleanup in tearDown methods: In the tearDown methods of the integration tests, the dataset deletion is performed even if the table deletion fails. This could lead to unnecessary operations and potential errors. It's better to ensure that table deletion is successful before attempting to delete the dataset.
  • Inconsistent naming: In CreateTableIT.java, the schema fields are named stringField and booleanField, while in CreateViewIT.java they are named timestampField, stringField, and booleanField. This inconsistency should be addressed for better readability and maintainability.

Merge Readiness

The pull request is almost ready for merging. However, the TODO comments should be addressed, and the redundant cleanup in the tearDown methods should be fixed. Additionally, the naming inconsistency in the schema fields should be resolved. Once these issues are addressed, the pull request can be merged. I am unable to approve this pull request, and recommend that others review and approve this code before merging.

Comment on lines 76 to 77
DeleteTable.deleteTable(GOOGLE_CLOUD_PROJECT, datasetName, tableName);
DeleteDataset.deleteDataset(GOOGLE_CLOUD_PROJECT, datasetName);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Consider adding a try-catch block around the DeleteTable.deleteTable call to handle potential exceptions during table deletion. If table deletion fails, log the error and proceed with dataset deletion. This ensures that the dataset is deleted even if the table deletion fails.

Suggested change
DeleteTable.deleteTable(GOOGLE_CLOUD_PROJECT, datasetName, tableName);
DeleteDataset.deleteDataset(GOOGLE_CLOUD_PROJECT, datasetName);
try {
DeleteTable.deleteTable(GOOGLE_CLOUD_PROJECT, datasetName, tableName);
} catch (Exception e) {
log.log(Level.SEVERE, "Error deleting table: " + e.getMessage(), e);
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1. I would also put the deletion of the table inside of a try-catch block.

Comment on lines 87 to 89
// Clean up.
DeleteTable.deleteTable(GOOGLE_CLOUD_PROJECT, datasetName, viewName);
DeleteTable.deleteTable(GOOGLE_CLOUD_PROJECT, datasetName, tableName);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Consider adding a try-catch block around the DeleteTable.deleteTable calls to handle potential exceptions during table deletion. If table deletion fails, log the error and proceed with dataset deletion. This ensures that the dataset is deleted even if the table deletion fails.

try {
  DeleteTable.deleteTable(GOOGLE_CLOUD_PROJECT, datasetName, viewName);
} catch (Exception e) {
  log.log(Level.SEVERE, "Error deleting view: " + e.getMessage(), e);
}

try {
  DeleteTable.deleteTable(GOOGLE_CLOUD_PROJECT, datasetName, tableName);
} catch (Exception e) {
  log.log(Level.SEVERE, "Error deleting table: " + e.getMessage(), e);
}

Comment on lines 76 to 78
Field.of("timestampField", StandardSQLTypeName.TIMESTAMP),
Field.of("stringField", StandardSQLTypeName.STRING),
Field.of("booleanField", StandardSQLTypeName.BOOL));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

In CreateTableIT.java, the schema fields are named stringField and booleanField, while here they are named timestampField, stringField, and booleanField. This inconsistency should be addressed for better readability and maintainability. Consider using consistent naming conventions across all test classes.

Schema schema =
    Schema.of(
        Field.of("stringField", StandardSQLTypeName.STRING),
        Field.of("booleanField", StandardSQLTypeName.BOOL));


package com.example.bigquery;

import com.google.cloud.bigquery.BigQuery;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue: insert region tags. Make sure that you use the same region tags as used in the same sample (create dataset) in other languages. It looks like Go uses bigquery_create_dataset.

See https://googlecloudplatform.github.io/samples-style-guide/#region-tags.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now, those samples (with region tags) are in a different repo, but I need the classes to test the samples in the list. I'm planning to migrate the region tags and use the samples in this PR and repo once the PRs 10034, 10035 and 10036 are merged as that migration is not part of the current scope.

Is this a good way to go?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, that makes sense. I assume that there is a work item or bug that tracks adding region tags to these files?

@After
public void tearDown() {
// Clean up
DeleteTable.deleteTable(GOOGLE_CLOUD_PROJECT, datasetName, tableName);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue: Use the BQ client to delete table and dataset, not the corresponding samples.

Although it is much easier to use the samples, unfortunately using the samples for clean up has some unwanted potential side effects. Specifically, if one of these samples start failing, then that will cause THIS test to start failing as well.

@After
public void tearDown() {
// Clean up.
DeleteTable.deleteTable(GOOGLE_CLOUD_PROJECT, datasetName, viewName);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See previous comment about tearDown().

CreateDataset.createDataset(GOOGLE_CLOUD_PROJECT, datasetName);

// Create temporary table.
tableName = "table_test_" + UUID.randomUUID().toString().substring(0, 8);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: add a preface like bigquery_ before the "table_test_" string.

These strings are crucial for identifying failing tests in the logs. We have multiple products that involve entities called "tables" and "datasets." When I'm scanning through the logs to see why a test failed, it's helpful if the name of the resource that caused the failure helps me pinpoint which service or set of tests the failure originates from.

Copy link
Contributor

@eapl-gemugami eapl-gemugami Mar 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alarconesparza as discussed internally, we could use a prefixer like Python does:
conftest.py#L22

Do we have an equivalent to the test_utils on the Java testing system? The prefix on Python looks similar to java-docs-samples-bigquery-cloud-client plus a timestamp.


// Create temporary dataset.
datasetName = RemoteBigQueryHelper.generateDatasetName();
CreateDataset.createDataset(GOOGLE_CLOUD_PROJECT, datasetName);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same principle applies here as my previous comment about using samples for tearDown().

Using samples in test setUp() and tearDown() can cause a cascading set of test failures.


// Create temporary dataset.
datasetName = RemoteBigQueryHelper.generateDatasetName();
CreateDataset.createDataset(GOOGLE_CLOUD_PROJECT, datasetName);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as above about using samples in setUp() and tearDown().


package com.example.bigquery;

import com.google.cloud.bigquery.BigQuery;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, that makes sense. I assume that there is a work item or bug that tracks adding region tags to these files?

Comment on lines 76 to 77
DeleteTable.deleteTable(GOOGLE_CLOUD_PROJECT, datasetName, tableName);
DeleteDataset.deleteDataset(GOOGLE_CLOUD_PROJECT, datasetName);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1. I would also put the deletion of the table inside of a try-catch block.

@telpirion telpirion merged commit 1975c4c into GoogleCloudPlatform:main Mar 11, 2025
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: bigquery Issues related to the BigQuery API. samples Issues that are directly related to samples.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants