Skip to content

Restore OData tests under .net 6 #2979

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
223 changes: 223 additions & 0 deletions src/NHibernate.Test/Async/Linq/ODataTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by AsyncGenerator.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------


using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNet.OData;
using Microsoft.AspNet.OData.Builder;
using Microsoft.AspNet.OData.Extensions;
using Microsoft.AspNet.OData.Query;
using Microsoft.AspNet.OData.Query.Expressions;
using Microsoft.AspNetCore.Http;
using Microsoft.OData.Edm;
using NHibernate.DomainModel.Northwind.Entities;
using NUnit.Framework;
using NHibernate.Linq;

namespace NHibernate.Test.Linq
{
using System.Threading.Tasks;
[TestFixture]
public class ODataTestsAsync : LinqTestCase
{
private IEdmModel _edmModel;

protected override void OnSetUp()
{
base.OnSetUp();

_edmModel = CreatEdmModel();
}

[TestCase("$expand=Customer", 830, "Customer")]
[TestCase("$expand=OrderLines", 830, "OrderLines")]
public async Task ExpandAsync(string queryString, int expectedRows, string expandedProperty)
{
var query = ApplyFilter(session.Query<Order>(), queryString);
Assert.That(query, Is.AssignableTo<IQueryable<ISelectExpandWrapper>>());

var results = await (((IQueryable<ISelectExpandWrapper>) query).ToListAsync());
Assert.That(results, Has.Count.EqualTo(expectedRows));

var dict = results[0].ToDictionary();
Assert.That(dict.TryGetValue(expandedProperty, out var value), Is.True);
Assert.That(value, Is.Not.Null);
}

[TestCase("$apply=groupby((Customer/CustomerId))", 89)]
[TestCase("$apply=groupby((Customer/CustomerId))&$orderby=Customer/CustomerId", 89)]
[TestCase("$apply=groupby((Customer/CustomerId, ShippingAddress/PostalCode), aggregate(OrderId with average as Average, Employee/EmployeeId with max as Max))", 89)]
[TestCase("$apply=groupby((Customer/CustomerId), aggregate(OrderId with sum as Total))&$skip=2", 87)]
public async Task OrderGroupByAsync(string queryString, int expectedRows)
{
var query = ApplyFilter(session.Query<Order>(), queryString);
Assert.That(query, Is.AssignableTo<IQueryable<DynamicTypeWrapper>>());

var results = await (((IQueryable<DynamicTypeWrapper>) query).ToListAsync());
Assert.That(results, Has.Count.EqualTo(expectedRows));
}

private class CustomerVm : BaseCustomerVm
{
}

private class BaseCustomerVm
{
public string Id { get; set; }

public string Name { get; set; }
}

[TestCase("$filter=Name eq 'Maria Anders'", 1)]
public async Task BasePropertyFilterAsync(string queryString, int expectedRows)
{
var query = ApplyFilter(
session.Query<Customer>().Select(o => new CustomerVm {Name = o.ContactName, Id = o.CustomerId}),
queryString);

var results = await (((IQueryable<CustomerVm>) query).ToListAsync());
Assert.That(results, Has.Count.EqualTo(expectedRows));
}

//GH-2362
[TestCase("$filter=CustomerId le 'ANATR'", 2)]
[TestCase("$filter=startswith(CustomerId, 'ANATR')", 1)]
[TestCase("$filter=endswith(CustomerId, 'ANATR')", 1)]
[TestCase("$filter=indexof(CustomerId, 'ANATR') eq 0", 1)]
public async Task StringFilterAsync(string queryString, int expectedCount)
{
Assert.That(
await (ApplyFilter(session.Query<Customer>(), queryString).Cast<Customer>().ToListAsync()),
Has.Count.EqualTo(expectedCount));
}

private IQueryable ApplyFilter<T>(IQueryable<T> query, string queryString)
{
var context = new ODataQueryContext(CreatEdmModel(), typeof(T), null) { };
var dataQuerySettings = new ODataQuerySettings {HandleNullPropagation = HandleNullPropagationOption.False};
var serviceProvider = new ODataServiceProvider(
new Dictionary<System.Type, object>()
{
{typeof(DefaultQuerySettings), new DefaultQuerySettings()},
{typeof(ODataOptions), new ODataOptions()},
{typeof(IEdmModel), _edmModel},
{typeof(ODataQuerySettings), dataQuerySettings},
});

HttpContext httpContext = new DefaultHttpContext();
httpContext.ODataFeature().RequestContainer = serviceProvider;
httpContext.RequestServices = serviceProvider;
var request = httpContext.Request;
Uri requestUri = new Uri($"http://localhost/?{queryString}");
request.Method = HttpMethods.Get;
request.Scheme = requestUri.Scheme;
request.Host = new HostString(requestUri.Host);
request.QueryString = new QueryString(requestUri.Query);
request.Path = new PathString(requestUri.AbsolutePath);
var options = new ODataQueryOptions(context, request);

return options.ApplyTo(query, dataQuerySettings);
}

private static IEdmModel CreatEdmModel()
{
var builder = new ODataConventionModelBuilder();

var addressModel = builder.ComplexType<Address>();
addressModel.Property(o => o.City);
addressModel.Property(o => o.Country);
addressModel.Property(o => o.Fax);
addressModel.Property(o => o.PhoneNumber);
addressModel.Property(o => o.PostalCode);
addressModel.Property(o => o.Region);
addressModel.Property(o => o.Street);

var customerModel = builder.EntitySet<Customer>(nameof(Customer));
customerModel.EntityType.HasKey(o => o.CustomerId);
customerModel.EntityType.Property(o => o.CompanyName);
customerModel.EntityType.Property(o => o.ContactTitle);
customerModel.EntityType.ComplexProperty(o => o.Address);
customerModel.EntityType.HasMany(o => o.Orders);

var orderLineModel = builder.EntitySet<OrderLine>(nameof(OrderLine));
orderLineModel.EntityType.HasKey(o => o.Id);
orderLineModel.EntityType.Property(o => o.Discount);
orderLineModel.EntityType.Property(o => o.Quantity);
orderLineModel.EntityType.Property(o => o.UnitPrice);
orderLineModel.EntityType.HasRequired(o => o.Order);

var orderModel = builder.EntitySet<Order>(nameof(Order));
orderModel.EntityType.HasKey(o => o.OrderId);
orderModel.EntityType.Property(o => o.Freight);
orderModel.EntityType.Property(o => o.OrderDate);
orderModel.EntityType.Property(o => o.RequiredDate);
orderModel.EntityType.Property(o => o.ShippedTo);
orderModel.EntityType.Property(o => o.ShippingDate);
orderModel.EntityType.ComplexProperty(o => o.ShippingAddress);
orderModel.EntityType.HasRequired(o => o.Customer);
orderModel.EntityType.HasOptional(o => o.Employee);
orderModel.EntityType.HasMany(o => o.OrderLines);

var employeeModel = builder.EntitySet<Employee>(nameof(Employee));
employeeModel.EntityType.HasKey(o => o.EmployeeId);
employeeModel.EntityType.Property(o => o.BirthDate);
employeeModel.EntityType.Property(o => o.Extension);
employeeModel.EntityType.Property(o => o.FirstName);
employeeModel.EntityType.Property(o => o.HireDate);
employeeModel.EntityType.Property(o => o.LastName);
employeeModel.EntityType.Property(o => o.Notes);
employeeModel.EntityType.Property(o => o.Title);
employeeModel.EntityType.HasMany(o => o.Orders);

builder.EntitySet<CustomerVm>(nameof(CustomerVm));

return builder.GetEdmModel();
}

private class ODataServiceProvider : IServiceProvider
{
private readonly Dictionary<System.Type, object> _singletonObjects = new Dictionary<System.Type, object>();

public ODataServiceProvider(Dictionary<System.Type, object> singletonObjects)
{
_singletonObjects = singletonObjects;
}

public object GetService(System.Type serviceType)
{
if (_singletonObjects.TryGetValue(serviceType, out var service))
{
return service;
}

var ctor = serviceType.GetConstructor(new System.Type[0]);
if (ctor != null)
{
return ctor.Invoke(new object[0]);
}

ctor = serviceType.GetConstructor(new[] { typeof(DefaultQuerySettings) });
if (ctor != null)
{
return ctor.Invoke(new object[] { GetService(typeof(DefaultQuerySettings)) });
}

ctor = serviceType.GetConstructor(new[] { typeof(IServiceProvider) });
if (ctor != null)
{
return ctor.Invoke(new object[] { this });
}

return null;
}
}
}
}
4 changes: 2 additions & 2 deletions src/NHibernate.Test/NHibernate.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
<Compile Remove="**\CfgTest\HbmOrderingFixture.cs" />
<Compile Remove="**\DynamicProxyTests\PeVerifyFixture.cs" />
<Compile Remove="**\Insertordering\**" />
<Compile Remove="**\Linq\ODataTests.cs" />
<Compile Remove="**\NHSpecificTest\NH1850\**" />
<Compile Remove="**\NHSpecificTest\NH2484\**" />
<Compile Remove="**\NHSpecificTest\NH2188\**" />
Expand All @@ -56,6 +55,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="log4net" Version="2.0.12" />
<PackageReference Include="Microsoft.AspNetCore.OData" Version="7.5.12" />
<PackageReference Include="Microsoft.Data.SqlClient" Version="3.0.0" />
<PackageReference Include="System.Data.SQLite.Core" Version="1.0.114.3" />
<PackageReference Include="System.Linq.Dynamic.Core" Version="1.2.12" />
Expand All @@ -73,11 +73,11 @@
<Reference Include="System.Transactions" />
<Reference Include="System.Threading.Tasks" />
<Reference Include="System.Data.OracleClient" />
<PackageReference Include="Microsoft.AspNetCore.OData" Version="7.5.12" />
<PackageReference Include="Microsoft.SqlServer.Compact" Version="4.0.8876.1" />
<PackageReference Include="Oracle.ManagedDataAccess" Version="19.12.0" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)'=='net6.0'">
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="6.0.1" />
<PackageReference Include="System.CodeDom" Version="4.7.0" />
<PackageReference Include="System.Data.SqlClient" Version="4.8.2" />
<PackageReference Include="System.Data.OracleClient" Version="1.0.8" />
Expand Down