Monday, May 27, 2013

How to Test RavenDB Indexes

What if you could spin up an entire database in memory for every unit test? You can!

RavenDB offers an EmbeddableDocumentStore NuGet Package that allows you to create a complete in memory instance of RavenDB. This makes writing integration tests for your custom Indexes extremely easy.

The Hibernating Rhinos team makes full use of this feature by including a full suite of unit tests in their RavenDB solution. They even encourage people to submit pull requests to their GitHub repository so that they can pull those tests directly into their source. This is a BRILLIANT integration of all these technologies to both encourage testing and provide an extremely stable product.

So then, how do you test your RavenDB indexes? Great question; let's get into the code!

  1. Define your Document
public class Doc
    public int InternalId { get; set; }
    public string Text { get; set; }
  1. Define your Index
public class Index : AbstractIndexCreationTask<Doc>
    public Index()
        Map = docs => from doc in docs
                      select new
        Analyzers.Add(d => d.Text, "Raven.Extensions.AlphanumericAnalyzer");
  1. Create your EmbeddableDocumentStore
  2. Insert your Index and Documents

In this example I am creating an abstract base class for my unit tests. The GetDocumentStore method provides an EmbeddableDocumentStore that comes pre-initialized with the default RavenDocumentsByEntityName index, your custom index, and a complete set of documents that have already been inserted. The Documents come from an abstract Documents property, which we will see implemented below in step 5.

protected abstract ICollection<TDoc> Documents { get; }
private EmbeddableDocumentStore NewDocumentStore()
    var documentStore = new EmbeddableDocumentStore
        Configuration =
            RunInUnreliableYetFastModeThatIsNotSuitableForProduction = true,
            RunInMemory = true
    // Create Default Index
    var defaultIndex = new RavenDocumentsByEntityName();
    // Create Custom Index
    var customIndex = new TIndex();
    // Insert Documents from Abstract Property
    using (var bulkInsert = documentStore.BulkInsert())
        foreach (var document in Documents)
    return documentStore;
  1. Write your Tests

These tests are testing a custom Alphanumeric analyzer. They will take in a series of lucene queries and assert that they match the correct internal Ids. These documents are being defined by our abstract Documents property from Step 4.

NOTE: Do not forget to include the WaitForNonStaleResults method on your queries, as your index may not be done building the first time you run your tests.

[InlineData(@"Text:Hello",              new[] {0})]
[InlineData(@"Text:file_name",          new[] {2})]
[InlineData(@"Text:name*",              new[] {2, 3})]
[InlineData(@"Text:my AND Text:txt",    new[] {2, 3})]
public void Query(string query, int[] expectedIds)
    int[] actualIds;
    using (var documentStore = NewDocumentStore())
    using (var session = documentStore.OpenSession())
        actualIds = session.Advanced
    Assert.Equal(expectedIds, actualIds);
protected override ICollection<Doc> Documents
        return new[]
                "Hello, world!",
            .Select((t, i) => new Doc
                InternalId = i,
                Text = t
Shout it


No comments:

Post a Comment

Real Time Web Analytics