Created
July 3, 2012 10:01
-
-
Save RobertTheGrey/3038836 to your computer and use it in GitHub Desktop.
Query Raven by Index in a unit test
This file contains hidden or 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
public static class DocumentStoreExtensions | |
{ | |
public static void SaveSessionAction(this IDocumentStore documentStore, Action<IDocumentSession> action) | |
{ | |
using (IDocumentSession session = documentStore.OpenSession()) | |
{ | |
action(session); | |
session.SaveChanges(); | |
documentStore.WaitForStaleIndexes(); | |
} | |
} | |
public static void WaitForStaleIndexes(this IDocumentStore documentStore) | |
{ | |
while (documentStore.DatabaseCommands.GetStatistics().StaleIndexes.Length != 0) | |
{ | |
Thread.Sleep(50); | |
} | |
} | |
} |
This file contains hidden or 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
public WhenLoadingTheTagsPage() | |
{ | |
_documentStore = new EmbeddableDocumentStore | |
{ | |
RunInMemory = true, | |
Conventions = new DocumentConvention | |
{ | |
DefaultQueryingConsistency = ConsistencyOptions.QueryYourWrites | |
} | |
}; | |
_documentStore.Initialize(); | |
_documentStore.RegisterListener(new PreventStaleQueries()); | |
IndexCreation.CreateIndexes(typeof(IndexMarker).Assembly, _documentStore); | |
_config = new ConfigurationStore(new TypeMapFactory(), MapperRegistry.AllMappers()); | |
_config.AddProfile<StoryViewModelProfile>(); | |
} | |
[Test] | |
public void ShouldReturnAListOfTagsWithStoryCount() | |
{ | |
Story normalStory = SampleData.GetSampleStory(); | |
Story legacyStory = SampleData.GetSampleLegacyStory(); | |
_documentStore.SaveSessionAction(session => | |
{ | |
session.Store(normalStory); //Contains 1 Tag | |
session.Store(legacyStory); //Contains 1 Tag | |
}); | |
using (var session = _documentStore.OpenSession()) | |
{ | |
var tagsByStoryCount = session | |
.Query<TagsCountByStory.Result, TagsCountByStory>() | |
.OrderBy(x => x.TagName); | |
Console.WriteLine(tagsByStoryCount.Count()); //Output is zero tags found | |
tagsByStoryCount.ShouldNotBeEmpty(); // FAIL: This is empty for some reason | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment