dotnet-dynamic-linq/PoweredSoft.DynamicLinq.Test/GroupingTests.cs

80 lines
2.6 KiB
C#
Raw Normal View History

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;
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 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-08 21:18:43 -05:00
[TestClass]
public class GroupingTests
{
[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
.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
}
}
}