ElasticSearch NEST 5.6.1 Query for unit test

2020-03-30 16:35发布

问题:

I wrote a bunch of queries to elastic search and I wanted to write a unit test for them. using this post moq an elastic connection I was able to preform a general mocking. But When I tried to view the Json which is being generated from my query I didn't manage to get it in any way. I tried to follow this post elsatic query moq, but it is relevant only to older versions of Nest because the method ConnectionStatus and RequestInformation is no longer available for an ISearchResponse object.

My test look as follow:

[TestMethod]
 public void VerifyElasticFuncJson()
{
//Arrange
var elasticService = new Mock<IElasticService>();
var elasticClient = new Mock<IElasticClient>();
var clinet = new ElasticClient();
var searchResponse = new Mock<ISearchResponse<ElasticLog>>();
elasticService.Setup(es => es.GetConnection())
    .Returns(elasticClient.Object);

elasticClient.Setup(ec => ec.Search(It.IsAny<Func<SearchDescriptor<ElasticLog>, 
                          ISearchRequest>>())).
                          Returns(searchResponse.Object);

//Act
var service = new ElasticCusipInfoQuery(elasticService.Object);
var FindFunc = service.MatchCusip("CusipA", HostName.GSMSIMPAPPR01, 
                                        LogType.Serilog);
var con = GetConnection();
var search =  con.Search<ElasticLog>(sd => sd
             .Type(LogType.Serilog)
             .Index("logstash-*")
             .Query(q => q
             .Bool(b => b
                    .Must(FindFunc)
                    )
               )     
             );
 **HERE I want to get the JSON** and assert it look as expected**
}

Is there any other way to achieve what I ask?

回答1:

The best way to do this would be to use the InMemoryConnection to capture the request bytes and compare this to the expected JSON. This is what the unit tests for NEST do. Something like

private static void Main()
{
    var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
    var connectionSettings = new ConnectionSettings(pool, new InMemoryConnection())
        .DefaultIndex("default")
        .DisableDirectStreaming();

    var client = new ElasticClient(connectionSettings);

    // Act
    var searchResponse = client.Search<Question>(s => s
       .Query(q => (q
         .Match(m => m
               .Field(f => f.Title)
               .Query("Kibana")
         ) || q
         .Match(m => m
               .Field(f => f.Title)
               .Query("Elasticsearch")
               .Boost(2)
         )) && +q
         .Range(t => t
               .Field(f => f.Score)
               .GreaterThan(0)
         )
       )
    );

    var actual = searchResponse.RequestJson();

    var expected = new 
    {
        query = new {
            @bool = new {
                must = new object[] {
                    new {
                        @bool = new {
                            should = new object[] {
                                new {
                                    match = new {
                                        title = new {
                                            query = "Kibana"
                                        }
                                    }
                                },
                                new {
                                    match = new {
                                        title = new {
                                            query = "Elasticsearch",
                                            boost = 2d
                                        }
                                    }
                                }
                            },
                        }
                    },
                    new {
                        @bool = new {
                            filter = new [] {
                                new {
                                    range = new {
                                        score = new {
                                            gt = 0d
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    };

    // Assert
    Console.WriteLine(JObject.DeepEquals(JToken.FromObject(expected), JToken.Parse(actual)));
}

public static class Extensions
{
    public static string RequestJson(this IResponse response) =>
        Encoding.UTF8.GetString(response.ApiCall.RequestBodyInBytes);
}

I've used an anonymous type for the expected JSON as it's easier to work with than an escaped JSON string.

One thing to note is that Json.NET's JObject.DeepEquals(...) will return true even when there are repeated object keys in a JSON object (so long as the last key/value matches). It's not likely something you'll encounter if you're only serializing NEST searches though, but something to be aware of.

If you're going to have many tests checking serialization, you'll want to create a single instance of ConnectionSettings and share with all, so that you can take advantage of the internal caches within it and your tests will run quicker than instantiating a new instance in each test.