public void TestGroupByOnExistingValues() { // Create group by var baseTable = TestTableHelper.CreateReactiveTable(); // Add values var row1 = baseTable.AddRow(); baseTable.SetValue(TestTableColumns.StringColumn, row1, "Value1"); var row2 = baseTable.AddRow(); baseTable.SetValue(TestTableColumns.StringColumn, row2, "Value2"); var row3 = baseTable.AddRow(); baseTable.SetValue(TestTableColumns.StringColumn, row3, "Value1"); // Modify grouped columns baseTable.SetValue(TestTableColumns.StringColumn, row2, "Value1"); baseTable.SetValue(TestTableColumns.StringColumn, row3, "Value3"); // Now group it var groupedTable = new AggregatedTable(baseTable); groupedTable.GroupBy <string>(TestTableColumns.StringColumn); var countCol = "CountCol"; groupedTable.AddAggregate((IReactiveColumn <string>)baseTable.GetColumnByName(TestTableColumns.StringColumn), countCol, () => new Count <string>()); var rowUpdates = new RowUpdateHandler(); groupedTable.Subscribe(rowUpdates); var colUpdates = new ColumnUpdateHandler(); groupedTable.Subscribe(colUpdates.OnColumnUpdate); groupedTable.FinishInitialisation(); Assert.AreEqual(2, groupedTable.RowCount); Assert.AreEqual(2, rowUpdates.CurrentRowCount); Assert.AreEqual("Value1", groupedTable.GetValue <string>(TestTableColumns.StringColumn, 0)); Assert.AreEqual("Value3", groupedTable.GetValue <string>(TestTableColumns.StringColumn, 1)); Assert.AreEqual(2, groupedTable.GetValue <int>(countCol, 0)); Assert.AreEqual(1, groupedTable.GetValue <int>(countCol, 1)); // Remove rows baseTable.DeleteRow(row1); Assert.AreEqual(2, groupedTable.RowCount); Assert.AreEqual(2, rowUpdates.CurrentRowCount); baseTable.DeleteRow(row2); Assert.AreEqual(1, groupedTable.RowCount); Assert.AreEqual(1, rowUpdates.CurrentRowCount); baseTable.DeleteRow(row3); Assert.AreEqual(0, groupedTable.RowCount); Assert.AreEqual(0, rowUpdates.CurrentRowCount); }
public void TestWithCalculatedColumns() { var baseTable = TestTableHelper.CreateReactiveTable(); var groupedTable = new AggregatedTable(baseTable); var column = groupedTable.GroupBy <string>(TestTableColumns.StringColumn); var rowUpdates = new RowUpdateHandler(); groupedTable.Subscribe(rowUpdates); var colUpdates = new ColumnUpdateHandler(); groupedTable.Subscribe(colUpdates.OnColumnUpdate); var groupedColumn = (IReactiveColumn <string>)column; const string groupedCalc1 = "Grouped.Calc1"; groupedTable.AddColumn(new ReactiveCalculatedColumn1 <string, string>(groupedCalc1, groupedColumn, s => s + "Calc")); var row1 = baseTable.AddRow(); baseTable.SetValue(TestTableColumns.StringColumn, row1, "Value1"); Assert.AreEqual(1, groupedTable.RowCount); Assert.AreEqual(1, rowUpdates.CurrentRowCount); Assert.AreEqual("Value1", groupedTable.GetValue <string>(TestTableColumns.StringColumn, colUpdates.LastRowUpdated)); Assert.AreEqual("Value1Calc", groupedTable.GetValue <string>(groupedCalc1, colUpdates.LastRowUpdated)); var row2 = baseTable.AddRow(); baseTable.SetValue(TestTableColumns.StringColumn, row2, "Value3"); Assert.AreEqual(2, groupedTable.RowCount); Assert.AreEqual(2, rowUpdates.CurrentRowCount); Assert.AreEqual("Value3", groupedTable.GetValue <string>(TestTableColumns.StringColumn, colUpdates.LastRowUpdated)); Assert.AreEqual("Value3Calc", groupedTable.GetValue <string>(groupedCalc1, colUpdates.LastRowUpdated)); var row3 = baseTable.AddRow(); baseTable.SetValue(TestTableColumns.StringColumn, row3, "Value3"); Assert.AreEqual(2, groupedTable.RowCount); Assert.AreEqual(2, rowUpdates.CurrentRowCount); Assert.AreEqual("Value3", groupedTable.GetValue <string>(TestTableColumns.StringColumn, colUpdates.LastRowUpdated)); Assert.AreEqual("Value3Calc", groupedTable.GetValue <string>(groupedCalc1, colUpdates.LastRowUpdated)); baseTable.SetValue(TestTableColumns.StringColumn, row1, "Value2"); Assert.AreEqual(2, groupedTable.RowCount); Assert.AreEqual(2, rowUpdates.CurrentRowCount); Assert.AreEqual("Value2", groupedTable.GetValue <string>(TestTableColumns.StringColumn, colUpdates.LastRowUpdated)); Assert.AreEqual("Value2Calc", groupedTable.GetValue <string>(groupedCalc1, colUpdates.LastRowUpdated)); }
public void TestRowUpdates() { // Create group by var baseTable = TestTableHelper.CreateReactiveTable(); var groupedTable = new AggregatedTable(baseTable); var rowUpdates = new RowUpdateHandler(); groupedTable.Subscribe(rowUpdates); var colUpdates = new ColumnUpdateHandler(); groupedTable.Subscribe(colUpdates.OnColumnUpdate); groupedTable.GroupBy <string>(TestTableColumns.StringColumn); var countColumn = "Aggregate.Count"; groupedTable.AddAggregate((IReactiveColumn <string>)baseTable.GetColumnByName(TestTableColumns.StringColumn), countColumn, () => new Count <string>()); var row1 = baseTable.AddRow(); baseTable.SetValue(TestTableColumns.StringColumn, row1, "Value1"); Assert.AreEqual(1, groupedTable.RowCount); Assert.AreEqual(1, groupedTable.GetValue <int>(countColumn, 0)); var row2 = baseTable.AddRow(); baseTable.SetValue(TestTableColumns.StringColumn, row2, "Value1"); Assert.AreEqual(1, groupedTable.RowCount); Assert.AreEqual(2, groupedTable.GetValue <int>(countColumn, 0)); var row3 = baseTable.AddRow(); baseTable.SetValue(TestTableColumns.StringColumn, row3, "Value2"); Assert.AreEqual(2, groupedTable.RowCount); Assert.AreEqual(2, groupedTable.GetValue <int>(countColumn, 0)); Assert.AreEqual(1, groupedTable.GetValue <int>(countColumn, 1)); baseTable.SetValue(TestTableColumns.StringColumn, row2, "Value2"); Assert.AreEqual(2, groupedTable.RowCount); Assert.AreEqual(1, groupedTable.GetValue <int>(countColumn, 0)); Assert.AreEqual(2, groupedTable.GetValue <int>(countColumn, 1)); Assert.AreEqual(2, rowUpdates.CurrentRowCount); CheckRowUpdate(rowUpdates, 0, 0, TableUpdateAction.Add); CheckRowUpdate(rowUpdates, 1, 1, TableUpdateAction.Add); CheckRowUpdate(rowUpdates, 1, 2, TableUpdateAction.Delete); CheckRowUpdate(rowUpdates, 1, 3, TableUpdateAction.Add); }
public void TestGroupByWithCount() { // Create group by var baseTable = TestTableHelper.CreateReactiveTable(); var groupedTable = new AggregatedTable(baseTable); var rowUpdates = new RowUpdateHandler(); groupedTable.Subscribe(rowUpdates); var colUpdates = new ColumnUpdateHandler(); groupedTable.Subscribe(colUpdates.OnColumnUpdate); groupedTable.GroupBy <string>(TestTableColumns.StringColumn); var countColumn = "Aggregate.Count"; groupedTable.AddAggregate((IReactiveColumn <string>)baseTable.GetColumnByName(TestTableColumns.StringColumn), countColumn, () => new Count <string>()); var row1 = baseTable.AddRow(); baseTable.SetValue(TestTableColumns.StringColumn, row1, "Value1"); Assert.AreEqual(1, groupedTable.RowCount); Assert.AreEqual(1, groupedTable.GetValue <int>(countColumn, 0)); var row2 = baseTable.AddRow(); baseTable.SetValue(TestTableColumns.StringColumn, row2, "Value1"); Assert.AreEqual(1, groupedTable.RowCount); Assert.AreEqual(2, groupedTable.GetValue <int>(countColumn, 0)); var row3 = baseTable.AddRow(); baseTable.SetValue(TestTableColumns.StringColumn, row3, "Value2"); Assert.AreEqual(2, groupedTable.RowCount); Assert.AreEqual(2, groupedTable.GetValue <int>(countColumn, 0)); Assert.AreEqual(1, groupedTable.GetValue <int>(countColumn, 1)); baseTable.SetValue(TestTableColumns.StringColumn, row2, "Value2"); Assert.AreEqual(2, groupedTable.RowCount); Assert.AreEqual(1, groupedTable.GetValue <int>(countColumn, 0)); Assert.AreEqual(2, groupedTable.GetValue <int>(countColumn, 1)); // Change it to the same value again and check that nothing changes baseTable.SetValue(TestTableColumns.StringColumn, row2, "Value2"); Assert.AreEqual(2, groupedTable.RowCount); Assert.AreEqual(1, groupedTable.GetValue <int>(countColumn, 0)); Assert.AreEqual(2, groupedTable.GetValue <int>(countColumn, 1)); }
public void TestGroupByMultipleColumns() { // Create group by var baseTable = TestTableHelper.CreateReactiveTable(); var groupedTable = new AggregatedTable(baseTable); groupedTable.GroupBy <string>(TestTableColumns.StringColumn); groupedTable.GroupBy <int>(TestTableColumns.IdColumn); var rowUpdates = new RowUpdateHandler(); groupedTable.Subscribe(rowUpdates); var colUpdates = new ColumnUpdateHandler(); groupedTable.Subscribe(colUpdates.OnColumnUpdate); // Add values var row1 = baseTable.AddRow(); baseTable.SetValue(TestTableColumns.StringColumn, row1, "Value1"); Assert.AreEqual(1, groupedTable.RowCount); Assert.AreEqual(1, rowUpdates.CurrentRowCount); Assert.AreEqual("Value1", groupedTable.GetValue <string>(TestTableColumns.StringColumn, colUpdates.LastRowUpdated)); baseTable.SetValue(TestTableColumns.IdColumn, row1, 42); Assert.AreEqual(1, groupedTable.RowCount); Assert.AreEqual(1, rowUpdates.CurrentRowCount); Assert.AreEqual(42L, groupedTable.GetValue <int>(TestTableColumns.IdColumn, colUpdates.LastRowUpdated)); baseTable.SetValue(TestTableColumns.StringColumn, row1, "Value2"); Assert.AreEqual("Value2", groupedTable.GetValue <string>(TestTableColumns.StringColumn, colUpdates.LastRowUpdated)); baseTable.SetValue(TestTableColumns.IdColumn, row1, 43); Assert.AreEqual(43, groupedTable.GetValue <int>(TestTableColumns.IdColumn, colUpdates.LastRowUpdated)); }
public void TestGroupByWithAverage() { // Create group by var baseTable = TestTableHelper.CreateReactiveTable(); var groupedTable = new AggregatedTable(baseTable); var rowUpdates = new RowUpdateHandler(); groupedTable.Subscribe(rowUpdates); var colUpdates = new ColumnUpdateHandler(); groupedTable.Subscribe(colUpdates.OnColumnUpdate); groupedTable.GroupBy <string>(TestTableColumns.StringColumn); var avgColumn = "Aggregate.Sum"; groupedTable.AddAggregate((IReactiveColumn <int>)baseTable.GetColumnByName(TestTableColumns.IdColumn), avgColumn, () => new Average <int>()); var avgColumn2 = "Aggregate.DecimalSum"; groupedTable.AddAggregate((IReactiveColumn <decimal>)baseTable.GetColumnByName(TestTableColumns.DecimalColumn), avgColumn2, () => new Average <decimal>()); var row1 = baseTable.AddRow(); baseTable.SetValue(TestTableColumns.StringColumn, row1, "Value1"); baseTable.SetValue(TestTableColumns.IdColumn, row1, 10); baseTable.SetValue(TestTableColumns.DecimalColumn, row1, 2.5m); Assert.AreEqual(1, groupedTable.RowCount); Assert.AreEqual(10.0, groupedTable.GetValue <double>(avgColumn, 0)); Assert.AreEqual(2.5, groupedTable.GetValue <double>(avgColumn2, 0)); var row2 = baseTable.AddRow(); baseTable.SetValue(TestTableColumns.StringColumn, row2, "Value1"); baseTable.SetValue(TestTableColumns.IdColumn, row2, 20); baseTable.SetValue(TestTableColumns.DecimalColumn, row2, 4.5m); Assert.AreEqual(1, groupedTable.RowCount); Assert.AreEqual(15.0, groupedTable.GetValue <double>(avgColumn, 0)); Assert.AreEqual(3.5, groupedTable.GetValue <double>(avgColumn2, 0)); var row3 = baseTable.AddRow(); baseTable.SetValue(TestTableColumns.StringColumn, row3, "Value2"); baseTable.SetValue(TestTableColumns.IdColumn, row3, 15); baseTable.SetValue(TestTableColumns.DecimalColumn, row3, 1.5m); Assert.AreEqual(2, groupedTable.RowCount); Assert.AreEqual(15.0, groupedTable.GetValue <double>(avgColumn, 0)); Assert.AreEqual(15.0, groupedTable.GetValue <double>(avgColumn, 1)); Assert.AreEqual(3.5, groupedTable.GetValue <double>(avgColumn2, 0)); Assert.AreEqual(1.5, groupedTable.GetValue <double>(avgColumn2, 1)); baseTable.SetValue(TestTableColumns.IdColumn, row2, 25); baseTable.SetValue(TestTableColumns.DecimalColumn, row2, 5.5m); Assert.AreEqual(2, groupedTable.RowCount); Assert.AreEqual(17.5, groupedTable.GetValue <double>(avgColumn, 0)); Assert.AreEqual(4.0, groupedTable.GetValue <double>(avgColumn2, 0)); // Now change the membership in the groups baseTable.SetValue(TestTableColumns.StringColumn, row2, "Value2"); Assert.AreEqual(10, groupedTable.GetValue <double>(avgColumn, 0)); Assert.AreEqual(20, groupedTable.GetValue <double>(avgColumn, 1)); Assert.AreEqual(2.5, groupedTable.GetValue <double>(avgColumn2, 0)); Assert.AreEqual(3.5, groupedTable.GetValue <double>(avgColumn2, 1)); }
public void TestGroupByOneColumn() { // Create group by var baseTable = TestTableHelper.CreateReactiveTable(); var groupedTable = new AggregatedTable(baseTable); groupedTable.GroupBy <string>(TestTableColumns.StringColumn); var rowUpdates = new RowUpdateHandler(); groupedTable.Subscribe(rowUpdates); var colUpdates = new ColumnUpdateHandler(); groupedTable.Subscribe(colUpdates.OnColumnUpdate); // Add values var row1 = baseTable.AddRow(); baseTable.SetValue(TestTableColumns.StringColumn, row1, "Value1"); Assert.AreEqual(1, groupedTable.RowCount); Assert.AreEqual(1, rowUpdates.CurrentRowCount); Assert.AreEqual("Value1", groupedTable.GetValue <string>(TestTableColumns.StringColumn, colUpdates.LastRowUpdated)); var row2 = baseTable.AddRow(); baseTable.SetValue(TestTableColumns.StringColumn, row2, "Value2"); Assert.AreEqual(2, groupedTable.RowCount); Assert.AreEqual(2, rowUpdates.CurrentRowCount); Assert.AreEqual("Value2", groupedTable.GetValue <string>(TestTableColumns.StringColumn, colUpdates.LastRowUpdated)); var row3 = baseTable.AddRow(); baseTable.SetValue(TestTableColumns.StringColumn, row3, "Value1"); Assert.AreEqual(2, groupedTable.RowCount); Assert.AreEqual(2, rowUpdates.CurrentRowCount); Assert.AreEqual("Value1", groupedTable.GetValue <string>(TestTableColumns.StringColumn, colUpdates.LastRowUpdated)); // Modify grouped columns baseTable.SetValue(TestTableColumns.StringColumn, row2, "Value1"); Assert.AreEqual(1, groupedTable.RowCount); Assert.AreEqual(1, rowUpdates.CurrentRowCount); Assert.AreEqual("Value1", groupedTable.GetValue <string>(TestTableColumns.StringColumn, colUpdates.LastRowUpdated)); baseTable.SetValue(TestTableColumns.StringColumn, row3, "Value3"); Assert.AreEqual(2, groupedTable.RowCount); Assert.AreEqual(2, rowUpdates.CurrentRowCount); Assert.AreEqual("Value3", groupedTable.GetValue <string>(TestTableColumns.StringColumn, colUpdates.LastRowUpdated)); // Remove rows baseTable.DeleteRow(row1); Assert.AreEqual(2, groupedTable.RowCount); Assert.AreEqual(2, rowUpdates.CurrentRowCount); baseTable.DeleteRow(row2); Assert.AreEqual(1, groupedTable.RowCount); Assert.AreEqual(1, rowUpdates.CurrentRowCount); baseTable.DeleteRow(row3); Assert.AreEqual(0, groupedTable.RowCount); Assert.AreEqual(0, rowUpdates.CurrentRowCount); }