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 .extension .RegisterExtension ;
66+ import org .junit .jupiter .api .function .Executable ;
67+ import org .junit .jupiter .params .ParameterizedTest ;
68+ import org .junit .jupiter .params .provider .MethodSource ;
7269
73- @ RunWith (Parameterized .class )
7470public 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+ @ RegisterExtension public static final TimeZoneRule timeZoneRule = new TimeZoneRule ("UTC" );
8273
83- @ ClassRule public static final TimeZoneRule timeZoneRule = new TimeZoneRule ("UTC" );
84-
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- }
101-
102- @ Parameters (name = "{index}: primitive array of {0}" )
10374 public static Collection <Object []> data () {
10475 timeZoneRule .enforce ();
10576 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")),
254225 });
255226 }
256227
257- @ Test
258- public void getArray () throws SQLException {
259- assertThat (array .getArray ()).isEqualTo (this .expected );
228+ @ ParameterizedTest (name = "{index}: primitive array of {0}" )
229+ @ MethodSource ("data" )
230+ public void getArray (
231+ StandardSQLTypeName currentType ,
232+ Tuple <Field , JsonStringArrayList <?>> schemaAndValue ,
233+ Object [] expected ,
234+ int javaSqlTypeCode )
235+ 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 (
243+ StandardSQLTypeName currentType ,
244+ Tuple <Field , JsonStringArrayList <?>> schemaAndValue ,
245+ Object [] expected ,
246+ int javaSqlTypeCode )
247+ throws SQLException {
248+ Array array = new BigQueryArrowArray (schemaAndValue .x (), schemaAndValue .y ());
264249 int fromIndex = 1 ;
265250 int toIndexExclusive = 3 ;
266251 Object [] expectedSlicedArray =
267- copyOfRange (this . expected , fromIndex , toIndexExclusive ); // copying index(1,2)
252+ copyOfRange (expected , fromIndex , toIndexExclusive ); // copying index(1,2)
268253
269254 // the first element is at index 1
270255 assertThat (array .getArray (fromIndex + 1 , 2 )).isEqualTo (expectedSlicedArray );
271256 }
272257
273- @ Test
274- public void getSlicedArrayWhenCountIsGreaterThanOriginalArrayLength () {
258+ @ ParameterizedTest (name = "{index}: primitive array of {0}" )
259+ @ MethodSource ("data" )
260+ public void getSlicedArrayWhenCountIsGreaterThanOriginalArrayLength (
261+ StandardSQLTypeName currentType ,
262+ Tuple <Field , JsonStringArrayList <?>> schemaAndValue ,
263+ Object [] expected ,
264+ int javaSqlTypeCode ) {
265+ Array array = new BigQueryArrowArray (schemaAndValue .x (), schemaAndValue .y ());
275266 IllegalArgumentException illegalArgumentException =
276267 assertThrows (IllegalArgumentException .class , () -> array .getArray (2 , 10 ));
277268 assertThat (illegalArgumentException .getMessage ())
278269 .isEqualTo ("The array index is out of range: 12, number of elements: 4." );
279270 }
280271
281- @ Test
282- public void getResultSet () throws SQLException {
283- ResultSet resultSet = this .array .getResultSet ();
272+ @ ParameterizedTest (name = "{index}: primitive array of {0}" )
273+ @ MethodSource ("data" )
274+ public void getResultSet (
275+ StandardSQLTypeName currentType ,
276+ Tuple <Field , JsonStringArrayList <?>> schemaAndValue ,
277+ Object [] expected ,
278+ int javaSqlTypeCode )
279+ throws SQLException {
280+ Array array = new BigQueryArrowArray (schemaAndValue .x (), schemaAndValue .y ());
281+ ResultSet resultSet = array .getResultSet ();
284282 Tuple <ArrayList <Object >, ArrayList <Object >> indexAndValues =
285283 nestedResultSetToColumnLists (resultSet );
286284 ArrayList <Object > indexList = indexAndValues .x ();
287285 ArrayList <Object > columnValues = indexAndValues .y ();
288286
289287 assertThat (indexList .toArray ()).isEqualTo (new Object [] {1 , 2 , 3 , 4 });
290- assertThat (columnValues .toArray ()).isEqualTo (this . expected );
288+ assertThat (columnValues .toArray ()).isEqualTo (expected );
291289 }
292290
293- @ Test
294- public void getSlicedResultSet () throws SQLException {
291+ @ ParameterizedTest (name = "{index}: primitive array of {0}" )
292+ @ MethodSource ("data" )
293+ public void getSlicedResultSet (
294+ StandardSQLTypeName currentType ,
295+ Tuple <Field , JsonStringArrayList <?>> schemaAndValue ,
296+ Object [] expected ,
297+ int javaSqlTypeCode )
298+ throws SQLException {
299+ Array array = new BigQueryArrowArray (schemaAndValue .x (), schemaAndValue .y ());
295300 int fromIndex = 1 ;
296301 int toIndexExclusive = 3 ;
297302 Object [] expectedSlicedArray =
298- copyOfRange (this . expected , fromIndex , toIndexExclusive ); // copying index(1,2)
303+ copyOfRange (expected , fromIndex , toIndexExclusive ); // copying index(1,2)
299304
300305 // the first element is at index 1
301306 ResultSet resultSet = array .getResultSet (fromIndex + 1 , 2 );
@@ -309,27 +314,54 @@ public void getSlicedResultSet() throws SQLException {
309314 assertThat (columnValues .toArray ()).isEqualTo (expectedSlicedArray );
310315 }
311316
312- @ Test
313- public void getSlicedResultSetWhenCountIsGreaterThanOriginalArrayLength () {
317+ @ ParameterizedTest (name = "{index}: primitive array of {0}" )
318+ @ MethodSource ("data" )
319+ public void getSlicedResultSetWhenCountIsGreaterThanOriginalArrayLength (
320+ StandardSQLTypeName currentType ,
321+ Tuple <Field , JsonStringArrayList <?>> schemaAndValue ,
322+ Object [] expected ,
323+ int javaSqlTypeCode ) {
324+ Array array = new BigQueryArrowArray (schemaAndValue .x (), schemaAndValue .y ());
314325 IllegalArgumentException illegalArgumentException =
315326 assertThrows (IllegalArgumentException .class , () -> array .getResultSet (2 , 10 ));
316327 assertThat (illegalArgumentException .getMessage ())
317328 .isEqualTo ("The array index is out of range: 12, number of elements: 4." );
318329 }
319330
320- @ Test
321- public void getBaseTypeName () throws SQLException {
322- assertThat (array .getBaseTypeName ()).isEqualTo (this .currentType .name ());
331+ @ ParameterizedTest (name = "{index}: primitive array of {0}" )
332+ @ MethodSource ("data" )
333+ public void getBaseTypeName (
334+ StandardSQLTypeName currentType ,
335+ Tuple <Field , JsonStringArrayList <?>> schemaAndValue ,
336+ Object [] expected ,
337+ int javaSqlTypeCode )
338+ throws SQLException {
339+ Array array = new BigQueryArrowArray (schemaAndValue .x (), schemaAndValue .y ());
340+ assertThat (array .getBaseTypeName ()).isEqualTo (currentType .name ());
323341 }
324342
325- @ Test
326- public void getBaseType () throws SQLException {
327- assertThat (array .getBaseType ()).isEqualTo (this .javaSqlTypeCode );
343+ @ ParameterizedTest (name = "{index}: primitive array of {0}" )
344+ @ MethodSource ("data" )
345+ public void getBaseType (
346+ StandardSQLTypeName currentType ,
347+ Tuple <Field , JsonStringArrayList <?>> schemaAndValue ,
348+ Object [] expected ,
349+ int javaSqlTypeCode )
350+ throws SQLException {
351+ Array array = new BigQueryArrowArray (schemaAndValue .x (), schemaAndValue .y ());
352+ assertThat (array .getBaseType ()).isEqualTo (javaSqlTypeCode );
328353 }
329354
330- @ Test
331- public void free () throws SQLException {
332- this .array .free ();
355+ @ ParameterizedTest (name = "{index}: primitive array of {0}" )
356+ @ MethodSource ("data" )
357+ public void free (
358+ StandardSQLTypeName currentType ,
359+ Tuple <Field , JsonStringArrayList <?>> schemaAndValue ,
360+ Object [] expected ,
361+ int javaSqlTypeCode )
362+ throws SQLException {
363+ Array array = new BigQueryArrowArray (schemaAndValue .x (), schemaAndValue .y ());
364+ array .free ();
333365
334366 ensureArrayIsInvalid (() -> array .getArray ());
335367 ensureArrayIsInvalid (() -> array .getArray (1 , 2 ));
@@ -339,8 +371,14 @@ public void free() throws SQLException {
339371 ensureArrayIsInvalid (() -> array .getBaseType ());
340372 }
341373
342- @ Test
343- public void getArrayWithCustomTypeMappingsIsNotSupported () {
374+ @ ParameterizedTest (name = "{index}: primitive array of {0}" )
375+ @ MethodSource ("data" )
376+ public void getArrayWithCustomTypeMappingsIsNotSupported (
377+ StandardSQLTypeName currentType ,
378+ Tuple <Field , JsonStringArrayList <?>> schemaAndValue ,
379+ Object [] expected ,
380+ int javaSqlTypeCode ) {
381+ Array array = new BigQueryArrowArray (schemaAndValue .x (), schemaAndValue .y ());
344382 Exception exception1 =
345383 assertThrows (SQLFeatureNotSupportedException .class , () -> array .getArray (emptyMap ()));
346384 Exception exception2 =
@@ -349,8 +387,14 @@ public void getArrayWithCustomTypeMappingsIsNotSupported() {
349387 assertThat (exception2 .getMessage ()).isEqualTo (CUSTOMER_TYPE_MAPPING_NOT_SUPPORTED );
350388 }
351389
352- @ Test
353- public void getResultSetWithCustomTypeMappingsIsNotSupported () {
390+ @ ParameterizedTest (name = "{index}: primitive array of {0}" )
391+ @ MethodSource ("data" )
392+ public void getResultSetWithCustomTypeMappingsIsNotSupported (
393+ StandardSQLTypeName currentType ,
394+ Tuple <Field , JsonStringArrayList <?>> schemaAndValue ,
395+ Object [] expected ,
396+ int javaSqlTypeCode ) {
397+ Array array = new BigQueryArrowArray (schemaAndValue .x (), schemaAndValue .y ());
354398 Exception exception1 =
355399 assertThrows (SQLFeatureNotSupportedException .class , () -> array .getResultSet (emptyMap ()));
356400 Exception exception2 =
@@ -360,7 +404,7 @@ public void getResultSetWithCustomTypeMappingsIsNotSupported() {
360404 assertThat (exception2 .getMessage ()).isEqualTo (CUSTOMER_TYPE_MAPPING_NOT_SUPPORTED );
361405 }
362406
363- private void ensureArrayIsInvalid (ThrowingRunnable block ) {
407+ private void ensureArrayIsInvalid (Executable block ) {
364408 Exception exception = assertThrows (IllegalStateException .class , block );
365409 assertThat (exception .getMessage ()).isEqualTo (INVALID_ARRAY );
366410 }
0 commit comments