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

254 lines
9.2 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-10-25 19:40:30 -04:00
using System.Collections;
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 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),
2018-10-25 21:02:17 -04:00
Sales = t.ToList(),
MaxNetSales = t.Max(t2 => t2.NetSales),
MinNetSales = t.Min(t2 => t2.NetSales),
First = t.First(),
Last = t.Last(),
FirstOrDefault = t.FirstOrDefault(),
LastOrDefault = t.LastOrDefault()
2018-03-14 21:08:17 -04:00
})
.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");
2018-10-25 21:02:17 -04:00
t.Max("NetSales", "MaxNetSales");
t.Min("NetSales", "MinNetSales");
t.First("First");
t.Last("Last");
t.FirstOrDefault("FirstOrDefault");
t.LastOrDefault("LastOrDefault");
2018-03-14 21:08:17 -04:00
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"));
2018-10-25 21:02:17 -04:00
Assert.AreEqual(left.MinNetSales, right.GetDynamicPropertyValue("MinNetSales"));
Assert.AreEqual(left.MaxNetSales, right.GetDynamicPropertyValue("MaxNetSales"));
Assert.AreEqual(left.First, right.GetDynamicPropertyValue("First"));
Assert.AreEqual(left.FirstOrDefault, right.GetDynamicPropertyValue("FirstOrDefault"));
Assert.AreEqual(left.Last, right.GetDynamicPropertyValue("Last"));
Assert.AreEqual(left.LastOrDefault, right.GetDynamicPropertyValue("LastOrDefault"));
2018-03-14 21:08:17 -04:00
QueryableAssert.AreEqual(left.Sales.AsQueryable(), right.GetDynamicPropertyValue<List<MockSale>>("Sales").AsQueryable());
}
2018-03-08 21:18:43 -05: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-10-25 19:40:30 -04:00
[TestMethod]
public void GroupWithoutNullCheckComplex()
{
var limitResult = TestData.Authors.Where(t => t.Posts != null).AsQueryable();
var posts = limitResult
.GroupBy(t => new
{
Titles = t.Posts.Select(t2 => t2.Title)
})
.Select(t => new
{
Titles = t.Key.Titles,
Data = t.ToList()
})
.ToList();
var posts2 = limitResult
.GroupBy(gb => gb.Path("Posts.Title", "Titles"))
.Select(sb =>
{
sb.Key("Titles");
sb.ToList("Data");
})
.ToDynamicClassList();
Assert.AreEqual(posts.Count, posts2.Count);
for(var i = 0; i < posts.Count; i++)
{
var expected = posts[0];
var actual = posts2[0];
var titles = actual.GetDynamicPropertyValue("Titles") as ICollection;
CollectionAssert.AreEqual(expected.Titles as ICollection, titles);
}
}
[TestMethod]
public void GroupWithNullCheckComplex()
{
var limitResult = TestData.Authors.AsQueryable();
var posts = limitResult
.GroupBy(t => new
{
Titles = t.Posts == null ? new List<string>() : t.Posts.Select(t2 => t2.Title)
})
.Select(t => new
{
Titles = t.Key.Titles,
Data = t.ToList()
})
.ToList();
var tempQueryable = limitResult
.GroupBy(gb => gb.NullChecking().Path("Posts.Title", "Titles"));
var posts2 = tempQueryable
.Select(sb =>
{
sb.Key("Titles");
sb.ToList("Data");
})
.ToDynamicClassList();
Assert.AreEqual(posts.Count, posts2.Count);
for (var i = 0; i < posts.Count; i++)
{
var expected = posts[0];
var actual = posts2[0];
var titles = actual.GetDynamicPropertyValue("Titles") as ICollection;
CollectionAssert.AreEqual(expected.Titles as ICollection, titles);
}
}
2018-03-08 21:18:43 -05:00
}
}