ElasticSearch NEST 5.6.1 查询单元测试



我写了一堆查询来弹性搜索,我想为它们写一个单元测试。 使用这篇文章 最小起订量 弹性连接 我能够进行一般的嘲笑。但是当我尝试查看从我的查询生成的 Json 时,我无法以任何方式获取它。 我试图遵循这篇文章 elsatic 查询 moq,但它仅与旧版本的 Nest 相关,因为该方法不再可用于ISearchResponse对象ConnectionStatusRequestInformation

我的测试如下所示:

[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**
}

还有其他方法可以实现我的要求吗?

执行此操作的最佳方法是使用InMemoryConnection捕获请求字节并将其与预期的 JSON 进行比较。这就是 NEST 单元测试的作用。类似的东西

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);
}

我对预期的 JSON 使用了匿名类型,因为它比转义的 JSON 字符串更容易使用。

需要注意的一点是,即使 JSON 对象中有重复的对象键(只要最后一个键/值匹配(,Json.NET 的JObject.DeepEquals(...)也会返回true。不过,如果您只是序列化 NEST 搜索,则不太可能遇到这种情况,但需要注意。

如果要让许多测试检查序列化,则需要创建单个ConnectionSettings实例并与所有人共享,以便可以利用其中的内部缓存,并且测试的运行速度将比在每个测试中实例化新实例更快。

最新更新