Last active
August 29, 2015 14:08
-
-
Save anemitoff/1be2a9f5743920dace98 to your computer and use it in GitHub Desktop.
RavenDB Include by Dictionary Key or Values for Load Failing
This file contains 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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using Raven.Client; | |
using Raven.Tests.Helpers; | |
using Xunit; | |
namespace Tests | |
{ | |
public class DictionaryIncludeTest : RavenTestBase | |
{ | |
public class Foo | |
{ | |
public string Id { get; set; } | |
public Dictionary<string, string> BarIdInKey { get; set; } | |
public Dictionary<string, string> BarIdInValue { get; set; } | |
protected Foo() { } | |
public Foo(Bar[] bars) | |
{ | |
BarIdInKey = bars.ToDictionary(b => b.Id, b=>"Some Value"); | |
BarIdInValue = bars.ToDictionary(b => "Some Key", b => b.Id); | |
} | |
} | |
public class Bar | |
{ | |
public Bar() { } | |
public string Id { get; set; } | |
public string Title { get; set; } | |
} | |
private dynamic MakeAndStoreEntities(IDocumentStore db) | |
{ | |
using (var session = db.OpenSession()) | |
{ | |
var bar = new Bar(); | |
session.Store(bar); | |
var foo = new Foo(new[] {bar}); | |
session.Store(foo); | |
session.SaveChanges(); | |
return new {foo, bar}; | |
} | |
} | |
[Fact] | |
public void Can_include_dictionary_key() | |
{ | |
var db = NewDocumentStore(); db.Initialize(); | |
var entities = MakeAndStoreEntities(db); | |
var session = db.OpenSession(); | |
var loaded = session.Include<Foo>(f => f.BarIdInKey.Keys).Load<Foo>(entities.foo.Id); | |
Assert.NotNull(loaded); | |
var bar = session.Load<Bar>(entities.bar.Id); | |
Assert.NotNull(bar); | |
// The following fails because NumberOfRequests is 2 | |
Assert.Equal(1, session.Advanced.NumberOfRequests); | |
} | |
[Fact] | |
public void Can_include_dictionary_value() | |
{ | |
var db = NewDocumentStore(); db.Initialize(); | |
var entities = MakeAndStoreEntities(db); | |
var session = db.OpenSession(); | |
var foo = session.Include<Foo>(f => f.BarIdInValue.Values.Select(x=>x)).Load<Foo>(entities.foo.Id); | |
Assert.NotNull(foo); | |
var bar = session.Load<Bar>(entities.bar.Id); | |
Assert.NotNull(bar); | |
// The following fails because NumberOfRequests is 2 | |
Assert.Equal(1, session.Advanced.NumberOfRequests); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment