better unit test to test grouping.
This commit is contained in:
parent
ed88de7c02
commit
9fe85fcb65
@ -7,6 +7,7 @@ using System.Threading.Tasks;
|
|||||||
using PoweredSoft.DynamicLinq;
|
using PoweredSoft.DynamicLinq;
|
||||||
using PoweredSoft.DynamicLinq.Dal;
|
using PoweredSoft.DynamicLinq.Dal;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
|
using PoweredSoft.DynamicLinq.Test.Helpers;
|
||||||
|
|
||||||
namespace PoweredSoft.DynamicLinq.Test
|
namespace PoweredSoft.DynamicLinq.Test
|
||||||
{
|
{
|
||||||
@ -33,81 +34,46 @@ namespace PoweredSoft.DynamicLinq.Test
|
|||||||
{
|
{
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
public void WantedSyntax()
|
public void WantedSyntax()
|
||||||
{/*
|
{
|
||||||
var regularSyntax = TestData.Sales
|
var normalSyntax = TestData.Sales
|
||||||
.GroupBy(t => t.ClientId);
|
.GroupBy(t => new { t.ClientId })
|
||||||
|
|
||||||
var dynamicSyntax = TestData.Sales
|
|
||||||
.AsQueryable()
|
|
||||||
.GroupBy("ClientId");
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
var regularSyntax2 = TestData.Sales
|
|
||||||
.GroupBy(t => new
|
|
||||||
{
|
|
||||||
t.ClientId,
|
|
||||||
B = t.NetSales
|
|
||||||
});*/
|
|
||||||
|
|
||||||
/*
|
|
||||||
var dynamicSyntax2 = TestData.Sales
|
|
||||||
.AsQueryable()
|
|
||||||
.GroupBy(t => t.Path("ClientId").Path("NetSales", "B"));
|
|
||||||
|
|
||||||
var dynamicSyntax3 = TestData.Sales
|
|
||||||
.AsQueryable()
|
|
||||||
.GroupBy(t => t.UseType(typeof(TestStructure)).EqualityComparer(typeof(TestStructureCompare)).Path("ClientId"));
|
|
||||||
|
|
||||||
var tryAs = dynamicSyntax3 as EnumerableQuery<IGrouping<TestStructure, MockSale>>;
|
|
||||||
var list = tryAs.Select(t => new
|
|
||||||
{
|
|
||||||
Key = t.Key,
|
|
||||||
Data = t.ToList()
|
|
||||||
}).ToList();
|
|
||||||
|
|
||||||
var list2 = TestData.Sales.GroupBy(t => new TestStructure { ClientId = t.ClientId }, new TestStructureCompare()).Select(t => new
|
|
||||||
{
|
|
||||||
Key = t.Key,
|
|
||||||
Data = t.ToList()
|
|
||||||
}).ToList();
|
|
||||||
|
|
||||||
int i = 0;*/
|
|
||||||
|
|
||||||
/*
|
|
||||||
var dynamicSyntax3 = TestData.Sales
|
|
||||||
.AsQueryable()
|
|
||||||
.GroupBy(t => t.ClientId)
|
|
||||||
.Select(t => new
|
.Select(t => new
|
||||||
{
|
{
|
||||||
TheClientId = t.Key,
|
TheClientId = t.Key.ClientId,
|
||||||
Count = t.Count(),
|
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
|
||||||
TestData.Sales.GroupBy(t => new { t.ClientId }).Select(t => new
|
|
||||||
{
|
|
||||||
TheClientId = t.Key.ClientId,
|
|
||||||
Count = t.Count(),
|
|
||||||
LongCount = t.LongCount(),
|
|
||||||
TaxAverage = t.Average(t2 => t2.Tax)
|
|
||||||
});*/
|
|
||||||
|
|
||||||
var dynamicSyntax2 = TestData.Sales
|
|
||||||
.AsQueryable()
|
.AsQueryable()
|
||||||
.GroupBy(t => t.Path("ClientId"))
|
.GroupBy(t => t.Path("ClientId"))
|
||||||
.Select(t =>
|
.Select(t =>
|
||||||
{
|
{
|
||||||
t.Key("TheClientId", "ClientId");
|
t.Key("TheClientId", "ClientId");
|
||||||
t.Count("Count");
|
t.Count("Count");
|
||||||
t.LongCount("LongCount");
|
t.LongCount("LongCount");
|
||||||
t.Sum("NetSales");
|
t.Sum("NetSales");
|
||||||
t.Average("Tax", "TaxAverage");
|
t.Average("Tax", "TaxAverage");
|
||||||
t.ToList("Sales");
|
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];
|
||||||
|
|
||||||
var result = dynamicSyntax2.ToObjectList();
|
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());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,8 +11,8 @@ namespace PoweredSoft.DynamicLinq
|
|||||||
{
|
{
|
||||||
public static class QueryableExtensions
|
public static class QueryableExtensions
|
||||||
{
|
{
|
||||||
public static IQueryable<T> Where<T>(this IQueryable<T> query, string path, ConditionOperators conditionOperator, object value,
|
public static IQueryable<T> Where<T>(this IQueryable<T> query, string path, ConditionOperators conditionOperator, object value,
|
||||||
QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField,
|
QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField,
|
||||||
QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any, StringComparison? stringComparision = null)
|
QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any, StringComparison? stringComparision = null)
|
||||||
{
|
{
|
||||||
query = query.Query(qb => qb.Compare(path, conditionOperator, value, convertStrategy: convertStrategy, collectionHandling: collectionHandling, stringComparision: stringComparision));
|
query = query.Query(qb => qb.Compare(path, conditionOperator, value, convertStrategy: convertStrategy, collectionHandling: collectionHandling, stringComparision: stringComparision));
|
||||||
@ -22,7 +22,7 @@ namespace PoweredSoft.DynamicLinq
|
|||||||
public static IQueryable<T> Where<T>(this IQueryable<T> query, Action<WhereBuilder> callback)
|
public static IQueryable<T> Where<T>(this IQueryable<T> query, Action<WhereBuilder> callback)
|
||||||
=> query.Query(callback);
|
=> query.Query(callback);
|
||||||
|
|
||||||
public static IQueryable<T> Query<T> (this IQueryable<T> query, Action<WhereBuilder> callback)
|
public static IQueryable<T> Query<T>(this IQueryable<T> query, Action<WhereBuilder> callback)
|
||||||
{
|
{
|
||||||
var queryBuilder = new WhereBuilder(query);
|
var queryBuilder = new WhereBuilder(query);
|
||||||
callback(queryBuilder);
|
callback(queryBuilder);
|
||||||
@ -115,5 +115,13 @@ namespace PoweredSoft.DynamicLinq
|
|||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
public static List<DynamicClass> ToDynamicClassList(this IQueryable query)
|
||||||
|
{
|
||||||
|
if (!typeof(DynamicClass).IsAssignableFrom(query.ElementType))
|
||||||
|
throw new Exception($"{query.ElementType} does not inherit DynamicClass");
|
||||||
|
|
||||||
|
var ret = query.Cast<DynamicClass>().ToList();
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user