Created
November 4, 2010 11:19
-
-
Save robashton/662343 to your computer and use it in GitHub Desktop.
Demoing the expected? behaviour with projections + select many
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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