如何编写用于构建Elasticsearch查询的Java单元测试?

huangapple go评论73阅读模式
英文:

How to write java unit tests for building elasticsearch queries?

问题

以下是您要求的翻译部分:

所以我正在为一个构建Elasticsearch查询的类编写Java单元测试(使用Mockito)。当前的做法相当粗糙,每次进行更改时更新测试工作非常繁琐。不幸的是,官方的Elasticsearch Java测试文档似乎已经停更,因此我希望有人能够提供一些建议,关于如何更好地编写我的测试。

源类是一个相当复杂的查询(Query)类,单元测试的主要目标是构建类的实例,然后将其序列化为JsonNode。接着,会遍历JsonNode并进行预期值的断言。这种测试风格似乎有点接近硬编码。以下是一个测试的示例:

@Test
public void toQueryBuilder_initialQuery() {
  // 在这里定义了 queryValue。
  JsonNode result = QueryBuilderSerialization.toJsonNode(new QueryConstraintA(queryValue)
                                             .toQueryBuilder());
  // 断言预期的查询结果
  assertEquals(EXPECTED_MIN, result
            .get("bool")
            .get("filter").get(0)
            .get("function_score")
            .get("min_score")
            .asInt());

  assertEquals(EXPECTED_LOWER_BOUND, countQuery.get(7)
            .get("function_score")
            .get("query")
            .get("nested")
            .get("query")
            .get("constant_score")
            .get("filter")
            .get("bool")
            .get("filter").get(0)
            .get("bool")
            .get("filter").get(0)
            .get("bool")
            .get("should").get(0)
            .get("range")
            .get(RANGE_FIELD_NAME)
            .get("from")
            .asInt());
}

仅在这个文件中,大约有1000行以这种风格编写的测试代码。因此,当您修改源查询时,可以想象会有多么繁琐的工作。是否有人对如何更好地完成这项工作有建议?提前感谢您的任何付出的时间。

英文:

So I'm writing java unit tests (mockito) for a class that builds elasticsearch queries. The current way it's being done is quite sloppy, it's such tedious work to update the tests whenever a change is made. Unfortunately, it appears the official ES documentation for java testing is dead, so I'm hoping someone out there has some advice as to how my tests can be better written.

The source class is a rather complex Query, and the unit tests are essentially building an instance of the class and serializing it to a JsonNode. Then the JsonNode is traversed and the expected value is asserted. It seems this style of testing is borderline hardcode. Here's an example of a test:

@Test
public void toQueryBuilder_initialQuery() {
  // queryValue defined here.
  JsonNode result = QueryBuilderSerialization.toJsonNode(new QueryConstraintA(queryValue)
                                             .toQueryBuilder());
  // assert expected queries
  assertEquals(EXPECTED_MIN, result
            .get("bool")
            .get("filter").get(0)
            .get("function_score")
            .get("min_score")
            .asInt());

  assertEquals(EXPECTED_LOWER_BOUND, countQuery.get(7)
            .get("function_score")
            .get("query")
            .get("nested")
            .get("query")
            .get("constant_score")
            .get("filter")
            .get("bool")
            .get("filter").get(0)
            .get("bool")
            .get("filter").get(0)
            .get("bool")
            .get("should").get(0)
            .get("range")
            .get(RANGE_FIELD_NAME)
            .get("from")
            .asInt());
}

There's roughly 1000 lines of tests written in this style, in just this file alone. So you can imagine how much of a pain it is when you modify the source query. Does anyone have a suggestion as to how this can be better done? Thanks in advance for any time put into this.

答案1

得分: 2

我不建议编写这些类型的测试。与其测试查询是否看起来正常,您真正想测试的是在运行时它是否返回了正确的输出。这需要更多的工作,但在我们的团队中,我们会针对 Elasticsearch 的本地 Docker 实例运行实际查询。这可以检查查询是否格式错误,并且它们确实按预期执行。这样做还可以使查询更具有韧性,以防有人重构查询(以提高延迟),而不更改任何逻辑。

英文:

I do not recommend writing these kind of tests. Instead of testing that the query looks OK, what you really want to test is that it returns a correct output when it runs. It's a lot more work, but in our shop we run the actual queries against a local Docker instance of Elasticsearch. This checks that the queries aren't malformed and that they really do what they are supposed to. It also makes them more resilient when someone refactors the query (to improve latency), without making any changes to the logic.

huangapple
  • 本文由 发表于 2020年10月8日 01:35:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/64249393.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定