|
| 1 | +using System; |
| 2 | +using Elastic.Xunit.XunitPlumbing; |
| 3 | +using FluentAssertions; |
| 4 | +using Nest; |
| 5 | +using Tests.Core.Extensions; |
| 6 | +using Tests.Core.ManagedElasticsearch.Clusters; |
| 7 | +using Tests.Domain; |
| 8 | +using Tests.Framework.EndpointTests.TestState; |
| 9 | + |
| 10 | +namespace Tests.Aggregations.Pipeline.CumulativeCardinality |
| 11 | +{ |
| 12 | + [SkipVersion("<7.4.0", "Introduced in 7.4")] |
| 13 | + public class CumulativeCardinalityAggregationUsageTests : AggregationUsageTestBase |
| 14 | + { |
| 15 | + public CumulativeCardinalityAggregationUsageTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(cluster, usage) { } |
| 16 | + |
| 17 | + protected override object AggregationJson => new |
| 18 | + { |
| 19 | + projects_started_per_month = new |
| 20 | + { |
| 21 | + date_histogram = new |
| 22 | + { |
| 23 | + field = "startedOn", |
| 24 | + calendar_interval = "month", |
| 25 | + }, |
| 26 | + aggs = new |
| 27 | + { |
| 28 | + commits = new |
| 29 | + { |
| 30 | + cardinality = new |
| 31 | + { |
| 32 | + field = "numberOfCommits" |
| 33 | + } |
| 34 | + }, |
| 35 | + cumulative_commits = new |
| 36 | + { |
| 37 | + cumulative_cardinality = new |
| 38 | + { |
| 39 | + buckets_path = "commits" |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + }; |
| 45 | + |
| 46 | + protected override Func<AggregationContainerDescriptor<Project>, IAggregationContainer> FluentAggs => a => a |
| 47 | + .DateHistogram("projects_started_per_month", dh => dh |
| 48 | + .Field(p => p.StartedOn) |
| 49 | + .CalendarInterval(DateInterval.Month) |
| 50 | + .Aggregations(aa => aa |
| 51 | + .Cardinality("commits", sm => sm |
| 52 | + .Field(p => p.NumberOfCommits) |
| 53 | + ) |
| 54 | + .CumulativeCardinality("cumulative_commits", d => d |
| 55 | + .BucketsPath("commits") |
| 56 | + ) |
| 57 | + ) |
| 58 | + ); |
| 59 | + |
| 60 | + protected override AggregationDictionary InitializerAggs => |
| 61 | + new DateHistogramAggregation("projects_started_per_month") |
| 62 | + { |
| 63 | + Field = "startedOn", |
| 64 | + CalendarInterval = DateInterval.Month, |
| 65 | + Aggregations = |
| 66 | + new CardinalityAggregation("commits", "numberOfCommits") && |
| 67 | + new CumulativeCardinalityAggregation("cumulative_commits", "commits") |
| 68 | + }; |
| 69 | + |
| 70 | + protected override void ExpectResponse(ISearchResponse<Project> response) |
| 71 | + { |
| 72 | + response.ShouldBeValid(); |
| 73 | + |
| 74 | + var projectsPerMonth = response.Aggregations.DateHistogram("projects_started_per_month"); |
| 75 | + projectsPerMonth.Should().NotBeNull(); |
| 76 | + projectsPerMonth.Buckets.Should().NotBeNull(); |
| 77 | + projectsPerMonth.Buckets.Count.Should().BeGreaterThan(0); |
| 78 | + |
| 79 | + foreach (var item in projectsPerMonth.Buckets) |
| 80 | + { |
| 81 | + var commitsDerivative = item.CumulativeCardinality("cumulative_commits"); |
| 82 | + commitsDerivative.Should().NotBeNull(); |
| 83 | + commitsDerivative.Value.Should().NotBe(null); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | +} |
0 commit comments