Skip to content

Commit

Permalink
update parameterized test (step 2) for jdbc
Browse files Browse the repository at this point in the history
 - initize database within the test methods
  • Loading branch information
llama90 committed Jun 8, 2024
1 parent b9edf08 commit 19aa9fd
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,15 @@ protected static Table getTable(String ymlFilePath, @SuppressWarnings("rawtypes"
clss.getClassLoader().getResourceAsStream(ymlFilePath), Table.class);
}


/**
* This method creates Connection object and DB table and also populate data into table for test.
*
* @throws SQLException on error
* @throws ClassNotFoundException on error
*/
public void setUp() throws SQLException, ClassNotFoundException {
protected void initializeDatabase(Table table) throws SQLException, ClassNotFoundException {
this.table = table;

TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
String url = "jdbc:h2:mem:JdbcToArrowTest";
String driver = "org.h2.Driver";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@
import java.sql.Types;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Stream;

import org.apache.arrow.memory.BufferAllocator;
import org.apache.arrow.memory.RootAllocator;
Expand All @@ -44,22 +44,16 @@
import org.apache.arrow.vector.types.pojo.Schema;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

/**
* Test options for dealing with unreliable ResultSetMetaData from JDBC drivers.
*/
@RunWith(Parameterized.class)
public class UnreliableMetaDataTest {
private final boolean reuseVectorSchemaRoot;
private BufferAllocator allocator;

public UnreliableMetaDataTest(boolean reuseVectorSchemaRoot) {
this.reuseVectorSchemaRoot = reuseVectorSchemaRoot;
}

@BeforeEach
public void beforeEach() {
allocator = new RootAllocator();
Expand All @@ -70,13 +64,13 @@ public void afterEach() {
allocator.close();
}

@Parameterized.Parameters(name = "reuseVectorSchemaRoot = {0}")
public static Collection<Object[]> getTestData() {
return Arrays.asList(new Object[][] { {false}, {true} });
public static Stream<Arguments> getTestData() {
return Arrays.stream(new Object[][] { {false}, {true} }).map(Arguments::of);
}

@Test
public void testUnreliableMetaDataPrecisionAndScale() throws Exception {
@ParameterizedTest
@MethodSource("getTestData")
public void testUnreliableMetaDataPrecisionAndScale(boolean reuseVectorSchemaRoot) throws Exception {
ResultSet rs = buildIncorrectPrecisionAndScaleMetaDataResultSet();
ResultSetMetaData rsmd = rs.getMetaData();
assertEquals(Types.DECIMAL, rsmd.getColumnType(1), "Column type should be Types.DECIMAL");
Expand Down Expand Up @@ -119,8 +113,9 @@ public void testUnreliableMetaDataPrecisionAndScale() throws Exception {
}
}

@Test
public void testInconsistentPrecisionAndScale() throws Exception {
@ParameterizedTest
@MethodSource("getTestData")
public void testInconsistentPrecisionAndScale(boolean reuseVectorSchemaRoot) throws Exception {
ResultSet rs = buildVaryingPrecisionAndScaleResultSet();
ResultSetMetaData rsmd = rs.getMetaData();
assertEquals(Types.DECIMAL, rsmd.getColumnType(1), "Column type should be Types.DECIMAL");
Expand Down Expand Up @@ -166,8 +161,9 @@ public void testInconsistentPrecisionAndScale() throws Exception {
}
}

@Test
public void testIncorrectNullability() throws Exception {
@ParameterizedTest
@MethodSource("getTestData")
public void testIncorrectNullability(boolean reuseVectorSchemaRoot) throws Exception {
// ARROW-17005: ResultSetMetaData may indicate a field is non-nullable even when there are nulls
ResultSetUtility.MockResultSetMetaData.MockColumnMetaData columnMetaData =
ResultSetUtility.MockResultSetMetaData.MockColumnMetaData.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import org.apache.arrow.vector.VarCharVector;
import org.apache.arrow.vector.VectorSchemaRoot;
import org.apache.arrow.vector.types.pojo.Schema;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
Expand All @@ -58,12 +57,13 @@ public class JdbcToArrowCharSetTest extends AbstractJdbcToArrowTest {
"h2/test1_charset_kr_h2.yml"
};

private void initializeDatabase(Table table) throws SQLException, ClassNotFoundException {
public void initializeDatabase(Table table) throws SQLException, ClassNotFoundException {
this.table = table;

String url = "jdbc:h2:mem:JdbcToArrowTest?characterEncoding=UTF-8";
String driver = "org.h2.Driver";
Class.forName(driver);
conn = DriverManager.getConnection(url);
this.table = table;
try (Statement stmt = conn.createStatement();) {
stmt.executeUpdate(table.getCreate());
for (String insert : table.getData()) {
Expand Down Expand Up @@ -91,7 +91,8 @@ public static Stream<Arguments> getTestData() throws SQLException, ClassNotFound
@ParameterizedTest
@MethodSource("getTestData")
public void testJdbcToArrowValues(Table table) throws SQLException, IOException, ClassNotFoundException {
initializeDatabase(table);
this.initializeDatabase(table);

testDataSets(sqlToArrow(conn, table.getQuery(), new RootAllocator(Integer.MAX_VALUE),
Calendar.getInstance()), false);
testDataSets(sqlToArrow(conn, table.getQuery(), new RootAllocator(Integer.MAX_VALUE)), false);
Expand All @@ -111,8 +112,11 @@ public void testJdbcToArrowValues(Table table) throws SQLException, IOException,
new JdbcToArrowConfigBuilder(new RootAllocator(Integer.MAX_VALUE), Calendar.getInstance()).build()), false);
}

@Test
public void testJdbcSchemaMetadata() throws SQLException {
@ParameterizedTest
@MethodSource("getTestData")
public void testJdbcSchemaMetadata(Table table) throws SQLException, ClassNotFoundException {
this.initializeDatabase(table);

JdbcToArrowConfig config = new JdbcToArrowConfigBuilder(new RootAllocator(0), Calendar.getInstance(), true).build();
ResultSetMetaData rsmd = conn.createStatement().executeQuery(table.getQuery()).getMetaData();
Schema schema = JdbcToArrowUtils.jdbcToArrowSchema(rsmd, config);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@
import org.apache.arrow.vector.VectorSchemaRoot;
import org.apache.arrow.vector.complex.ListVector;
import org.apache.arrow.vector.types.pojo.Schema;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
Expand Down Expand Up @@ -135,7 +134,9 @@ public static Stream<Arguments> getTestData() throws SQLException, ClassNotFound
*/
@ParameterizedTest
@MethodSource("getTestData")
public void testJdbcToArrowValues(Table table) throws SQLException, IOException {
public void testJdbcToArrowValues(Table table) throws SQLException, IOException, ClassNotFoundException {
this.initializeDatabase(table);

testDataSets(sqlToArrow(conn, table.getQuery(), new RootAllocator(Integer.MAX_VALUE),
Calendar.getInstance()), false);
testDataSets(sqlToArrow(conn, table.getQuery(), new RootAllocator(Integer.MAX_VALUE)), false);
Expand All @@ -158,8 +159,11 @@ public void testJdbcToArrowValues(Table table) throws SQLException, IOException
.build()), false);
}

@Test
public void testJdbcSchemaMetadata() throws SQLException {
@ParameterizedTest
@MethodSource("getTestData")
public void testJdbcSchemaMetadata(Table table) throws SQLException, ClassNotFoundException {
this.initializeDatabase(table);

JdbcToArrowConfig config = new JdbcToArrowConfigBuilder(new RootAllocator(0), Calendar.getInstance(), true)
.setArraySubTypeByColumnNameMap(ARRAY_SUB_TYPE_BY_COLUMN_NAME_MAP)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ public static Stream<Arguments> getTestData() throws IOException {
*/
@ParameterizedTest
@MethodSource("getTestData")
public void testJdbcToArrowValues(Table table) throws SQLException, IOException {
public void testJdbcToArrowValues(Table table) throws SQLException, IOException, ClassNotFoundException {
this.initializeDatabase(table);

this.table = getTable("h2/test1_map_h2.yml", JdbcToArrowMapDataTypeTest.class);
Calendar calendar = Calendar.getInstance();
ResultSetMetaData rsmd = getQueryMetaData(table.getQuery());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@
import org.apache.arrow.vector.complex.ListVector;
import org.apache.arrow.vector.complex.MapVector;
import org.apache.arrow.vector.types.pojo.Schema;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
Expand Down Expand Up @@ -114,7 +113,9 @@ public static Stream<Arguments> getTestData() throws SQLException, ClassNotFound
*/
@ParameterizedTest
@MethodSource("getTestData")
public void testJdbcToArrowValues(Table table) throws SQLException, IOException {
public void testJdbcToArrowValues(Table table) throws SQLException, IOException, ClassNotFoundException {
this.initializeDatabase(table);

testDataSets(sqlToArrow(conn, table.getQuery(), new RootAllocator(Integer.MAX_VALUE),
Calendar.getInstance()), false);
testDataSets(sqlToArrow(conn, table.getQuery(), new RootAllocator(Integer.MAX_VALUE)), false);
Expand All @@ -141,8 +142,11 @@ public void testJdbcToArrowValues(Table table) throws SQLException, IOException
.build()), true);
}

@Test
public void testJdbcSchemaMetadata() throws SQLException {
@ParameterizedTest
@MethodSource("getTestData")
public void testJdbcSchemaMetadata(Table table) throws SQLException, ClassNotFoundException {
this.initializeDatabase(table);

JdbcToArrowConfig config = new JdbcToArrowConfigBuilder(new RootAllocator(0), Calendar.getInstance(), true)
.setArraySubTypeByColumnNameMap(ARRAY_SUB_TYPE_BY_COLUMN_NAME_MAP)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ public static Stream<Arguments> getTestData() throws SQLException, ClassNotFound
*/
@ParameterizedTest
@MethodSource("getTestData")
public void testJdbcToArrowValues(Table table) throws SQLException, IOException {
public void testJdbcToArrowValues(Table table) throws SQLException, IOException, ClassNotFoundException {
this.initializeDatabase(table);

testDataSets(sqlToArrow(conn, table.getQuery(), new RootAllocator(Integer.MAX_VALUE)), false);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
import org.apache.arrow.vector.complex.ListVector;
import org.apache.arrow.vector.complex.MapVector;
import org.apache.arrow.vector.types.pojo.Schema;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
Expand All @@ -70,17 +69,6 @@ public class JdbcToArrowTest extends AbstractJdbcToArrowTest {

private static final String[] testFiles = {"h2/test1_all_datatypes_h2.yml"};

/**
* Constructor which populates the table object for each test iteration.
*
* @param table Table object
* @param reuseVectorSchemaRoot A flag indicating if we should reuse vector schema roots.
*/
public JdbcToArrowTest(Table table, boolean reuseVectorSchemaRoot) {
this.table = table;
this.reuseVectorSchemaRoot = reuseVectorSchemaRoot;
}

/**
* Get the test data as a collection of Table objects for each test iteration.
*
Expand All @@ -99,7 +87,9 @@ public static Stream<Arguments> getTestData() throws SQLException, ClassNotFound
*/
@ParameterizedTest
@MethodSource("getTestData")
public void testJdbcToArrowValues(Table table) throws SQLException, IOException {
public void testJdbcToArrowValues(Table table) throws SQLException, IOException, ClassNotFoundException {
this.initializeDatabase(table);

testDataSets(sqlToArrow(conn, table.getQuery(), new RootAllocator(Integer.MAX_VALUE),
Calendar.getInstance()), false);
testDataSets(sqlToArrow(conn, table.getQuery(), new RootAllocator(Integer.MAX_VALUE)), false);
Expand Down Expand Up @@ -127,8 +117,11 @@ public void testJdbcToArrowValues(Table table) throws SQLException, IOException
.build()), true);
}

@Test
public void testJdbcSchemaMetadata() throws SQLException {
@ParameterizedTest
@MethodSource("getTestData")
public void testJdbcSchemaMetadata(Table table) throws SQLException, ClassNotFoundException {
this.initializeDatabase(table);

Calendar calendar = Calendar.getInstance();
ResultSetMetaData rsmd = getQueryMetaData(table.getQuery());
JdbcToArrowConfig config = new JdbcToArrowConfigBuilder(new RootAllocator(0), calendar, true)
Expand Down Expand Up @@ -212,8 +205,11 @@ public void testDataSets(VectorSchemaRoot root, boolean isIncludeMapVector) {
}
}

@Test
public void runLargeNumberOfRows() throws IOException, SQLException {
@ParameterizedTest
@MethodSource("getTestData")
public void runLargeNumberOfRows(Table table) throws IOException, SQLException, ClassNotFoundException {
this.initializeDatabase(table);

BufferAllocator allocator = new RootAllocator(Integer.MAX_VALUE);
int x = 0;
final int targetRows = 600000;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
import org.apache.arrow.vector.TimeStampVector;
import org.apache.arrow.vector.VectorSchemaRoot;
import org.apache.arrow.vector.types.pojo.Schema;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
Expand Down Expand Up @@ -88,8 +87,9 @@ public static Stream<Arguments> getTestData() throws SQLException, ClassNotFound
*/
@ParameterizedTest
@MethodSource("getTestData")
public void testJdbcToArrowValues(Table table) throws SQLException, IOException {
this.table = table;
public void testJdbcToArrowValues(Table table) throws SQLException, IOException, ClassNotFoundException {
this.initializeDatabase(table);

testDataSets(sqlToArrow(conn, table.getQuery(), new RootAllocator(Integer.MAX_VALUE),
Calendar.getInstance(TimeZone.getTimeZone(table.getTimezone()))), false);
testDataSets(sqlToArrow(conn.createStatement().executeQuery(table.getQuery()),
Expand All @@ -109,8 +109,11 @@ public void testJdbcToArrowValues(Table table) throws SQLException, IOException
Calendar.getInstance(TimeZone.getTimeZone(table.getTimezone()))).build()), false);
}

@Test
public void testJdbcSchemaMetadata() throws SQLException {
@ParameterizedTest
@MethodSource("getTestData")
public void testJdbcSchemaMetadata(Table table) throws SQLException, ClassNotFoundException {
this.initializeDatabase(table);

Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone(table.getTimezone()));
JdbcToArrowConfig config = new JdbcToArrowConfigBuilder(new RootAllocator(0), calendar, true).build();
ResultSetMetaData rsmd = conn.createStatement().executeQuery(table.getQuery()).getMetaData();
Expand Down
Loading

0 comments on commit 19aa9fd

Please sign in to comment.