-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathGetApplyTests.cs
More file actions
173 lines (154 loc) · 6.37 KB
/
GetApplyTests.cs
File metadata and controls
173 lines (154 loc) · 6.37 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
using AutoMapper.AspNet.OData;
using AutoMapper.OData.EFCore.Tests.Data;
using DAL.EFCore;
using Domain.OData;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.OData;
using Microsoft.AspNetCore.OData.Query;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Xunit;
namespace AutoMapper.OData.EFCore.Tests
{
public class GetApplyTests
{
public GetApplyTests()
{
Initialize();
}
#region Fields
private IServiceProvider serviceProvider;
#endregion Fields
private void Initialize()
{
IServiceCollection services = new ServiceCollection();
IMvcCoreBuilder builder = new TestMvcCoreBuilder
{
Services = services
};
builder.AddOData();
services.AddDbContext<MyDbContext>
(
options =>
{
options.UseInMemoryDatabase("MyDbContext");
options.UseInternalServiceProvider(new ServiceCollection().AddEntityFrameworkInMemoryDatabase().BuildServiceProvider());
},
ServiceLifetime.Transient
)
.AddSingleton<IConfigurationProvider>(new MapperConfiguration(cfg => cfg.AddMaps(typeof(GetTests).Assembly)))
.AddTransient<IMapper>(sp => new Mapper(sp.GetRequiredService<IConfigurationProvider>(), sp.GetService))
.AddTransient<IApplicationBuilder>(sp => new ApplicationBuilder(sp))
.AddRouting()
.AddLogging();
serviceProvider = services.BuildServiceProvider();
MyDbContext context = serviceProvider.GetRequiredService<MyDbContext>();
context.Database.EnsureCreated();
DatabaseInitializer.SeedDatabase(context);
}
[Fact]
public async void OpsTenantGroupbyNameCreatedDate()
{
Test(Get<OpsTenant, TMandator>("/opstenant?$apply=groupby((Name, CreatedDate))"));
Test(await GetAsync<OpsTenant, TMandator>("/opstenant?$apply=groupby((Name, CreatedDate))"));
void Test(ICollection<OpsTenant> collection)
{
Assert.Equal(2, collection.Count);
Assert.Equal("One", collection.First().Name);
Assert.Equal("Two", collection.Last().Name);
Assert.Equal(2, collection.First().Buildings.Count);
Assert.Equal(3, collection.Last().Buildings.Count);
}
}
[Fact]
public async void OpsTenantFilterName()
{
Test(Get<OpsTenant, TMandator>("/opstenant?$apply=filter(Name eq 'One')"));
Test(await GetAsync<OpsTenant, TMandator>("/opstenant?$apply=filter(Name eq 'One')"));
void Test(ICollection<OpsTenant> collection)
{
Assert.Equal(1, collection.Count);
Assert.Equal("One", collection.First().Name);
}
}
[Fact]
public async void BuildingGroupbyTenantAggregateFloorTotal()
{
Test(Get<CoreBuilding, TBuilding>("/corebuilding?$apply=groupby((Builder), aggregate(FloorAmount with sum as FloorTotal))&$orderby=FloorTotal"));
Test(await GetAsync<CoreBuilding, TBuilding>("/corebuilding?$apply=groupby((Builder), aggregate(FloorAmount with sum as FloorTotal))&$orderby=FloorTotal"));
void Test(ICollection<CoreBuilding> collection)
{
Assert.Equal(3, collection.Count);
Assert.Equal("Sam", collection.First().Builder.Name);
// CoreBuilding does not have .FloorTotal
// Assert.Equal(9, collection.First().FloorTotal);
}
}
private ICollection<TModel> Get<TModel, TData>(string query, ODataQueryOptions<TModel> options = null) where TModel : class where TData : class
{
return
(
DoGet
(
serviceProvider.GetRequiredService<IMapper>(),
serviceProvider.GetRequiredService<MyDbContext>()
)
).ToList();
IQueryable<TModel> DoGet(IMapper mapper, MyDbContext context)
{
return context.Set<TData>().GetQuery
(
mapper,
options ?? GetODataQueryOptions<TModel>(query),
new QuerySettings { ODataSettings = new ODataSettings { HandleNullPropagation = HandleNullPropagationOption.False } }
);
}
}
private async Task<ICollection<TModel>> GetAsync<TModel, TData>(string query, IQueryable<TData> dataQueryable, ODataQueryOptions<TModel> options = null, QuerySettings querySettings = null) where TModel : class where TData : class
{
return
(
await DoGet
(
serviceProvider.GetRequiredService<IMapper>()
)
).ToList();
async Task<IQueryable<TModel>> DoGet(IMapper mapper)
{
return await dataQueryable.GetQueryAsync
(
mapper,
options ?? GetODataQueryOptions<TModel>(query),
querySettings
);
}
}
private async Task<ICollection<TModel>> GetAsync<TModel, TData>(string query, ODataQueryOptions<TModel> options = null, QuerySettings querySettings = null) where TModel : class where TData : class
{
return await GetAsync
(
query,
serviceProvider.GetRequiredService<MyDbContext>().Set<TData>(),
options,
querySettings
);
}
private ODataQueryOptions _oDataQueryOptions;
private ODataQueryOptions<TModel> GetODataQueryOptions<TModel>(string query) where TModel : class
{
if (_oDataQueryOptions == null)
{
_oDataQueryOptions = ODataHelpers.GetODataQueryOptions<TModel>
(
query,
serviceProvider
);
}
return (ODataQueryOptions<TModel>)_oDataQueryOptions;
}
}
}