Skip to content

Commit

Permalink
new code unit test.
Browse files Browse the repository at this point in the history
moved entity framework test to in memory database.
  • Loading branch information
David Lebee committed Oct 23, 2018
1 parent 3ec2b6b commit 2cd1d27
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 3 deletions.
28 changes: 28 additions & 0 deletions PoweredSoft.DynamicLinq.Test/CountTests.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
3 changes: 2 additions & 1 deletion PoweredSoft.DynamicLinq.Test/EntityFrameworkTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ namespace PoweredSoft.DynamicLinq.Test
public class EntityFrameworkTests
{
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)
{
Expand Down
74 changes: 74 additions & 0 deletions PoweredSoft.DynamicLinq.Test/GroupingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,34 @@ internal class TestStructure
[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"));
}

[TestMethod]
public void WantedSyntax()
{
Expand Down Expand Up @@ -75,5 +103,51 @@ public void WantedSyntax()
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());
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
<Compile Include="EntityFrameworkTests.cs" />
<Compile Include="StringComparision.cs" />
<Compile Include="TestData.cs" />
<Compile Include="CountTests.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
Expand Down
3 changes: 3 additions & 0 deletions PoweredSoft.DynamicLinq/Extensions/EnumerableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ public static IQueryable GroupBy<T>(this IEnumerable<T> list, Action<GroupBuilde
public static IQueryable GroupBy(this IEnumerable list, Type type, Action<GroupBuilder> 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)
{
var copy = list.ToList();
Expand Down
33 changes: 31 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Shortcuts allow to avoid specifying the condition operator by having it handy in
```csharp
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
```csharp
Expand All @@ -51,10 +51,11 @@ TestData.Sales
t.LongCount("LongCount");
t.Sum("NetSales");
t.Average("Tax", "TaxAverage");
t.Aggregate("Tax", SelectTypes.Average, "TaxAverage2"); // Starting 1.0.5
t.ToList("Sales");
});
```
Is equivalent to
> Is equivalent to
```csharp
TestSales
.GroupBy(t => new { t.ClientId })
Expand All @@ -64,10 +65,38 @@ TestSales
LongCount = t.LongCount(),
NetSales = t.Sum(t2 => t2.NetSales),
TaxAverage = t.Average(t2 => t2.Tax),
TaxAverage2 = t.Average(t2 => t2.Tax),
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

```csharp
Expand Down

0 comments on commit 2cd1d27

Please sign in to comment.