0

我需要对匹配多个属性的文档集合进行查询。
(来自邮件列表的交叉帖子:https ://groups.google.com/forum/?fromgroups=#!topic/ravendb/r5f1zr2jd_o )

这是文件:

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

这是测试:

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

最后一个断言是失败的。我必须承认我不确定这是我的错误还是失败。
据我了解,这应该有效。

PS。我尝试过使用独立的文档存储以及嵌入而不在内存中运行,但结果相同。

4

1 回答 1

2

你得到过时的结果。在单元测试中,您需要留出时间进行索引。

添加.Customize(x=> x.WaitForNonStaleResults())到您的查询中,测试应该通过。

另外,我认为您Id在剪切/粘贴时将属性从问题中排除,因为它不会按原样编译。

更新

根据评论中的讨论,问题在于您将[JsonProperty]属性应用于Id属性。由于该Id属性表示文档键,并且没有作为 JSON 文档的一部分进行序列化,因此您不能将[JsonProperty]属性应用于它。

于 2013-04-05T20:06:12.077 回答