RavenDB Build 888 - Failing test for Projection with Integer Id

53 views
Skip to first unread message

Maverix

unread,
May 31, 2012, 2:06:30 AM5/31/12
to rav...@googlegroups.com
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<ResultUsers_Emails>().Where(v => v.Query.StartsWith(name)).AsProjection<Result>();
            var contacts = index.Take(5).ToArray();
            if (contacts != null && contacts.Length > 0return 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("jo...@smithy.com");
            user.emails.Add("john....@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 { getset; }
        public string displayName { getset; }
        public List<string> emails { getset; }
    }
 
    public class Result {
        public int Id { getset; }
        public string name { getset; }
        public string email { getset; }
        public string Query { getset; }
    }
 
    public class Users_Emails : AbstractIndexCreationTask<UserInfoResult> {
        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.nameFieldStorage.Yes);
            Stores.Add(x => x.email, FieldStorage.Yes);
        }
    }
 
    public class NonStaleQueryListener : IDocumentQueryListener {
        public void BeforeQueryExecuted(IDocumentQueryCustomization customization) {
            customization.WaitForNonStaleResults();
        }
    } 
}

Maverix

unread,
May 31, 2012, 3:26:33 AM5/31/12
to rav...@googlegroups.com
Seems to work if I omit the Id from the map select.
I'm guessing the RavenDB Client is able to populate the correct ID via inference?

Oren Eini (Ayende Rahien)

unread,
May 31, 2012, 5:23:30 AM5/31/12
to rav...@googlegroups.com
Unrelated, but you do know that you can change:


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);


To:


            IndexCreation.CreateIndexes( typeof(Users_Emails).Assembly,
                store.DatabaseCommands,                 store.Conventions);

Oren Eini (Ayende Rahien)

unread,
May 31, 2012, 5:24:12 AM5/31/12
to rav...@googlegroups.com
 Id = contact.Id,
You are trying to make this into an integer, but this is actually a string on the server side, not an int.

Maverix

unread,
May 31, 2012, 10:16:00 PM5/31/12
to rav...@googlegroups.com
Hi Oren,
 
I'm aware that you can pull indexes out of an assembly, but in my project I have two types of databases: a membership database and tenant databases.
By using the TypeCatalog I can ensure i'm not pulling in the wrong indexes.
 
I also use this pattern for testing to speed up the test run (minimising unwanted index creation)
Reply all
Reply to author
Forward
0 new messages