public void testSimpleGroupBy() throws Exception {
Query q = new Query();
q.from(new FromItem(table2).setAlias("r"));
Column roleColumn = table2.getColumnByName(COLUMN_ROLE_ROLE_NAME);
q.select(new SelectItem(roleColumn));
q.groupBy(new GroupByItem(new SelectItem(roleColumn)));
assertEquals("SELECT r.name FROM MetaModelSchema.role r GROUP BY r.name", q.toString());
DataContext dc = getDataContext();
DataSet data = dc.executeQuery(q);
assertEquals(1, data.getSelectItems().length);
assertEquals("r.name", data.getSelectItems()[0].toString());
TableModel tableModel = new DataSetTableModel(data);
assertEquals(3, tableModel.getRowCount());
q.select(new SelectItem(FunctionType.COUNT, "*", "c"));
q.where(new FilterItem(new SelectItem(roleColumn), OperatorType.EQUALS_TO, "founder"));
data = dc.executeQuery(q);
assertEquals(2, data.getSelectItems().length);
assertEquals("r.name", data.getSelectItems()[0].toString());
assertEquals("COUNT(*) AS c", data.getSelectItems()[1].toString());
tableModel = new DataSetTableModel(data);
assertEquals(1, tableModel.getRowCount());
assertEquals("founder", tableModel.getValueAt(0, 0));
assertEquals(2l, tableModel.getValueAt(0, 1));
q.select(new SelectItem(FunctionType.SUM, table2.getColumns()[0]));
assertEquals(
"SELECT r.name, COUNT(*) AS c, SUM(r.contributor_id) FROM MetaModelSchema.role r WHERE r.name = 'founder' GROUP BY r.name",
q.toString());
data = dc.executeQuery(q);
assertEquals(3, data.getSelectItems().length);