11using AutoMapper . AspNet . OData ;
2+ using AutoMapper . OData . EFCore . Tests . Binders ;
23using AutoMapper . OData . EFCore . Tests . Data ;
34using AutoMapper . OData . EFCore . Tests . Model ;
45using DAL . EFCore ;
56using Domain . OData ;
67using Microsoft . AspNetCore . Builder ;
8+ using Microsoft . AspNetCore . Http ;
9+ using Microsoft . AspNetCore . Mvc . ApplicationParts ;
710using Microsoft . AspNetCore . OData ;
811using Microsoft . AspNetCore . OData . Extensions ;
912using Microsoft . AspNetCore . OData . Query ;
13+ using Microsoft . AspNetCore . OData . Query . Expressions ;
1014using Microsoft . EntityFrameworkCore ;
1115using Microsoft . Extensions . DependencyInjection ;
1216using Microsoft . Extensions . Logging . Abstractions ;
17+ using Microsoft . OData . Edm ;
18+ using Microsoft . OData . ModelBuilder ;
19+ using Microsoft . OData . UriParser ;
1320using System ;
1421using System . Collections . Generic ;
1522using System . Linq ;
@@ -2000,7 +2007,7 @@ public GetQueryTestsFixture()
20002007 ) ,
20012008 ServiceLifetime . Transient
20022009 )
2003- . AddSingleton < IConfigurationProvider > ( new MapperConfiguration ( cfg => cfg . AddMaps ( typeof ( GetTests ) . Assembly ) , new NullLoggerFactory ( ) ) )
2010+ . AddSingleton < IConfigurationProvider > ( new MapperConfiguration ( cfg => cfg . AddMaps ( typeof ( GetQueryTests ) . Assembly ) , new NullLoggerFactory ( ) ) )
20042011 . AddTransient < IMapper > ( sp => new Mapper ( sp . GetRequiredService < IConfigurationProvider > ( ) , sp . GetService ) )
20052012 . AddTransient < IApplicationBuilder > ( sp => new ApplicationBuilder ( sp ) )
20062013 . AddRouting ( )
@@ -2016,4 +2023,82 @@ public GetQueryTestsFixture()
20162023
20172024 internal IServiceProvider ServiceProvider ;
20182025 }
2026+
2027+ public static class ODataHelpers
2028+ {
2029+ public static ODataQueryOptions < T > GetODataQueryOptions < T > ( string queryString , IServiceProvider serviceProvider , string customNamespace = null , bool enableLowerCamelCase = false ) where T : class
2030+ {
2031+ ODataConventionModelBuilder builder = new ODataConventionModelBuilder ( ) ;
2032+ if ( customNamespace != null )
2033+ builder . Namespace = customNamespace ;
2034+
2035+ if ( enableLowerCamelCase )
2036+ builder . EnableLowerCamelCase ( ) ;
2037+
2038+ builder . EntitySet < T > ( typeof ( T ) . Name ) ;
2039+ IEdmModel model = builder . GetEdmModel ( ) ;
2040+ IEdmEntitySet entitySet = model . EntityContainer . FindEntitySet ( typeof ( T ) . Name ) ;
2041+ ODataPath path = new ODataPath ( new EntitySetSegment ( entitySet ) ) ;
2042+
2043+ var oDataQueryOptions = new ODataQueryOptions < T >
2044+ (
2045+ new ODataQueryContext ( model , typeof ( T ) , path ) ,
2046+ BuildRequest ( serviceProvider , model , new Uri ( BASEADDRESS + queryString ) )
2047+ ) ;
2048+
2049+ return oDataQueryOptions ;
2050+ }
2051+
2052+ public static ODataQueryOptions < Model . CategoryModel > GetODataQueryOptionsWithDuplicateEntityName ( string queryString , IServiceProvider serviceProvider , string customNamespace = null )
2053+ {
2054+ ODataConventionModelBuilder builder = new ODataConventionModelBuilder ( ) ;
2055+ if ( customNamespace != null )
2056+ builder . Namespace = customNamespace ;
2057+
2058+ builder . EnableLowerCamelCase ( ) ;
2059+
2060+ builder . EntitySet < X . CategoryModel > ( typeof ( X . CategoryModel ) . Name + "X" ) ;
2061+ builder . EntitySet < Model . CategoryModel > ( nameof ( Model . CategoryModel ) ) ;
2062+ IEdmModel model = builder . GetEdmModel ( ) ;
2063+ IEdmEntitySet entitySet = model . EntityContainer . FindEntitySet ( typeof ( Model . CategoryModel ) . Name ) ;
2064+ ODataPath path = new ODataPath ( new EntitySetSegment ( entitySet ) ) ;
2065+
2066+ var oDataQueryOptions = new ODataQueryOptions < Model . CategoryModel >
2067+ (
2068+ new ODataQueryContext ( model , typeof ( Model . CategoryModel ) , path ) ,
2069+ BuildRequest ( serviceProvider , model , new Uri ( BASEADDRESS + queryString ) )
2070+ ) ;
2071+
2072+ return oDataQueryOptions ;
2073+ }
2074+
2075+ static HttpRequest BuildRequest ( IServiceProvider serviceProvider , IEdmModel model , Uri uri )
2076+ {
2077+ var request = new DefaultHttpContext ( )
2078+ {
2079+ RequestServices = serviceProvider
2080+ } . Request ;
2081+
2082+ var oDataOptions = new ODataOptions ( ) . AddRouteComponents ( "key" , model ,
2083+ x => x . AddSingleton < ISearchBinder , OpsTenantSearchBinder > ( ) ) ;
2084+ var ( _, routeProvider ) = oDataOptions . RouteComponents [ "key" ] ;
2085+
2086+ request . ODataFeature ( ) . Services = routeProvider ;
2087+
2088+ request . Method = "GET" ;
2089+ request . Host = new HostString ( uri . Host , uri . Port ) ;
2090+ request . Path = uri . LocalPath ;
2091+ request . QueryString = new QueryString ( uri . Query ) ;
2092+
2093+ return request ;
2094+ }
2095+
2096+ static readonly string BASEADDRESS = "http://localhost:16324" ;
2097+ }
2098+
2099+ internal class TestMvcCoreBuilder : IMvcCoreBuilder
2100+ {
2101+ public ApplicationPartManager PartManager { get ; set ; }
2102+ public IServiceCollection Services { get ; set ; }
2103+ }
20192104}
0 commit comments