Created
January 28, 2015 18:00
-
-
Save di97mni/668efbfb4e4796192f57 to your computer and use it in GitHub Desktop.
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.Collections.Generic; | |
using System.Linq; | |
using System.Threading.Tasks; | |
using Raven.Abstractions.Indexing; | |
using Raven.Client; | |
using Raven.Client.Indexes; | |
using Raven.Tests.Helpers; | |
using Xunit; | |
namespace Pagination | |
{ | |
public class IndexTest : RavenTestBase | |
{ | |
[Fact] | |
public async Task CanIndexAndQuery() | |
{ | |
using (var store = NewDocumentStore()) | |
{ | |
await new SampleData_Index().ExecuteAsync(store.AsyncDatabaseCommands, store.Conventions); | |
await new SampleTransformer().ExecuteAsync(store.AsyncDatabaseCommands, store.Conventions); | |
using (var session = store.OpenAsyncSession()) | |
{ | |
for (int i = 0; i < 10; i++) | |
{ | |
await session.StoreAsync(new SampleData | |
{ | |
Children = new List<Child> | |
{ | |
new Child {FirstName = "FirstA" + i, LastName = "LastA" + i}, | |
new Child {FirstName = "FirstB" + i, LastName = "LastB" + i}, | |
new Child {FirstName = "FirstC" + i, LastName = "LastC" + i} | |
} | |
}); | |
} | |
await session.SaveChangesAsync(); | |
} | |
using (var session = store.OpenAsyncSession()) | |
{ | |
RavenQueryStatistics stats; | |
var result = await session.Query<SampleData, SampleData_Index>() | |
.TransformWith<SampleTransformer, SampleViewModel>() | |
.Statistics(out stats) | |
.Customize(customization => customization.WaitForNonStaleResultsAsOfNow()) | |
.Skip(3) | |
.Take(4) | |
.ToListAsync(); | |
Assert.Equal(4, result.Count); | |
} | |
} | |
} | |
} | |
public class SampleViewModel | |
{ | |
public string Name { get; set; } | |
public override string ToString() | |
{ | |
return string.Format("Name: {0}", Name); | |
} | |
} | |
public class SampleTransformer : AbstractTransformerCreationTask<SampleData> | |
{ | |
public SampleTransformer() | |
{ | |
TransformResults = items => from item in items | |
from child in item.Children | |
select new | |
{ | |
Name = string.Format("{0} {1}", child.FirstName, child.LastName) | |
}; | |
} | |
} | |
public class SampleData | |
{ | |
public IEnumerable<Child> Children { get; set; } | |
} | |
public class Child | |
{ | |
public string FirstName { get; set; } | |
public string LastName { get; set; } | |
} | |
public class SampleData_Index : AbstractIndexCreationTask<SampleData> | |
{ | |
public SampleData_Index() | |
{ | |
Map = docs => from doc in docs | |
from child in doc.Children | |
select new | |
{ | |
child.LastName, | |
child.FirstName | |
}; | |
StoreAllFields(FieldStorage.Yes); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment