Skip to content

test(bigquery-jdbc): add unit tests for constraint metadata methods#13719

Open
keshavdandeva wants to merge 3 commits into
mainfrom
jdbc/unit-test-getkeysmethods
Open

test(bigquery-jdbc): add unit tests for constraint metadata methods#13719
keshavdandeva wants to merge 3 commits into
mainfrom
jdbc/unit-test-getkeysmethods

Conversation

@keshavdandeva

@keshavdandeva keshavdandeva commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

b/532929906

This PR introduces unit testing for the 4 recently migrated constraint metadata methods (getPrimaryKeys, getImportedKeys, getExportedKeys, and getCrossReference) in BigQueryDatabaseMetaDataTest, alongside some important test infrastructure hardening.

Key Changes:

  • Exhaustive Constraint Unit Tests: Added 8 granular test methods covering both the "happy path" (_hasKeys) and empty state (_noKeys) for every metadata endpoint, ensuring BigQuery's TableConstraints structure is properly translated into JDBC standard formats (PKTABLE_NAME, KEY_SEQ, etc.) without relying on slow integration tests.
  • Mockito Helpers: Extracted repetitive dataset pagination and TableDefinition setup into clean helper methods (mockDatasetIteration, mockTableWithConstraints).
  • Robust Varargs Mocking: Fixed fragile Mockito setups across the file by migrating 13 occurrences of any(BigQuery.*ListOption.class) to array matchers (any(BigQuery.*ListOption[].class)). This prevents spontaneous NullPointerExceptions if production code alters the number of varargs it passes.
  • Resource Leak Prevention: Identified and retrofitted 13 newer test methods to properly manage ResultSet lifecycles using try-with-resources, ensuring no AutoCloseable resources are leaked during test execution.

@keshavdandeva keshavdandeva force-pushed the jdbc/unit-test-getkeysmethods branch from fa89f61 to d95a9f2 Compare July 9, 2026 13:56

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request adds comprehensive unit tests to BigQueryDatabaseMetaDataTest.java for verifying metadata retrieval methods such as getPrimaryKeys, getImportedKeys, getExportedKeys, and getCrossReference. The feedback recommends improving the robustness of Mockito varargs matching by using array matchers and wrapping ResultSet instances in try-with-resources blocks to prevent potential resource leaks.

…` to BQ API (#13692)

b/532249777

This PR migrates the `getImportedKeys` and `getCrossReference` JDBC
metadata methods in `BigQueryDatabaseMetaData` to retrieve constraints
natively via the Google Cloud Java Client API instead of running legacy
raw SQL files on BigQuery.

## Changes

### **API-based Implementations**
- Migrated `getImportedKeys(...)` to fetch and scan foreign keys from
standard table definitions.
- Migrated `getCrossReference(...)` to matching parent and foreign table
references using client-side definitions.
- Utilized the shared parallel table processor utilities
(`processTargetTablesConcurrently(...)` and `processSingleTable(...)`)
to run dataset scans concurrently.

### **Sorting & Correctness**
- Separated sorting behavior according to the JDBC specification:
- `getImportedKeys` is sorted by parent table: `PKTABLE_CAT,
PKTABLE_SCHEM, PKTABLE_NAME, KEY_SEQ`. Introduced
`definePkTableSortComparator` for this.
- `getCrossReference` (and future `getExportedKeys`) is sorted by child
table: `FKTABLE_CAT, FKTABLE_SCHEM, FKTABLE_NAME, KEY_SEQ`. Uses
`defineFkTableSortComparator`.
- Introduced a private helper `matchesOrNullWildcard(...)` to simplify
and clean up catalog/schema bounds comparisons.
@keshavdandeva keshavdandeva force-pushed the jdbc/unit-test-getkeysmethods branch from d95a9f2 to 0349c8e Compare July 9, 2026 14:03
@keshavdandeva

Copy link
Copy Markdown
Contributor Author

/gemini review

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request updates mock expectations in BigQueryDatabaseMetaDataTest to use array types for list options, wraps ResultSet usages in try-with-resources blocks, and adds new unit tests for retrieving primary, imported, exported, and cross-referenced keys. The review feedback suggests removing the redundant mockDatasetIteration helper method as it is not required by the metadata methods under test, and expanding the test suite to cover composite keys to verify multi-column handling and correct sequence ordering.

@keshavdandeva keshavdandeva marked this pull request as ready for review July 9, 2026 14:21
@keshavdandeva keshavdandeva requested review from a team as code owners July 9, 2026 14:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant