From 5ea5f6419aa7535d6bab80b74545af31422beab8 Mon Sep 17 00:00:00 2001 From: Blaise Taylor Date: Tue, 5 Apr 2022 15:39:34 -0400 Subject: [PATCH] Web and EF6 tests for $orderby filtered count. (#129) * Web and EF6 tests for orderby filtered count. * PackageReleaseNotes --- .../AutoMapper.AspNetCore.OData.EF6.csproj | 2 +- .../AutoMapper.AspNetCore.OData.EFCore.csproj | 2 +- .../LinqExtensions.cs | 5 +++-- AutoMapper.OData.EF6.Tests/GetQueryTests.cs | 16 ++++++++++++++++ AutoMapper.OData.EF6.Tests/GetTests.cs | 16 ++++++++++++++++ Web.Tests/GetTests.cs | 17 +++++++++++++++++ 6 files changed, 54 insertions(+), 4 deletions(-) diff --git a/AutoMapper.AspNetCore.OData.EF6/AutoMapper.AspNetCore.OData.EF6.csproj b/AutoMapper.AspNetCore.OData.EF6/AutoMapper.AspNetCore.OData.EF6.csproj index d059505..ac90add 100644 --- a/AutoMapper.AspNetCore.OData.EF6/AutoMapper.AspNetCore.OData.EF6.csproj +++ b/AutoMapper.AspNetCore.OData.EF6/AutoMapper.AspNetCore.OData.EF6.csproj @@ -6,7 +6,7 @@ AutoMapper.AspNetCore.OData.EF6 Creates LINQ expressions from ODataQueryOptions and executes the query. false - Supporting $skip and $top without $orderby. + Adding filter to $orderby $count. linq expressions odata efcore icon.png https://github.com/AutoMapper/AutoMapper.Extensions.OData diff --git a/AutoMapper.AspNetCore.OData.EFCore/AutoMapper.AspNetCore.OData.EFCore.csproj b/AutoMapper.AspNetCore.OData.EFCore/AutoMapper.AspNetCore.OData.EFCore.csproj index 58ad5f0..6333869 100644 --- a/AutoMapper.AspNetCore.OData.EFCore/AutoMapper.AspNetCore.OData.EFCore.csproj +++ b/AutoMapper.AspNetCore.OData.EFCore/AutoMapper.AspNetCore.OData.EFCore.csproj @@ -6,7 +6,7 @@ AutoMapper.AspNetCore.OData.EFCore Creates LINQ expressions from ODataQueryOptions and executes the query. false - Supporting $skip and $top without $orderby. + Adding filter to $orderby $count. linq expressions odata efcore icon.png https://github.com/AutoMapper/AutoMapper.Extensions.OData diff --git a/AutoMapper.AspNetCore.OData.EFCore/LinqExtensions.cs b/AutoMapper.AspNetCore.OData.EFCore/LinqExtensions.cs index 907827c..c52c7fa 100644 --- a/AutoMapper.AspNetCore.OData.EFCore/LinqExtensions.cs +++ b/AutoMapper.AspNetCore.OData.EFCore/LinqExtensions.cs @@ -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 { diff --git a/AutoMapper.OData.EF6.Tests/GetQueryTests.cs b/AutoMapper.OData.EF6.Tests/GetQueryTests.cs index 754250a..232446d 100644 --- a/AutoMapper.OData.EF6.Tests/GetQueryTests.cs +++ b/AutoMapper.OData.EF6.Tests/GetQueryTests.cs @@ -566,6 +566,22 @@ void Test(ICollection collection) } } + [Fact] + public async void OpsTenantOrderByFilteredCount() + { + Test(Get("/opstenant?$expand=Buildings&$orderby=Buildings/$count($filter=Name eq 'One L1') desc")); + Test(await GetAsync("/opstenant?$expand=Buildings&$orderby=Buildings/$count($filter=Name eq 'One L1') desc")); + + void Test(ICollection 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: diff --git a/AutoMapper.OData.EF6.Tests/GetTests.cs b/AutoMapper.OData.EF6.Tests/GetTests.cs index 7634954..481ee76 100644 --- a/AutoMapper.OData.EF6.Tests/GetTests.cs +++ b/AutoMapper.OData.EF6.Tests/GetTests.cs @@ -435,6 +435,22 @@ void Test(ICollection collection) } } + [Fact] + public async void OpsTenantOrderByFilteredCount() + { + Test(Get("/opstenant?$expand=Buildings&$orderby=Buildings/$count($filter=Name eq 'One L1') desc")); + Test(await GetAsync("/opstenant?$expand=Buildings&$orderby=Buildings/$count($filter=Name eq 'One L1') desc")); + + void Test(ICollection 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] diff --git a/Web.Tests/GetTests.cs b/Web.Tests/GetTests.cs index a62955e..73dbcb8 100644 --- a/Web.Tests/GetTests.cs +++ b/Web.Tests/GetTests.cs @@ -430,6 +430,23 @@ void Test(ICollection collection) } } + [Theory] + [InlineData("16324")] + [InlineData("16325")] + public async void OpsTenantOrderByFilteredCount(string port) + { + Test(await Get("/opstenant?$expand=Buildings&$orderby=Buildings/$count($filter=Name eq 'One L1') desc", port)); + + void Test(ICollection 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