-
-
Save tzarger/4320197 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
{ | |
public class TestClass : RavenTestBase | |
{ | |
[Fact] | |
public void Test_Average_BAD() | |
{ | |
var documentStore = NewDocumentStore(); | |
documentStore.Initialize(); | |
documentStore.ExecuteIndex(new Foos_AverageValue_BAD()); | |
decimal totalValue = 0; | |
int numValues = 0; | |
using (var session = documentStore.OpenSession()) | |
{ | |
for (int i = 0; i < 1000; i++) | |
{ | |
session.Store(new Foo { Value = i }); | |
totalValue += i; | |
numValues++; | |
} | |
session.SaveChanges(); | |
} | |
using (var session = documentStore.OpenSession()) | |
{ | |
var result = session.Query<Foos_AverageValue_BAD.Result, Foos_AverageValue_BAD>() | |
.Customize(x => x.WaitForNonStaleResults()) | |
.First(); | |
var average = totalValue / numValues; | |
Assert.Equal(average, result.Average); | |
} | |
} | |
[Fact] | |
public void Test_Average_GOOD() | |
{ | |
var documentStore = NewDocumentStore(); | |
documentStore.Initialize(); | |
documentStore.ExecuteIndex(new Foos_AverageValue_GOOD()); | |
decimal totalValue = 0; | |
int numValues = 0; | |
using (var session = documentStore.OpenSession()) | |
{ | |
for (int i = 0; i < 1000; i++) | |
{ | |
session.Store(new Foo { Value = i }); | |
totalValue += i; | |
numValues++; | |
} | |
session.SaveChanges(); | |
} | |
using (var session = documentStore.OpenSession()) | |
{ | |
var result = session.Query<Foos_AverageValue_GOOD.Result, Foos_AverageValue_GOOD>() | |
.Customize(x => x.WaitForNonStaleResults()) | |
.First(); | |
var average = totalValue / numValues; | |
Assert.Equal(average, result.Average); | |
} | |
} | |
public class Foo | |
{ | |
public string Id { get; set; } | |
public decimal Value { get; set; } | |
} | |
public class Foos_AverageValue_BAD : AbstractIndexCreationTask<Foo, Foos_AverageValue_BAD.Result> | |
{ | |
public class Result | |
{ | |
public decimal Average { get; set; } | |
} | |
public Foos_AverageValue_BAD() | |
{ | |
Map = foos => from foo in foos | |
select new | |
{ | |
Average = foo.Value | |
}; | |
Reduce = results => from result in results | |
group result by 0 | |
into g | |
select new | |
{ | |
Average = g.Average(x => x.Average), | |
}; | |
} | |
} | |
public class Foos_AverageValue_GOOD : AbstractIndexCreationTask<Foo, Foos_AverageValue_GOOD.Result> | |
{ | |
public class Result | |
{ | |
public decimal Sum { get; set; } | |
public int Count { get; set; } | |
public decimal Average { get; set; } | |
} | |
public Foos_AverageValue_GOOD() | |
{ | |
Map = foos => from foo in foos | |
select new | |
{ | |
Sum = foo.Value, | |
Count = 1, | |
Average = 0 | |
}; | |
Reduce = results => from result in results | |
group result by 0 | |
into g | |
let sum = g.Sum(x => x.Sum) | |
let count = g.Sum(x => x.Count) | |
select new | |
{ | |
Sum = sum, | |
Count = count, | |
Average = sum / count | |
}; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment