From d2a6baf1e5c706a70a4e5bf401b4368c88307566 Mon Sep 17 00:00:00 2001 From: PogI Date: Fri, 29 Apr 2016 15:26:22 +0400 Subject: [PATCH] [BACKLOG-6461] more tests --- .../agile/CsvTransformGeneratorIT.java | 107 +++++++++++++++++ .../agile/CsvTransformGeneratorTest.java | 111 +++++++++++++++++- 2 files changed, 217 insertions(+), 1 deletion(-) diff --git a/test-src/org/pentaho/platform/dataaccess/datasource/wizard/service/agile/CsvTransformGeneratorIT.java b/test-src/org/pentaho/platform/dataaccess/datasource/wizard/service/agile/CsvTransformGeneratorIT.java index c59483488f..cac7aa5d22 100644 --- a/test-src/org/pentaho/platform/dataaccess/datasource/wizard/service/agile/CsvTransformGeneratorIT.java +++ b/test-src/org/pentaho/platform/dataaccess/datasource/wizard/service/agile/CsvTransformGeneratorIT.java @@ -214,6 +214,15 @@ public void testGoodTransform() throws Exception { } + public void testInit_DefaultDb() throws Exception { + IPentahoSession session = new StandaloneSession( "test" ); + KettleSystemListener.environmentInit( session ); + + ModelInfo info = createModel(); + CsvTransformGenerator gen = new CsvTransformGenerator( info ); + assertEquals( getDatabaseMeta(), gen.getTargetDatabaseMeta() ); + } + public void testCreateTable() throws Exception { IPentahoSession session = new StandaloneSession( "test" ); @@ -237,6 +246,67 @@ public void testCreateTable() throws Exception { assertEquals( (long) 0, rowCount ); } + public void testCreateTable_noIgnoredColumns() throws Exception { + + IPentahoSession session = new StandaloneSession( "test" ); + KettleSystemListener.environmentInit( session ); + + DatabaseMeta dbMeta = getDatabaseMeta(); + ModelInfo info = createModel(); + ColumnInfo[] columns = info.getColumns(); + columns[ 4 ].setIgnore( false ); + info.setColumns( columns ); + CsvTransformGenerator gen = new CsvTransformGenerator( info, dbMeta ); + + String tableName = info.getStageTableName(); + + try { + gen.execSqlStatement( getDropTableStatement( tableName ), dbMeta, null ); + } catch ( CsvTransformGeneratorException e ) { + // it is OK if the table doesn't exist previously + } + gen.createOrModifyTable( session ); + + // check the results + long rowCount = this.getRowCount( tableName ); + assertEquals( (long) 0, rowCount ); + + + DatabaseMeta databaseMeta = getDatabaseMeta(); + assertNotNull( databaseMeta ); + Database database = new Database( databaseMeta ); + assertNotNull( database ); + database.connect(); + + Connection connection = null; + Statement stmt = null; + ResultSet sqlResult = null; + + try { + connection = database.getConnection(); + assertNotNull( connection ); + stmt = database.getConnection().createStatement(); + + boolean ok = stmt.execute( "select * from " + tableName ); + assertTrue( ok ); + sqlResult = stmt.getResultSet(); + assertNotNull( sqlResult ); + assertEquals( 1, sqlResult.findColumn( "PC_0" ) ); + assertEquals( 2, sqlResult.findColumn( "PC_1" ) ); + assertEquals( 3, sqlResult.findColumn( "PC_2" ) ); + assertEquals( 4, sqlResult.findColumn( "PC_3" ) ); + assertEquals( 5, sqlResult.findColumn( "PC_4" ) ); + assertEquals( 6, sqlResult.findColumn( "PC_5" ) ); + assertEquals( 7, sqlResult.findColumn( "PC_6" ) ); + assertEquals( 8, sqlResult.findColumn( "PC_7" ) ); + assertEquals( 9, sqlResult.findColumn( "PC_8" ) ); + } finally { + sqlResult.close(); + stmt.close(); + connection.close(); + } + } + public void testCreateTable_longColumnNames() throws Exception { IPentahoSession session = new StandaloneSession( "test" ); @@ -264,6 +334,43 @@ public void testCreateTable_longColumnNames() throws Exception { assertEquals( 0, gen.getTransformStats().getErrorCount() ); } + public void testCreateTable_CannotCreateCutLongNamesStep() throws Exception { + + IPentahoSession session = new StandaloneSession( "test" ); + KettleSystemListener.environmentInit( session ); + + DatabaseMeta dbMeta = getDatabaseMeta(); + ModelInfo info = createModel(); + CsvTransformGenerator gen = spy( new CsvTransformGenerator( info, dbMeta ) ); + final int maxColumnNameLength = 3; + doReturn( maxColumnNameLength ).when( gen ).getMaxColumnNameLength(); + + String tableName = info.getStageTableName(); + + try { + gen.execSqlStatement( getDropTableStatement( tableName ), dbMeta, null ); + } catch ( CsvTransformGeneratorException e ) { + // it is OK if the table doesn't exist previously + } + try { + this.getRowCount( tableName ); + fail( "Test table must not exist now" ); + } catch ( SQLException e ) { + // ok + } + + doThrow( new RuntimeException() ).when( gen ).createCutLongNamesStep( any( RowMetaInterface.class ), eq( maxColumnNameLength ), anyString() ); + // This error is not critical. It's supposed to be skipped. + + gen.createOrModifyTable( session ); + verify( gen, atLeastOnce() ).createCutLongNamesStep( any( TransMeta.class ), any( List.class ), eq( maxColumnNameLength ), anyString() ); + + // check the results. + long rowCount = this.getRowCount( tableName ); + assertEquals( (long) 0, rowCount ); + assertEquals( 0, gen.getTransformStats().getErrorCount() ); + } + public void testCreateTable_littleMaxColumnNameLength() throws Exception { IPentahoSession session = new StandaloneSession( "test" ); diff --git a/test-src/org/pentaho/platform/dataaccess/datasource/wizard/service/agile/CsvTransformGeneratorTest.java b/test-src/org/pentaho/platform/dataaccess/datasource/wizard/service/agile/CsvTransformGeneratorTest.java index 1486c25760..aa5e23f415 100644 --- a/test-src/org/pentaho/platform/dataaccess/datasource/wizard/service/agile/CsvTransformGeneratorTest.java +++ b/test-src/org/pentaho/platform/dataaccess/datasource/wizard/service/agile/CsvTransformGeneratorTest.java @@ -16,10 +16,29 @@ */ package org.pentaho.platform.dataaccess.datasource.wizard.service.agile; +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.spy; + +import java.sql.DatabaseMetaData; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; import org.junit.Assert; import org.junit.Test; +import org.pentaho.di.core.database.Database; +import org.pentaho.di.core.database.DatabaseMeta; +import org.pentaho.di.core.exception.KettleDatabaseException; +import org.pentaho.di.core.row.RowMetaInterface; +import org.pentaho.di.core.row.ValueMetaInterface; import org.pentaho.di.trans.step.StepMeta; import org.pentaho.di.trans.step.StepMetaInterface; import org.pentaho.di.trans.steps.selectvalues.SelectValuesMeta; @@ -28,6 +47,65 @@ public class CsvTransformGeneratorTest { + @Test + public void testGetMaxColumnNameLength() throws Exception { + + CsvTransformGenerator ctgNoDatabase10 = spy( new CsvTransformGenerator( new ModelInfo(), null ) ); + Database db10 = mock( Database.class ); + doReturn( db10 ).when( ctgNoDatabase10 ).getDatabase( any( DatabaseMeta.class ) ); + DatabaseMetaData dbmd10 = mock( DatabaseMetaData.class ); + doReturn( dbmd10 ).when( db10 ).getDatabaseMetaData(); + doReturn( 10 ).when( dbmd10 ).getMaxColumnNameLength(); + assertEquals( 10, ctgNoDatabase10.getMaxColumnNameLength() ); + + CsvTransformGenerator ctgNoDatabase0 = spy( new CsvTransformGenerator( new ModelInfo(), null ) ); + Database db0 = mock( Database.class ); + doReturn( db0 ).when( ctgNoDatabase0 ).getDatabase( any( DatabaseMeta.class ) ); + DatabaseMetaData dbmd0 = mock( DatabaseMetaData.class ); + doReturn( dbmd0 ).when( db0 ).getDatabaseMetaData(); + doReturn( 0 ).when( dbmd0 ).getMaxColumnNameLength(); + assertEquals( 0, ctgNoDatabase0.getMaxColumnNameLength() ); + + CsvTransformGenerator ctgDatabase1 = spy( new CsvTransformGenerator( new ModelInfo(), null ) ); + Database db1 = mock( Database.class ); + DatabaseMetaData dbmd1 = mock( DatabaseMetaData.class ); + doReturn( dbmd1 ).when( db1 ).getDatabaseMetaData(); + doReturn( -1 ).when( dbmd1 ).getMaxColumnNameLength(); + assertEquals( 0, ctgDatabase1.getMaxColumnNameLength() ); + + CsvTransformGenerator ctgDbmdE1 = spy( new CsvTransformGenerator( new ModelInfo(), null ) ); + Database dbDbmdE1 = mock( Database.class ); + DatabaseMetaData dbmdE1 = mock( DatabaseMetaData.class ); + doReturn( dbmdE1 ).when( dbDbmdE1 ).getDatabaseMetaData(); + doThrow( new SQLException() ).when( dbmdE1 ).getMaxColumnNameLength(); + assertEquals( 0, ctgDbmdE1.getMaxColumnNameLength() ); + + CsvTransformGenerator ctgDbmdE2 = spy( new CsvTransformGenerator( new ModelInfo(), null ) ); + Database dbDbmdE2 = mock( Database.class ); + DatabaseMetaData dbmdE2 = mock( DatabaseMetaData.class ); + doReturn( dbmdE2 ).when( dbDbmdE2 ).getDatabaseMetaData(); + doThrow( new RuntimeException() ).when( dbmdE2 ).getMaxColumnNameLength(); + assertEquals( 0, ctgDbmdE2.getMaxColumnNameLength() ); + + CsvTransformGenerator ctgDbmdE3 = spy( new CsvTransformGenerator( new ModelInfo(), null ) ); + Database dbDbmdE3 = mock( Database.class ); + doThrow( new KettleDatabaseException() ).when( dbDbmdE3 ).getDatabaseMetaData(); + assertEquals( 0, ctgDbmdE3.getMaxColumnNameLength() ); + + CsvTransformGenerator ctgDbmdE4 = spy( new CsvTransformGenerator( new ModelInfo(), null ) ); + Database dbDbmdE4 = mock( Database.class ); + doThrow( new RuntimeException() ).when( dbDbmdE4 ).getDatabaseMetaData(); + assertEquals( 0, ctgDbmdE4.getMaxColumnNameLength() ); + + CsvTransformGenerator ctgNoDatabase = spy( new CsvTransformGenerator( new ModelInfo(), null ) ); + doReturn( null ).when( ctgNoDatabase ).getDatabase( any( DatabaseMeta.class ) ); + assertEquals( 0, ctgNoDatabase.getMaxColumnNameLength() ); + + CsvTransformGenerator ctgDatabaseErr = spy( new CsvTransformGenerator( new ModelInfo(), null ) ); + doThrow( new RuntimeException() ).when( ctgDatabaseErr ).getDatabase( any( DatabaseMeta.class ) ); + assertEquals( 0, ctgDatabaseErr.getMaxColumnNameLength() ); + } + @Test public void testCreateCutLongNamesStep_shortColumnNames() throws Exception { CsvTransformGenerator ctg = new CsvTransformGenerator( new ModelInfo(), null ); @@ -38,6 +116,38 @@ public void testCreateCutLongNamesStep_shortColumnNames() throws Exception { Assert.assertNull( "step", step ); } + @Test + /** + * Very artificial case. + * org.pentaho.di.core.row.RowMeta provides unique names, + * though it's not required in org.pentaho.di.core.row.RowMetaInterface + */ + public void testCreateCutLongNamesStep_dupColumnNames() throws Exception { + CsvTransformGenerator ctg = new CsvTransformGenerator( new ModelInfo(), null ); + int maxColumnNameLength = 18; + String stepName = "TEST_STEP_DupLongNames"; + CutLongNamesStepContext testData = new CutLongNamesStepContext(); + RowMetaInterface fields = spy( testData.fields ); + List vmList = new ArrayList( testData.fields.getValueMetaList() ); + vmList.set( 1, vmList.get( 0 ) ); + doReturn( vmList ).when( fields ).getValueMetaList(); + + String[] fieldNamesDups = new String[] {"a", "a", "A_1", "b_1", "LONGlonglong", "longlonglong_again", "a_2", "lonGlo_1"}; + String[] fieldRenames = new String[] {"a", "a_1", "A_1_1", "b_1", "LONGlonglong", "longlonglong_again", "a_2", "lonGlo_1"}; + + StepMeta step = ctg.createCutLongNamesStep( fields, maxColumnNameLength, stepName ); + assertNotNull( "step", step ); + assertEquals( "step name", stepName, step.getName() ); + StepMetaInterface stepMetaIntegrface = step.getStepMetaInterface(); + assertNotNull( "stepMetaIntegrface", stepMetaIntegrface ); + assertTrue( "stepMetaIntegrface instanceof SelectValuesMeta", stepMetaIntegrface instanceof SelectValuesMeta ); + SelectValuesMeta svm = (SelectValuesMeta) stepMetaIntegrface; + String[] selectName = svm.getSelectName(); + assertArrayEquals( "selectName", fieldNamesDups, selectName ); + String[] selectRename = svm.getSelectRename(); + assertArrayEquals( "selectName", fieldRenames, selectRename ); + } + @Test public void testCreateCutLongNamesStep_longColumnNames() throws Exception { CsvTransformGenerator ctg = new CsvTransformGenerator( new ModelInfo(), null ); @@ -74,5 +184,4 @@ public void testCreateCutLongNamesStep_littleMaxColumnNameLength() throws Except } } - }