question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unit Testing Search Descriptiors

See original GitHub issue

NEST/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:closed
  • Created 7 years ago
  • Comments:12 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
russcamcommented, Apr 12, 2016

If you were looking to unit test in a similar way to how you have been, the nearest verbatim way would be

void Main()
{
    var searchDescriptor = new SearchDescriptor<Document>()
        .From(1)
        .Size(10)
        .Index("a,b,c")
        .Query(q => q.MatchAll());


    var connectionSettings = new ConnectionSettings();
    var client = new ElasticClient(connectionSettings);

    var query = ((ISearchRequest)searchDescriptor);

    Assert.AreEqual("a,b,c", ((IUrlParameter)query.Index).GetString(client.ConnectionSettings));
    Assert.AreEqual(10, query.Size);
    Assert.AreEqual(1, query.From);

    var expectedQuery = new
    {
        match_all = new {}
    };

    Assert.IsTrue(JObject.DeepEquals(
        JObject.Parse(client.Serializer.SerializeToString(expectedQuery)), 
        JObject.Parse(client.Serializer.SerializeToString(query.Query)))
    );
}

How Indices is serialized depends on ConnectionSettings as index names may be inferred from types, and this setup is on ConnectionSettings (in this example they aren’t, but just mentioning anyway).

query.Query implements the visitor pattern so it is possible to pass an IQueryVisitor into query.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 with JObject.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

var expectedQuery = new
{
    query = new
    {
        match_all = new { }
    },
    from = 1,
    size = 10
};

Assert.IsTrue(JObject.DeepEquals(
    JObject.Parse(client.Serializer.SerializeToString(expectedQuery)), 
    JObject.Parse(client.Serializer.SerializeToString(searchDescriptor)))
);

You may still want to check that you’re hitting the right index(es) and type(s), but hopefully this gives you some ideas.

1reaction
markwalsh-liverpoolcommented, Apr 13, 2016

Brilliant answer, thank you very much.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found