Last active
December 14, 2015 04:18
-
-
Save afifmohammed/5026962 to your computer and use it in GitHub Desktop.
test for a spatial query over a multi map reduce index
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
namespace MultiMapSpatialTests | |
{ | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using NUnit.Framework; | |
using Raven.Client.Embedded; | |
using Raven.Client.Indexes; | |
[Serializable] | |
public class Deal | |
{ | |
public string Id { get; set; } | |
public List<Location> Locations { get; set; } | |
public List<Product> Products { get; set; } | |
public List<Schedule> Schedules { get; set; } | |
public class Location | |
{ | |
public double Lat { get; set; } | |
public double Lng { get; set; } | |
} | |
public class Product | |
{ | |
public string Id { get; set; } | |
public decimal Price { get; set; } | |
} | |
public class Schedule | |
{ | |
public DateTime From { get; set; } | |
public DateTime Till { get; set; } | |
} | |
} | |
[Serializable] | |
public class DealOrders | |
{ | |
public string Id { get; set; } | |
public string DealId { get; set; } | |
public int Sold { get; set; } | |
} | |
[Serializable] | |
public class DealStock | |
{ | |
public string Id { get; set; } | |
public string DealId { get; set; } | |
public int AllocatedStock { get; set; } | |
} | |
class Deals_ByStockAndSold : AbstractMultiMapIndexCreationTask<Deals_ByStockAndSold.Result> | |
{ | |
public class Result | |
{ | |
public string DealId { get; set; } | |
public decimal Price { get; set; } | |
public int TotalPurchases { get; set; } | |
public int Allocated { get; set; } | |
public DateTime From { get; set; } | |
public DateTime Till { get; set; } | |
public Loc[] Locations { get; set; } | |
public class Loc | |
{ | |
public double Lat { get; set; } | |
public double Lng { get; set; } | |
} | |
} | |
public Deals_ByStockAndSold() | |
{ | |
AddMap<Deal>(deals => from deal in deals | |
from sch in deal.Schedules | |
select new | |
{ | |
DealId = deal.Id, | |
Price = deal.Products.Min(x => x.Price), | |
TotalPurchases = 0, | |
Allocated = 0, | |
sch.From, | |
sch.Till, | |
Locations = deal.Locations.Select(x => new { x.Lat, x.Lng}), | |
_ = (object)null | |
}); | |
AddMap<DealStock>(stocks => from stock in stocks | |
select new | |
{ | |
stock.DealId, | |
Price = (decimal)0, | |
TotalPurchases = 0, | |
Allocated = stock.AllocatedStock, | |
From = DateTime.MaxValue, | |
Till = DateTime.MinValue, | |
Locations = new[] { new { Lat = (double)-99999, Lng = (double)-99999 } }, | |
_ = (object)null | |
}); | |
AddMap<DealOrders>(orders => from order in orders | |
select new | |
{ | |
order.DealId, | |
Price = (decimal)0, | |
TotalPurchases = order.Sold, | |
Allocated = 0, | |
From = DateTime.MaxValue, | |
Till = DateTime.MinValue, | |
Locations = new[] { new { Lat = (double)-99999, Lng = (double)-99999 } }, | |
_ = (object)null | |
}); | |
Reduce = results => from r in results | |
group r by r.DealId | |
into dealmap | |
from deal in dealmap | |
select new | |
{ | |
DealId = dealmap.Key, | |
Price = dealmap.Sum(x => x.Price), | |
TotalPurchases = dealmap.Sum(x => x.TotalPurchases), | |
Allocated = dealmap.Sum(x => x.Allocated), | |
deal.From, | |
deal.Till, | |
Locations = dealmap.SelectMany(x => x.Locations), | |
_ = dealmap.SelectMany(x => x.Locations).Select(x => SpatialIndex.Generate(x.Lat, x.Lng)) | |
}; | |
} | |
} | |
[TestFixture] | |
class Class1 | |
{ | |
[Test] | |
public void IndexShouldWork() | |
{ | |
using (var d = new EmbeddableDocumentStore {RunInMemory = true, UseEmbeddedHttpServer = true}.Initialize()) | |
{ | |
d.ExecuteIndex(new Deals_ByStockAndSold()); | |
var errors = ((EmbeddableDocumentStore) d).DocumentDatabase.Statistics.Errors; | |
Assert.That(errors, Is.Empty); | |
using (var s = d.OpenSession()) | |
{ | |
s.Store(new Deal | |
{ | |
Id = "deals/1", | |
Locations = new List<Deal.Location> { new Deal.Location { Lat = -37.8232, Lng = 144.9729 } }, | |
Products = new List<Deal.Product> { new Deal.Product { Id = "products/1", Price = 10}}, | |
Schedules = new List<Deal.Schedule> | |
{ | |
new Deal.Schedule { From = DateTime.Now.Date.AddDays(-1).AddHours(12), Till = DateTime.Now.Date.AddDays(1).AddHours(12)}, | |
new Deal.Schedule { From = DateTime.Now.Date.AddDays(1).AddHours(12), Till = DateTime.Now.Date.AddDays(2).AddHours(12)} | |
} | |
}); | |
s.Store(new DealOrders { Id = "dealorders/deals/1", DealId = "deals/1", Sold = 1 }); | |
s.Store(new DealStock { Id = "dealstocks/deals/1", DealId = "deals/1", AllocatedStock = 10 }); | |
s.Store(new Deal | |
{ | |
Id = "deals/2", | |
Locations = new List<Deal.Location> { new Deal.Location { Lat = -31.9428, Lng = 115.8439 } }, | |
Products = new List<Deal.Product> { new Deal.Product { Id = "products/1", Price = 10 } }, | |
Schedules = new List<Deal.Schedule> | |
{ | |
new Deal.Schedule { From = DateTime.Now.Date.AddDays(-1).AddHours(12), Till = DateTime.Now.Date.AddDays(1).AddHours(12)}, | |
new Deal.Schedule { From = DateTime.Now.Date.AddDays(1).AddHours(12), Till = DateTime.Now.Date.AddDays(2).AddHours(12)} | |
} | |
}); | |
s.Store(new DealOrders { Id = "dealorders/deals/2", DealId = "deals/2", Sold = 4 }); | |
s.Store(new DealStock { Id = "dealstocks/deals/2", DealId = "deals/2", AllocatedStock = 15}); | |
s.SaveChanges(); | |
var _ = s.Query<Deals_ByStockAndSold.Result, Deals_ByStockAndSold>() | |
.Customize(x => x.WaitForNonStaleResults()) | |
.Where(x => x.From >= DateTime.Now.AddDays(-1)) | |
.Where(x => x.Till > DateTime.Now.AddDays(1)) | |
.ToList(); | |
} | |
errors = ((EmbeddableDocumentStore)d).DocumentDatabase.Statistics.Errors; | |
Assert.That(errors, Is.Empty); | |
using (var s = d.OpenSession()) | |
{ | |
var deals = s.Advanced.LuceneQuery<Deals_ByStockAndSold.Result, Deals_ByStockAndSold>() | |
.WithinRadiusOf(radius: 50, latitude: -37.8167, longitude: 145.1333) // melbourne east | |
.SortByDistance() | |
.ToList(); | |
Assert.That(deals, Is.Not.Empty); | |
Assert.That(deals.First().DealId, Is.EqualTo("deals/1")); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment