Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions google-cloud-bigquery-jdbc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -276,9 +276,21 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.11.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.11.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.11.4</version>
<scope>test</scope>
</dependency>
<dependency>
Expand All @@ -287,6 +299,12 @@
<version>4.11.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>4.11.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<profiles>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@
import org.apache.arrow.vector.util.JsonStringArrayList;
import org.apache.arrow.vector.util.JsonStringHashMap;
import org.apache.arrow.vector.util.Text;
import org.junit.Rule;
import org.junit.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.api.Test;

public class ArrowFormatTypeBigQueryCoercionUtilityTest {

@Rule public final TimeZoneRule timeZoneRule = new TimeZoneRule("UTC");
@RegisterExtension public final TimeZoneRule timeZoneRule = new TimeZoneRule("UTC");

private static final Range RANGE_DATE =
Range.newBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -62,44 +62,20 @@
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.BeforeEach;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.api.function.Executable;

@RunWith(Parameterized.class)
public class BigQueryArrowArrayOfPrimitivesTest {

private final Field schema;
private final JsonStringArrayList<?> arrayValues;
private final Object[] expected;
private final int javaSqlTypeCode;
private Array array;
private final StandardSQLTypeName currentType;
public class BigQueryArrowArrayOfPrimitivesTest {

@ClassRule public static final TimeZoneRule timeZoneRule = new TimeZoneRule("UTC");
@RegisterExtension
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);
Expand Down Expand Up @@ -254,48 +230,58 @@ 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);
}

@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);
Expand All @@ -309,27 +295,35 @@ 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));
Expand All @@ -339,8 +333,10 @@ 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 =
Expand All @@ -349,8 +345,10 @@ 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 =
Expand All @@ -360,7 +358,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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import static java.lang.Boolean.TRUE;
import static java.util.Arrays.asList;
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;
Expand All @@ -48,15 +48,15 @@
import org.apache.arrow.vector.util.JsonStringArrayList;
import org.apache.arrow.vector.util.JsonStringHashMap;
import org.apache.arrow.vector.util.Text;
import org.junit.Before;
import org.junit.Test;
import org.junit.function.ThrowingRunnable;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;

public class BigQueryArrowArrayOfStructTest {

private Array array;

@Before
@BeforeEach
public void setUp() {
FieldList profileSchema =
FieldList.of(
Expand Down Expand Up @@ -198,7 +198,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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@
import org.apache.arrow.vector.types.pojo.FieldType;
import org.apache.arrow.vector.util.JsonStringArrayList;
import org.apache.arrow.vector.util.Text;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class BigQueryArrowResultSetTest {

Expand Down Expand Up @@ -194,7 +194,7 @@ private JsonStringArrayList<Long> getJsonStringArrayList() {
return jsonStringArrayList;
}

@Before
@BeforeEach
public void setUp() throws SQLException, IOException {
buffer = new LinkedBlockingDeque<>();
bufferWithTwoRows = new LinkedBlockingDeque<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
import static java.time.Month.MARCH;
import static java.util.Arrays.asList;
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;
Expand All @@ -63,17 +63,17 @@
import org.apache.arrow.vector.util.JsonStringArrayList;
import org.apache.arrow.vector.util.JsonStringHashMap;
import org.apache.arrow.vector.util.Text;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.api.Test;

public class BigQueryArrowStructTest {

@Rule public final TimeZoneRule timeZoneRule = new TimeZoneRule("UTC");
@RegisterExtension public final TimeZoneRule timeZoneRule = new TimeZoneRule("UTC");

private Struct structWithPrimitiveValues;

@Before
@BeforeEach
public void setUp() {
Tuple<FieldList, JsonStringHashMap<String, Object>> schemaAndValues =
arrowStructOf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package com.google.cloud.bigquery.jdbc;

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertFalse;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.CALLS_REAL_METHODS;
import static org.mockito.Mockito.doReturn;
Expand All @@ -28,16 +28,16 @@
import com.google.cloud.bigquery.JobId;
import com.google.cloud.bigquery.JobStatistics.QueryStatistics;
import java.lang.reflect.Field;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class BigQueryBaseResultSetTest {
private BigQuery bigQuery;
private BigQueryBaseResultSet resultSet;
private Job job;
private QueryStatistics statistics;

@Before
@BeforeEach
public void setUp() {
// Using mock() for QueryStatistics because Builder() seems to not be available
// from outside.
Expand Down
Loading
Loading