Skip to content

Commit 017803f

Browse files
committed
Extended stats deserialization unit test (#4109)
Add a unit test to assert that ExtendedStats aggregation can deserialize the provided response in #4103 (cherry picked from commit 1e45ee8)
1 parent 3d5a94c commit 017803f

File tree

2 files changed

+85
-1
lines changed

2 files changed

+85
-1
lines changed

src/Nest/Aggregations/AggregateFormatter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ private IAggregate GetStatsAggregate(ref JsonReader reader, IReadOnlyDictionary<
436436

437437
reader.ReadNext(); // ,
438438
var propertyName = reader.ReadPropertyName();
439-
while (reader.GetCurrentJsonToken() != JsonToken.EndObject && propertyName.Contains(Parser.AsStringSuffix))
439+
while (reader.GetCurrentJsonToken() != JsonToken.EndObject && propertyName.EndsWith(Parser.AsStringSuffix))
440440
{
441441
reader.ReadNext(); // <value>
442442
if (reader.GetCurrentJsonToken() == JsonToken.ValueSeparator)
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
using System;
2+
using System.Text;
3+
using Elastic.Xunit.XunitPlumbing;
4+
using Elasticsearch.Net;
5+
using FluentAssertions;
6+
using Nest;
7+
8+
namespace Tests.Reproduce
9+
{
10+
public class GithubIssue4013
11+
{
12+
[U]
13+
public void CanDeserializeExtendedStats()
14+
{
15+
var json = @"{
16+
""took"" : 1,
17+
""timed_out"" : false,
18+
""_shards"" : {
19+
""total"" : 2,
20+
""successful"" : 2,
21+
""skipped"" : 0,
22+
""failed"" : 0
23+
},
24+
""hits"" : {
25+
""total"" : 1100,
26+
""max_score"" : 0.0,
27+
""hits"" : [ ]
28+
},
29+
""aggregations"": {
30+
""extended_stats#1"": {
31+
""count"": 3,
32+
""min"": 1569521764937,
33+
""max"": 1569526264937,
34+
""avg"": 1569524464937,
35+
""sum"": 4708573394811,
36+
""min_as_string"": ""2019-09-26T18:16:04.937Z"",
37+
""max_as_string"": ""2019-09-26T19:31:04.937Z"",
38+
""avg_as_string"": ""2019-09-26T19:01:04.937Z"",
39+
""sum_as_string"": ""2119-03-18T09:03:14.811Z"",
40+
""sum_of_squares"": 7.390221138118668e+24,
41+
""variance"": 3779929134421.3335,
42+
""std_deviation"": 1944203.9847766317,
43+
""std_deviation_bounds"": {
44+
""upper"": 1569528353344.9695,
45+
""lower"": 1569520576529.0305
46+
},
47+
""sum_of_squares_as_string"": ""292278994-08-17T07:12:55.807Z"",
48+
""variance_as_string"": ""2089-10-12T04:18:54.421Z"",
49+
""std_deviation_as_string"": ""1970-01-01T00:32:24.203Z"",
50+
""std_deviation_bounds_as_string"": {
51+
""upper"": ""2019-09-26T20:05:53.344Z"",
52+
""lower"": ""2019-09-26T17:56:16.529Z""
53+
}
54+
}
55+
}
56+
}";
57+
58+
var bytes = Encoding.UTF8.GetBytes(json);
59+
var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
60+
var connectionSettings = new ConnectionSettings(pool, new InMemoryConnection(bytes));
61+
var client = new ElasticClient(connectionSettings);
62+
63+
Action searchResponse = () => client.Search<object>(s => s.AllIndices());
64+
65+
searchResponse.Should().NotThrow();
66+
67+
var response = client.Search<object>(s => s.AllIndices());
68+
69+
var extendedStats = response.Aggregations.ExtendedStats("1");
70+
extendedStats.Should().NotBeNull();
71+
extendedStats.Count.Should().Be(3);
72+
extendedStats.Min.Should().Be(1569521764937);
73+
extendedStats.Max.Should().Be(1569526264937);
74+
extendedStats.Average.Should().Be(1569524464937);
75+
extendedStats.Sum.Should().Be(4708573394811);
76+
extendedStats.SumOfSquares.Should().Be(7.390221138118668e+24);
77+
extendedStats.Variance.Should().Be(3779929134421.3335);
78+
extendedStats.StdDeviation.Should().Be(1944203.9847766317);
79+
extendedStats.StdDeviationBounds.Should().NotBeNull();
80+
extendedStats.StdDeviationBounds.Upper.Should().Be(1569528353344.9695);
81+
extendedStats.StdDeviationBounds.Lower.Should().Be(1569520576529.0305);
82+
}
83+
}
84+
}

0 commit comments

Comments
 (0)