use of org.pentaho.platform.dataaccess.datasource.wizard.models.ModelInfo in project data-access by pentaho.
the class CsvDatasourceServiceImplTest method testHasPermissions.
@Test
public void testHasPermissions() throws Exception {
hasPermissions = true;
final ISystemSettings systemSettings = mock(ISystemSettings.class);
when(systemSettings.getSystemSetting("data-access-override", "false")).thenReturn("false");
PentahoSystem.setSystemSettingsService(systemSettings);
String filename = "anotherStageFile_CsvFile.csv";
File file = createTmpCsvFile(filename);
file.deleteOnExit();
try {
ModelInfo modelInfo = service.stageFile(filename, ",", "\n", true, "utf-8");
CsvFileInfo fileInfo = modelInfo.getFileInfo();
assertEquals("One header row", 1, fileInfo.getHeaderRows());
assertEquals("Header + content row", 2, fileInfo.getContents().size());
assertEquals(filename, fileInfo.getTmpFilename());
final FileInfo[] stagedFiles = service.getStagedFiles();
assertNotNull(stagedFiles);
boolean present = false;
for (FileInfo info : stagedFiles) {
if (filename.equals(info.getName())) {
present = true;
break;
}
}
assertTrue(present);
final String encoding = service.getEncoding(filename);
assertNotNull(encoding);
final List<String> previewRows = service.getPreviewRows(filename, true, 1, "utf-8");
assertNotNull(previewRows);
assertEquals(1, previewRows.size());
assertEquals("col1,col2", previewRows.get(0));
final DatasourceDTO datasourceDto = mock(DatasourceDTO.class);
when(datasourceDto.getCsvModelInfo()).thenReturn(modelInfo);
try {
final FileTransformStats fileTransformStats = service.generateDomain(datasourceDto);
} catch (Exception e) {
// Testing this logic is not a purpose of this junit
}
// Passed permissions check
verify(datasourceDto, times(1)).getCsvModelInfo();
} finally {
file.delete();
}
}
use of org.pentaho.platform.dataaccess.datasource.wizard.models.ModelInfo in project data-access by pentaho.
the class CsvTransformGeneratorIT method testDropNonExistingTable.
/**
* Given a name of a non-existing table to drop.
* <br/>
* When StagingTransformGenerator is called to drop this table,
* then it shouldn't execute drop statement.
*/
public void testDropNonExistingTable() throws Exception {
IPentahoSession session = new StandaloneSession("test");
KettleSystemListener.environmentInit(session);
DatabaseMeta dbMeta = getDatabaseMeta();
ModelInfo info = createModel();
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
}
// now make sure we do not execute drop statement for non-existing table
try {
gen.dropTable(tableName);
} catch (CsvTransformGeneratorException e) {
// no need to forward exception, just fail the test
fail();
}
}
use of org.pentaho.platform.dataaccess.datasource.wizard.models.ModelInfo in project data-access by pentaho.
the class CsvTransformGeneratorIT method testSemiColonAfterQuoteIsFound.
public void testSemiColonAfterQuoteIsFound() throws Exception {
IPentahoSession session = new StandaloneSession("test");
KettleSystemListener.environmentInit(session);
DatabaseMeta realDbMeta = getDatabaseMeta();
ModelInfo info = createModel();
final DatabaseMeta dbMeta = Mockito.mock(DatabaseMeta.class);
final Database db = Mockito.mock(Database.class);
CsvTransformGenerator gen = new CsvTransformGenerator(info, realDbMeta) {
@Override
Database getDatabase(final DatabaseMeta databaseMeta) {
assertSame(dbMeta, databaseMeta);
return db;
}
};
gen.execSqlStatement("UPDATE \"csv_test4\" SET \"YEAR_ID_KTL\"=\"YEAR_ID\";\nALTER TABLE \"csv_test4\" DROP ( \"YEAR_ID\" )", dbMeta, new StringBuilder());
Mockito.verify(db).execStatement("UPDATE \"csv_test4\" SET \"YEAR_ID_KTL\"=\"YEAR_ID\"");
Mockito.verify(db).execStatement("ALTER TABLE \"csv_test4\" DROP ( \"YEAR_ID\" )");
// again but with single quotes
gen.execSqlStatement("UPDATE \"csv_test4\" SET \"YEAR_ID_KTL\"=\'YEAR_ID\';\nALTER TABLE \"csv_test4\" DROP ( \'YEAR_ID\' )", dbMeta, new StringBuilder());
Mockito.verify(db).execStatement("UPDATE \"csv_test4\" SET \"YEAR_ID_KTL\"=\'YEAR_ID\'");
Mockito.verify(db).execStatement("ALTER TABLE \"csv_test4\" DROP ( \'YEAR_ID\' )");
}
use of org.pentaho.platform.dataaccess.datasource.wizard.models.ModelInfo in project data-access by pentaho.
the class CsvTransformGeneratorIT method testLoadTableTruncate.
public void testLoadTableTruncate() throws Exception {
IPentahoSession session = new StandaloneSession("test");
KettleSystemListener.environmentInit(session);
// create the model
ModelInfo info = createModel();
CsvTransformGenerator gen = new CsvTransformGenerator(info, getDatabaseMeta());
String tableName = info.getStageTableName();
try {
gen.execSqlStatement(getDropTableStatement(tableName), getDatabaseMeta(), null);
} catch (CsvTransformGeneratorException e) {
// table might not be there yet, it is OK
}
// generate the database table
gen.createOrModifyTable(session);
// load the table
loadTable(gen, info, true, session);
// check the results
long rowCount = this.getRowCount(tableName);
assertEquals((long) 235, rowCount);
// load again, no truncate
loadTable(gen, info, false, session);
// check the results
rowCount = this.getRowCount(tableName);
assertEquals((long) 470, rowCount);
// load again, with truncate
loadTable(gen, info, true, session);
// check the results
rowCount = this.getRowCount(tableName);
assertEquals((long) 235, rowCount);
}
use of org.pentaho.platform.dataaccess.datasource.wizard.models.ModelInfo in project data-access by pentaho.
the class CsvTransformGeneratorIT method testModifyEmptyTable_RemoveColumn.
public void testModifyEmptyTable_RemoveColumn() throws Exception {
CsvTransformGenerator gen = getCleanTransformGen();
IPentahoSession session = new StandaloneSession("test");
// create the model
ModelInfo info = gen.getModelInfo();
// generate the database table initially
gen.createOrModifyTable(session);
String removedColumn = info.getColumns()[info.getColumns().length - 1].getId();
// now, lets update it by changing the model info slightly.. add a column
removeColumnFromModel(info);
gen.createOrModifyTable(session);
// make sure the table has an extra integer column in it
String tableName = info.getStageTableName();
String sql = "select " + removedColumn + " from " + tableName + ";";
try {
gen.execSqlStatement(sql, getDatabaseMeta(), null);
fail("Column should have been removed and an error raised");
} catch (CsvTransformGeneratorException e) {
// expected, the column should not be there to select
}
}
Aggregations