Skip to content

Commit

Permalink
[TEST] - Additional test coverage for Database Metas
Browse files Browse the repository at this point in the history
  • Loading branch information
mbatchelor committed Aug 18, 2016
1 parent 2dc47e5 commit f3cf73f
Show file tree
Hide file tree
Showing 6 changed files with 345 additions and 129 deletions.
105 changes: 54 additions & 51 deletions core/test-src/org/pentaho/di/core/database/AS400DatabaseMetaTest.java
Expand Up @@ -41,28 +41,28 @@

public class AS400DatabaseMetaTest {

AS400DatabaseMeta a4;
AS400DatabaseMeta a4ODBC;
AS400DatabaseMeta nativeMeta;
AS400DatabaseMeta odbcMeta;

@Before
public void setupOnce() throws Exception {
a4 = new AS400DatabaseMeta();
a4.setAccessType( DatabaseMeta.TYPE_ACCESS_NATIVE );
a4ODBC = new AS400DatabaseMeta();
a4ODBC.setAccessType( DatabaseMeta.TYPE_ACCESS_ODBC );
nativeMeta = new AS400DatabaseMeta();
nativeMeta.setAccessType( DatabaseMeta.TYPE_ACCESS_NATIVE );
odbcMeta = new AS400DatabaseMeta();
odbcMeta.setAccessType( DatabaseMeta.TYPE_ACCESS_ODBC );
KettleClientEnvironment.init();
}

@Test
public void testSettings() throws Exception {
int[] aTypes =
new int[] { DatabaseMeta.TYPE_ACCESS_NATIVE, DatabaseMeta.TYPE_ACCESS_ODBC, DatabaseMeta.TYPE_ACCESS_JNDI };
assertArrayEquals( aTypes, a4.getAccessTypeList() );
assertEquals( "sun.jdbc.odbc.JdbcOdbcDriver", a4ODBC.getDriverClass() );
assertEquals( "com.ibm.as400.access.AS400JDBCDriver", a4.getDriverClass() );
assertEquals( 65536, a4.getMaxTextFieldLength() );
assertEquals( "jdbc:odbc:FOO", a4ODBC.getURL( null, null, "FOO" ) );
assertEquals( "jdbc:as400://foo/bar", a4.getURL( "foo", "1500", "bar" ) ); // note - AS400 driver ignores the port
assertArrayEquals( aTypes, nativeMeta.getAccessTypeList() );
assertEquals( "sun.jdbc.odbc.JdbcOdbcDriver", odbcMeta.getDriverClass() );
assertEquals( "com.ibm.as400.access.AS400JDBCDriver", nativeMeta.getDriverClass() );
assertEquals( 65536, nativeMeta.getMaxTextFieldLength() );
assertEquals( "jdbc:odbc:FOO", odbcMeta.getURL( null, null, "FOO" ) );
assertEquals( "jdbc:as400://foo/bar", nativeMeta.getURL( "foo", "1500", "bar" ) ); // note - AS400 driver ignores the port
String[] expectedReservedWords = new String[] {
// http://publib.boulder.ibm.com/infocenter/iseries/v5r4/index.jsp
// This is the list of currently reserved DB2 UDB for iSeries words. Words may be added at any time.
Expand Down Expand Up @@ -100,64 +100,67 @@ public void testSettings() throws Exception {
"UPDATE", "USAGE", "USER", "USING", "VALUE", "VALUES", "VARIABLE", "VARIANT", "VERSION", "VIEW", "VOLATILE",
"WHEN", "WHERE", "WHILE", "WITH", "WITHOUT", "WRITE", "YEAR", "YEARS" };

assertArrayEquals( expectedReservedWords, a4.getReservedWords() );
assertArrayEquals( new String[] { "jt400.jar" }, a4.getUsedLibraries() );
assertFalse( a4.supportsFloatRoundingOnUpdate() );
assertEquals( 32672, a4.getMaxVARCHARLength() );
assertTrue( a4.supportsSequences() );
assertTrue( a4.supportsSequenceNoMaxValueOption() );
assertArrayEquals( expectedReservedWords, nativeMeta.getReservedWords() );
assertArrayEquals( new String[] { "jt400.jar" }, nativeMeta.getUsedLibraries() );
assertFalse( nativeMeta.supportsFloatRoundingOnUpdate() );
assertEquals( 32672, nativeMeta.getMaxVARCHARLength() );
assertTrue( nativeMeta.supportsSequences() );
assertTrue( nativeMeta.supportsSequenceNoMaxValueOption() );

}

@Test
public void testSQLStatements() {
assertEquals( "DELETE FROM FOO", a4.getTruncateTableStatement( "FOO" ) );
assertEquals( "ALTER TABLE FOO ADD BAR VARCHAR(100)", a4.getAddColumnStatement( "FOO", new ValueMetaString( "BAR",
assertEquals( "DELETE FROM FOO", nativeMeta.getTruncateTableStatement( "FOO" ) );
assertEquals( "ALTER TABLE FOO ADD BAR VARCHAR(100)", nativeMeta.getAddColumnStatement( "FOO", new ValueMetaString( "BAR",
100, 0 ), "", false, "", false ) );

assertEquals( "ALTER TABLE FOO ALTER COLUMN BAR SET TIMESTAMP", a4.getModifyColumnStatement( "FOO",
assertEquals( "ALTER TABLE FOO ALTER COLUMN BAR SET TIMESTAMP", nativeMeta.getModifyColumnStatement( "FOO",
new ValueMetaTimestamp( "BAR" ), "", false, "", false ) ); // Fixed: http://jira.pentaho.com/browse/PDI-15570

assertEquals( "ALTER TABLE FOO ALTER COLUMN BAR SET TIMESTAMP", a4.getModifyColumnStatement( "FOO",
new ValueMetaDate( "BAR" ), "", false, "", false ) );

assertEquals( "ALTER TABLE FOO ALTER COLUMN BAR SET CHAR(1)", a4.getModifyColumnStatement( "FOO",
new ValueMetaBoolean( "BAR" ), "", false, "", false ) );

assertEquals( "ALTER TABLE FOO ALTER COLUMN BAR SET DOUBLE", a4.getModifyColumnStatement( "FOO",
new ValueMetaNumber( "BAR", 0, 0 ), "", false, "", false ) );
assertEquals( "SELECT SEQNAME FROM SYSCAT.SEQUENCES", nativeMeta.getSQLListOfSequences() );
assertEquals( "SELECT * FROM SYSCAT.SEQUENCES WHERE SEQNAME = 'FOO'", nativeMeta.getSQLSequenceExists( "FOO" ) );
assertEquals( "SELECT PREVIOUS VALUE FOR FOO FROM SYSIBM.SYSDUMMY1", nativeMeta.getSQLCurrentSequenceValue( "FOO" ) );
assertEquals( "SELECT NEXT VALUE FOR FOO FROM SYSIBM.SYSDUMMY1", nativeMeta.getSQLNextSequenceValue( "FOO" ) );
}

assertEquals( "ALTER TABLE FOO ALTER COLUMN BAR SET DOUBLE", a4.getModifyColumnStatement( "FOO",
new ValueMetaInteger( "BAR", 0, 0 ), "", false, "", false ) );
@Test
public void testGetFieldDefinition() {
assertEquals( "FOO TIMESTAMP",
nativeMeta.getFieldDefinition( new ValueMetaDate( "FOO" ), "", "", false, true, false ) );
assertEquals( "TIMESTAMP",
nativeMeta.getFieldDefinition( new ValueMetaTimestamp( "FOO" ), "", "", false, false, false ) );
assertEquals( "CHAR(1)",
nativeMeta.getFieldDefinition( new ValueMetaBoolean( "FOO" ), "", "", false, false, false ) );

assertEquals( "ALTER TABLE FOO ALTER COLUMN BAR SET DOUBLE", a4.getModifyColumnStatement( "FOO",
new ValueMetaBigNumber( "BAR", 0, 0 ), "", false, "", false ) );
assertEquals( "DOUBLE",
nativeMeta.getFieldDefinition( new ValueMetaNumber( "FOO" ), "", "", false, false, false ) );
assertEquals( "DECIMAL(5)",
nativeMeta.getFieldDefinition( new ValueMetaInteger( "FOO", 5, 0 ), "", "", false, false, false ) );
assertEquals( "DECIMAL(5, 3)",
nativeMeta.getFieldDefinition( new ValueMetaNumber( "FOO", 5, 3 ), "", "", false, false, false ) );

assertEquals( "ALTER TABLE FOO ALTER COLUMN BAR SET DECIMAL(10, 15)", a4.getModifyColumnStatement( "FOO",
new ValueMetaNumber( "BAR", 10, 15 ), "", false, "", false ) );
assertEquals( "DECIMAL",
nativeMeta.getFieldDefinition( new ValueMetaBigNumber( "FOO", 0, 3 ), "", "", false, false, false ) ); // This is a bug

assertEquals( "ALTER TABLE FOO ALTER COLUMN BAR SET DECIMAL(10)", a4.getModifyColumnStatement( "FOO",
new ValueMetaBigNumber( "BAR", 10, 0 ), "", false, "", false ) );
assertEquals( "CLOB",
nativeMeta.getFieldDefinition( new ValueMetaString( "FOO", DatabaseMeta.CLOB_LENGTH + 1, 0 ), "", "", false, false, false ) );

assertEquals( "ALTER TABLE FOO ALTER COLUMN BAR SET DOUBLE", a4.getModifyColumnStatement( "FOO",
new ValueMetaInteger( "BAR", 0, 10 ), "", false, "", false ) );
assertEquals( String.format( "VARCHAR(%d)", ( nativeMeta.getMaxVARCHARLength() - 1 ) ),
nativeMeta.getFieldDefinition( new ValueMetaString( "FOO", nativeMeta.getMaxVARCHARLength() - 1, 0 ), "", "", false, false, false ) );

assertEquals( "ALTER TABLE FOO ALTER COLUMN BAR SET DECIMAL(10)", a4.getModifyColumnStatement( "FOO",
new ValueMetaInteger( "BAR", 10, 0 ), "", false, "", false ) );
assertEquals( "CLOB",
nativeMeta.getFieldDefinition( new ValueMetaString( "FOO", nativeMeta.getMaxVARCHARLength() + 1, 0 ), "", "", false, false, false ) );

assertEquals( "ALTER TABLE FOO ALTER COLUMN BAR SET CLOB", a4.getModifyColumnStatement( "FOO", new ValueMetaString(
"BAR", 32673, 0 ), "", false, "", false ) );
assertEquals( "CLOB",
nativeMeta.getFieldDefinition( new ValueMetaString( "FOO", DatabaseMeta.CLOB_LENGTH - 1, 0 ), "", "", false, false, false ) );

assertEquals( "ALTER TABLE FOO ALTER COLUMN BAR SET VARCHAR(32672)", a4.getModifyColumnStatement( "FOO",
new ValueMetaString( "BAR", 32672, 0 ), "", false, "", false ) );
assertEquals( " UNKNOWN",
nativeMeta.getFieldDefinition( new ValueMetaInternetAddress( "FOO" ), "", "", false, false, false ) );

assertEquals( "ALTER TABLE FOO ALTER COLUMN BAR SET UNKNOWN", a4.getModifyColumnStatement( "FOO",
new ValueMetaInternetAddress( "BAR" ), "", false, "", false ) );
assertEquals( " UNKNOWN" + System.getProperty( "line.separator" ),
nativeMeta.getFieldDefinition( new ValueMetaInternetAddress( "FOO" ), "", "", false, false, true ) );

assertEquals( "SELECT SEQNAME FROM SYSCAT.SEQUENCES", a4.getSQLListOfSequences() );
assertEquals( "SELECT * FROM SYSCAT.SEQUENCES WHERE SEQNAME = 'FOO'", a4.getSQLSequenceExists( "FOO" ) );
assertEquals( "SELECT PREVIOUS VALUE FOR FOO FROM SYSIBM.SYSDUMMY1", a4.getSQLCurrentSequenceValue( "FOO" ) );
assertEquals( "SELECT NEXT VALUE FOR FOO FROM SYSIBM.SYSDUMMY1", a4.getSQLNextSequenceValue( "FOO" ) );
}

}
Expand Up @@ -27,14 +27,19 @@
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import java.sql.DatabaseMetaData;
import java.sql.Date;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.pentaho.di.core.KettleClientEnvironment;
import org.pentaho.di.core.row.value.ValueMetaDate;
import org.pentaho.di.core.row.value.ValueMetaString;
Expand Down Expand Up @@ -330,5 +335,40 @@ public void testGettersSetters() {
assertEquals( "FOO", jndiMeta.getPreferredSchemaName() );
}

private int rowCnt = 0;

@Test
public void testCheckIndexExists() throws Exception {
Database db = Mockito.mock( Database.class );
ResultSet rs = Mockito.mock( ResultSet.class );
DatabaseMetaData dmd = Mockito.mock( DatabaseMetaData.class );
DatabaseMeta dm = Mockito.mock( DatabaseMeta.class );
Mockito.when( dm.getQuotedSchemaTableCombination( "", "FOO" ) ).thenReturn( "FOO" );
Mockito.when( rs.next() ).thenAnswer( new Answer<Boolean>() {
public Boolean answer( InvocationOnMock invocation ) throws Throwable {
rowCnt++;
return new Boolean( rowCnt < 3 );
}
} );
Mockito.when( db.getDatabaseMetaData() ).thenReturn( dmd );
Mockito.when( dmd.getIndexInfo( null, null, "FOO", false, true ) ).thenReturn( rs );
Mockito.when( rs.getString( "COLUMN_NAME" ) ).thenAnswer( new Answer<String>() {
@Override
public String answer( InvocationOnMock invocation ) throws Throwable {
if ( rowCnt == 1 ) {
return "ROW1COL2";
} else if ( rowCnt == 2 ) {
return "ROW2COL2";
} else {
return null;
}
}
} );
Mockito.when( db.getDatabaseMeta() ).thenReturn( dm );
assertTrue( odbcMeta.checkIndexExists( db, "", "FOO", new String[] { "ROW1COL2", "ROW2COL2" } ) );
assertFalse( odbcMeta.checkIndexExists( db, "", "FOO", new String[] { "ROW2COL2", "NOTTHERE" } ) );
assertFalse( odbcMeta.checkIndexExists( db, "", "FOO", new String[] { "NOTTHERE", "ROW1COL2" } ) );

}

}
87 changes: 61 additions & 26 deletions core/test-src/org/pentaho/di/core/database/DB2DatabaseMetaTest.java
Expand Up @@ -147,32 +147,6 @@ public void testSQLStatements() {

assertEquals( "ALTER TABLE FOO ADD COLUMN BAR VARCHAR(15)",
nativeMeta.getAddColumnStatement( "FOO", new ValueMetaString( "BAR", 15, 0 ), "", false, "", false ) );
assertEquals( "ALTER TABLE FOO ADD COLUMN BAR TIMESTAMP",
nativeMeta.getAddColumnStatement( "FOO", new ValueMetaDate( "BAR" ), "", false, "", false ) );
assertEquals( "ALTER TABLE FOO ADD COLUMN BAR TIMESTAMP",
nativeMeta.getAddColumnStatement( "FOO", new ValueMetaTimestamp( "BAR" ), "", false, "", false ) );
assertEquals( "ALTER TABLE FOO ADD COLUMN BAR CHARACTER(1)",
nativeMeta.getAddColumnStatement( "FOO", new ValueMetaBoolean( "BAR" ), "", false, "", false ) );
assertEquals( "ALTER TABLE FOO ADD COLUMN BAR FLOAT",
nativeMeta.getAddColumnStatement( "FOO", new ValueMetaNumber( "BAR", 0, 0 ), "", false, "", false ) );
assertEquals( "ALTER TABLE FOO ADD COLUMN BAR INTEGER",
nativeMeta.getAddColumnStatement( "FOO", new ValueMetaInteger( "BAR" ), "", false, "", false ) );
assertEquals( "ALTER TABLE FOO ADD COLUMN BAR DECIMAL(10)",
nativeMeta.getAddColumnStatement( "FOO", new ValueMetaNumber( "BAR", 10, -7 ), "", false, "", false ) );
assertEquals( "ALTER TABLE FOO ADD COLUMN BAR DECIMAL(22, 7)",
nativeMeta.getAddColumnStatement( "FOO", new ValueMetaBigNumber( "BAR", 22, 7 ), "", false, "", false ) );
assertEquals( "ALTER TABLE FOO ADD COLUMN BAR FLOAT",
nativeMeta.getAddColumnStatement( "FOO", new ValueMetaNumber( "BAR", -10, 7 ), "", false, "", false ) );
assertEquals( "ALTER TABLE FOO ADD COLUMN BAR DECIMAL(5, 7)",
nativeMeta.getAddColumnStatement( "FOO", new ValueMetaNumber( "BAR", 5, 7 ), "", false, "", false ) );
assertEquals( "ALTER TABLE FOO ADD COLUMN BAR UNKNOWN",
nativeMeta.getAddColumnStatement( "FOO", new ValueMetaInternetAddress( "BAR" ), "", false, "", false ) );

assertEquals( "ALTER TABLE FOO ADD COLUMN BAR INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 0, INCREMENT BY 1, NOCACHE)",
nativeMeta.getAddColumnStatement( "FOO", new ValueMetaInteger( "BAR" ), "BAR", true, "", false ) );

assertEquals( "ALTER TABLE FOO ADD COLUMN BAR BIGINT NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 0, INCREMENT BY 1, NOCACHE)",
nativeMeta.getAddColumnStatement( "FOO", new ValueMetaNumber( "BAR", 26, 8 ), "BAR", true, "", false ) );

String lineSep = System.getProperty( "line.separator" );

Expand All @@ -193,4 +167,65 @@ public void testSQLStatements() {
assertEquals( "SELECT NEXT VALUE FOR FOO FROM SYSIBM.SYSDUMMY1", nativeMeta.getSQLNextSequenceValue( "FOO" ) );
assertEquals( "insert into FOO(FOOVERSION) values (1)", nativeMeta.getSQLInsertAutoIncUnknownDimensionRow( "FOO", "FOOKEY", "FOOVERSION" ) );
}

@Test
public void testGetFieldDefinition() {
assertEquals( "FOO TIMESTAMP",
nativeMeta.getFieldDefinition( new ValueMetaDate( "FOO" ), "", "", false, true, false ) );
assertEquals( "TIMESTAMP",
nativeMeta.getFieldDefinition( new ValueMetaTimestamp( "FOO" ), "", "", false, false, false ) );
assertEquals( "CHARACTER(1)",
nativeMeta.getFieldDefinition( new ValueMetaBoolean( "FOO" ), "", "", false, false, false ) );

assertEquals( "BIGINT NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 0, INCREMENT BY 1, NOCACHE)",
nativeMeta.getFieldDefinition( new ValueMetaBigNumber( "FOO", 8, 0 ), "FOO", "", true, false, false ) );

assertEquals( "BIGINT NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 0, INCREMENT BY 1, NOCACHE)",
nativeMeta.getFieldDefinition( new ValueMetaNumber( "FOO", 12, 0 ), "FOO", "", true, false, false ) );
assertEquals( "INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 0, INCREMENT BY 1, NOCACHE)",
nativeMeta.getFieldDefinition( new ValueMetaInteger( "FOO", 12, 0 ), "FOO", "", true, false, false ) );

assertEquals( "FLOAT",
nativeMeta.getFieldDefinition( new ValueMetaNumber( "FOO", 0, 0 ), "", "", false, false, false ) );
assertEquals( "DECIMAL(12)",
nativeMeta.getFieldDefinition( new ValueMetaBigNumber( "FOO", 12, 0 ), "", "", false, false, false ) ); // Pretty sure this is a bug - should be an Integer here.
assertEquals( "DECIMAL(12, 4)",
nativeMeta.getFieldDefinition( new ValueMetaBigNumber( "FOO", 12, 4 ), "", "", false, false, false ) );

assertEquals( "INTEGER",
nativeMeta.getFieldDefinition( new ValueMetaInteger( "FOO", 10, 0 ), "", "", false, false, false ) );

int realMaxBeforeCLOB = Math.max( nativeMeta.getMaxVARCHARLength(), DatabaseMeta.CLOB_LENGTH );

int realMinBeforeCLOB = Math.min( nativeMeta.getMaxVARCHARLength(), DatabaseMeta.CLOB_LENGTH );

assertEquals( "CLOB",
nativeMeta.getFieldDefinition( new ValueMetaString( "FOO", realMaxBeforeCLOB + 1, 0 ), "", "", false, false, false ) );

assertEquals( "CLOB",
nativeMeta.getFieldDefinition( new ValueMetaString( "FOO", realMaxBeforeCLOB, 0 ), "", "", false, false, false ) );
assertEquals( String.format( "VARCHAR(%d)", realMinBeforeCLOB - 1 ),
nativeMeta.getFieldDefinition( new ValueMetaString( "FOO", realMinBeforeCLOB - 1, 0 ), "", "", false, false, false ) );
assertEquals( "VARCHAR()",
nativeMeta.getFieldDefinition( new ValueMetaString( "FOO", 0, 0 ), "", "", false, false, false ) ); // Definitely a bug here - VARCHAR() is not valid SQL anywhere . . .

// Binary Stuff . . .
assertEquals( String.format( "BLOB(%d)", realMaxBeforeCLOB + 1 ),
nativeMeta.getFieldDefinition( new ValueMetaBinary( "FOO", realMaxBeforeCLOB + 1, 0 ), "", "", false, false, false ) );

assertEquals( "BLOB",
nativeMeta.getFieldDefinition( new ValueMetaBinary( "FOO", 0, 0 ), "", "", false, false, false ) );
assertEquals( "CHAR(150) FOR BIT DATA",
nativeMeta.getFieldDefinition( new ValueMetaBinary( "FOO", 150, 0 ), "", "", false, false, false ) );



// Then unknown . . .
assertEquals( " UNKNOWN",
nativeMeta.getFieldDefinition( new ValueMetaInternetAddress( "FOO" ), "", "", false, false, false ) );

assertEquals( " UNKNOWN" + System.getProperty( "line.separator" ),
nativeMeta.getFieldDefinition( new ValueMetaInternetAddress( "FOO" ), "", "", false, false, true ) );
}

}

0 comments on commit f3cf73f

Please sign in to comment.