-
Notifications
You must be signed in to change notification settings - Fork 1.1k
[SpannerToSourceDb] Fixing flaky UT and also improving coverage #3874
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
Open
darshan-sj
wants to merge
3
commits into
GoogleCloudPlatform:main
Choose a base branch
from
darshan-sj:reverse-ut-1
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1231,4 +1231,175 @@ public void testGetPkColumnValues_MissingValueInJson() { | |
|
|
||
| assertNull(response); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGetDMLStatement_PkColSpannerColNameThrowsNoSuchElement() { | ||
| ISchemaMapper schemaMapper = Mockito.mock(ISchemaMapper.class); | ||
| Ddl spannerDdl = Ddl.builder().build(); | ||
| spannerDdl = Mockito.spy(spannerDdl); | ||
| Table spannerTable = Mockito.mock(Table.class); | ||
| SourceSchema sourceSchema = Mockito.mock(SourceSchema.class); | ||
| SourceTable sourceTable = Mockito.mock(SourceTable.class); | ||
| SourceColumn sourceCol = Mockito.mock(SourceColumn.class); | ||
|
|
||
| Mockito.doReturn(spannerTable).when(spannerDdl).table("Singers"); | ||
| Mockito.when(schemaMapper.getSourceTableName("", "Singers")).thenReturn("Singers"); | ||
| Mockito.when(sourceSchema.table("Singers")).thenReturn(sourceTable); | ||
| Mockito.when(sourceTable.primaryKeyColumns()).thenReturn(ImmutableList.of("SingerId")); | ||
| Mockito.when(sourceTable.column("SingerId")).thenReturn(sourceCol); | ||
| Mockito.when(sourceCol.type()).thenReturn("int"); | ||
| Mockito.when(sourceTable.name()).thenReturn("Singers"); | ||
|
|
||
| Mockito.when(schemaMapper.getSpannerColumnName("", "Singers", "SingerId")) | ||
| .thenThrow(new NoSuchElementException("Not found")); | ||
|
|
||
| DMLGeneratorRequest request = | ||
| new DMLGeneratorRequest.Builder( | ||
| "INSERT", "Singers", new JSONObject(), new JSONObject(), "+00:00") | ||
| .setSchemaMapper(schemaMapper) | ||
| .setDdl(spannerDdl) | ||
| .setSourceSchema(sourceSchema) | ||
| .setCommitTimestamp(Timestamp.now()) | ||
| .build(); | ||
|
|
||
| assertThrows( | ||
| InvalidDMLGenerationException.class, () -> cassandraDMLGenerator.getDMLStatement(request)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGetDMLStatement_PkColSpannerColNameNull() { | ||
| ISchemaMapper schemaMapper = Mockito.mock(ISchemaMapper.class); | ||
| Ddl spannerDdl = Ddl.builder().build(); | ||
| spannerDdl = Mockito.spy(spannerDdl); | ||
| Table spannerTable = Mockito.mock(Table.class); | ||
| SourceSchema sourceSchema = Mockito.mock(SourceSchema.class); | ||
| SourceTable sourceTable = Mockito.mock(SourceTable.class); | ||
| SourceColumn sourceCol = Mockito.mock(SourceColumn.class); | ||
|
|
||
| Mockito.doReturn(spannerTable).when(spannerDdl).table("Singers"); | ||
| Mockito.when(schemaMapper.getSourceTableName("", "Singers")).thenReturn("Singers"); | ||
| Mockito.when(sourceSchema.table("Singers")).thenReturn(sourceTable); | ||
| Mockito.when(sourceTable.primaryKeyColumns()).thenReturn(ImmutableList.of("SingerId")); | ||
| Mockito.when(sourceTable.column("SingerId")).thenReturn(sourceCol); | ||
| Mockito.when(sourceCol.type()).thenReturn("int"); | ||
| Mockito.when(sourceTable.name()).thenReturn("Singers"); | ||
|
|
||
| Mockito.when(schemaMapper.getSpannerColumnName("", "Singers", "SingerId")).thenReturn(null); | ||
|
|
||
| DMLGeneratorRequest request = | ||
| new DMLGeneratorRequest.Builder( | ||
| "INSERT", "Singers", new JSONObject(), new JSONObject(), "+00:00") | ||
| .setSchemaMapper(schemaMapper) | ||
| .setDdl(spannerDdl) | ||
| .setSourceSchema(sourceSchema) | ||
| .setCommitTimestamp(Timestamp.now()) | ||
| .build(); | ||
|
|
||
| assertThrows( | ||
| InvalidDMLGenerationException.class, () -> cassandraDMLGenerator.getDMLStatement(request)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGetDMLStatement_PkColSpannerColDefNull() { | ||
| ISchemaMapper schemaMapper = Mockito.mock(ISchemaMapper.class); | ||
| Ddl spannerDdl = Ddl.builder().build(); | ||
| spannerDdl = Mockito.spy(spannerDdl); | ||
| Table spannerTable = Mockito.mock(Table.class); | ||
| SourceSchema sourceSchema = Mockito.mock(SourceSchema.class); | ||
| SourceTable sourceTable = Mockito.mock(SourceTable.class); | ||
| SourceColumn sourceCol = Mockito.mock(SourceColumn.class); | ||
|
|
||
| Mockito.doReturn(spannerTable).when(spannerDdl).table("Singers"); | ||
| Mockito.when(schemaMapper.getSourceTableName("", "Singers")).thenReturn("Singers"); | ||
| Mockito.when(sourceSchema.table("Singers")).thenReturn(sourceTable); | ||
| Mockito.when(sourceTable.primaryKeyColumns()).thenReturn(ImmutableList.of("SingerId")); | ||
| Mockito.when(sourceTable.column("SingerId")).thenReturn(sourceCol); | ||
| Mockito.when(sourceCol.type()).thenReturn("int"); | ||
| Mockito.when(sourceTable.name()).thenReturn("Singers"); | ||
|
|
||
| Mockito.when(schemaMapper.getSpannerColumnName("", "Singers", "SingerId")) | ||
| .thenReturn("SingerId"); | ||
| Mockito.when(spannerTable.column("SingerId")).thenReturn(null); | ||
|
|
||
| DMLGeneratorRequest request = | ||
| new DMLGeneratorRequest.Builder( | ||
| "INSERT", "Singers", new JSONObject(), new JSONObject(), "+00:00") | ||
| .setSchemaMapper(schemaMapper) | ||
| .setDdl(spannerDdl) | ||
| .setSourceSchema(sourceSchema) | ||
| .setCommitTimestamp(Timestamp.now()) | ||
| .build(); | ||
|
|
||
| assertThrows( | ||
| InvalidDMLGenerationException.class, () -> cassandraDMLGenerator.getDMLStatement(request)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGetDMLStatement_ColValuesSpannerColDefNullAndKeyValuesHasNonPk() { | ||
| ISchemaMapper schemaMapper = Mockito.mock(ISchemaMapper.class); | ||
| Table spannerTable = Mockito.mock(Table.class); | ||
| SourceSchema sourceSchema = Mockito.mock(SourceSchema.class); | ||
| SourceTable sourceTable = Mockito.mock(SourceTable.class); | ||
| SourceColumn pkCol = Mockito.mock(SourceColumn.class); | ||
| SourceColumn nonPkCol = Mockito.mock(SourceColumn.class); | ||
| SourceColumn missingCol = Mockito.mock(SourceColumn.class); | ||
| Column spannerPkCol = Mockito.mock(Column.class); | ||
| Column spannerNonPkCol = Mockito.mock(Column.class); | ||
|
|
||
| Ddl spannerDdl = Ddl.builder().build(); | ||
| spannerDdl = Mockito.spy(spannerDdl); | ||
| Mockito.doReturn(spannerTable).when(spannerDdl).table("Singers"); | ||
|
|
||
| Mockito.when(schemaMapper.getSourceTableName("", "Singers")).thenReturn("Singers"); | ||
| Mockito.when(sourceSchema.table("Singers")).thenReturn(sourceTable); | ||
| Mockito.when(sourceTable.primaryKeyColumns()).thenReturn(ImmutableList.of("SingerId")); | ||
| Mockito.when(sourceTable.column("SingerId")).thenReturn(pkCol); | ||
| Mockito.when(pkCol.type()).thenReturn("int"); | ||
| Mockito.when(sourceTable.name()).thenReturn("Singers"); | ||
|
|
||
| Mockito.when(schemaMapper.getSpannerColumnName("", "Singers", "SingerId")) | ||
| .thenReturn("SingerId"); | ||
| Mockito.when(spannerTable.column("SingerId")).thenReturn(spannerPkCol); | ||
| Mockito.when(spannerPkCol.name()).thenReturn("SingerId"); | ||
| Mockito.when(spannerPkCol.type()).thenReturn(Type.int64()); | ||
|
|
||
| Mockito.when(sourceTable.columns()).thenReturn(ImmutableList.of(pkCol, nonPkCol, missingCol)); | ||
| Mockito.when(nonPkCol.name()).thenReturn("LastName"); | ||
| Mockito.when(nonPkCol.type()).thenReturn("varchar"); | ||
| Mockito.when(missingCol.name()).thenReturn("MissingCol"); | ||
| Mockito.when(missingCol.type()).thenReturn("varchar"); | ||
|
|
||
| Mockito.when(schemaMapper.getSpannerColumnName("", "Singers", "LastName")) | ||
| .thenReturn("LastName"); | ||
| Mockito.when(spannerTable.column("LastName")).thenReturn(spannerNonPkCol); | ||
| Mockito.when(spannerNonPkCol.name()).thenReturn("LastName"); | ||
| Mockito.when(spannerNonPkCol.type()).thenReturn(Type.string()); | ||
|
|
||
| Mockito.when(schemaMapper.getSpannerColumnName("", "Singers", "MissingCol")) | ||
| .thenReturn("MissingCol"); | ||
| Mockito.when(spannerTable.column("MissingCol")).thenReturn(null); | ||
|
|
||
| JSONObject keyValuesJson = new JSONObject(); | ||
| keyValuesJson.put("SingerId", "999"); | ||
| keyValuesJson.put("LastName", "Smith"); | ||
|
|
||
| DMLGeneratorRequest request = | ||
| new DMLGeneratorRequest.Builder( | ||
| "INSERT", "Singers", new JSONObject(), keyValuesJson, "+00:00") | ||
| .setSchemaMapper(schemaMapper) | ||
| .setDdl(spannerDdl) | ||
| .setSourceSchema(sourceSchema) | ||
| .setCommitTimestamp(Timestamp.now()) | ||
| .build(); | ||
|
|
||
| DMLGeneratorResponse response = cassandraDMLGenerator.getDMLStatement(request); | ||
|
|
||
| assertTrue(response instanceof PreparedStatementGeneratedResponse); | ||
| PreparedStatementGeneratedResponse prepResponse = (PreparedStatementGeneratedResponse) response; | ||
|
|
||
| assertTrue(prepResponse.getDmlStatement().contains("\"LastName\"")); | ||
| assertTrue(!prepResponse.getDmlStatement().contains("\"MissingCol\"")); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. better to use assertFalse here for readbility |
||
|
|
||
| assertEquals(3, prepResponse.getValues().size()); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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: