Skip to content

Commit c767fcb

Browse files
authored
Restore OData tests for .NET 6 (#2979)
1 parent a14c19e commit c767fcb

File tree

2 files changed

+225
-2
lines changed

2 files changed

+225
-2
lines changed
Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
1+
//------------------------------------------------------------------------------
2+
// <auto-generated>
3+
// This code was generated by AsyncGenerator.
4+
//
5+
// Changes to this file may cause incorrect behavior and will be lost if
6+
// the code is regenerated.
7+
// </auto-generated>
8+
//------------------------------------------------------------------------------
9+
10+
11+
using System;
12+
using System.Collections.Generic;
13+
using System.Linq;
14+
using Microsoft.AspNet.OData;
15+
using Microsoft.AspNet.OData.Builder;
16+
using Microsoft.AspNet.OData.Extensions;
17+
using Microsoft.AspNet.OData.Query;
18+
using Microsoft.AspNet.OData.Query.Expressions;
19+
using Microsoft.AspNetCore.Http;
20+
using Microsoft.OData.Edm;
21+
using NHibernate.DomainModel.Northwind.Entities;
22+
using NUnit.Framework;
23+
using NHibernate.Linq;
24+
25+
namespace NHibernate.Test.Linq
26+
{
27+
using System.Threading.Tasks;
28+
[TestFixture]
29+
public class ODataTestsAsync : LinqTestCase
30+
{
31+
private IEdmModel _edmModel;
32+
33+
protected override void OnSetUp()
34+
{
35+
base.OnSetUp();
36+
37+
_edmModel = CreatEdmModel();
38+
}
39+
40+
[TestCase("$expand=Customer", 830, "Customer")]
41+
[TestCase("$expand=OrderLines", 830, "OrderLines")]
42+
public async Task ExpandAsync(string queryString, int expectedRows, string expandedProperty)
43+
{
44+
var query = ApplyFilter(session.Query<Order>(), queryString);
45+
Assert.That(query, Is.AssignableTo<IQueryable<ISelectExpandWrapper>>());
46+
47+
var results = await (((IQueryable<ISelectExpandWrapper>) query).ToListAsync());
48+
Assert.That(results, Has.Count.EqualTo(expectedRows));
49+
50+
var dict = results[0].ToDictionary();
51+
Assert.That(dict.TryGetValue(expandedProperty, out var value), Is.True);
52+
Assert.That(value, Is.Not.Null);
53+
}
54+
55+
[TestCase("$apply=groupby((Customer/CustomerId))", 89)]
56+
[TestCase("$apply=groupby((Customer/CustomerId))&$orderby=Customer/CustomerId", 89)]
57+
[TestCase("$apply=groupby((Customer/CustomerId, ShippingAddress/PostalCode), aggregate(OrderId with average as Average, Employee/EmployeeId with max as Max))", 89)]
58+
[TestCase("$apply=groupby((Customer/CustomerId), aggregate(OrderId with sum as Total))&$skip=2", 87)]
59+
public async Task OrderGroupByAsync(string queryString, int expectedRows)
60+
{
61+
var query = ApplyFilter(session.Query<Order>(), queryString);
62+
Assert.That(query, Is.AssignableTo<IQueryable<DynamicTypeWrapper>>());
63+
64+
var results = await (((IQueryable<DynamicTypeWrapper>) query).ToListAsync());
65+
Assert.That(results, Has.Count.EqualTo(expectedRows));
66+
}
67+
68+
private class CustomerVm : BaseCustomerVm
69+
{
70+
}
71+
72+
private class BaseCustomerVm
73+
{
74+
public string Id { get; set; }
75+
76+
public string Name { get; set; }
77+
}
78+
79+
[TestCase("$filter=Name eq 'Maria Anders'", 1)]
80+
public async Task BasePropertyFilterAsync(string queryString, int expectedRows)
81+
{
82+
var query = ApplyFilter(
83+
session.Query<Customer>().Select(o => new CustomerVm {Name = o.ContactName, Id = o.CustomerId}),
84+
queryString);
85+
86+
var results = await (((IQueryable<CustomerVm>) query).ToListAsync());
87+
Assert.That(results, Has.Count.EqualTo(expectedRows));
88+
}
89+
90+
//GH-2362
91+
[TestCase("$filter=CustomerId le 'ANATR'", 2)]
92+
[TestCase("$filter=startswith(CustomerId, 'ANATR')", 1)]
93+
[TestCase("$filter=endswith(CustomerId, 'ANATR')", 1)]
94+
[TestCase("$filter=indexof(CustomerId, 'ANATR') eq 0", 1)]
95+
public async Task StringFilterAsync(string queryString, int expectedCount)
96+
{
97+
Assert.That(
98+
await (ApplyFilter(session.Query<Customer>(), queryString).Cast<Customer>().ToListAsync()),
99+
Has.Count.EqualTo(expectedCount));
100+
}
101+
102+
private IQueryable ApplyFilter<T>(IQueryable<T> query, string queryString)
103+
{
104+
var context = new ODataQueryContext(CreatEdmModel(), typeof(T), null) { };
105+
var dataQuerySettings = new ODataQuerySettings {HandleNullPropagation = HandleNullPropagationOption.False};
106+
var serviceProvider = new ODataServiceProvider(
107+
new Dictionary<System.Type, object>()
108+
{
109+
{typeof(DefaultQuerySettings), new DefaultQuerySettings()},
110+
{typeof(ODataOptions), new ODataOptions()},
111+
{typeof(IEdmModel), _edmModel},
112+
{typeof(ODataQuerySettings), dataQuerySettings},
113+
});
114+
115+
HttpContext httpContext = new DefaultHttpContext();
116+
httpContext.ODataFeature().RequestContainer = serviceProvider;
117+
httpContext.RequestServices = serviceProvider;
118+
var request = httpContext.Request;
119+
Uri requestUri = new Uri($"http://localhost/?{queryString}");
120+
request.Method = HttpMethods.Get;
121+
request.Scheme = requestUri.Scheme;
122+
request.Host = new HostString(requestUri.Host);
123+
request.QueryString = new QueryString(requestUri.Query);
124+
request.Path = new PathString(requestUri.AbsolutePath);
125+
var options = new ODataQueryOptions(context, request);
126+
127+
return options.ApplyTo(query, dataQuerySettings);
128+
}
129+
130+
private static IEdmModel CreatEdmModel()
131+
{
132+
var builder = new ODataConventionModelBuilder();
133+
134+
var addressModel = builder.ComplexType<Address>();
135+
addressModel.Property(o => o.City);
136+
addressModel.Property(o => o.Country);
137+
addressModel.Property(o => o.Fax);
138+
addressModel.Property(o => o.PhoneNumber);
139+
addressModel.Property(o => o.PostalCode);
140+
addressModel.Property(o => o.Region);
141+
addressModel.Property(o => o.Street);
142+
143+
var customerModel = builder.EntitySet<Customer>(nameof(Customer));
144+
customerModel.EntityType.HasKey(o => o.CustomerId);
145+
customerModel.EntityType.Property(o => o.CompanyName);
146+
customerModel.EntityType.Property(o => o.ContactTitle);
147+
customerModel.EntityType.ComplexProperty(o => o.Address);
148+
customerModel.EntityType.HasMany(o => o.Orders);
149+
150+
var orderLineModel = builder.EntitySet<OrderLine>(nameof(OrderLine));
151+
orderLineModel.EntityType.HasKey(o => o.Id);
152+
orderLineModel.EntityType.Property(o => o.Discount);
153+
orderLineModel.EntityType.Property(o => o.Quantity);
154+
orderLineModel.EntityType.Property(o => o.UnitPrice);
155+
orderLineModel.EntityType.HasRequired(o => o.Order);
156+
157+
var orderModel = builder.EntitySet<Order>(nameof(Order));
158+
orderModel.EntityType.HasKey(o => o.OrderId);
159+
orderModel.EntityType.Property(o => o.Freight);
160+
orderModel.EntityType.Property(o => o.OrderDate);
161+
orderModel.EntityType.Property(o => o.RequiredDate);
162+
orderModel.EntityType.Property(o => o.ShippedTo);
163+
orderModel.EntityType.Property(o => o.ShippingDate);
164+
orderModel.EntityType.ComplexProperty(o => o.ShippingAddress);
165+
orderModel.EntityType.HasRequired(o => o.Customer);
166+
orderModel.EntityType.HasOptional(o => o.Employee);
167+
orderModel.EntityType.HasMany(o => o.OrderLines);
168+
169+
var employeeModel = builder.EntitySet<Employee>(nameof(Employee));
170+
employeeModel.EntityType.HasKey(o => o.EmployeeId);
171+
employeeModel.EntityType.Property(o => o.BirthDate);
172+
employeeModel.EntityType.Property(o => o.Extension);
173+
employeeModel.EntityType.Property(o => o.FirstName);
174+
employeeModel.EntityType.Property(o => o.HireDate);
175+
employeeModel.EntityType.Property(o => o.LastName);
176+
employeeModel.EntityType.Property(o => o.Notes);
177+
employeeModel.EntityType.Property(o => o.Title);
178+
employeeModel.EntityType.HasMany(o => o.Orders);
179+
180+
builder.EntitySet<CustomerVm>(nameof(CustomerVm));
181+
182+
return builder.GetEdmModel();
183+
}
184+
185+
private class ODataServiceProvider : IServiceProvider
186+
{
187+
private readonly Dictionary<System.Type, object> _singletonObjects = new Dictionary<System.Type, object>();
188+
189+
public ODataServiceProvider(Dictionary<System.Type, object> singletonObjects)
190+
{
191+
_singletonObjects = singletonObjects;
192+
}
193+
194+
public object GetService(System.Type serviceType)
195+
{
196+
if (_singletonObjects.TryGetValue(serviceType, out var service))
197+
{
198+
return service;
199+
}
200+
201+
var ctor = serviceType.GetConstructor(new System.Type[0]);
202+
if (ctor != null)
203+
{
204+
return ctor.Invoke(new object[0]);
205+
}
206+
207+
ctor = serviceType.GetConstructor(new[] { typeof(DefaultQuerySettings) });
208+
if (ctor != null)
209+
{
210+
return ctor.Invoke(new object[] { GetService(typeof(DefaultQuerySettings)) });
211+
}
212+
213+
ctor = serviceType.GetConstructor(new[] { typeof(IServiceProvider) });
214+
if (ctor != null)
215+
{
216+
return ctor.Invoke(new object[] { this });
217+
}
218+
219+
return null;
220+
}
221+
}
222+
}
223+
}

src/NHibernate.Test/NHibernate.Test.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
<Compile Remove="**\CfgTest\HbmOrderingFixture.cs" />
4141
<Compile Remove="**\DynamicProxyTests\PeVerifyFixture.cs" />
4242
<Compile Remove="**\Insertordering\**" />
43-
<Compile Remove="**\Linq\ODataTests.cs" />
4443
<Compile Remove="**\NHSpecificTest\NH1850\**" />
4544
<Compile Remove="**\NHSpecificTest\NH2484\**" />
4645
<Compile Remove="**\NHSpecificTest\NH2188\**" />
@@ -56,6 +55,7 @@
5655
</ItemGroup>
5756
<ItemGroup>
5857
<PackageReference Include="log4net" Version="2.0.12" />
58+
<PackageReference Include="Microsoft.AspNetCore.OData" Version="7.5.12" />
5959
<PackageReference Include="Microsoft.Data.SqlClient" Version="3.0.0" />
6060
<PackageReference Include="System.Data.SQLite.Core" Version="1.0.114.3" />
6161
<PackageReference Include="System.Linq.Dynamic.Core" Version="1.2.12" />
@@ -73,11 +73,11 @@
7373
<Reference Include="System.Transactions" />
7474
<Reference Include="System.Threading.Tasks" />
7575
<Reference Include="System.Data.OracleClient" />
76-
<PackageReference Include="Microsoft.AspNetCore.OData" Version="7.5.12" />
7776
<PackageReference Include="Microsoft.SqlServer.Compact" Version="4.0.8876.1" />
7877
<PackageReference Include="Oracle.ManagedDataAccess" Version="19.12.0" />
7978
</ItemGroup>
8079
<ItemGroup Condition="'$(TargetFramework)'=='net6.0'">
80+
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="6.0.1" />
8181
<PackageReference Include="System.CodeDom" Version="4.7.0" />
8282
<PackageReference Include="System.Data.SqlClient" Version="4.8.2" />
8383
<PackageReference Include="System.Data.OracleClient" Version="1.0.8" />

0 commit comments

Comments
 (0)