-
Notifications
You must be signed in to change notification settings - Fork 1.1k
chore(bq jdbc): migrate to junit5 #12079
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
Merged
Merged
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -38,7 +38,7 @@ | |
| import static java.time.Month.MARCH; | ||
| import static java.util.Arrays.copyOfRange; | ||
| import static java.util.Collections.emptyMap; | ||
| import static org.junit.Assert.assertThrows; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
|
||
| import com.google.cloud.Tuple; | ||
| import com.google.cloud.bigquery.Field; | ||
|
|
@@ -62,44 +62,15 @@ | |
| import java.util.stream.Stream; | ||
| import org.apache.arrow.vector.util.JsonStringArrayList; | ||
| import org.apache.arrow.vector.util.Text; | ||
| import org.junit.Before; | ||
| import org.junit.ClassRule; | ||
| import org.junit.Test; | ||
| import org.junit.function.ThrowingRunnable; | ||
| import org.junit.runner.RunWith; | ||
| import org.junit.runners.Parameterized; | ||
| import org.junit.runners.Parameterized.Parameters; | ||
| import org.junit.jupiter.api.extension.RegisterExtension; | ||
| import org.junit.jupiter.api.function.Executable; | ||
| import org.junit.jupiter.params.ParameterizedTest; | ||
| import org.junit.jupiter.params.provider.MethodSource; | ||
|
|
||
| @RunWith(Parameterized.class) | ||
| public class BigQueryArrowArrayOfPrimitivesTest { | ||
|
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. |
||
|
|
||
| private final Field schema; | ||
| private final JsonStringArrayList<?> arrayValues; | ||
| private final Object[] expected; | ||
| private final int javaSqlTypeCode; | ||
| private Array array; | ||
| private final StandardSQLTypeName currentType; | ||
| @RegisterExtension public static final TimeZoneRule timeZoneRule = new TimeZoneRule("UTC"); | ||
|
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. |
||
|
|
||
| @ClassRule public static final TimeZoneRule timeZoneRule = new TimeZoneRule("UTC"); | ||
|
|
||
| public BigQueryArrowArrayOfPrimitivesTest( | ||
| StandardSQLTypeName currentType, | ||
| Tuple<Field, JsonStringArrayList<?>> schemaAndValue, | ||
| Object[] expected, | ||
| int javaSqlTypeCode) { | ||
| this.currentType = currentType; | ||
| this.schema = schemaAndValue.x(); | ||
| this.arrayValues = schemaAndValue.y(); | ||
| this.expected = expected; | ||
| this.javaSqlTypeCode = javaSqlTypeCode; | ||
| } | ||
|
|
||
| @Before | ||
| public void setUp() { | ||
| array = new BigQueryArrowArray(this.schema, this.arrayValues); | ||
| } | ||
|
|
||
| @Parameters(name = "{index}: primitive array of {0}") | ||
| public static Collection<Object[]> data() { | ||
| timeZoneRule.enforce(); | ||
| LocalDateTime aTimeStamp = LocalDateTime.of(2023, MARCH, 30, 11, 14, 19, 820227000); | ||
|
|
@@ -254,48 +225,82 @@ STRING, new Text("one"), new Text("two"), new Text("three"), new Text("four")), | |
| }); | ||
| } | ||
|
|
||
| @Test | ||
| public void getArray() throws SQLException { | ||
| assertThat(array.getArray()).isEqualTo(this.expected); | ||
| @ParameterizedTest(name = "{index}: primitive array of {0}") | ||
| @MethodSource("data") | ||
| public void getArray( | ||
| StandardSQLTypeName currentType, | ||
| Tuple<Field, JsonStringArrayList<?>> schemaAndValue, | ||
| Object[] expected, | ||
| int javaSqlTypeCode) | ||
| throws SQLException { | ||
| Array array = new BigQueryArrowArray(schemaAndValue.x(), schemaAndValue.y()); | ||
| assertThat(array.getArray()).isEqualTo(expected); | ||
|
Comment on lines
+228
to
+237
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. |
||
| } | ||
|
|
||
| @Test | ||
| public void getSlicedArray() throws SQLException { | ||
| @ParameterizedTest(name = "{index}: primitive array of {0}") | ||
| @MethodSource("data") | ||
| public void getSlicedArray( | ||
| StandardSQLTypeName currentType, | ||
| Tuple<Field, JsonStringArrayList<?>> schemaAndValue, | ||
| Object[] expected, | ||
| int javaSqlTypeCode) | ||
| throws SQLException { | ||
| Array array = new BigQueryArrowArray(schemaAndValue.x(), schemaAndValue.y()); | ||
| int fromIndex = 1; | ||
| int toIndexExclusive = 3; | ||
| Object[] expectedSlicedArray = | ||
| copyOfRange(this.expected, fromIndex, toIndexExclusive); // copying index(1,2) | ||
| copyOfRange(expected, fromIndex, toIndexExclusive); // copying index(1,2) | ||
|
|
||
| // the first element is at index 1 | ||
| assertThat(array.getArray(fromIndex + 1, 2)).isEqualTo(expectedSlicedArray); | ||
| } | ||
|
|
||
| @Test | ||
| public void getSlicedArrayWhenCountIsGreaterThanOriginalArrayLength() { | ||
| @ParameterizedTest(name = "{index}: primitive array of {0}") | ||
| @MethodSource("data") | ||
| public void getSlicedArrayWhenCountIsGreaterThanOriginalArrayLength( | ||
| StandardSQLTypeName currentType, | ||
| Tuple<Field, JsonStringArrayList<?>> schemaAndValue, | ||
| Object[] expected, | ||
| int javaSqlTypeCode) { | ||
| Array array = new BigQueryArrowArray(schemaAndValue.x(), schemaAndValue.y()); | ||
| IllegalArgumentException illegalArgumentException = | ||
| assertThrows(IllegalArgumentException.class, () -> array.getArray(2, 10)); | ||
| assertThat(illegalArgumentException.getMessage()) | ||
| .isEqualTo("The array index is out of range: 12, number of elements: 4."); | ||
| } | ||
|
|
||
| @Test | ||
| public void getResultSet() throws SQLException { | ||
| ResultSet resultSet = this.array.getResultSet(); | ||
| @ParameterizedTest(name = "{index}: primitive array of {0}") | ||
| @MethodSource("data") | ||
| public void getResultSet( | ||
| StandardSQLTypeName currentType, | ||
| Tuple<Field, JsonStringArrayList<?>> schemaAndValue, | ||
| Object[] expected, | ||
| int javaSqlTypeCode) | ||
| throws SQLException { | ||
| Array array = new BigQueryArrowArray(schemaAndValue.x(), schemaAndValue.y()); | ||
| ResultSet resultSet = array.getResultSet(); | ||
| Tuple<ArrayList<Object>, ArrayList<Object>> indexAndValues = | ||
| nestedResultSetToColumnLists(resultSet); | ||
| ArrayList<Object> indexList = indexAndValues.x(); | ||
| ArrayList<Object> columnValues = indexAndValues.y(); | ||
|
|
||
| assertThat(indexList.toArray()).isEqualTo(new Object[] {1, 2, 3, 4}); | ||
| assertThat(columnValues.toArray()).isEqualTo(this.expected); | ||
| assertThat(columnValues.toArray()).isEqualTo(expected); | ||
| } | ||
|
|
||
| @Test | ||
| public void getSlicedResultSet() throws SQLException { | ||
| @ParameterizedTest(name = "{index}: primitive array of {0}") | ||
| @MethodSource("data") | ||
| public void getSlicedResultSet( | ||
| StandardSQLTypeName currentType, | ||
| Tuple<Field, JsonStringArrayList<?>> schemaAndValue, | ||
| Object[] expected, | ||
| int javaSqlTypeCode) | ||
| throws SQLException { | ||
| Array array = new BigQueryArrowArray(schemaAndValue.x(), schemaAndValue.y()); | ||
| int fromIndex = 1; | ||
| int toIndexExclusive = 3; | ||
| Object[] expectedSlicedArray = | ||
| copyOfRange(this.expected, fromIndex, toIndexExclusive); // copying index(1,2) | ||
| copyOfRange(expected, fromIndex, toIndexExclusive); // copying index(1,2) | ||
|
|
||
| // the first element is at index 1 | ||
| ResultSet resultSet = array.getResultSet(fromIndex + 1, 2); | ||
|
|
@@ -309,27 +314,54 @@ public void getSlicedResultSet() throws SQLException { | |
| assertThat(columnValues.toArray()).isEqualTo(expectedSlicedArray); | ||
| } | ||
|
|
||
| @Test | ||
| public void getSlicedResultSetWhenCountIsGreaterThanOriginalArrayLength() { | ||
| @ParameterizedTest(name = "{index}: primitive array of {0}") | ||
| @MethodSource("data") | ||
| public void getSlicedResultSetWhenCountIsGreaterThanOriginalArrayLength( | ||
| StandardSQLTypeName currentType, | ||
| Tuple<Field, JsonStringArrayList<?>> schemaAndValue, | ||
| Object[] expected, | ||
| int javaSqlTypeCode) { | ||
| Array array = new BigQueryArrowArray(schemaAndValue.x(), schemaAndValue.y()); | ||
| IllegalArgumentException illegalArgumentException = | ||
| assertThrows(IllegalArgumentException.class, () -> array.getResultSet(2, 10)); | ||
| assertThat(illegalArgumentException.getMessage()) | ||
| .isEqualTo("The array index is out of range: 12, number of elements: 4."); | ||
| } | ||
|
|
||
| @Test | ||
| public void getBaseTypeName() throws SQLException { | ||
| assertThat(array.getBaseTypeName()).isEqualTo(this.currentType.name()); | ||
| @ParameterizedTest(name = "{index}: primitive array of {0}") | ||
| @MethodSource("data") | ||
| public void getBaseTypeName( | ||
| StandardSQLTypeName currentType, | ||
| Tuple<Field, JsonStringArrayList<?>> schemaAndValue, | ||
| Object[] expected, | ||
| int javaSqlTypeCode) | ||
| throws SQLException { | ||
| Array array = new BigQueryArrowArray(schemaAndValue.x(), schemaAndValue.y()); | ||
| assertThat(array.getBaseTypeName()).isEqualTo(currentType.name()); | ||
| } | ||
|
|
||
| @Test | ||
| public void getBaseType() throws SQLException { | ||
| assertThat(array.getBaseType()).isEqualTo(this.javaSqlTypeCode); | ||
| @ParameterizedTest(name = "{index}: primitive array of {0}") | ||
| @MethodSource("data") | ||
| public void getBaseType( | ||
| StandardSQLTypeName currentType, | ||
| Tuple<Field, JsonStringArrayList<?>> schemaAndValue, | ||
| Object[] expected, | ||
| int javaSqlTypeCode) | ||
| throws SQLException { | ||
| Array array = new BigQueryArrowArray(schemaAndValue.x(), schemaAndValue.y()); | ||
| assertThat(array.getBaseType()).isEqualTo(javaSqlTypeCode); | ||
| } | ||
|
|
||
| @Test | ||
| public void free() throws SQLException { | ||
| this.array.free(); | ||
| @ParameterizedTest(name = "{index}: primitive array of {0}") | ||
| @MethodSource("data") | ||
| public void free( | ||
| StandardSQLTypeName currentType, | ||
| Tuple<Field, JsonStringArrayList<?>> schemaAndValue, | ||
| Object[] expected, | ||
| int javaSqlTypeCode) | ||
| throws SQLException { | ||
| Array array = new BigQueryArrowArray(schemaAndValue.x(), schemaAndValue.y()); | ||
| array.free(); | ||
|
|
||
| ensureArrayIsInvalid(() -> array.getArray()); | ||
| ensureArrayIsInvalid(() -> array.getArray(1, 2)); | ||
|
|
@@ -339,8 +371,14 @@ public void free() throws SQLException { | |
| ensureArrayIsInvalid(() -> array.getBaseType()); | ||
| } | ||
|
|
||
| @Test | ||
| public void getArrayWithCustomTypeMappingsIsNotSupported() { | ||
| @ParameterizedTest(name = "{index}: primitive array of {0}") | ||
| @MethodSource("data") | ||
| public void getArrayWithCustomTypeMappingsIsNotSupported( | ||
| StandardSQLTypeName currentType, | ||
| Tuple<Field, JsonStringArrayList<?>> schemaAndValue, | ||
| Object[] expected, | ||
| int javaSqlTypeCode) { | ||
| Array array = new BigQueryArrowArray(schemaAndValue.x(), schemaAndValue.y()); | ||
| Exception exception1 = | ||
| assertThrows(SQLFeatureNotSupportedException.class, () -> array.getArray(emptyMap())); | ||
| Exception exception2 = | ||
|
|
@@ -349,8 +387,14 @@ public void getArrayWithCustomTypeMappingsIsNotSupported() { | |
| assertThat(exception2.getMessage()).isEqualTo(CUSTOMER_TYPE_MAPPING_NOT_SUPPORTED); | ||
| } | ||
|
|
||
| @Test | ||
| public void getResultSetWithCustomTypeMappingsIsNotSupported() { | ||
| @ParameterizedTest(name = "{index}: primitive array of {0}") | ||
| @MethodSource("data") | ||
| public void getResultSetWithCustomTypeMappingsIsNotSupported( | ||
| StandardSQLTypeName currentType, | ||
| Tuple<Field, JsonStringArrayList<?>> schemaAndValue, | ||
| Object[] expected, | ||
| int javaSqlTypeCode) { | ||
| Array array = new BigQueryArrowArray(schemaAndValue.x(), schemaAndValue.y()); | ||
| Exception exception1 = | ||
| assertThrows(SQLFeatureNotSupportedException.class, () -> array.getResultSet(emptyMap())); | ||
| Exception exception2 = | ||
|
|
@@ -360,7 +404,7 @@ public void getResultSetWithCustomTypeMappingsIsNotSupported() { | |
| assertThat(exception2.getMessage()).isEqualTo(CUSTOMER_TYPE_MAPPING_NOT_SUPPORTED); | ||
| } | ||
|
|
||
| private void ensureArrayIsInvalid(ThrowingRunnable block) { | ||
| private void ensureArrayIsInvalid(Executable block) { | ||
| Exception exception = assertThrows(IllegalStateException.class, block); | ||
| assertThat(exception.getMessage()).isEqualTo(INVALID_ARRAY); | ||
| } | ||
|
|
||
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
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.
The migration from JUnit 4's
@RunWith(Parameterized.class)and@Parametersto JUnit 5's@ParameterizedTestand@MethodSourceis correctly implemented. This improves test flexibility and readability.