Skip to content

Commit

Permalink
Web and EF6 tests for $orderby filtered count. (#129)
Browse files Browse the repository at this point in the history
* Web and EF6 tests for orderby filtered count.

* PackageReleaseNotes
  • Loading branch information
BlaiseD committed Apr 5, 2022
1 parent e179606 commit 5ea5f64
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<PackageId>AutoMapper.AspNetCore.OData.EF6</PackageId>
<Description>Creates LINQ expressions from ODataQueryOptions and executes the query.</Description>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<PackageReleaseNotes>Supporting $skip and $top without $orderby.</PackageReleaseNotes>
<PackageReleaseNotes>Adding filter to $orderby $count.</PackageReleaseNotes>
<PackageTags>linq expressions odata efcore</PackageTags>
<PackageIcon>icon.png</PackageIcon>
<RepositoryUrl>https://github.com/AutoMapper/AutoMapper.Extensions.OData</RepositoryUrl>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<PackageId>AutoMapper.AspNetCore.OData.EFCore</PackageId>
<Description>Creates LINQ expressions from ODataQueryOptions and executes the query.</Description>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<PackageReleaseNotes>Supporting $skip and $top without $orderby.</PackageReleaseNotes>
<PackageReleaseNotes>Adding filter to $orderby $count.</PackageReleaseNotes>
<PackageTags>linq expressions odata efcore</PackageTags>
<PackageIcon>icon.png</PackageIcon>
<RepositoryUrl>https://github.com/AutoMapper/AutoMapper.Extensions.OData</RepositoryUrl>
Expand Down
5 changes: 3 additions & 2 deletions AutoMapper.AspNetCore.OData.EFCore/LinqExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -390,9 +390,10 @@ public static Expression GetOrderByCountCall(this Expression expression, CountNo

if (countNode.FilterClause is not null)
{
Type filterType = TypeExtensions.GetClrType(countNode.FilterClause.ItemType, TypeExtensions.GetEdmToClrTypeMappings());
string memberFullName = countNode.GetPropertyPath();
Type filterType = sourceType.GetMemberInfoFromFullName(memberFullName).GetMemberType().GetUnderlyingElementType();
LambdaExpression filterExpression = countNode.FilterClause.GetFilterExpression(filterType);
countSelector = param.MakeSelector(countNode.GetPropertyPath()).GetCountCall(filterExpression);
countSelector = param.MakeSelector(memberFullName).GetCountCall(filterExpression);
}
else
{
Expand Down
16 changes: 16 additions & 0 deletions AutoMapper.OData.EF6.Tests/GetQueryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,22 @@ void Test(ICollection<OpsTenant> collection)
}
}

[Fact]
public async void OpsTenantOrderByFilteredCount()
{
Test(Get<OpsTenant, TMandator>("/opstenant?$expand=Buildings&$orderby=Buildings/$count($filter=Name eq 'One L1') desc"));
Test(await GetAsync<OpsTenant, TMandator>("/opstenant?$expand=Buildings&$orderby=Buildings/$count($filter=Name eq 'One L1') desc"));

void Test(ICollection<OpsTenant> collection)
{
Assert.Equal(2, collection.Count);
Assert.NotNull(collection.First().Buildings);
Assert.Equal("One", collection.First().Name);
Assert.Equal(2, collection.First().Buildings.Count);
Assert.Equal(3, collection.Last().Buildings.Count);
}
}

//System.NotSupportedException
/*
* StackTrace:
Expand Down
16 changes: 16 additions & 0 deletions AutoMapper.OData.EF6.Tests/GetTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,22 @@ void Test(ICollection<OpsTenant> collection)
}
}

[Fact]
public async void OpsTenantOrderByFilteredCount()
{
Test(Get<OpsTenant, TMandator>("/opstenant?$expand=Buildings&$orderby=Buildings/$count($filter=Name eq 'One L1') desc"));
Test(await GetAsync<OpsTenant, TMandator>("/opstenant?$expand=Buildings&$orderby=Buildings/$count($filter=Name eq 'One L1') desc"));

void Test(ICollection<OpsTenant> collection)
{
Assert.Equal(2, collection.Count);
Assert.NotNull(collection.First().Buildings);
Assert.Equal("One", collection.First().Name);
Assert.Equal(2, collection.First().Buildings.Count);
Assert.Equal(3, collection.Last().Buildings.Count);
}
}

//Exception: 'The Include path 'Mandator->Buildings' results in a cycle.
//Cycles are not allowed in no-tracking queries. Either use a tracking query or remove the cycle.'
[Fact]
Expand Down
17 changes: 17 additions & 0 deletions Web.Tests/GetTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,23 @@ void Test(ICollection<OpsTenant> collection)
}
}

[Theory]
[InlineData("16324")]
[InlineData("16325")]
public async void OpsTenantOrderByFilteredCount(string port)
{
Test(await Get<OpsTenant>("/opstenant?$expand=Buildings&$orderby=Buildings/$count($filter=Name eq 'One L1') desc", port));

void Test(ICollection<OpsTenant> collection)
{
Assert.Equal(2, collection.Count);
Assert.NotNull(collection.First().Buildings);
Assert.Equal("One", collection.First().Name);
Assert.Equal(2, collection.First().Buildings.Count);
Assert.Equal(3, collection.Last().Buildings.Count);
}
}

[Theory]
[InlineData("16324")]
//EF 6 seems to have a problem with circular reference Building/Tenant/Buildings
Expand Down

0 comments on commit 5ea5f64

Please sign in to comment.