Skip to content

Instantly share code, notes, and snippets.

@mattjohnsonpint
Created October 9, 2012 18:50
Show Gist options
  • Select an option

  • Save mattjohnsonpint/3860670 to your computer and use it in GitHub Desktop.

Select an option

Save mattjohnsonpint/3860670 to your computer and use it in GitHub Desktop.
RavenDB Bug
using System.Linq;
using Raven.Client.Embedded;
using Raven.Client.Indexes;
using Xunit;
namespace RavenTests
{
public class Tests
{
[Fact]
public void TestOrderByName()
{
// This test passes.
var documentStore = new EmbeddableDocumentStore { RunInMemory = true };
documentStore.Initialize();
IndexCreation.CreateIndexes(GetType().Assembly, documentStore);
using (var session = documentStore.OpenSession())
{
var foo = new Foo {Id = "foos/1", Names = new[] {"Alice", "Bob"}};
session.Store(foo);
session.SaveChanges();
}
using (var session = documentStore.OpenSession())
{
var results = session.Query<Foos_ByName.Result, Foos_ByName>()
.Customize(x => x.WaitForNonStaleResults())
.OrderBy(x => x.Name)
.ToList();
Assert.NotNull(results);
Assert.Equal(2, results.Count);
Assert.Equal("foos/1", results[0].Id);
Assert.Equal("Alice", results[0].Name);
Assert.Equal("foos/1", results[1].Id);
Assert.Equal("Bob", results[1].Name);
}
}
[Fact]
public void TestOrderById()
{
// This test fails with the following error:
//
// The field '__document_id' is not indexed, cannot sort on fields that are not indexed
//
// It would appear that sort=Id is geting translated to sort=__document_id on the client side.
// If I do the identical query in Raven Studio, sorting by Id, it works fine.
var documentStore = new EmbeddableDocumentStore { RunInMemory = true };
documentStore.Initialize();
IndexCreation.CreateIndexes(GetType().Assembly, documentStore);
using (var session = documentStore.OpenSession())
{
var foo = new Foo { Id = "foos/1", Names = new[] { "Alice", "Bob" } };
session.Store(foo);
session.SaveChanges();
}
using (var session = documentStore.OpenSession())
{
var results = session.Query<Foos_ByName.Result, Foos_ByName>()
.Customize(x => x.WaitForNonStaleResults())
.OrderBy(x => x.Id) // This is the only difference from the passing test
.ToList();
Assert.NotNull(results);
Assert.Equal(2, results.Count);
Assert.Equal("foos/1", results[0].Id);
Assert.Equal("Alice", results[0].Name);
Assert.Equal("foos/1", results[1].Id);
Assert.Equal("Bob", results[1].Name);
}
}
}
public class Foo
{
public string Id { get; set; }
public string[] Names { get; set; }
}
public class Foos_ByName : AbstractIndexCreationTask<Foo, Foos_ByName.Result>
{
public class Result
{
public string Id { get; set; }
public string Name { get; set; }
}
public Foos_ByName()
{
Map = foos => from foo in foos
from name in foo.Names
select new
{
foo.Id,
Name = name
};
// I shouldn't even need this since there's no reduction happening.
// Without it the index does not build properly! Why? Is this a bug, or by design?
Reduce = results => from result in results
group result by result
into g
select new
{
g.Key.Id,
g.Key.Name
};
}
}
}
@mattjohnsonpint

Copy link
Copy Markdown
Author

Correction, I had the wrong grouping in the reduce step (result singular, not results plural). Doesn't change the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment