Last active
December 11, 2015 06:09
-
-
Save mattjohnsonpint/4557250 to your computer and use it in GitHub Desktop.
Example for SO Q14368021
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.Linq; | |
using Raven.Client.Indexes; | |
using Raven.Tests.Helpers; | |
using Xunit; | |
namespace RavenTests.StackOverflow | |
{ | |
public class Q14368021 : RavenTestBase | |
{ | |
public abstract class Product | |
{ | |
public int Id { get; set; } | |
public string Name { get; set; } | |
public string Manufacturer { get; set; } | |
} | |
public class ProductA : Product | |
{ | |
public string Foo { get; set; } | |
} | |
public class ProductB : Product | |
{ | |
public string Bar { get; set; } | |
} | |
public class Offer | |
{ | |
public int Id { get; set; } | |
public string SellerUserId { get; set; } | |
public Product Product { get; set; } | |
public decimal Price { get; set; } | |
} | |
public class Products_ByLowestOfferPrice : AbstractIndexCreationTask<Offer, Products_ByLowestOfferPrice.Result> | |
{ | |
public class Result | |
{ | |
public Product Product { get; set; } | |
public decimal Price { get; set; } | |
public string ProductType { get; set; } | |
} | |
public Products_ByLowestOfferPrice() | |
{ | |
Map = offers => from offer in offers | |
select new | |
{ | |
offer.Product, | |
offer.Price, | |
ProductType = AsDocument(offer.Product)["$type"] | |
}; | |
Reduce = results => from result in results | |
group result by result.Product.Id | |
into g | |
select new | |
{ | |
g.First().Product, | |
Price = g.Min(x => x.Price), | |
g.First().ProductType, | |
}; | |
} | |
} | |
[Fact] | |
public void Test() | |
{ | |
using (var documentStore = NewDocumentStore()) | |
{ | |
documentStore.ExecuteIndex(new Products_ByLowestOfferPrice()); | |
using (var session = documentStore.OpenSession()) | |
{ | |
var product1 = new ProductA { Id = 1, Name = "A1", Manufacturer = "XYZ", Foo = "ABC" }; | |
var product2 = new ProductA { Id = 2, Name = "A2", Manufacturer = "XYZ", Foo = "ABC" }; | |
var product3 = new ProductB { Id = 3, Name = "B3", Manufacturer = "XYZ", Bar = "DEF" }; | |
var product4 = new ProductB { Id = 4, Name = "B4", Manufacturer = "XYZ", Bar = "DEF" }; | |
session.Store(product1); | |
session.Store(product2); | |
session.Store(product3); | |
session.Store(product4); | |
session.Store(new Offer { Id = 1, Product = product1, Price = 10 }); | |
session.Store(new Offer { Id = 2, Product = product1, Price = 20 }); | |
session.Store(new Offer { Id = 3, Product = product2, Price = 10 }); | |
session.Store(new Offer { Id = 4, Product = product2, Price = 20 }); | |
session.Store(new Offer { Id = 5, Product = product3, Price = 10 }); | |
session.Store(new Offer { Id = 6, Product = product3, Price = 20 }); | |
session.Store(new Offer { Id = 7, Product = product4, Price = 10 }); | |
session.Store(new Offer { Id = 8, Product = product4, Price = 20 }); | |
session.SaveChanges(); | |
} | |
using (var session = documentStore.OpenSession()) | |
{ | |
var productType = documentStore.Conventions.GetClrTypeName(typeof(ProductA)); | |
var results = session.Query<Products_ByLowestOfferPrice.Result, Products_ByLowestOfferPrice>() | |
.Customize(x => x.WaitForNonStaleResults()) // only for testing purposes | |
.Where(x => x.ProductType == productType) | |
.ToList(); | |
Assert.Equal(2, results.Count); | |
Assert.IsType<ProductA>(results[0].Product); | |
Assert.IsType<ProductA>(results[1].Product); | |
Assert.Equal(1, results[0].Product.Id); | |
Assert.Equal(2, results[1].Product.Id); | |
Assert.Equal(10, results[0].Price); | |
Assert.Equal(10, results[1].Price); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment