-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
NorthwindODataQueryTestFixture.cs
96 lines (77 loc) · 3.33 KB
/
NorthwindODataQueryTestFixture.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Microsoft.AspNetCore.OData.Extensions;
using Microsoft.AspNetCore.OData.Routing.Conventions;
using Microsoft.AspNetCore.OData.Routing.Template;
using Microsoft.EntityFrameworkCore.TestModels.Northwind;
using Microsoft.Extensions.Hosting;
using Microsoft.OData.Edm;
using Microsoft.OData.ModelBuilder;
namespace Microsoft.EntityFrameworkCore.Query;
public class NorthwindODataQueryTestFixture : NorthwindQuerySqlServerFixture<NoopModelCustomizer>, IODataQueryTestFixture
{
private IHost _selfHostServer;
protected override string StoreName
=> "ODataNorthwind";
public NorthwindODataQueryTestFixture()
{
(BaseAddress, ClientFactory, _selfHostServer)
= ODataQueryTestFixtureInitializer.Initialize<NorthwindODataContext>(
StoreName,
GetEdmModel(),
new List<IODataControllerActionConvention> { new OrderDetailsControllerActionConvention() });
}
private static IEdmModel GetEdmModel()
{
var modelBuilder = new ODataConventionModelBuilder();
modelBuilder.EntitySet<Customer>("Customers");
modelBuilder.EntitySet<Order>("Orders");
modelBuilder.EntityType<OrderDetail>().HasKey(e => new { e.OrderID, e.ProductID });
modelBuilder.EntitySet<OrderDetail>("Order Details");
return modelBuilder.GetEdmModel();
}
public string BaseAddress { get; }
public IHttpClientFactory ClientFactory { get; }
public override async Task DisposeAsync()
{
if (_selfHostServer != null)
{
await _selfHostServer.StopAsync();
_selfHostServer.Dispose();
_selfHostServer = null;
}
}
}
public class OrderDetailsControllerActionConvention : IODataControllerActionConvention
{
public int Order
=> 0;
public bool AppliesToController(ODataControllerActionContext context)
=> context.Controller.ControllerName == "OrderDetails";
public bool AppliesToAction(ODataControllerActionContext context)
{
if (context.Action.ActionName == "Get")
{
var entitySet = context.Model.EntityContainer.FindEntitySet("Order Details");
var route = new EntitySetSegmentTemplate(entitySet);
var parameters = context.Action.ActionMethod.GetParameters();
if (parameters.Length == 0)
{
var path = new ODataPathTemplate(route);
context.Action.AddSelector("get", context.Prefix, context.Model, path, context.Options.RouteOptions);
return true;
}
if (parameters.Length == 2
&& parameters[0].Name == "keyOrderId"
&& parameters[1].Name == "keyProductId")
{
var keys = new Dictionary<string, string> { { "OrderID", "{keyOrderId}" }, { "ProductID", "{keyProductId}" } };
var keyTemplate = new KeySegmentTemplate(keys, entitySet.EntityType(), entitySet);
var path = new ODataPathTemplate(route, keyTemplate);
context.Action.AddSelector("get", context.Prefix, context.Model, path, context.Options.RouteOptions);
return true;
}
}
return false;
}
}