Skip to content

Commit

Permalink
Add coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
sualeh committed Aug 17, 2024
1 parent 799cb33 commit cb88b99
Showing 1 changed file with 30 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@ public class PostgreSQLEnumDataTypeHelperTest {
@Test
public void testGetEnumDataTypeInfo() throws Exception {
final Column column = mock(Column.class);
final ColumnDataType columnDataType = mock(ColumnDataType.class);
final ColumnDataType columnDataType = mockColumnDataType("enum_type", true);
final Connection connection = mock(Connection.class);

when(columnDataType.getName()).thenReturn("enum_type");
when(connection.createStatement()).thenReturn(mock(java.sql.Statement.class));
when(connection.createStatement().executeQuery(anyString()))
.thenReturn(mock(java.sql.ResultSet.class));
Expand All @@ -46,32 +45,43 @@ public void testGetEnumDataTypeInfo() throws Exception {

@Test
public void testGetEnumValues() throws Exception {
final String columnDataTypeName = "enum_type";
final Column column = mock(Column.class);
final ColumnDataType columnDataType = mock(ColumnDataType.class);
final ColumnDataType columnDataType = mockColumnDataType("enum_type", true);
final Connection connection = mock(Connection.class);
final Statement mockStatement = mock(java.sql.Statement.class);
final ResultSet mockResultSet =
TestUtility.createMockResultSet(
new String[] {"TYPE_CATALOG", "TYPE_SCHEMA", "TYPE_NAME", "ENUM_LABEL"},
new Object[][] {
{null, "", columnDataTypeName, "Moe"},
{null, "", columnDataTypeName, "Larry"},
{null, "", columnDataTypeName, "Curly"}
{null, "", columnDataType.getName(), "Moe"},
{null, "", columnDataType.getName(), "Larry"},
{null, "", columnDataType.getName(), "Curly"}
});

when(columnDataType.getName()).thenReturn(columnDataTypeName);
when(connection.createStatement()).thenReturn(mockStatement);
when(mockStatement.execute(anyString())).thenReturn(true);
when(mockStatement.getResultSet()).thenReturn(mockResultSet);

final PostgreSQLEnumDataTypeHelper helper = new PostgreSQLEnumDataTypeHelper();
final EnumDataTypeInfo enumDataTypeInfo =
helper.getEnumDataTypeInfo(column, columnDataType, connection);

EnumDataTypeInfo enumDataTypeInfo;
List<String> enumValues;
// Test column data type first time
enumDataTypeInfo = helper.getEnumDataTypeInfo(column, columnDataType, connection);

assertThat(enumDataTypeInfo.getType(), is(enumerated_data_type));

final List<String> enumValues = enumDataTypeInfo.getEnumValues();
enumValues = enumDataTypeInfo.getEnumValues();

assertThat(enumValues.size(), is(3));
assertThat(enumValues, containsInAnyOrder("Moe", "Larry", "Curly"));

// Test column data type second time
enumDataTypeInfo = helper.getEnumDataTypeInfo(column, columnDataType, connection);

assertThat(enumDataTypeInfo.getType(), is(enumerated_data_type));

enumDataTypeInfo.getEnumValues();

assertThat(enumValues.size(), is(3));
assertThat(enumValues, containsInAnyOrder("Moe", "Larry", "Curly"));
Expand All @@ -80,10 +90,9 @@ public void testGetEnumValues() throws Exception {
@Test
public void testSQLException() throws Exception {
final Column column = mock(Column.class);
final ColumnDataType columnDataType = mock(ColumnDataType.class);
final ColumnDataType columnDataType = mockColumnDataType("enum_type", true);
final Connection connection = mock(Connection.class);

when(columnDataType.getName()).thenReturn("enum_type");
when(connection.createStatement()).thenThrow(SQLException.class);

final PostgreSQLEnumDataTypeHelper helper = new PostgreSQLEnumDataTypeHelper();
Expand All @@ -96,4 +105,12 @@ public void testSQLException() throws Exception {

assertThat(enumValues.size(), is(0));
}

private ColumnDataType mockColumnDataType(
final String columnDataTypeName, final boolean isEnumerated) {
final ColumnDataType columnDataType = mock(ColumnDataType.class);
when(columnDataType.getName()).thenReturn(columnDataTypeName);
when(columnDataType.isEnumerated()).thenReturn(isEnumerated);
return columnDataType;
}
}

0 comments on commit cb88b99

Please sign in to comment.