Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@pdegenhardt
Last active December 11, 2015 12:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pdegenhardt/4601829 to your computer and use it in GitHub Desktop.
Save pdegenhardt/4601829 to your computer and use it in GitHub Desktop.
RavenDB Transform not able to access dictionary property
using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Raven.Client.Embedded;
using Raven.Client.Indexes;
using Raven.Client.Linq;
using Raven.Client;
using System.Linq;
using Raven.Abstractions.Indexing;
namespace RavenTest
{
[TestClass]
public class DictionaryTests
{
Product[] sampleData = new []
{
new Product
{
Id = "products/1",
Name = "A",
Specials = new Dictionary<SpecialType,string>
{
{ SpecialType.Something1, "A1" }
},
DefaultOffer=SpecialType.Something1
},
new Product
{
Id = "products/2",
Name = "B",
Specials = new Dictionary<SpecialType, string>
{
{ SpecialType.Something2, "B2" }
},
DefaultOffer = SpecialType.Something2
},
new Product
{
Id = "products/3",
Name = "C",
Specials = new Dictionary<SpecialType, string> { },
DefaultOffer = SpecialType.None
}
};
[TestMethod]
public void ProjectViaTransform()
{
using (var documentStore = this.GetDocumentStore())
{
documentStore.ExecuteIndex(new ProductSummariesViaTransform());
// Now query and trasnform
using (var session = documentStore.OpenSession())
{
var result = session.Query<Product, ProductSummariesViaTransform>()
.Customize(x => x.WaitForNonStaleResults())
.AsProjection<ProductSummary>()
.ToList();
Assert.AreEqual(3, result.Count);
Assert.AreEqual("B2", result.First(x => x.Id == "products/2").SpecialOffer);
Assert.AreEqual("B", result.First(x => x.Id == "products/2").Name);
Assert.IsNotNull(result.FirstOrDefault(x => x.SpecialOffer == null));
}
}
}
private EmbeddableDocumentStore GetDocumentStore()
{
var documentStore = new EmbeddableDocumentStore { RunInMemory = true };
documentStore.Initialize();
// Store some documents
using (var session = documentStore.OpenSession())
{
foreach (var p in sampleData)
session.Store(p);
session.SaveChanges();
}
return documentStore;
}
public class ProductSummariesViaTransform : AbstractIndexCreationTask<Product>
{
public ProductSummariesViaTransform()
{
Map = products => from p in products
select new { p.Name };
TransformResults = (db, products) =>
from product in products
let p = db.Load<Product>(product.Id)
select new
{
Id = product.Id,
Name = product.Name,
SpecialOffer = p.Specials[p.DefaultOffer]
};
}
}
public class Product
{
public string Id { get; set; }
public string Name { get; set; }
public SpecialType DefaultOffer { get; set; }
public Dictionary<SpecialType, string> Specials { get; set; }
}
public class ProductSummary
{
public string Id { get; set; }
public string Name { get; set; }
public string SpecialOffer { get; set; }
}
public enum SpecialType
{
None = 0,
Something1,
Something2
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment