azzlack
azzlack

Reputation: 1191

Querying for RavenDB documents using multiple properties

I need to make a query against a document collection that matches several properties.
(Cross post from the mailing list: https://groups.google.com/forum/?fromgroups=#!topic/ravendb/r5f1zr2jd_o)

Here is the document:

public class SessionToken
{
    [JsonProperty("jti")]
    public string Id { get; set; }

    [JsonProperty("aud")]
    public Uri Audience { get; set; }

    [JsonProperty("sub")]
    public string Subject { get; set; }

    [JsonProperty("claims")]
    public Dictionary<string, string> Claims { get; set; }
}

And here is the test:

[TestFixture] 
public class RavenDbTests 
{ 
    private IDocumentStore documentStore; 

    [SetUp]   
    public void SetUp() 
    { 
        this.documentStore = new EmbeddableDocumentStore() { RunInMemory = true }; 
        this.documentStore.Initialize(); 
    } 

    [Test] 
    public async void FirstOrDefault_WhenSessionTokenExists_ShouldReturnSessionToken() 
    { 
        var c = new SessionToken() 
                { 
                    Audience = new Uri("http://localhost"), 
                    Subject = "NUnit", 
                    Claims = new Dictionary<string, string>() 
                                { 
                                    { ClaimTypes.System, "NUnit" } 
                                } 
                }; 

        using (var session = this.documentStore.OpenAsyncSession())
        {
            await session.StoreAsync(c); 
            await session.SaveChangesAsync(); 

            // Check if the token exists in the database without using Where clause 
            var allTokens = await session.Query<SessionToken>().ToListAsync(); 
            Assert.That(allTokens.Any(x => x.Subject == "NUnit" && x.Audience == new Uri("http://localhost"))); 

            // Try getting token back with Where clause
            var token = await session.Query<SessionToken>().Customize(x => x.WaitForNonStaleResults()).Where(x => x.Subject == "NUnit" && x.Audience == new Uri("http://localhost")).ToListAsync(); 
            Assert.IsNotNullOrEmpty(token.First().Id); 
        } 
    } 
}

The last Assert is the one that is failing. I must admit Im not sure whether this is a bug or a failure on my part.
As far as I understand, this is supposed to work.

PS. I´ve tried with a standalone document store as well as embedded without running in memory, but with same result.

Upvotes: 0

Views: 245

Answers (1)

Matt Johnson-Pint
Matt Johnson-Pint

Reputation: 241450

You are getting stale results. In a unit test, you need to allow time for indexing to occur.

Add .Customize(x=> x.WaitForNonStaleResults()) to your queries and the test should pass.

Also, I think you left the Id property off your question when you cut/paste because it doesn't compile as-is.

UPDATE

Per discussion in comments, the issue was that you were applying the [JsonProperty] attribute to the Id property. Since the Id property represents the document key, and is not serialized as part of the JSON document, you can't apply the [JsonProperty] attribute to it.

Upvotes: 2

Related Questions