3838import static java .time .Month .MARCH ;
3939import static java .util .Arrays .copyOfRange ;
4040import static java .util .Collections .emptyMap ;
41- import static org .junit .Assert .assertThrows ;
41+ import static org .junit .jupiter . api . Assertions .assertThrows ;
4242
4343import com .google .cloud .Tuple ;
4444import com .google .cloud .bigquery .Field ;
6262import java .util .stream .Stream ;
6363import org .apache .arrow .vector .util .JsonStringArrayList ;
6464import org .apache .arrow .vector .util .Text ;
65- import org .junit .Before ;
66- import org .junit .ClassRule ;
67- import org .junit .Test ;
68- import org .junit .function .ThrowingRunnable ;
69- import org .junit .runner .RunWith ;
70- import org .junit .runners .Parameterized ;
71- import org .junit .runners .Parameterized .Parameters ;
65+ import org .junit .jupiter .api .BeforeEach ;
66+ import org .junit .jupiter .api .extension .RegisterExtension ;
67+ import org .junit .jupiter .params .ParameterizedTest ;
68+ import org .junit .jupiter .params .provider .MethodSource ;
69+ import org .junit .jupiter .api .function .Executable ;
7270
73- @ RunWith (Parameterized .class )
74- public class BigQueryArrowArrayOfPrimitivesTest {
7571
76- private final Field schema ;
77- private final JsonStringArrayList <?> arrayValues ;
78- private final Object [] expected ;
79- private final int javaSqlTypeCode ;
80- private Array array ;
81- private final StandardSQLTypeName currentType ;
72+ public class BigQueryArrowArrayOfPrimitivesTest {
8273
83- @ ClassRule public static final TimeZoneRule timeZoneRule = new TimeZoneRule ("UTC" );
74+ @ RegisterExtension
75+ public static final TimeZoneRule timeZoneRule = new TimeZoneRule ("UTC" );
8476
85- public BigQueryArrowArrayOfPrimitivesTest (
86- StandardSQLTypeName currentType ,
87- Tuple <Field , JsonStringArrayList <?>> schemaAndValue ,
88- Object [] expected ,
89- int javaSqlTypeCode ) {
90- this .currentType = currentType ;
91- this .schema = schemaAndValue .x ();
92- this .arrayValues = schemaAndValue .y ();
93- this .expected = expected ;
94- this .javaSqlTypeCode = javaSqlTypeCode ;
95- }
96-
97- @ Before
98- public void setUp () {
99- array = new BigQueryArrowArray (this .schema , this .arrayValues );
100- }
10177
102- @ Parameters ( name = "{index}: primitive array of {0}" )
78+
10379 public static Collection <Object []> data () {
10480 timeZoneRule .enforce ();
10581 LocalDateTime aTimeStamp = LocalDateTime .of (2023 , MARCH , 30 , 11 , 14 , 19 , 820227000 );
@@ -254,48 +230,58 @@ STRING, new Text("one"), new Text("two"), new Text("three"), new Text("four")),
254230 });
255231 }
256232
257- @ Test
258- public void getArray () throws SQLException {
259- assertThat (array .getArray ()).isEqualTo (this .expected );
233+ @ ParameterizedTest (name = "{index}: primitive array of {0}" )
234+ @ MethodSource ("data" )
235+ public void getArray (StandardSQLTypeName currentType , Tuple <Field , JsonStringArrayList <?>> schemaAndValue , Object [] expected , int javaSqlTypeCode ) throws SQLException {
236+ Array array = new BigQueryArrowArray (schemaAndValue .x (), schemaAndValue .y ());
237+ assertThat (array .getArray ()).isEqualTo (expected );
260238 }
261239
262- @ Test
263- public void getSlicedArray () throws SQLException {
240+ @ ParameterizedTest (name = "{index}: primitive array of {0}" )
241+ @ MethodSource ("data" )
242+ public void getSlicedArray (StandardSQLTypeName currentType , Tuple <Field , JsonStringArrayList <?>> schemaAndValue , Object [] expected , int javaSqlTypeCode ) throws SQLException {
243+ Array array = new BigQueryArrowArray (schemaAndValue .x (), schemaAndValue .y ());
264244 int fromIndex = 1 ;
265245 int toIndexExclusive = 3 ;
266246 Object [] expectedSlicedArray =
267- copyOfRange (this . expected , fromIndex , toIndexExclusive ); // copying index(1,2)
247+ copyOfRange (expected , fromIndex , toIndexExclusive ); // copying index(1,2)
268248
269249 // the first element is at index 1
270250 assertThat (array .getArray (fromIndex + 1 , 2 )).isEqualTo (expectedSlicedArray );
271251 }
272252
273- @ Test
274- public void getSlicedArrayWhenCountIsGreaterThanOriginalArrayLength () {
253+ @ ParameterizedTest (name = "{index}: primitive array of {0}" )
254+ @ MethodSource ("data" )
255+ public void getSlicedArrayWhenCountIsGreaterThanOriginalArrayLength (StandardSQLTypeName currentType , Tuple <Field , JsonStringArrayList <?>> schemaAndValue , Object [] expected , int javaSqlTypeCode ) {
256+ Array array = new BigQueryArrowArray (schemaAndValue .x (), schemaAndValue .y ());
275257 IllegalArgumentException illegalArgumentException =
276258 assertThrows (IllegalArgumentException .class , () -> array .getArray (2 , 10 ));
277259 assertThat (illegalArgumentException .getMessage ())
278260 .isEqualTo ("The array index is out of range: 12, number of elements: 4." );
279261 }
280262
281- @ Test
282- public void getResultSet () throws SQLException {
283- ResultSet resultSet = this .array .getResultSet ();
263+ @ ParameterizedTest (name = "{index}: primitive array of {0}" )
264+ @ MethodSource ("data" )
265+ public void getResultSet (StandardSQLTypeName currentType , Tuple <Field , JsonStringArrayList <?>> schemaAndValue , Object [] expected , int javaSqlTypeCode ) throws SQLException {
266+ Array array = new BigQueryArrowArray (schemaAndValue .x (), schemaAndValue .y ());
267+ ResultSet resultSet = array .getResultSet ();
284268 Tuple <ArrayList <Object >, ArrayList <Object >> indexAndValues =
285269 nestedResultSetToColumnLists (resultSet );
286270 ArrayList <Object > indexList = indexAndValues .x ();
287271 ArrayList <Object > columnValues = indexAndValues .y ();
288272
289273 assertThat (indexList .toArray ()).isEqualTo (new Object [] {1 , 2 , 3 , 4 });
290- assertThat (columnValues .toArray ()).isEqualTo (this . expected );
274+ assertThat (columnValues .toArray ()).isEqualTo (expected );
291275 }
292276
293- @ Test
294- public void getSlicedResultSet () throws SQLException {
277+ @ ParameterizedTest (name = "{index}: primitive array of {0}" )
278+ @ MethodSource ("data" )
279+ public void getSlicedResultSet (StandardSQLTypeName currentType , Tuple <Field , JsonStringArrayList <?>> schemaAndValue , Object [] expected , int javaSqlTypeCode ) throws SQLException {
280+ Array array = new BigQueryArrowArray (schemaAndValue .x (), schemaAndValue .y ());
295281 int fromIndex = 1 ;
296282 int toIndexExclusive = 3 ;
297283 Object [] expectedSlicedArray =
298- copyOfRange (this . expected , fromIndex , toIndexExclusive ); // copying index(1,2)
284+ copyOfRange (expected , fromIndex , toIndexExclusive ); // copying index(1,2)
299285
300286 // the first element is at index 1
301287 ResultSet resultSet = array .getResultSet (fromIndex + 1 , 2 );
@@ -309,27 +295,35 @@ public void getSlicedResultSet() throws SQLException {
309295 assertThat (columnValues .toArray ()).isEqualTo (expectedSlicedArray );
310296 }
311297
312- @ Test
313- public void getSlicedResultSetWhenCountIsGreaterThanOriginalArrayLength () {
298+ @ ParameterizedTest (name = "{index}: primitive array of {0}" )
299+ @ MethodSource ("data" )
300+ public void getSlicedResultSetWhenCountIsGreaterThanOriginalArrayLength (StandardSQLTypeName currentType , Tuple <Field , JsonStringArrayList <?>> schemaAndValue , Object [] expected , int javaSqlTypeCode ) {
301+ Array array = new BigQueryArrowArray (schemaAndValue .x (), schemaAndValue .y ());
314302 IllegalArgumentException illegalArgumentException =
315303 assertThrows (IllegalArgumentException .class , () -> array .getResultSet (2 , 10 ));
316304 assertThat (illegalArgumentException .getMessage ())
317305 .isEqualTo ("The array index is out of range: 12, number of elements: 4." );
318306 }
319307
320- @ Test
321- public void getBaseTypeName () throws SQLException {
322- assertThat (array .getBaseTypeName ()).isEqualTo (this .currentType .name ());
308+ @ ParameterizedTest (name = "{index}: primitive array of {0}" )
309+ @ MethodSource ("data" )
310+ public void getBaseTypeName (StandardSQLTypeName currentType , Tuple <Field , JsonStringArrayList <?>> schemaAndValue , Object [] expected , int javaSqlTypeCode ) throws SQLException {
311+ Array array = new BigQueryArrowArray (schemaAndValue .x (), schemaAndValue .y ());
312+ assertThat (array .getBaseTypeName ()).isEqualTo (currentType .name ());
323313 }
324314
325- @ Test
326- public void getBaseType () throws SQLException {
327- assertThat (array .getBaseType ()).isEqualTo (this .javaSqlTypeCode );
315+ @ ParameterizedTest (name = "{index}: primitive array of {0}" )
316+ @ MethodSource ("data" )
317+ public void getBaseType (StandardSQLTypeName currentType , Tuple <Field , JsonStringArrayList <?>> schemaAndValue , Object [] expected , int javaSqlTypeCode ) throws SQLException {
318+ Array array = new BigQueryArrowArray (schemaAndValue .x (), schemaAndValue .y ());
319+ assertThat (array .getBaseType ()).isEqualTo (javaSqlTypeCode );
328320 }
329321
330- @ Test
331- public void free () throws SQLException {
332- this .array .free ();
322+ @ ParameterizedTest (name = "{index}: primitive array of {0}" )
323+ @ MethodSource ("data" )
324+ public void free (StandardSQLTypeName currentType , Tuple <Field , JsonStringArrayList <?>> schemaAndValue , Object [] expected , int javaSqlTypeCode ) throws SQLException {
325+ Array array = new BigQueryArrowArray (schemaAndValue .x (), schemaAndValue .y ());
326+ array .free ();
333327
334328 ensureArrayIsInvalid (() -> array .getArray ());
335329 ensureArrayIsInvalid (() -> array .getArray (1 , 2 ));
@@ -339,8 +333,10 @@ public void free() throws SQLException {
339333 ensureArrayIsInvalid (() -> array .getBaseType ());
340334 }
341335
342- @ Test
343- public void getArrayWithCustomTypeMappingsIsNotSupported () {
336+ @ ParameterizedTest (name = "{index}: primitive array of {0}" )
337+ @ MethodSource ("data" )
338+ public void getArrayWithCustomTypeMappingsIsNotSupported (StandardSQLTypeName currentType , Tuple <Field , JsonStringArrayList <?>> schemaAndValue , Object [] expected , int javaSqlTypeCode ) {
339+ Array array = new BigQueryArrowArray (schemaAndValue .x (), schemaAndValue .y ());
344340 Exception exception1 =
345341 assertThrows (SQLFeatureNotSupportedException .class , () -> array .getArray (emptyMap ()));
346342 Exception exception2 =
@@ -349,8 +345,10 @@ public void getArrayWithCustomTypeMappingsIsNotSupported() {
349345 assertThat (exception2 .getMessage ()).isEqualTo (CUSTOMER_TYPE_MAPPING_NOT_SUPPORTED );
350346 }
351347
352- @ Test
353- public void getResultSetWithCustomTypeMappingsIsNotSupported () {
348+ @ ParameterizedTest (name = "{index}: primitive array of {0}" )
349+ @ MethodSource ("data" )
350+ public void getResultSetWithCustomTypeMappingsIsNotSupported (StandardSQLTypeName currentType , Tuple <Field , JsonStringArrayList <?>> schemaAndValue , Object [] expected , int javaSqlTypeCode ) {
351+ Array array = new BigQueryArrowArray (schemaAndValue .x (), schemaAndValue .y ());
354352 Exception exception1 =
355353 assertThrows (SQLFeatureNotSupportedException .class , () -> array .getResultSet (emptyMap ()));
356354 Exception exception2 =
@@ -360,7 +358,7 @@ public void getResultSetWithCustomTypeMappingsIsNotSupported() {
360358 assertThat (exception2 .getMessage ()).isEqualTo (CUSTOMER_TYPE_MAPPING_NOT_SUPPORTED );
361359 }
362360
363- private void ensureArrayIsInvalid (ThrowingRunnable block ) {
361+ private void ensureArrayIsInvalid (Executable block ) {
364362 Exception exception = assertThrows (IllegalStateException .class , block );
365363 assertThat (exception .getMessage ()).isEqualTo (INVALID_ARRAY );
366364 }
0 commit comments