/// <summary> /// Sets up a new <see cref="AggregateBuilder"/> with all the columns (See <see cref="AggregateDimensions"/>), WHERE logic (See <see cref="RootFilterContainer"/>, Pivot /// etc. /// </summary> /// <remarks>Note that some elements e.g. axis are automatically handles at query generation time and therefore do not have to be injected into the <see cref="AggregateBuilder"/></remarks> /// <param name="topX">Maximum number of rows to return, the proper way to do this is via <see cref="AggregateTopX"/></param> /// <returns></returns> public AggregateBuilder GetQueryBuilder(int?topX = null) { if (string.IsNullOrWhiteSpace(CountSQL)) { throw new NotSupportedException("Cannot generate an AggregateBuilder because the AggregateConfiguration '" + this + "' has no Count SQL, usually this is the case for 'Cohort Set' Aggregates or 'Patient Index Table' Aggregates. In either case you should use CohortQueryBuilder instead of AggregateBuilder"); } var allForcedJoins = ForcedJoins.ToArray(); AggregateBuilder builder; var limitationSQLIfAny = topX == null ? null : "TOP " + topX.Value; if (allForcedJoins.Any()) { builder = new AggregateBuilder(limitationSQLIfAny, CountSQL, this, allForcedJoins); } else { builder = new AggregateBuilder(limitationSQLIfAny, CountSQL, this); } builder.AddColumnRange(AggregateDimensions.ToArray()); builder.RootFilterContainer = RootFilterContainer; if (PivotOnDimensionID != null) { builder.SetPivotToDimensionID(PivotDimension); } return(builder); }
public void GroupBy_CategoryWithCount_Correct(DatabaseType type) { Catalogue catalogue; ExtractionInformation[] extractionInformations; TableInfo tableInfo; var tbl = UploadTestDataAsTableToServer(type, out catalogue, out extractionInformations, out tableInfo); //setup the aggregate var categoryDimension = extractionInformations.Single(e => e.GetRuntimeName().Equals("Category", StringComparison.CurrentCultureIgnoreCase)); var configuration = new AggregateConfiguration(CatalogueRepository, catalogue, "GroupBy_Category"); var dimension = new AggregateDimension(CatalogueRepository, categoryDimension, configuration); try { //get the result of the aggregate var builder = new AggregateBuilder(null, configuration.CountSQL, configuration); builder.AddColumn(dimension); var resultTable = GetResultForBuilder(builder, tbl); VerifyRowExist(resultTable, "T", 7); VerifyRowExist(resultTable, "F", 2); VerifyRowExist(resultTable, "E&, %a' mp;E", 3); VerifyRowExist(resultTable, "G", 2); } finally { Destroy(tbl, configuration, catalogue, tableInfo); } }
public async Task Two_different_aggregate_types_can_have_the_same_id_and_not_see_each_others_events() { Builders[typeof(FirstAggregate)] = events => AggregateBuilder.Build(new FirstAggregate(), events); Builders[typeof(SecondAggregate)] = events => AggregateBuilder.Build(new SecondAggregate(), events); var id = Guid.NewGuid(); var first = new FirstAggregate(); first.SetID(id); first.AddEvents(); var second = new SecondAggregate(); second.SetID(id); second.AddEvents(); await Session.Save(first); await Session.Save(second); await Session.Commit(); var one = await Session.LoadAggregate <FirstAggregate>(id); var two = await Session.LoadAggregate <SecondAggregate>(id); one.SeenEvents.ShouldBe(5); two.SeenEvents.ShouldBe(5); }
public void GroupBy_CategoryWithSum_Correct(DatabaseType type) { var tbl = UploadTestDataAsTableToServer(type, out var catalogue, out var extractionInformations, out var tableInfo); //setup the aggregate var categoryDimension = extractionInformations.Single(e => e.GetRuntimeName().Equals("Category", StringComparison.CurrentCultureIgnoreCase)); var configuration = new AggregateConfiguration(CatalogueRepository, catalogue, "GroupBy_Category"); var dimension = new AggregateDimension(CatalogueRepository, categoryDimension, configuration); configuration.CountSQL = "sum(NumberInTrouble)"; configuration.SaveToDatabase(); try { //get the result of the aggregate var builder = new AggregateBuilder(null, configuration.CountSQL, configuration); builder.AddColumn(dimension); var resultTable = GetResultForBuilder(builder, tbl); VerifyRowExist(resultTable, "T", 139); VerifyRowExist(resultTable, "F", 60); VerifyRowExist(resultTable, "E&, %a' mp;E", 137); VerifyRowExist(resultTable, "G", 100); Assert.AreEqual(4, resultTable.Rows.Count); } finally { Destroy(tbl, configuration, catalogue, tableInfo); } }
public void GroupBy_CategoryWithSum_WHEREStatement(DatabaseType type) { var tbl = UploadTestDataAsTableToServer(type, out var catalogue, out var extractionInformations, out var tableInfo); //setup the aggregate var categoryDimension = extractionInformations.Single(e => e.GetRuntimeName().Equals("Category", StringComparison.CurrentCultureIgnoreCase)); var configuration = new AggregateConfiguration(CatalogueRepository, catalogue, "GroupBy_Category"); var dimension = new AggregateDimension(CatalogueRepository, categoryDimension, configuration); configuration.CountSQL = "sum(NumberInTrouble)"; configuration.SaveToDatabase(); try { //get the result of the aggregate var builder = new AggregateBuilder(null, configuration.CountSQL, configuration); builder.AddColumn(dimension); AddWHEREToBuilder_CategoryIsTOrNumberGreaterThan42(builder, type); var resultTable = GetResultForBuilder(builder, tbl); //T is matched on all records so they are summed VerifyRowExist(resultTable, "T", 139); //VerifyRowExist(resultTable, "F", 60); //F does not have any records over 42 and isn't T so shouldnt be matched VerifyRowExist(resultTable, "E&, %a' mp;E", 59); //E has 1 records over 42 VerifyRowExist(resultTable, "G", 100); //47 + 53 Assert.AreEqual(3, resultTable.Rows.Count); } finally { Destroy(tbl, configuration, catalogue, tableInfo); } }
public void GroupBy_AxisWithCount_WHERECorrect(DatabaseType type) { var tbl = UploadTestDataAsTableToServer(type, out var catalogue, out var extractionInformations, out var tableInfo); //setup the aggregate with axis AggregateDimension dimension; var configuration = SetupAggregateWithAxis(type, extractionInformations, catalogue, out dimension); configuration.CountSQL = "count(NumberInTrouble)"; configuration.SaveToDatabase(); try { //get the result of the aggregate var builder = new AggregateBuilder(null, configuration.CountSQL, configuration); builder.AddColumn(dimension); AddWHEREToBuilder_CategoryIsTOrNumberGreaterThan42(builder, type); var resultTable = GetResultForBuilder(builder, tbl); //axis is ordered ascending by date starting in 2000 so that row should come first Assert.IsTrue(AreBasicallyEquals("2000", resultTable.Rows[0][0])); VerifyRowExist(resultTable, "2000", null); VerifyRowExist(resultTable, "2001", 4); //4 are T or > 42 VerifyRowExist(resultTable, "2002", 2); VerifyRowExist(resultTable, "2003", 2); //only the first date in the test data is <= 2003-01-01 } finally { Destroy(tbl, configuration, catalogue, tableInfo); } }
public void Test_AggregateBuilder_MySql_Top32() { _ti.DatabaseType = DatabaseType.MySql; _ti.SaveToDatabase(); var builder = new AggregateBuilder(null, "count(*)", null); builder.AddColumn(_dimension1); var topx = new AggregateTopX(CatalogueRepository, _configuration, 32); topx.OrderByDimensionIfAny_ID = _dimension1.ID; topx.SaveToDatabase(); builder.AggregateTopX = topx; Assert.AreEqual(CollapseWhitespace(@"/**/ SELECT Col1, count(*) AS MyCount FROM T1 group by Col1 order by Col1 desc LIMIT 32"), CollapseWhitespace(builder.SQL.Trim())); topx.DeleteInDatabase(); }
protected StorageSessionTests() { Builders = new Dictionary <Type, Func <IEnumerable <IEvent>, object> >(); Projections = new List <Projector>(); Editor = EditorID.Parse("wat"); Builders[typeof(Toggle)] = events => AggregateBuilder.Build(new Toggle(), events); }
public void EmptyBuilderReturnsExpectedProperties() { var sut = new AggregateBuilder(); Assert.That(sut.Identifier, Is.Null); Assert.That(sut.ExpectedVersion, Is.EqualTo(Int32.MinValue)); Assert.That(sut.Root, Is.Null); }
public void GroupBy_PivotWithSum_HAVING_Top1_WHERE(DatabaseType type) { Catalogue catalogue; ExtractionInformation[] extractionInformations; TableInfo tableInfo; var tbl = UploadTestDataAsTableToServer(type, out catalogue, out extractionInformations, out tableInfo); //setup the aggregate pivot (and axis) AggregateDimension axisDimension; AggregateDimension pivotDimension; var configuration = SetupAggregateWithPivot(type, extractionInformations, catalogue, out axisDimension, out pivotDimension); configuration.CountSQL = "sum(NumberInTrouble)"; configuration.PivotOnDimensionID = pivotDimension.ID; //pivot on the Category configuration.HavingSQL = "count(*)<5"; //throws out 'T' configuration.SaveToDatabase(); var topx = new AggregateTopX(CatalogueRepository, configuration, 1); //Top 1 (highest count columns should be used for pivot) topx.OrderByDirection = AggregateTopXOrderByDirection.Descending; topx.SaveToDatabase(); try { //get the result of the aggregate var builder = new AggregateBuilder(null, configuration.CountSQL, configuration); builder.AddColumn(axisDimension); builder.AddColumn(pivotDimension); builder.SetPivotToDimensionID(pivotDimension); AddWHEREToBuilder_CategoryIsTOrNumberGreaterThan42(builder, type); var resultTable = GetResultForBuilder(builder, tbl); //axis is ordered ascending by date starting in 2000 so that row should come first Assert.IsTrue(AreBasicallyEquals("2000", resultTable.Rows[0][0])); //where logic matches T in spades but HAVING statement throws it out for having more than 4 records total Assert.AreEqual("E&, %a' mp;E", resultTable.Columns[1].ColumnName); //Only E appears because of Top 1 pivot statement VerifyRowExist(resultTable, "2000", null); //all E records are discarded except 59 because that is the WHERE logic VerifyRowExist(resultTable, "2001", null); VerifyRowExist(resultTable, "2002", null); VerifyRowExist(resultTable, "2003", null); VerifyRowExist(resultTable, "2004", null); VerifyRowExist(resultTable, "2005", 59); VerifyRowExist(resultTable, "2006", null); VerifyRowExist(resultTable, "2007", null); } finally { Destroy(tbl, topx, configuration, catalogue, tableInfo); } }
public void GroupBy_PivotWithSum_Top2AlphabeticalAsc_WHEREStatement(DatabaseType type) { Catalogue catalogue; ExtractionInformation[] extractionInformations; TableInfo tableInfo; var tbl = UploadTestDataAsTableToServer(type, out catalogue, out extractionInformations, out tableInfo); //setup the aggregate pivot (and axis) AggregateDimension axisDimension; AggregateDimension pivotDimension; var configuration = SetupAggregateWithPivot(type, extractionInformations, catalogue, out axisDimension, out pivotDimension); configuration.CountSQL = "sum(NumberInTrouble)"; configuration.PivotOnDimensionID = pivotDimension.ID; //pivot on the Category configuration.SaveToDatabase(); var topx = new AggregateTopX(CatalogueRepository, configuration, 2); topx.OrderByDirection = AggregateTopXOrderByDirection.Descending; topx.OrderByDimensionIfAny_ID = pivotDimension.ID; topx.OrderByDirection = AggregateTopXOrderByDirection.Ascending; topx.SaveToDatabase(); try { //get the result of the aggregate var builder = new AggregateBuilder(null, configuration.CountSQL, configuration); builder.AddColumn(axisDimension); builder.AddColumn(pivotDimension); builder.SetPivotToDimensionID(pivotDimension); AddWHEREToBuilder_CategoryIsTOrNumberGreaterThan42(builder, type); var resultTable = GetResultForBuilder(builder, tbl); //axis is ordered ascending by date starting in 2000 so that row should come first Assert.IsTrue(AreBasicallyEquals("2000", resultTable.Rows[0][0])); //sort in AggregateTopX is the pivot dimension asc (i.e. order alphabetically) Assert.AreEqual("E&, %a' mp;E", resultTable.Columns[1].ColumnName); Assert.AreEqual("G", resultTable.Columns[2].ColumnName); //E,G (note that only 1 value appears for E because WHERE throws out rest). Also note the two columns are E and G because that is Top 2 when alphabetically sorted of the pivot values (E,F,G,T) that match the filter (F doesn't) VerifyRowExist(resultTable, "2000", null, null); //no records in 2000 but it is important it appears still because that is what the axis says VerifyRowExist(resultTable, "2001", null, 53); VerifyRowExist(resultTable, "2002", null, null); VerifyRowExist(resultTable, "2003", null, null); VerifyRowExist(resultTable, "2004", null, null); VerifyRowExist(resultTable, "2005", 59, null); VerifyRowExist(resultTable, "2006", null, null); VerifyRowExist(resultTable, "2007", null, null); } finally { Destroy(tbl, topx, configuration, catalogue, tableInfo); } }
public async Task <EntityId> Post(CreateTodo create) { var evnt = TodoItemAggregate.New(create.Title); var storedEvent = await _eventStore.Store(evnt); var aggregate = AggregateBuilder <TodoItemAggregate> .Rehydrate(new [] { storedEvent }); return(aggregate.Id); }
public void UsingFirstInAggregateShouldGiveCorrectJson() { var builder = new AggregateBuilder <TestType1>(); var aggregate = builder.UniqueValues(x => x.DataType, 10, new { Last = builder.First(x => x.Value, y => y.Date) }); var test = aggregate.ToChildJProperty(); var expected = ExpectedFirstResult(); Assert.True(JToken.DeepEquals(test, expected)); }
public void Test1() { var builder = new AggregateBuilder <TestType1>(); var aggregate = builder.UniqueValues(x => x.Value, 10, new { Maximum = builder.Maximum(x => x.Value) }); var test = aggregate.ToChildJProperty(); var expected = ExpectedResult(); Assert.True(JToken.DeepEquals(test, expected)); }
} // getExpectedColumnType() /** * Executes the function * * @param values * the values to be evaluated. If a value is null it won't be * evaluated * @return the result of the function execution. The return type class is * dependent on the FunctionType and the values provided. COUNT * yields a Long, AVG and SUM yields Double values and MAX and MIN * yields the type of the provided values. */ // @Override public Object evaluate(params Object[] values) { AggregateBuilder <object> builder = createAggregateBuilder <object>(); foreach (Object item in values) { builder.add(item); } return(builder.getAggregate()); }
public void TestAggregateBuilding_NoConfigurationNoDimensions() { var builder = new AggregateBuilder(null, "count(*)", null, new [] { _ti }); Assert.AreEqual(CollapseWhitespace(@"/**/ SELECT count(*) AS MyCount FROM T1"), CollapseWhitespace(builder.SQL)); }
public void GroupBy_PivotWithSum_Top2BasedonCountColumnDesc(DatabaseType type) { Catalogue catalogue; ExtractionInformation[] extractionInformations; TableInfo tableInfo; var tbl = UploadTestDataAsTableToServer(type, out catalogue, out extractionInformations, out tableInfo); //setup the aggregate pivot (and axis) AggregateDimension axisDimension; AggregateDimension pivotDimension; var configuration = SetupAggregateWithPivot(type, extractionInformations, catalogue, out axisDimension, out pivotDimension); configuration.CountSQL = "sum(NumberInTrouble)"; configuration.PivotOnDimensionID = pivotDimension.ID; //pivot on the Category configuration.SaveToDatabase(); var topx = new AggregateTopX(CatalogueRepository, configuration, 2); topx.OrderByDirection = AggregateTopXOrderByDirection.Descending; topx.SaveToDatabase(); try { //get the result of the aggregate var builder = new AggregateBuilder(null, configuration.CountSQL, configuration); builder.AddColumn(axisDimension); builder.AddColumn(pivotDimension); builder.SetPivotToDimensionID(pivotDimension); var resultTable = GetResultForBuilder(builder, tbl); //axis is ordered ascending by date starting in 2000 so that row should come first Assert.IsTrue(AreBasicallyEquals("2000", resultTable.Rows[0][0])); Assert.AreEqual("T", resultTable.Columns[1].ColumnName); Assert.AreEqual("E&, %a' mp;E", resultTable.Columns[2].ColumnName); //T,E,G - F does not appear because WHERE throws it out (both counts are below 42) VerifyRowExist(resultTable, "2000", null, null); //no records in 2000 but it is important it appears still because that is what the axis says VerifyRowExist(resultTable, "2001", 67, 37); VerifyRowExist(resultTable, "2002", 30, 41); VerifyRowExist(resultTable, "2003", 42, null); VerifyRowExist(resultTable, "2004", null, null); VerifyRowExist(resultTable, "2005", null, 59); VerifyRowExist(resultTable, "2006", null, null); VerifyRowExist(resultTable, "2007", null, null); } finally { Destroy(tbl, topx, configuration, catalogue, tableInfo); } }
public void NestedAggregateWithLast() { var builder = new AggregateBuilder <TestType1>(); var aggregate = builder.UniqueValues(x => x.Value, 10, builder.UniqueValues(x => x.Value, 10, new { Last = builder.Last(x => x.Value) })); var test = aggregate.ToChildJProperty(); var expected = ExpectedLastNestedResult(); Assert.True(JToken.DeepEquals(test, expected)); }
protected void CreateToggle(params IEvent[] events) { var create = new ToggleCreated( Editor, ToggleID.CreateNew(), "Test Toggle", ""); Toggle = new Toggle(); AggregateBuilder.Build(Toggle, new[] { create.AsAct(create.NewToggleID) }.Concat(events)); }
public void IdentifiedByReturnsExpectedResult() { var sut = new AggregateBuilder(); var expectedVersion = sut.ExpectedVersion; var root = sut.Root; var result = sut.IdentifiedBy("identifier"); Assert.That(result, Is.SameAs(sut)); Assert.That(sut.Identifier, Is.EqualTo("identifier")); Assert.That(sut.ExpectedVersion, Is.EqualTo(expectedVersion)); Assert.That(sut.Root, Is.SameAs(root)); }
public void ExpectVersionReturnsExpectedResult() { var sut = new AggregateBuilder(); var identifier = sut.Identifier; var root = sut.Root; var result = sut.ExpectVersion(123); Assert.That(result, Is.SameAs(sut)); Assert.That(sut.Identifier, Is.EqualTo(identifier)); Assert.That(sut.ExpectedVersion, Is.EqualTo(123)); Assert.That(sut.Root, Is.SameAs(root)); }
private DataTable GetResultForBuilder(AggregateBuilder builder, DiscoveredTable tbl) { string sql = builder.SQL; using (var con = tbl.Database.Server.GetConnection()) { con.Open(); var da = tbl.Database.Server.GetDataAdapter(sql, con); var toReturn = new DataTable(); da.Fill(toReturn); return(toReturn); } }
public void WithRootReturnsExpectedResult() { var sut = new AggregateBuilder(); var identifier = sut.Identifier; var expectedVersion = sut.ExpectedVersion; var root = new AggregateRootEntityStub(); var result = sut.WithRoot(root); Assert.That(result, Is.SameAs(sut)); Assert.That(sut.Identifier, Is.EqualTo(identifier)); Assert.That(sut.ExpectedVersion, Is.EqualTo(expectedVersion)); Assert.That(sut.Root, Is.SameAs(root)); }
public void BuildReturnsExpectedResult() { var sut = new AggregateBuilder(); const string identifier = "identifier"; const int expectedVersion = 123; var root = new AggregateRootEntityStub(); var result = sut.IdentifiedBy(identifier).ExpectVersion(expectedVersion).WithRoot(root).Build(); Assert.That(result, Is.Not.Null); Assert.That(result.Identifier, Is.EqualTo(identifier)); Assert.That(result.ExpectedVersion, Is.EqualTo(expectedVersion)); Assert.That(result.Root, Is.SameAs(root)); }
public void When_a_timestampable_event_is_loaded() { var stamp = new DateTime(2017, 2, 3); var act = new Event <Stamped> { AggregateID = _aggregate.ID, TimeStamp = stamp, Data = new Stamped() }; AggregateBuilder.Build(_aggregate, new[] { act }); _aggregate.LastEvent.ShouldBe(stamp); }
public void When_loading_from_events() { var events = new IEvent[] { new TestEventOne().AsAct(), new TestEventOne().AsAct(), new TestEventOne().AsAct() }; AggregateBuilder.Build(_aggregate, events); _aggregate.ShouldSatisfyAllConditions( () => ((IEvented)_aggregate).GetPendingEvents().ShouldBeEmpty(), () => _aggregate.SeenEvents.ShouldBe(events.Select(e => e.Data)) ); }
public void GenerateAggregateManuallyTest() { CreateFunction(CatalogueRepository); //do a count * on the query builder AggregateBuilder queryBuilder = new AggregateBuilder("", "count(*)", null, new[] { _function.TableInfoCreated }); Assert.IsTrue(queryBuilder.SQL.Contains(@"SELECT")); Assert.IsTrue(queryBuilder.SQL.Contains(@"count(*)")); Assert.IsTrue(queryBuilder.SQL.Contains(@"DECLARE @name AS varchar(50);")); Assert.IsTrue(queryBuilder.SQL.Contains(@"SET @name='fish';")); Assert.IsTrue(queryBuilder.SQL.Contains("..MyAwesomeFunction(@startNumber,@stopNumber,@name) AS MyAwesomeFunction")); Console.WriteLine(queryBuilder.SQL); }
public void TestAggregateBuilding_AS_InCount() { var builder = new AggregateBuilder(null, "count(cast(1 AS int))", null); builder.AddColumn(_dimension1); Assert.AreEqual(CollapseWhitespace(@"/**/ SELECT Col1, count(cast(1 AS int)) AS MyCount FROM T1 group by Col1 order by Col1"), CollapseWhitespace(builder.SQL)); }
public void TestAggregateBuilding_NoConfigurationOneDimension() { var builder = new AggregateBuilder(null, "count(*)", null); builder.AddColumn(_dimension1); Assert.AreEqual(CollapseWhitespace(@"/**/ SELECT Col1, count(*) AS MyCount FROM T1 group by Col1 order by Col1"), CollapseWhitespace(builder.SQL)); }
public void GroupBy_PivotWithSum_Correct(DatabaseType type) { var tbl = UploadTestDataAsTableToServer(type, out var catalogue, out var extractionInformations, out var tableInfo); //setup the aggregate pivot (and axis) AggregateDimension axisDimension; AggregateDimension pivotDimension; var configuration = SetupAggregateWithPivot(type, extractionInformations, catalogue, out axisDimension, out pivotDimension); configuration.CountSQL = "sum(NumberInTrouble)"; configuration.PivotOnDimensionID = pivotDimension.ID; //pivot on the Category configuration.SaveToDatabase(); try { //get the result of the aggregate var builder = new AggregateBuilder(null, configuration.CountSQL, configuration); builder.AddColumn(axisDimension); builder.AddColumn(pivotDimension); builder.SetPivotToDimensionID(pivotDimension); var resultTable = GetResultForBuilder(builder, tbl); //axis is ordered ascending by date starting in 2000 so that row should come first Assert.IsTrue(AreBasicallyEquals("2000", resultTable.Rows[0][0])); Assert.AreEqual("T", resultTable.Columns[1].ColumnName); Assert.AreEqual("E&, %a' mp;E", resultTable.Columns[2].ColumnName); Assert.AreEqual("F", resultTable.Columns[3].ColumnName); Assert.AreEqual("G", resultTable.Columns[4].ColumnName); //T,E,F,G VerifyRowExist(resultTable, "2000", null, null, null, null);//no records in 2000 but it is important it appears still because that is what the axis says VerifyRowExist(resultTable, "2001", 67, 37, null, 53); VerifyRowExist(resultTable, "2002", 30, 41, 60, null); VerifyRowExist(resultTable, "2003", 42, null, null, null); VerifyRowExist(resultTable, "2004", null, null, null, null); VerifyRowExist(resultTable, "2005", null, 59, null, null); VerifyRowExist(resultTable, "2006", null, null, null, null); VerifyRowExist(resultTable, "2007", null, null, null, null); } finally { Destroy(tbl, configuration, catalogue, tableInfo); } }