Skip to content

Instantly share code, notes, and snippets.

@robashton
Created November 4, 2010 11:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robashton/662345 to your computer and use it in GitHub Desktop.
Save robashton/662345 to your computer and use it in GitHub Desktop.
Demonstrating expected? behaviour with projections/ravendb/selectmany
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xunit;
using Raven.Database;
using Raven.Database.Indexing;
using Raven.Client.Indexes;
using Newtonsoft.Json.Linq;
using Raven.Client.Client;
using Raven.Client.Linq;
using System.Threading;
namespace Raven.Tests.Bugs
{
public class QueryResultCountsWithProjections : IDisposable
{
EmbeddableDocumentStore store = null;
public QueryResultCountsWithProjections()
{
store = new EmbeddableDocumentStore()
{
RunInMemory = true
};
store.Initialize();
PopulateDatastore();
}
public void Dispose()
{
store.Dispose();
}
[Fact]
public void WhenNoProjectionIsIssuedDuplicateDocumentsAreSkipped()
{
using (var session = store.OpenSession())
{
var results = session.Query<Blog>("SelectManyIndexWithNoTransformer").ToArray();
Assert.Equal(1, results.Length);
}
}
// FAILS AND RETURNS TWO BECAUSE OF SELECT MANY
[Fact]
public void WhenProjectionIsIssuedAgainstEntityDuplicateDocumentsAreSkipped()
{
using (var session = store.OpenSession())
{
var results = session.Query<Blog>("SelectManyIndexWithNoTransformer")
.Select(x=> new { x.Title, x.Tags })
.ToArray();
Assert.Equal(1, results.Length);
}
}
[Fact]
// FAILS AND RETURNS TWO BECAUSE OF SELECT MANY
public void WhenDynamicQueryInvokedAgainstTagsDuplicateDocumentsAreSkipped()
{
using (var session = store.OpenSession())
{
var results = session.Query<Blog>()
.Customize(x=>x.WaitForNonStaleResults())
.Where(x => x.Tags.Any(y => y.Name.StartsWith("t")))
.Select(x=> new {
x.Title,
x.Tags
})
.ToArray();
Assert.Equal(1, results.Length);
}
}
[Fact]
public void WhenProjectionIsIssuedAgainstEntityWithTransformResultsDuplicateDocumentsAreSkipped()
{
using (var session = store.OpenSession())
{
var results = session.Query<Blog>("SelectManyIndexWithTransformer")
.As<BlogProjection>()
.ToArray();
Assert.Equal(1, results.Length);
}
}
[Fact]
public void WhenProjectionIsIssuedAgainstIndexAndNotEntityDuplicateDocumentsAreNotSkipped()
{
using (var session = store.OpenSession())
{
var results = session.Query<BlogProjection>("SelectManyIndexWithNoTransformer")
.Select(x=> new
{
x.Title,
x.Tags
})
.ToArray();
Assert.Equal(2, results.Length);
}
}
private void PopulateDatastore()
{
store.DatabaseCommands.PutIndex("SelectManyIndexWithNoTransformer",
new IndexDefinition<Blog>()
{
Map = docs => from doc in docs
from tag in doc.Tags
select new
{
doc.Title,
tag.Name
}
}.ToIndexDefinition(store.Conventions));
store.DatabaseCommands.PutIndex("SelectManyIndexWithTransformer",
new IndexDefinition<Blog, Blog>()
{
Map = docs => from doc in docs
from tag in doc.Tags
select new
{
doc.Title,
tag.Name
},
TransformResults = (database, results) => from result in results
select new
{
result.Title,
result.Tags
}
}.ToIndexDefinition(store.Conventions));
using (var session = store.OpenSession())
{
session.Store(new Blog()
{
Title = "1",
Tags = new BlogTag[]{
new BlogTag() { Name = "tagOne" },
new BlogTag() { Name = "tagTwo" }
}
});
session.SaveChanges();
}
while (store.DocumentDatabase.Statistics.StaleIndexes.Length > 0)
{
Thread.Sleep(10);
}
}
public class BlogProjection
{
public string Title { get; set; }
public BlogTag[] Tags { get; set; }
}
public class Blog
{
public string Id { get; set; }
public string Title { get; set; }
public BlogTag[] Tags { get; set; }
}
public class BlogTag
{
public string Name { get; set; }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment