On Thu, May 31, 2012 at 9:06 AM, Maverix <ajha
...@gmail.com> wrote:
> I'm not sure if this is 'by design' but here is the failing test anyway.
> using System.Collections.Generic;using System.ComponentModel.Composition.Hosting;using System.Linq;using Microsoft.VisualStudio.TestTools.UnitTesting;using Raven.Abstractions.Indexing;using Raven.Client;using Raven.Client.Document;using Raven.Client.Indexes;using Raven.Client.Linq;using Raven.Client.Listeners;using Raven.Client.Embedded;
> namespace SLC.CoreTests
> {
> [TestClass]
> public class SearchFailingTest
> {
> public static DocumentStore Store() {
> DocumentStore store = new EmbeddableDocumentStore {
> RunInMemory = true,
> UseEmbeddedHttpServer = true,
> Conventions = { DefaultQueryingConsistency = ConsistencyOptions.QueryYourWrites }
> };
> store.Initialize();
> store.RegisterListener(new NonStaleQueryListener());
> var indexes = (from type in typeof(Users_Emails).Assembly.GetTypes()
> where type.IsSubclassOf(typeof(AbstractIndexCreationTask))
> select type).ToArray();
> IndexCreation.CreateIndexes(
> new CompositionContainer(new TypeCatalog(indexes)),
> store.DatabaseCommands,
> store.Conventions);
> return store;
> }
> public Result[] Lookup(IDocumentSession session, string name, bool first = true) {
> var index = session.Query<Result, Users_Emails>().Where(v => v.Query.StartsWith(name)).AsProjection<Result>();
> var contacts = index.Take(5).ToArray();
> if (contacts != null && contacts.Length > 0) return contacts;
> if (first) {
> var suggestions = index.Suggest();
> if (suggestions.Suggestions != null && suggestions.Suggestions.Length > 0)
> return Lookup(session, suggestions.Suggestions.FirstOrDefault(), false);
> }
> return new Result[0];
> }
> [TestMethod]
> public void TestMethod() {
> UserInfo user = new UserInfo { displayName = "John Smith" };
> user.emails.Add("j...@smithy.com");
> user.emails.Add("john.sm...@workplace.com");
> using (var store = Store()) {
> using (var session = store.OpenSession()) {
> session.Store(user);
> session.SaveChanges();
> }
> using (var session = store.OpenSession()) {
> var results = Lookup(session, "john");
> Assert.IsNotNull(results);
> }
> }
> }
> }
> public class UserInfo {
> public UserInfo() { emails = new List<string>(); }
> public int Id { get; set; }
> public string displayName { get; set; }
> public List<string> emails { get; set; }
> }
> public class Result {
> public int Id { get; set; }
> public string name { get; set; }
> public string email { get; set; }
> public string Query { get; set; }
> }
> public class Users_Emails : AbstractIndexCreationTask<UserInfo, Result> {
> public Users_Emails() {
> Map = contacts => from contact in contacts
> where contact.emails != null && contact.emails.Count > 0
> from email in contact.emails
> select new {
> Id = contact.Id,
> name = contact.displayName,
> email = email,
> Query = new object[] {
> contact.displayName,
> }
> };
> Indexes.Add(x => x.Query, FieldIndexing.Analyzed);
> Stores.Add(x => x.Id, FieldStorage.Yes);
> Stores.Add(x => x.name, FieldStorage.Yes);
> Stores.Add(x => x.email, FieldStorage.Yes);
> }
> }
> public class NonStaleQueryListener : IDocumentQueryListener {
> public void BeforeQueryExecuted(IDocumentQueryCustomization customization) {
> customization.WaitForNonStaleResults();
> }
> }
> }