/**
* Test applyQuery method.
*/
public void testApplyQuery() throws InvalidQueryException, DataSourceException,
TypeMismatchException {
DataTable data = createData();
// Test select.
DataTable result = DataSourceHelper.applyQuery(DataSourceHelper.parseQuery("select population"),
data, ULocale.US);
assertEquals(1, result.getNumberOfColumns());
assertEquals(4, result.getNumberOfRows());
assertEquals(new NumberValue(300), result.getRow(1).getCell(0).getValue());
data = createData();
// Test where.
result = DataSourceHelper.applyQuery(DataSourceHelper.parseQuery(
"select name,vegeterian where population > 100"), data, ULocale.US);
assertEquals(2, result.getNumberOfColumns());
assertEquals(1, result.getNumberOfRows());
assertEquals(new TextValue("Sloth"), result.getRow(0).getCell(0).getValue());
assertEquals(BooleanValue.TRUE, result.getRow(0).getCell(1).getValue());
data = createData();
// Test group by.
result = DataSourceHelper.applyQuery(DataSourceHelper.parseQuery(
"select vegeterian,sum(population) group by vegeterian"), data, ULocale.US);
assertEquals(2, result.getNumberOfColumns());
assertEquals(2, result.getNumberOfRows());
assertEquals(BooleanValue.FALSE, result.getRow(0).getCell(0).getValue());
assertEquals(new NumberValue(130), result.getRow(0).getCell(1).getValue());
data = createData();
// Test pivot.
result = DataSourceHelper.applyQuery(DataSourceHelper.parseQuery(
"select sum(population) pivot vegeterian"), data, ULocale.US);
assertEquals(2, result.getNumberOfColumns());
assertEquals(1, result.getNumberOfRows());
assertEquals("false", result.getColumnDescription(0).getLabel());
assertEquals(new NumberValue(130), result.getRow(0).getCell(0).getValue());
data = createData();
// Test order by.
result = DataSourceHelper.applyQuery(DataSourceHelper.parseQuery(
"select name order by population"), data, ULocale.US);
assertEquals(1, result.getNumberOfColumns());
assertEquals(4, result.getNumberOfRows());
assertEquals(new TextValue("Leopard"), result.getRow(0).getCell(0).getValue());
assertEquals(new TextValue("Tiger"), result.getRow(1).getCell(0).getValue());
assertEquals(new TextValue("Aye-aye"), result.getRow(2).getCell(0).getValue());
assertEquals(new TextValue("Sloth"), result.getRow(3).getCell(0).getValue());
data = createData();
// Test limit and offset.
result = DataSourceHelper.applyQuery(DataSourceHelper.parseQuery("limit 1 offset 1"), data,
ULocale.US);
assertEquals(4, result.getNumberOfColumns());
assertEquals(1, result.getNumberOfRows());
assertEquals(new TextValue("Sloth"), result.getRow(0).getCell(0).getValue());
data = createData();
// Test label and format.
result = DataSourceHelper.applyQuery(DataSourceHelper.parseQuery(
"label population 'Population size (thousands)' format population \"'$'#'k'\""), data,
ULocale.US);
assertEquals(4, result.getNumberOfColumns());
assertEquals(4, result.getNumberOfRows(), 4);
assertEquals("Population size (thousands)",
result.getColumnDescription("population").getLabel());
int populationIndex = result.getColumnIndex("population");
assertEquals("$100k", result.getRow(0).getCell(populationIndex).getFormattedValue());
// Test that validation can fail
try {
DataSourceHelper.parseQuery("select min(min(a))");
fail();