Skip to content

Instantly share code, notes, and snippets.

@mattjohnsonpint
Created October 10, 2012 19:39
Show Gist options
  • Save mattjohnsonpint/3867917 to your computer and use it in GitHub Desktop.
Save mattjohnsonpint/3867917 to your computer and use it in GitHub Desktop.
RavenDB Bug with skipping empty arrays in reduce projection
using System.Linq;
using Raven.Client.Embedded;
using Raven.Client.Indexes;
using Xunit;
namespace RavenTests
{
public class Tests
{
[Fact]
public void Test()
{
/*
This test fails on the last assertion.
results[1].Markings should be an empty array of strings, but instead it's null.
After running the test, query the index in raven studio and you'll see the following projections:
{ {
"Color": "Blue", "Color": "Red",
"Markings": [ "Count": "2"
"A", }
"B",
"C",
"D"
],
"Count": "2"
}
I would expect to see an empty array in the second projection, but the entire field is missing.
They should look like this instead:
{ {
"Color": "Blue", "Color": "Blue",
"Markings": [ "Markings": [
"A", ],
"B", "Count": "2"
"C", }
"D"
],
"Count": "2"
}
*/
var documentStore = new EmbeddableDocumentStore { RunInMemory = true };
documentStore.Initialize();
IndexCreation.CreateIndexes(GetType().Assembly, documentStore);
using (var session = documentStore.OpenSession())
{
session.Store(new Ball { Id = "balls/1", Color = "Blue", Markings = new[] { "A", "B" } });
session.Store(new Ball { Id = "balls/2", Color = "Blue", Markings = new[] { "C", "D" } });
session.Store(new Ball { Id = "balls/3", Color = "Red", Markings = new string[] { } });
session.Store(new Ball { Id = "balls/4", Color = "Red", Markings = new string[] { } });
session.SaveChanges();
}
using (var session = documentStore.OpenSession())
{
var results = session.Query<Balls_ByColor.Result, Balls_ByColor>()
.Customize(x => x.WaitForNonStaleResults())
.OrderBy(x => x.Color)
.ToList();
Assert.NotNull(results);
Assert.Equal(2, results.Count);
Assert.Equal("Blue", results[0].Color);
Assert.Equal(2, results[0].Count);
Assert.Equal(new[] { "A", "B", "C", "D" }, results[0].Markings);
Assert.Equal("Red", results[1].Color);
Assert.Equal(2, results[1].Count);
Assert.Equal(new string[] { }, results[1].Markings);
}
}
}
public class Ball
{
public string Id { get; set; }
public string Color { get; set; }
public string[] Markings { get; set; }
}
public class Balls_ByColor : AbstractIndexCreationTask<Ball, Balls_ByColor.Result>
{
public class Result
{
public string Color { get; set; }
public string[] Markings { get; set; }
public int Count { get; set; }
}
public Balls_ByColor()
{
Map = balls => from ball in balls
select new
{
ball.Color,
ball.Markings,
Count = 1
};
Reduce = results => from result in results
group result by result.Color
into g
select new
{
Color = g.Key,
Markings = g.Select(x => x.Markings),
Count = g.Sum(x=> x.Count)
};
}
}
}
@mattjohnsonpint
Copy link
Author

Correction, I was calculating the Count wrong in the first revision.

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