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);
        }
Beispiel #2
0
        public void TestFilterFilledTable()
        {
            var rawTable = TestTableHelper.CreateReactiveTable();

            AddRow(rawTable, 1, "Blah1", 123.123m);
            Assert.AreEqual(1, rawTable.RowCount);

            AddRow(rawTable, 2, "Blah2", 123.123m);
            Assert.AreEqual(2, rawTable.RowCount);

            var filteredTable = rawTable.Filter(new TestPredicate(new List <string> {
                TestTableColumns.IdColumn
            }, false));

            var updateHandler = new RowUpdateHandler();

            filteredTable.Subscribe(updateHandler);

            Assert.AreEqual(0, filteredTable.RowCount);
            Assert.AreEqual(0, updateHandler.CurrentRowCount);

            var filteredTable2 = rawTable.Filter(new TestPredicate(new List <string> {
                TestTableColumns.IdColumn
            }, true));

            var updateHandler2 = new RowUpdateHandler();

            filteredTable2.Subscribe(updateHandler2);

            Assert.AreEqual(2, filteredTable2.RowCount);
            // No updates will be fired as existing rows are processed in the constructor
            Assert.AreEqual(0, updateHandler2.CurrentRowCount);
        }
        public void Test2Column()
        {
            var          table = TestTableHelper.CreateReactiveTable();
            const string calculatedColumnId = "CalculatedColumn";

            table.AddColumn(new ReactiveCalculatedColumn2 <string, int, string>(
                                calculatedColumnId,
                                (IReactiveColumn <int>)table.GetColumnByName(TestTableColumns.IdColumn),
                                (IReactiveColumn <string>)table.GetColumnByName(TestTableColumns.StringColumn),
                                (i, s) => "The int " + s + " is " + i));

            var updates = new List <TableUpdate>();

            table.Subscribe(updates.Add);

            var row1 = table.AddRow();

            table.SetValue(TestTableColumns.IdColumn, row1, 42);
            table.SetValue(TestTableColumns.StringColumn, row1, "value");

            Assert.AreEqual("The int value is 42", table.GetValue <string>(calculatedColumnId, row1));
            Assert.AreEqual(5, updates.Count);
            Assert.AreEqual(1, updates.Count(u => u.RowIndex == row1 && u.Action == TableUpdateAction.Add));
            Assert.AreEqual(1, updates.Count(u => u.RowIndex == row1 && u.Action == TableUpdateAction.Update &&
                                             u.Column.ColumnId == TestTableColumns.IdColumn));
            Assert.AreEqual(1, updates.Count(u => u.RowIndex == row1 && u.Action == TableUpdateAction.Update &&
                                             u.Column.ColumnId == TestTableColumns.StringColumn));
            // One after each update
            Assert.AreEqual(2, updates.Count(u => u.RowIndex == row1 && u.Action == TableUpdateAction.Update &&
                                             u.Column.ColumnId == calculatedColumnId));
        }
        public void TestFillingTable()
        {
            var       sourceTable = TestTableHelper.CreateReactiveTable();
            const int count       = 100;

            var table = new SortedTable(sourceTable);

            table.SortBy(TestTableColumns.StringColumn, Comparer <string> .Default);

            AddValuesInReverseOrder(count, sourceTable);
            CheckValuesAreSorted(count, table);
        }
        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 TestColumnsAdded()
        {
            var baseTable    = TestTableHelper.CreateReactiveTable();
            var groupedTable = new AggregatedTable(baseTable);

            groupedTable.GroupBy <string>(TestTableColumns.StringColumn);
            groupedTable.AddAggregate((IReactiveColumn <string>)groupedTable.GetColumnByName(TestTableColumns.StringColumn),
                                      "Test",
                                      () => new Count <string>());

            Assert.AreEqual(2, groupedTable.Columns.Count);
            Assert.AreEqual(TestTableColumns.StringColumn, groupedTable.Columns.First().ColumnId);
            Assert.AreEqual("Test", groupedTable.Columns.Skip(1).First().ColumnId);
        }
        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 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);
        }
Beispiel #9
0
        public void TestSimpleGroupBy()
        {
            var table   = TestTableHelper.CreateReactiveTable();
            var stream  = table;
            var groupBy = stream
//                .Where(update => update.Column.ColumnId == TestTableColumns.StringColumn)
                          .GroupBy(update => table.GetValue <string>(TestTableColumns.StringColumn, update.RowIndex));

            groupBy.Subscribe(
                group =>
            {
                if (group.Key != default(string))
                {
                    // Now we have a valid group and we can raise a new row event
                    Console.WriteLine("New key {0}", group.Key);

                    group.Subscribe(
                        // Here we have a col change and we can raise a col change event
                        update => Console.WriteLine("{0}\tId : {1}", group.Key, table.GetValue <int>(TestTableColumns.IdColumn, update.RowIndex)));

                    // We can do aggregates on the columns here
                    // Need to store the previous value in order to be able to update the sum correctly
                    // ohterwise we would need to start again from the beginning
                    group.Scan(0, (i, update) => i + table.GetValue <int>(TestTableColumns.IdColumn, update.RowIndex))
                    .Subscribe(i => Console.WriteLine("{2}\tSum of {0} : {1}", group.Key, i, group.Key));
                }
            });

            var rowIndex = table.AddRow();

            table.SetValue(TestTableColumns.IdColumn, rowIndex, 1);
            table.SetValue(TestTableColumns.StringColumn, rowIndex, "Name 1");

            var rowIndex2 = table.AddRow();

            table.SetValue(TestTableColumns.IdColumn, rowIndex2, 2);
            table.SetValue(TestTableColumns.StringColumn, rowIndex2, "Name 1");

            var rowIndex3 = table.AddRow();

            table.SetValue(TestTableColumns.IdColumn, rowIndex3, 3);
            table.SetValue(TestTableColumns.StringColumn, rowIndex3, "Name 2");

            table.SetValue(TestTableColumns.StringColumn, rowIndex, "Name 2");

            table.SetValue(TestTableColumns.IdColumn, rowIndex2, 4);
        }
Beispiel #10
0
        public void TestChangingFilterFilledTable()
        {
            var rawTable = TestTableHelper.CreateReactiveTable();

            bool[] visible       = { true };
            var    filteredTable = (FilteredTable)rawTable.Filter(
                new DelegatePredicate1 <string>(TestTableColumns.StringColumn, s => visible[0]));

            var updateHandler = new RowUpdateHandler();

            filteredTable.Subscribe(updateHandler);

            AddRow(rawTable, 1, "Blah1", 123.123m);
            Assert.AreEqual(1, rawTable.RowCount);
            Assert.AreEqual(1, filteredTable.RowCount);
            Assert.AreEqual(1, updateHandler.CurrentRowCount);
            Assert.AreEqual("Blah1", filteredTable.GetValue <string>(TestTableColumns.StringColumn, updateHandler.LastRowUpdated));

            AddRow(rawTable, 2, "Blah2", 123.123m);
            Assert.AreEqual(2, rawTable.RowCount);
            Assert.AreEqual(2, filteredTable.RowCount);
            Assert.AreEqual(2, updateHandler.CurrentRowCount);
            Assert.AreEqual("Blah2", filteredTable.GetValue <string>(TestTableColumns.StringColumn, updateHandler.LastRowUpdated));

            visible[0] = false;
            filteredTable.PredicateChanged();

            Assert.AreEqual(2, rawTable.RowCount);
            Assert.AreEqual(0, filteredTable.RowCount);
            Assert.AreEqual(0, updateHandler.CurrentRowCount);

            visible[0] = true;
            filteredTable.PredicateChanged();

            Assert.AreEqual(2, rawTable.RowCount);
            Assert.AreEqual(2, filteredTable.RowCount);
            Assert.AreEqual(2, updateHandler.CurrentRowCount);

            visible[0] = false;
            filteredTable.PredicateChanged();

            Assert.AreEqual(2, rawTable.RowCount);
            Assert.AreEqual(0, filteredTable.RowCount);
            Assert.AreEqual(0, updateHandler.CurrentRowCount);
        }
Beispiel #11
0
        public void TestExistingRowsBug()
        {
            var rawTable = TestTableHelper.CreateReactiveTable();

            AddRow(rawTable, 1, "Foo1", 1);
            AddRow(rawTable, 2, "Foo2", 2);
            AddRow(rawTable, 3, "Foo3", 3);

            decimal[] filter        = { 0 };
            var       filteredTable = (FilteredTable)rawTable.Filter(new DelegatePredicate1 <decimal>(
                                                                         TestTableColumns.DecimalColumn, s => s > filter[0]));

            var updateHandler = new RowUpdateHandler();

            filteredTable.Subscribe(updateHandler);

            AddRow(rawTable, 4, "Foo4", 4);
            AddRow(rawTable, 5, "Foo5", 5);
            Assert.AreEqual(5, filteredTable.RowCount);

            filter[0] = 50;
            filteredTable.PredicateChanged();
            Assert.AreEqual(0, filteredTable.RowCount);

            AddRow(rawTable, 6, "Foo6", 6);

            filter[0] = 3;
            filteredTable.PredicateChanged();
            Assert.AreEqual(3, filteredTable.RowCount);

            AddRow(rawTable, 7, "Foo7", 7);
            Assert.AreEqual(4, filteredTable.RowCount);

            filter[0] = 50;
            filteredTable.PredicateChanged();
            Assert.AreEqual(0, filteredTable.RowCount);

            filter[0] = 2;
            filteredTable.PredicateChanged();
            Assert.AreEqual(5, filteredTable.RowCount);
        }
Beispiel #12
0
        public void TestExclusionFilter()
        {
            var rawTable      = TestTableHelper.CreateReactiveTable();
            var filteredTable = rawTable.Filter(new TestPredicate(new List <string> {
                TestTableColumns.IdColumn
            }, false));

            var updateHandler = new RowUpdateHandler();

            filteredTable.Subscribe(updateHandler);

            AddRow(rawTable, 1, "Blah1", 123.123m);
            Assert.AreEqual(1, rawTable.RowCount);
            Assert.AreEqual(0, filteredTable.RowCount);
            Assert.AreEqual(0, updateHandler.CurrentRowCount);

            AddRow(rawTable, 2, "Blah2", 123.123m);
            Assert.AreEqual(2, rawTable.RowCount);
            Assert.AreEqual(0, filteredTable.RowCount);
            Assert.AreEqual(0, updateHandler.CurrentRowCount);
        }
Beispiel #13
0
        public void TestSimpleCount()
        {
            var table       = TestTableHelper.CreateReactiveTable();
            var stream      = table;
            var countStream = stream.ColumnUpdates()
                              .Where(update => update.Column.ColumnId == TestTableColumns.IdColumn)
                              .Scan(0, (i, update) => i + 1);

//            var token = table.Subscribe<ColumnUpdate>(i => Console.WriteLine("Count is {0}", i));
            var token = countStream.Subscribe(i => Console.WriteLine("Count is {0}", i));

            var rowIndex = table.AddRow();

            table.SetValue(TestTableColumns.IdColumn, rowIndex, 1);

            var rowIndex2 = table.AddRow();

            table.SetValue(TestTableColumns.IdColumn, rowIndex2, 2);

            token.Dispose();
        }
Beispiel #14
0
        public void TestOneColumnFilter()
        {
            var rawTable      = TestTableHelper.CreateReactiveTable();
            var filteredTable = rawTable.Filter(
                new DelegatePredicate1 <string>(TestTableColumns.StringColumn,
                                                s => !string.IsNullOrEmpty(s) && s.EndsWith("2")));

            var updateHandler = new RowUpdateHandler();

            filteredTable.Subscribe(updateHandler);

            AddRow(rawTable, 1, "Blah1", 123.123m);
            Assert.AreEqual(1, rawTable.RowCount);
            Assert.AreEqual(0, filteredTable.RowCount);
            Assert.AreEqual(0, updateHandler.CurrentRowCount);

            AddRow(rawTable, 2, "Blah2", 123.123m);
            Assert.AreEqual(2, rawTable.RowCount);
            Assert.AreEqual(1, filteredTable.RowCount);
            Assert.AreEqual(1, updateHandler.CurrentRowCount);
            Assert.AreEqual("Blah2", filteredTable.GetValue <string>(TestTableColumns.StringColumn, updateHandler.LastRowUpdated));
        }
        public void TestUpdatingSortKey()
        {
            var       sourceTable = TestTableHelper.CreateReactiveTable();
            const int count       = 100;

            AddValuesInReverseOrder(count, sourceTable);

            var table = new SortedTable(sourceTable);

            table.SortBy(TestTableColumns.StringColumn, Comparer <string> .Default);
            CheckValuesAreSorted(count, table);

            var rowId = 5;

            sourceTable.SetValue(TestTableColumns.StringColumn, rowId, "_");

            Assert.AreEqual("_", table.GetValue(TestTableColumns.StringColumn, 0));

            rowId = 6;
            sourceTable.SetValue(TestTableColumns.StringColumn, rowId, "X");

            Assert.AreEqual("X", table.GetValue(TestTableColumns.StringColumn, count - 1));
        }
Beispiel #16
0
        public void TestNullFilter()
        {
            var rawTable      = TestTableHelper.CreateReactiveTable();
            var filteredTable = rawTable.Filter(new TestPredicate(new List <string> {
                TestTableColumns.IdColumn
            }, true));

            var updateHandler = new RowUpdateHandler();

            filteredTable.Subscribe(updateHandler);

            AddRow(rawTable, 1, "Blah1", 123.123m);
            Assert.AreEqual(1, rawTable.RowCount);
            Assert.AreEqual(1, filteredTable.RowCount);
            Assert.AreEqual(1, updateHandler.CurrentRowCount);
            Assert.AreEqual("Blah1", filteredTable.GetValue <string>(TestTableColumns.StringColumn, updateHandler.LastRowUpdated));

            AddRow(rawTable, 2, "Blah2", 123.123m);
            Assert.AreEqual(2, rawTable.RowCount);
            Assert.AreEqual(2, filteredTable.RowCount);
            Assert.AreEqual(2, updateHandler.CurrentRowCount);
            Assert.AreEqual("Blah2", filteredTable.GetValue <string>(TestTableColumns.StringColumn, updateHandler.LastRowUpdated));
        }
        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);
        }