Unit Testing Search Descriptiors
See original GitHub issueNEST/Elasticsearch.Net version: 2.0+
Elasticsearch version: 2.0+
Description of the problem including expected versus actual behavior: Unable to unit test SearchDescriptors
Steps to reproduce: Previously I used to be able to cast search descriptors into SearchRequests and do this:
var query = ((ISearchRequest)searchDescriptor);
Assert.AreEqual(query.Indices.First().Name, "test");
Assert.AreEqual(query.Size, 10);
Assert.AreEqual(query.From, 1);
Assert.AreEqual(query.Query.Filtered.Filter.And.Filters.First().Bool.Must.Skip(1).First().Term.Value, false);
But this now only offers some accessors (Size and From are still members). How can I test the index I am pointing at and also the query?
Issue Analytics
- State:
- Created 7 years ago
- Comments:12 (4 by maintainers)
Top Results From Across the Web
Testing Python descriptors
I'm writing some descriptors to encapsulate data validation and want to write tests for them. I'm wondering if I should test them by...
Read more >Unit Testing: Definition, Examples, and Critical Best Practices
The objective of a unit test is to test an entity in the code, ensure that it is coded correctly with no errors,...
Read more >SQL unit testing best practices
In this article, you will find some suggestions for writing an effective SQL unit test and also we will make a bit of...
Read more >Unit testing fundamentals - Visual Studio (Windows)
Learn how Visual Studio Test Explorer provides a flexible and efficient way to run your unit tests and view their results.
Read more >unittest — Unit testing framework
It supports test automation, sharing of setup and shutdown code for tests, aggregation of tests into collections, and independence of the tests from...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
If you were looking to unit test in a similar way to how you have been, the nearest verbatim way would be
How
Indices
is serialized depends onConnectionSettings
as index names may be inferred from types, and this setup is onConnectionSettings
(in this example they aren’t, but just mentioning anyway).query.Query
implements the visitor pattern so it is possible to pass anIQueryVisitor
intoquery.Query.Accept()
and make your assertions that way, or alternatively, as I have done here, simply assert that the serialized form of the query matches the serialized form of an anonymous type. The latter is probably more readable and easier to maintain than implementing a solution with a visitor and is what we do for our tests.It’s a good idea to serialize to a string then load a
JObject
from the string and compare these withJObject.DeepEquals()
as I’ve done in this example, in order to reduce brittleness arising from the order in which properties are serialized to json when serializing the expected query and the actual query.Taking the “asserting that the serialized form matches our expectation” further, we can just simply assert that the whole query matches an anonymous type
You may still want to check that you’re hitting the right index(es) and type(s), but hopefully this gives you some ideas.
Brilliant answer, thank you very much.