Using InMemoryConnection to test ElasticSearch

百般思念 提交于 2019-11-28 12:43:21
Russ Cam

InMemoryConnection doesn't actually send any requests or receive any responses from Elasticsearch; used in conjunction with .SetConnectionStatusHandler() on Connection settings (or .OnRequestCompleted() in NEST 2.x+), it's a convenient way to see the serialized form of requests.

When not using InMemoryConnection but still setting .SetConnectionStatusHandler() or .OnRequestCompleted(), depending on NEST version, it's a convenient way to also see the responses, when .ExposeRawResponse(true) is also set in NEST 1.x, or .DisableDirectStreaming() is set in NEST 2.x+, respectively.

An example with NEST 1.x

void Main()
{
    var settings = new ConnectionSettings(new Uri("http://localhost:9200"))
        .ExposeRawResponse(true)
        .PrettyJson()
        .SetDefaultIndex("myIndexName")
        .MapDefaultTypeNames(d => d.Add(typeof(myPoco), string.Empty))
        .SetConnectionStatusHandler(r =>
        {
            // log out the requests
            if (r.Request != null)
            {
                Console.WriteLine("{0} {1} \n{2}\n", r.RequestMethod.ToUpperInvariant(), r.RequestUrl,
                    Encoding.UTF8.GetString(r.Request));
            }
            else
            {
                Console.WriteLine("{0} {1}\n", r.RequestMethod.ToUpperInvariant(), r.RequestUrl);
            }

            Console.WriteLine();

            if (r.ResponseRaw != null)
            {
                Console.WriteLine("Status: {0}\n{1}\n\n{2}\n", r.HttpStatusCode, Encoding.UTF8.GetString(r.ResponseRaw), new String('-', 30));
            }
            else
            {
                Console.WriteLine("Status: {0}\n\n{1}\n", r.HttpStatusCode, new String('-', 30));
            }
        });

    var client = new ElasticClient(settings, new InMemoryConnection());

    int skipCount = 0;
    int takeSize = 100;

    var searchResults = client.Search<myPoco>(x => x
        .Query(q => q
        .Bool(b => b
        .Must(m =>
            m.Match(mt1 => mt1.OnField(f1 => f1.status).Query("New")))))
        .Skip(skipCount)
        .Take(takeSize)
    );
}

public class myPoco
{
    public string status { get; set;}
}

yields

POST http://localhost:9200/myIndexName/_search?pretty=true 
{
  "from": 0,
  "size": 100,
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "status": {
              "query": "New"
            }
          }
        }
      ]
    }
  }
}


Status: 0
{ "USING NEST IN MEMORY CONNECTION"  : null }

------------------------------

And for NEST 2.x

void Main()
{
    var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
    var defaultIndex = "default-index";
    var connectionSettings = new ConnectionSettings(pool, new InMemoryConnection())
            .DefaultIndex(defaultIndex)
            .PrettyJson()
            .DisableDirectStreaming()
            .OnRequestCompleted(response =>
                {
                    // log out the request
                    if (response.RequestBodyInBytes != null)
                    {
                        Console.WriteLine(
                            $"{response.HttpMethod} {response.Uri} \n" +
                            $"{Encoding.UTF8.GetString(response.RequestBodyInBytes)}");
                    }
                    else
                    {
                        Console.WriteLine($"{response.HttpMethod} {response.Uri}");
                    }

                    Console.WriteLine();

                    // log out the response
                    if (response.ResponseBodyInBytes != null)
                    {
                        Console.WriteLine($"Status: {response.HttpStatusCode}\n" +
                                 $"{Encoding.UTF8.GetString(response.ResponseBodyInBytes)}\n" +
                                 $"{new string('-', 30)}\n");
                    }
                    else
                    {
                        Console.WriteLine($"Status: {response.HttpStatusCode}\n" +
                                 $"{new string('-', 30)}\n");
                    }
                });

    var client = new ElasticClient(connectionSettings);

    int skipCount = 0;
    int takeSize = 100;

    var searchResults = client.Search<myPoco>(x => x
        .Query(q => q
        .Bool(b => b
        .Must(m =>
            m.Match(mt1 => mt1.Field(f1 => f1.status).Query("New")))))
        .Skip(skipCount)
        .Take(takeSize)
    );
}

You can of course mock/stub responses from the client using your favourite mocking framework and depending on the client interface, IElasticClient, if that's a route you want to take, although asserting the serialized form of a request matches your expectations in the SUT may be sufficient.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!