|
| 1 | +// Licensed to Elasticsearch B.V under one or more agreements. |
| 2 | +// Elasticsearch B.V licenses this file to you under the Apache 2.0 License. |
| 3 | +// See the LICENSE file in the project root for more information |
| 4 | + |
| 5 | +using System.IO; |
| 6 | +using System.Text; |
| 7 | +using Elastic.Elasticsearch.Xunit.XunitPlumbing; |
| 8 | +using FluentAssertions; |
| 9 | +using Nest; |
| 10 | + |
| 11 | +namespace Tests.Aggregations |
| 12 | +{ |
| 13 | + public class SearchAggregationSerializationTests |
| 14 | + { |
| 15 | + private const string ExpectedAggsJson = @"{""aggs"":{""test-agg"":{""terms"":{""field"":""my-field""}}}}"; |
| 16 | + private const string AggregationsJson = @"{""aggregations"":{""test-agg"":{""terms"":{""field"":""my-field""}}}}"; |
| 17 | + |
| 18 | + [U] public void SerializesAsExpected() |
| 19 | + { |
| 20 | + var client = new ElasticClient(); |
| 21 | + |
| 22 | + var request = new SearchRequest |
| 23 | + { |
| 24 | + Aggregations = new TermsAggregation("test-agg") |
| 25 | + { |
| 26 | + Field = "my-field" |
| 27 | + } |
| 28 | + }; |
| 29 | + |
| 30 | + var stream = new MemoryStream(); |
| 31 | + client.RequestResponseSerializer.Serialize(request, stream); |
| 32 | + |
| 33 | + stream.Position = 0; |
| 34 | + var reader = new StreamReader(stream); |
| 35 | + var json = reader.ReadToEnd(); |
| 36 | + |
| 37 | + json.Should().Be(ExpectedAggsJson); |
| 38 | + } |
| 39 | + |
| 40 | + [U] |
| 41 | + public void DeserializesAggsAsExpected() |
| 42 | + { |
| 43 | + var client = new ElasticClient(); |
| 44 | + var stream = new MemoryStream(Encoding.UTF8.GetBytes(ExpectedAggsJson)); |
| 45 | + var request = client.RequestResponseSerializer.Deserialize<SearchRequest>(stream); |
| 46 | + request.Aggregations.Should().HaveCount(1); |
| 47 | + } |
| 48 | + |
| 49 | + [U] |
| 50 | + public void DeserializesAggregationsAsExpected() |
| 51 | + { |
| 52 | + var client = new ElasticClient(); |
| 53 | + var stream = new MemoryStream(Encoding.UTF8.GetBytes(AggregationsJson)); |
| 54 | + var request = client.RequestResponseSerializer.Deserialize<SearchRequest>(stream); |
| 55 | + request.Aggregations.Should().HaveCount(1); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + public class AggregationContainer_AggregationSerializationTests |
| 60 | + { |
| 61 | + private const string ExpectedAggsJson = @"{""aggs"":{""sub-agg"":{""terms"":{""field"":""my-field""}}},""terms"":{""field"":""my-field""}}"; |
| 62 | + private const string AggregationsJson = @"{""aggregations"":{""sub-agg"":{""terms"":{""field"":""my-field""}}},""terms"":{""field"":""my-field""}}"; |
| 63 | + |
| 64 | + [U] |
| 65 | + public void SerializesAsExpected() |
| 66 | + { |
| 67 | + var client = new ElasticClient(); |
| 68 | + |
| 69 | + AggregationContainer aggsDictionary = new TermsAggregation("test-agg") |
| 70 | + { |
| 71 | + Field = "my-field", |
| 72 | + Aggregations = new TermsAggregation("sub-agg") |
| 73 | + { |
| 74 | + Field = "my-field" |
| 75 | + } |
| 76 | + }; |
| 77 | + |
| 78 | + var stream = new MemoryStream(); |
| 79 | + client.RequestResponseSerializer.Serialize(aggsDictionary, stream); |
| 80 | + |
| 81 | + stream.Position = 0; |
| 82 | + var reader = new StreamReader(stream); |
| 83 | + var json = reader.ReadToEnd(); |
| 84 | + |
| 85 | + json.Should().Be(ExpectedAggsJson); |
| 86 | + } |
| 87 | + |
| 88 | + [U] |
| 89 | + public void DeserializesAggsAsExpected() |
| 90 | + { |
| 91 | + var client = new ElasticClient(); |
| 92 | + var stream = new MemoryStream(Encoding.UTF8.GetBytes(ExpectedAggsJson)); |
| 93 | + var request = client.RequestResponseSerializer.Deserialize<AggregationContainer>(stream); |
| 94 | + request.Aggregations.Should().HaveCount(1); |
| 95 | + } |
| 96 | + |
| 97 | + [U] |
| 98 | + public void DeserializesAggregationsAsExpected() |
| 99 | + { |
| 100 | + var client = new ElasticClient(); |
| 101 | + var stream = new MemoryStream(Encoding.UTF8.GetBytes(AggregationsJson)); |
| 102 | + var request = client.RequestResponseSerializer.Deserialize<AggregationContainer>(stream); |
| 103 | + request.Aggregations.Should().HaveCount(1); |
| 104 | + } |
| 105 | + } |
| 106 | +} |
0 commit comments