2018-03-08 21:18:43 -05:00
|
|
|
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
2018-03-09 00:22:12 -05:00
|
|
|
|
using PoweredSoft.DynamicLinq;
|
2018-03-13 22:01:21 -04:00
|
|
|
|
using PoweredSoft.DynamicLinq.Dal;
|
2018-03-13 23:22:10 -04:00
|
|
|
|
using System.Diagnostics;
|
2018-03-14 21:08:17 -04:00
|
|
|
|
using PoweredSoft.DynamicLinq.Test.Helpers;
|
2018-03-08 21:18:43 -05:00
|
|
|
|
|
|
|
|
|
namespace PoweredSoft.DynamicLinq.Test
|
|
|
|
|
{
|
2018-03-12 19:00:02 -04:00
|
|
|
|
internal class TestStructureCompare : IEqualityComparer<TestStructure>
|
2018-03-12 18:23:02 -04:00
|
|
|
|
{
|
2018-03-12 19:00:02 -04:00
|
|
|
|
public bool Equals(TestStructure x, TestStructure y)
|
|
|
|
|
{
|
|
|
|
|
return x?.ClientId == y?.ClientId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int GetHashCode(TestStructure obj)
|
|
|
|
|
{
|
|
|
|
|
return obj.ClientId;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal class TestStructure
|
|
|
|
|
{
|
|
|
|
|
public int ClientId { get; set; }
|
2018-03-12 18:23:02 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-03-08 21:18:43 -05:00
|
|
|
|
[TestClass]
|
|
|
|
|
public class GroupingTests
|
|
|
|
|
{
|
2018-10-23 17:12:39 -04:00
|
|
|
|
[TestMethod]
|
|
|
|
|
public void TestEmptyGroup()
|
|
|
|
|
{
|
|
|
|
|
var subject = TestData.Sales;
|
|
|
|
|
|
|
|
|
|
var normalSyntax = subject
|
|
|
|
|
.GroupBy(t => true)
|
|
|
|
|
.Select(t => new
|
|
|
|
|
{
|
|
|
|
|
NetSalesSum = t.Sum(t2 => t2.NetSales),
|
|
|
|
|
NetSalesAvg = t.Average(t2 => t2.NetSales)
|
|
|
|
|
})
|
|
|
|
|
.First();
|
|
|
|
|
|
|
|
|
|
var dynamicSyntax = subject
|
|
|
|
|
.EmptyGroupBy(typeof(MockSale))
|
|
|
|
|
.Select(sb =>
|
|
|
|
|
{
|
|
|
|
|
sb.Sum("NetSales", "NetSalesSum");
|
|
|
|
|
sb.Average("NetSales", "NetSalesAvg");
|
|
|
|
|
})
|
|
|
|
|
.ToDynamicClassList()
|
|
|
|
|
.First();
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(normalSyntax.NetSalesAvg, dynamicSyntax.GetDynamicPropertyValue<decimal>("NetSalesAvg"));
|
|
|
|
|
Assert.AreEqual(normalSyntax.NetSalesSum, dynamicSyntax.GetDynamicPropertyValue<decimal>("NetSalesSum"));
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-08 21:18:43 -05:00
|
|
|
|
[TestMethod]
|
|
|
|
|
public void WantedSyntax()
|
2018-03-14 21:08:17 -04:00
|
|
|
|
{
|
|
|
|
|
var normalSyntax = TestData.Sales
|
|
|
|
|
.GroupBy(t => new { t.ClientId })
|
2018-03-13 22:01:21 -04:00
|
|
|
|
.Select(t => new
|
|
|
|
|
{
|
2018-03-14 21:08:17 -04:00
|
|
|
|
TheClientId = t.Key.ClientId,
|
2018-03-13 22:01:21 -04:00
|
|
|
|
Count = t.Count(),
|
2018-03-14 21:08:17 -04:00
|
|
|
|
LongCount = t.LongCount(),
|
|
|
|
|
NetSales = t.Sum(t2 => t2.NetSales),
|
|
|
|
|
TaxAverage = t.Average(t2 => t2.Tax),
|
|
|
|
|
Sales = t.ToList()
|
|
|
|
|
})
|
|
|
|
|
.ToList();
|
2018-03-08 22:59:18 -05:00
|
|
|
|
|
2018-03-14 21:08:17 -04:00
|
|
|
|
var dynamicSyntax = TestData.Sales
|
2018-03-12 23:26:41 -04:00
|
|
|
|
.AsQueryable()
|
|
|
|
|
.GroupBy(t => t.Path("ClientId"))
|
|
|
|
|
.Select(t =>
|
|
|
|
|
{
|
2018-03-14 21:08:17 -04:00
|
|
|
|
t.Key("TheClientId", "ClientId");
|
|
|
|
|
t.Count("Count");
|
|
|
|
|
t.LongCount("LongCount");
|
|
|
|
|
t.Sum("NetSales");
|
|
|
|
|
t.Average("Tax", "TaxAverage");
|
|
|
|
|
t.ToList("Sales");
|
|
|
|
|
})
|
|
|
|
|
.ToDynamicClassList();
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(normalSyntax.Count, dynamicSyntax.Count);
|
|
|
|
|
for(var i = 0; i < normalSyntax.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
var left = normalSyntax[i];
|
|
|
|
|
var right = dynamicSyntax[i];
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(left.TheClientId, right.GetDynamicPropertyValue("TheClientId"));
|
|
|
|
|
Assert.AreEqual(left.Count, right.GetDynamicPropertyValue("Count"));
|
|
|
|
|
Assert.AreEqual(left.LongCount, right.GetDynamicPropertyValue("LongCount"));
|
|
|
|
|
Assert.AreEqual(left.TaxAverage, right.GetDynamicPropertyValue("TaxAverage"));
|
|
|
|
|
QueryableAssert.AreEqual(left.Sales.AsQueryable(), right.GetDynamicPropertyValue<List<MockSale>>("Sales").AsQueryable());
|
|
|
|
|
}
|
2018-03-08 21:18:43 -05:00
|
|
|
|
}
|
2018-10-23 17:12:39 -04:00
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void TestingSelectBuilderAggregateFluent()
|
|
|
|
|
{
|
|
|
|
|
var normalSyntax = TestData.Sales
|
|
|
|
|
.GroupBy(t => new { t.ClientId })
|
|
|
|
|
.Select(t => new
|
|
|
|
|
{
|
|
|
|
|
TheClientId = t.Key.ClientId,
|
|
|
|
|
Count = t.Count(),
|
|
|
|
|
LongCount = t.LongCount(),
|
|
|
|
|
NetSales = t.Sum(t2 => t2.NetSales),
|
|
|
|
|
TaxAverage = t.Average(t2 => t2.Tax),
|
|
|
|
|
Sales = t.ToList()
|
|
|
|
|
})
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
var dynamicSyntax = TestData.Sales
|
|
|
|
|
.AsQueryable()
|
|
|
|
|
.GroupBy(t => t.Path("ClientId"))
|
|
|
|
|
.Select(t =>
|
|
|
|
|
{
|
|
|
|
|
t.Aggregate("Key.ClientId", SelectTypes.Key, "TheClientId");
|
|
|
|
|
// should not have to specify a path, but a property is a must
|
|
|
|
|
t.Aggregate(null, SelectTypes.Count, "Count");
|
|
|
|
|
// support both ways it can use path to guess property so testing this too
|
|
|
|
|
t.Aggregate("LongCount", SelectTypes.LongCount);
|
|
|
|
|
t.Aggregate("NetSales", SelectTypes.Sum);
|
|
|
|
|
t.Aggregate("Tax", SelectTypes.Average, "TaxAverage");
|
|
|
|
|
t.ToList("Sales");
|
|
|
|
|
})
|
|
|
|
|
.ToDynamicClassList();
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(normalSyntax.Count, dynamicSyntax.Count);
|
|
|
|
|
for (var i = 0; i < normalSyntax.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
var left = normalSyntax[i];
|
|
|
|
|
var right = dynamicSyntax[i];
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(left.TheClientId, right.GetDynamicPropertyValue("TheClientId"));
|
|
|
|
|
Assert.AreEqual(left.Count, right.GetDynamicPropertyValue("Count"));
|
|
|
|
|
Assert.AreEqual(left.LongCount, right.GetDynamicPropertyValue("LongCount"));
|
|
|
|
|
Assert.AreEqual(left.TaxAverage, right.GetDynamicPropertyValue("TaxAverage"));
|
|
|
|
|
QueryableAssert.AreEqual(left.Sales.AsQueryable(), right.GetDynamicPropertyValue<List<MockSale>>("Sales").AsQueryable());
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-03-08 21:18:43 -05:00
|
|
|
|
}
|
|
|
|
|
}
|