Skip to content

[SpannerToSourceDb] Fixing flaky UT and also improving coverage#3874

Open
darshan-sj wants to merge 3 commits into
GoogleCloudPlatform:mainfrom
darshan-sj:reverse-ut-1
Open

[SpannerToSourceDb] Fixing flaky UT and also improving coverage#3874
darshan-sj wants to merge 3 commits into
GoogleCloudPlatform:mainfrom
darshan-sj:reverse-ut-1

Conversation

@darshan-sj
Copy link
Copy Markdown
Contributor

@darshan-sj darshan-sj commented Jun 3, 2026

Context - #3830 (comment)

  1. Some test cases in SourceWriterFnTest.java‎ were flaky because SourceProcessorFactoryTest was not restoring the static variables of SourceProcessorFactory which it mocked in the tests. Fixed the SourceProcessorFactoryTest and also removed the flaky tests in SourceWriterFnTest since they were not adding much value.
  2. Improved test coverage in other files.

@darshan-sj darshan-sj requested a review from a team as a code owner June 3, 2026 06:18
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request focuses on improving the robustness of the SpannerToSourceDb migration tool by enhancing error handling in DML generation and refactoring factory initialization. Additionally, it significantly increases test coverage by adding comprehensive unit tests for various edge cases in DML generation, type handling, and shadow table creation, ensuring more stable and predictable behavior.

Highlights

  • Error Handling Improvements: Updated DML generation logic to properly log warnings and return null when schema mapping fails, rather than silently continuing.
  • Code Refactoring: Refactored SourceProcessorFactory to use static Map.of() for cleaner initialization and improved maintainability.
  • Test Coverage: Added extensive unit tests for DML generation and type handling to cover edge cases, error scenarios, and improve overall reliability.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@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

This pull request refactors SourceProcessorFactory to use immutable maps for generators, connection helpers, and connection URLs, and updates column mapping logic in CassandraDMLGenerator and DMLGeneratorUtils to log warnings and return null upon mapping failures. It also adds several unit tests for edge cases. However, making connectionHelperMap final and removing its setter prevents tests from mocking connection helpers, resulting in unused local variables in SourceProcessorFactoryTest and potential test flakiness from real connection attempts. Additionally, a duplicate mock setup line was identified in CassandraDMLGeneratorTest.

@darshan-sj darshan-sj force-pushed the reverse-ut-1 branch 2 times, most recently from d19b1e6 to e647fa5 Compare June 3, 2026 06:28
@darshan-sj
Copy link
Copy Markdown
Contributor Author

/gemini review

Copy link
Copy Markdown
Contributor

@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

This pull request improves error handling and robustness in the Spanner-to-SourceDB template by returning null and logging warnings when Spanner column mappings are missing in CassandraDMLGenerator and DMLGeneratorUtils. It also introduces comprehensive unit tests for these edge cases, type-handling failures, and shadow table creation exceptions, while removing some ignored tests in SourceWriterFnTest. The feedback points out a critical mismatch in the mock timeout duration within the new ShadowTableCreatorTest exception test, which could prevent the mock from being triggered and cause the test to fail.

@codecov
Copy link
Copy Markdown

codecov Bot commented Jun 3, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 55.58%. Comparing base (b42fff3) to head (8ecd288).

Additional details and impacted files
@@             Coverage Diff              @@
##               main    #3874      +/-   ##
============================================
+ Coverage     55.53%   55.58%   +0.04%     
- Complexity     6576     6584       +8     
============================================
  Files          1103     1103              
  Lines         67570    67573       +3     
  Branches       7581     7581              
============================================
+ Hits          37527    37561      +34     
+ Misses        27627    27603      -24     
+ Partials       2416     2409       -7     
Components Coverage Δ
spanner-templates 88.52% <100.00%> (+0.21%) ⬆️
spanner-import-export 68.54% <ø> (+0.01%) ⬆️
spanner-live-forward-migration 90.19% <ø> (-0.04%) ⬇️
spanner-live-reverse-replication 84.41% <100.00%> (+0.44%) ⬆️
spanner-bulk-migration 92.60% <ø> (-0.02%) ⬇️
gcs-spanner-dv 88.96% <ø> (-0.04%) ⬇️
Files with missing lines Coverage Δ
...2/templates/dbutils/dml/CassandraDMLGenerator.java 97.04% <100.00%> (+7.16%) ⬆️
...rt/v2/templates/dbutils/dml/DMLGeneratorUtils.java 98.11% <100.00%> (+10.49%) ⬆️
...ates/dbutils/processor/SourceProcessorFactory.java 81.94% <100.00%> (+0.25%) ⬆️

... and 9 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@darshan-sj
Copy link
Copy Markdown
Contributor Author

/gemini review

Copy link
Copy Markdown
Contributor

@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

This pull request improves error handling in CassandraDMLGenerator and DMLGeneratorUtils by logging warnings and returning null when Spanner columns are missing from the schema mapping. It also adds extensive unit tests covering these edge cases, type handling exceptions, and shadow table creation errors, while cleaning up unused imports and ignored tests. The review feedback suggests dynamically backing up and restoring the connection helper map in SourceProcessorFactoryTest instead of hardcoding it, which prevents the tests from becoming fragile when new database types are introduced.

@darshan-sj
Copy link
Copy Markdown
Contributor Author

/gemini review

Copy link
Copy Markdown
Contributor

@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

This pull request improves error handling in CassandraDMLGenerator and DMLGeneratorUtils by logging warnings and returning null when a Spanner column name is not found in the schema mapping. It also introduces a package-private getter in SourceProcessorFactory to facilitate saving and restoring static state in tests, cleans up unused imports and ignored tests in SourceWriterFnTest, and adds comprehensive unit tests across several test files. Feedback is provided on SourceProcessorFactory to return an unmodifiable map from getConnectionHelperMap() to prevent accidental modifications of internal static state.

PreparedStatementGeneratedResponse prepResponse = (PreparedStatementGeneratedResponse) response;

assertTrue(prepResponse.getDmlStatement().contains("\"LastName\""));
assertTrue(!prepResponse.getDmlStatement().contains("\"MissingCol\""));
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.

better to use assertFalse here for readbility

spannerColName = schemaMapper.getSpannerColumnName("", sourceTable.name(), sourceColName);
} catch (NoSuchElementException e) {
continue;
LOG.warn(
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.

I think we should restore this change , what if there are 2 pks in mysql , 1 in spanner and the 2nd one in mysql has default set , then the insert statement would be perfectly valid, i tested:

create table test1(id int, id1 int default 1, name varchar(1000), PRIMARY key (id,id1));


insert into test1(id) values (1);

when(sourceTable.name()).thenReturn("src_table");

when(schemaMapper.getSpannerColumnName("", "src_table", "col1"))
.thenThrow(new java.util.NoSuchElementException("Not found"));
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.

please use imports: java.util.NoSuchElementException

}

@Test
public void testGetPkColumnValues_SpannerColDefNull() {
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.

actually when the column definition null it should be treated same as NoSuchElementException which means we should skip this column. Thoughts?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants