new code unit test.

moved entity framework test to in memory database.
This commit is contained in:
David Lebee 2018-10-23 16:12:39 -05:00
parent 3ec2b6b394
commit 2cd1d27df0
6 changed files with 139 additions and 3 deletions

View File

@ -0,0 +1,28 @@
using System;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace PoweredSoft.DynamicLinq.Test
{
[TestClass]
public class CountTests
{
[TestMethod]
public void Count()
{
var normalSyntax = TestData.Sales.Count();
var nonGenericQueryable = (IQueryable)TestData.Sales.AsQueryable();
var dynamicSyntax = nonGenericQueryable.Count();
Assert.AreEqual(normalSyntax, dynamicSyntax);
}
[TestMethod]
public void LongCount()
{
var normalSyntax = TestData.Sales.LongCount();
var nonGenericQueryable = (IQueryable)TestData.Sales.AsQueryable();
var dynamicSyntax = nonGenericQueryable.LongCount();
Assert.AreEqual(normalSyntax, dynamicSyntax);
}
}
}

View File

@ -14,7 +14,8 @@ namespace PoweredSoft.DynamicLinq.Test
public class EntityFrameworkTests public class EntityFrameworkTests
{ {
public static string testConnectionString => public static string testConnectionString =>
"data source=(local); initial catalog=blogtests;persist security info=True; Integrated Security=SSPI;"; @"Server=(localdb)\mssqllocaldb;Database=EFProviders.InMemory;Trusted_Connection=True;";
//"data source=(local); initial catalog=blogtests;persist security info=True; Integrated Security=SSPI;";
public static void SeedForTests(BlogContext context) public static void SeedForTests(BlogContext context)
{ {

View File

@ -32,6 +32,34 @@ namespace PoweredSoft.DynamicLinq.Test
[TestClass] [TestClass]
public class GroupingTests 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"));
}
[TestMethod] [TestMethod]
public void WantedSyntax() public void WantedSyntax()
{ {
@ -75,5 +103,51 @@ namespace PoweredSoft.DynamicLinq.Test
QueryableAssert.AreEqual(left.Sales.AsQueryable(), right.GetDynamicPropertyValue<List<MockSale>>("Sales").AsQueryable()); QueryableAssert.AreEqual(left.Sales.AsQueryable(), right.GetDynamicPropertyValue<List<MockSale>>("Sales").AsQueryable());
} }
} }
[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());
}
}
} }
} }

View File

@ -77,6 +77,7 @@
<Compile Include="EntityFrameworkTests.cs" /> <Compile Include="EntityFrameworkTests.cs" />
<Compile Include="StringComparision.cs" /> <Compile Include="StringComparision.cs" />
<Compile Include="TestData.cs" /> <Compile Include="TestData.cs" />
<Compile Include="CountTests.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="packages.config" /> <None Include="packages.config" />

View File

@ -47,6 +47,9 @@ namespace PoweredSoft.DynamicLinq
public static IQueryable GroupBy(this IEnumerable list, Type type, Action<GroupBuilder> callback) public static IQueryable GroupBy(this IEnumerable list, Type type, Action<GroupBuilder> callback)
=> list.AsQueryable().GroupBy(type, callback); => list.AsQueryable().GroupBy(type, callback);
public static IQueryable EmptyGroupBy(this IEnumerable list, Type underlyingType)
=> list.AsQueryable().EmptyGroupBy(underlyingType);
public static List<T> Reversed<T>(this List<T> list) public static List<T> Reversed<T>(this List<T> list)
{ {
var copy = list.ToList(); var copy = list.ToList();

View File

@ -32,7 +32,7 @@ Shortcuts allow to avoid specifying the condition operator by having it handy in
```csharp ```csharp
queryable.Query(t => t.Contains("FirstName", "Dav").OrContains("FirstName", "Jo")); queryable.Query(t => t.Contains("FirstName", "Dav").OrContains("FirstName", "Jo"));
``` ```
You may visit this test for more examples: https://github.com/PoweredSoft/DynamicLinq/blob/master/PoweredSoft.DynamicLinq.Test/ShortcutTests.cs > You may visit this test for more examples: https://github.com/PoweredSoft/DynamicLinq/blob/master/PoweredSoft.DynamicLinq.Test/ShortcutTests.cs
### Simple Query ### Simple Query
```csharp ```csharp
@ -51,10 +51,11 @@ TestData.Sales
t.LongCount("LongCount"); t.LongCount("LongCount");
t.Sum("NetSales"); t.Sum("NetSales");
t.Average("Tax", "TaxAverage"); t.Average("Tax", "TaxAverage");
t.Aggregate("Tax", SelectTypes.Average, "TaxAverage2"); // Starting 1.0.5
t.ToList("Sales"); t.ToList("Sales");
}); });
``` ```
Is equivalent to > Is equivalent to
```csharp ```csharp
TestSales TestSales
.GroupBy(t => new { t.ClientId }) .GroupBy(t => new { t.ClientId })
@ -64,10 +65,38 @@ TestSales
LongCount = t.LongCount(), LongCount = t.LongCount(),
NetSales = t.Sum(t2 => t2.NetSales), NetSales = t.Sum(t2 => t2.NetSales),
TaxAverage = t.Average(t2 => t2.Tax), TaxAverage = t.Average(t2 => t2.Tax),
TaxAverage2 = t.Average(t2 => t2.Tax),
Sales = t.ToList() Sales = t.ToList()
}); });
``` ```
### Empty Group By
This is common to create aggregate totals.
```csharp
someQueryable.EmptyGroupBy(typeof(SomeClass));
```
> Is equivalent to
```csharp
someQueryableOfT.GroupBy(t => true);
```
### Count shortcut
```csharp
IQueryable someQueryable = <something>;
someQueryable.Count();
```
> Is equivalent to
```csharp
IQueryable<T> someQueryableOfT = <something>;
someQsomeQueryableOfTueryable.Count();
```
### Select ### Select
```csharp ```csharp