-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathAllTests.cs
410 lines (360 loc) · 16.2 KB
/
AllTests.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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
using AutoMapper.AspNet.OData;
using DAL.EFCore;
using Domain.OData;
using Microsoft.AspNet.OData;
using Microsoft.AspNet.OData.Builder;
using Microsoft.AspNet.OData.Extensions;
using Microsoft.AspNet.OData.Query;
using Microsoft.AspNet.OData.Routing;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Internal;
using Microsoft.AspNetCore.Routing;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.OData.Edm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Xunit;
namespace AutoMapper.OData.EFCore.Tests
{
public class AllTests
{
public AllTests()
{
Initialize();
}
#region Fields
private IServiceProvider serviceProvider;
#endregion Fields
private void Initialize()
{
IServiceCollection services = new ServiceCollection();
services.AddOData();
services.AddDbContext<MyDbContext>
(
options =>
{
options.UseInMemoryDatabase("MyDbContext");
options.UseInternalServiceProvider(new ServiceCollection().AddEntityFrameworkInMemoryDatabase().BuildServiceProvider());
},
ServiceLifetime.Transient
)
.AddSingleton<AutoMapper.IConfigurationProvider>(new MapperConfiguration(cfg => cfg.AddMaps(typeof(AllTests).Assembly)))
.AddTransient<IMapper>(sp => new Mapper(sp.GetRequiredService<AutoMapper.IConfigurationProvider>(), sp.GetService))
.AddTransient<IApplicationBuilder>(sp => new Microsoft.AspNetCore.Builder.Internal.ApplicationBuilder(sp))
.AddTransient<IRouteBuilder>(sp => new RouteBuilder(sp.GetRequiredService<IApplicationBuilder>()));
serviceProvider = services.BuildServiceProvider();
MyDbContext context = serviceProvider.GetRequiredService<MyDbContext>();
context.Database.EnsureCreated();
Seed_Database(context);
}
[Fact]
public async void OpsTenant_expand_Buildings_filter_eq_and_order_by()
{
Test(await Get<OpsTenant, TMandator>("/opstenant?$top=5&$expand=Buildings&$filter=Name eq 'One'&$orderby=Name desc"));
void Test(ICollection<OpsTenant> collection)
{
Assert.True(collection.Count == 1);
Assert.True(collection.First().Buildings.Count == 2);
Assert.True(collection.First().Name == "One");
}
}
[Fact]
public async void OpsTenant_expand_Buildings_filter_ne_and_order_by()
{
Test(await Get<OpsTenant, TMandator>("/opstenant?$top=5&$expand=Buildings&$filter=Name ne 'One'&$orderby=Name desc"));
void Test(ICollection<OpsTenant> collection)
{
Assert.True(collection.Count == 1);
Assert.True(collection.First().Buildings.Count == 2);
Assert.True(collection.First().Name == "Two");
}
}
[Fact]
public async void OpsTenant_filter_eq_no_expand()
{
Test(await Get<OpsTenant, TMandator>("/opstenant?$filter=Name eq 'One'"));
void Test(ICollection<OpsTenant> collection)
{
Assert.True(collection.Count == 1);
Assert.True(collection.First().Buildings.Count == 0);
Assert.True(collection.First().Name == "One");
}
}
[Fact]
public async void OpsTenant_expand_Buildings_no_filter_and_order_by()
{
Test(await Get<OpsTenant, TMandator>("/opstenant?$top=5&$expand=Buildings&$orderby=Name desc"));
void Test(ICollection<OpsTenant> collection)
{
Assert.True(collection.Count == 2);
Assert.True(collection.First().Buildings.Count == 2);
Assert.True(collection.First().Name == "Two");
}
}
[Fact]
public async void OpsTenant_no_expand_no_filter_and_order_by()
{
Test(await Get<OpsTenant, TMandator>("/opstenant?$orderby=Name desc"));
void Test(ICollection<OpsTenant> collection)
{
Assert.True(collection.Count == 2);
Assert.True(collection.First().Buildings.Count == 0);
Assert.True(collection.First().Name == "Two");
}
}
[Fact]
public async void OpsTenant_no_expand_filter_eq_and_order_by()
{
Test(await Get<OpsTenant, TMandator>("/opstenant?$top=5&$filter=Name eq 'One'&$orderby=Name desc"));
void Test(ICollection<OpsTenant> collection)
{
Assert.True(collection.Count == 1);
Assert.True(collection.First().Buildings.Count == 0);
Assert.True(collection.First().Name == "One");
}
}
[Fact]
public async void OpsTenant_expand_Buildings_expand_Builder_expand_City_filter_ne_and_order_by()
{
Test(await Get<OpsTenant, TMandator>("/opstenant?$top=5&$expand=Buildings($expand=Builder($expand=City))&$filter=Name ne 'One'&$orderby=Name desc"));
void Test(ICollection<OpsTenant> collection)
{
Assert.True(collection.Count == 1);
Assert.True(collection.First().Buildings.Count == 2);
Assert.True(collection.First().Buildings.First().Builder != null);
Assert.True(collection.First().Buildings.First().Builder.City != null);
Assert.True(collection.First().Name == "Two");
}
}
[Fact]
public async void Building_expand_Builder_Tenant_filter_eq_and_order_by()
{
Test(await Get<CoreBuilding, TBuilding>("/corebuilding?$top=5&$expand=Builder,Tenant&$filter=name eq 'One L1'"));
void Test(ICollection<CoreBuilding> collection)
{
Assert.True(collection.Count == 1);
Assert.True(collection.First().Builder.Name == "Sam");
Assert.True(collection.First().Tenant.Name == "One");
Assert.True(collection.First().Name == "One L1");
}
}
[Fact]
public async void Building_expand_Builder_Tenant_filter_on_nested_property_and_order_by()
{
Test(await Get<CoreBuilding, TBuilding>("/corebuilding?$top=5&$expand=Builder,Tenant&$filter=Builder/Name eq 'Sam'&$orderby=Name asc"));
void Test(ICollection<CoreBuilding> collection)
{
Assert.True(collection.Count == 2);
Assert.True(collection.First().Builder.Name == "Sam");
Assert.True(collection.First().Tenant.Name == "One");
Assert.True(collection.First().Name == "One L1");
}
}
[Fact]
public async void Building_expand_Builder_Tenant_expand_City_filter_on_property_and_order_by()
{
Test(await Get<CoreBuilding, TBuilding>("/corebuilding?$top=5&$expand=Builder($expand=City),Tenant&$filter=Name ne 'One L2'&$orderby=Name desc"));
void Test(ICollection<CoreBuilding> collection)
{
Assert.True(collection.Count == 3);
Assert.True(collection.First().Builder.City != null);
Assert.True(collection.First().Name != "One L2");
}
}
[Fact]
public async void Building_expand_Builder_Tenant_expand_City_filter_on_nested_nested_property_with_count()
{
string query = "/corebuilding?$top=5&$expand=Builder($expand=City),Tenant&$filter=Builder/City/Name eq 'Leeds'&$count=true";
ODataQueryOptions<CoreBuilding> options = ODataHelpers.GetODataQueryOptions<CoreBuilding>
(
query,
serviceProvider,
serviceProvider.GetRequiredService<IRouteBuilder>()
);
Test
(
await Get<CoreBuilding, TBuilding>
(
query,
options
)
);
void Test(ICollection<CoreBuilding> collection)
{
Assert.Equal(1, options.Request.ODataFeature().TotalCount);
Assert.True(collection.Count == 1);
Assert.True(collection.First().Builder.City.Name == "Leeds");
Assert.True(collection.First().Name == "Two L2");
}
}
[Fact]
public async void Building_expand_Builder_Tenant_expand_City_order_by_name()
{
Test(await Get<CoreBuilding, TBuilding>("/corebuilding?$top=5&$expand=Builder($expand=City),Tenant&$orderby=Name desc"));
void Test(ICollection<CoreBuilding> collection)
{
Assert.True(collection.Count == 4);
Assert.True(collection.First().Builder.City.Name == "Leeds");
Assert.True(collection.First().Name == "Two L2");
}
}
[Fact]
public async void Building_expand_Builder_Tenant_expand_City_order_by_name_then_by_identity()
{
Test(await Get<CoreBuilding, TBuilding>("/corebuilding?$top=5&$expand=Builder($expand=City),Tenant&$orderby=Name desc,Identity"));
void Test(ICollection<CoreBuilding> collection)
{
Assert.True(collection.Count == 4);
Assert.True(collection.First().Builder.City.Name == "Leeds");
Assert.True(collection.First().Name == "Two L2");
}
}
[Fact]
public async void Building_expand_Builder_Tenant_expand_City_order_by_builderName()
{
Test(await Get<CoreBuilding, TBuilding>("/corebuilding?$top=5&$expand=Builder($expand=City),Tenant&$orderby=Builder/Name"));
void Test(ICollection<CoreBuilding> collection)
{
Assert.True(collection.Count == 4);
Assert.True(collection.First().Builder.City.Name == "London");
Assert.True(collection.First().Name == "Two L1");
}
}
[Fact]
public async void Building_expand_Builder_Tenant_expand_City_order_by_builderName_skip_3_take_1_with_count()
{
string query = "/corebuilding?$skip=3&$top=1&$expand=Builder($expand=City),Tenant&$orderby=Name desc,Identity&$count=true";
ODataQueryOptions<CoreBuilding> options = ODataHelpers.GetODataQueryOptions<CoreBuilding>
(
query,
serviceProvider,
serviceProvider.GetRequiredService<IRouteBuilder>()
);
Test
(
await Get<CoreBuilding, TBuilding>
(
query,
options
)
);
void Test(ICollection<CoreBuilding> collection)
{
Assert.Equal(4, options.Request.ODataFeature().TotalCount);
Assert.True(collection.Count == 1);
Assert.True(collection.First().Builder.City.Name == "London");
Assert.True(collection.First().Name == "One L1");
}
}
[Fact]
public async void Building_expand_Builder_Tenant_expand_City_order_by_builderName_skip_3_take_1_no_count()
{
string query = "/corebuilding?$skip=3&$top=1&$expand=Builder($expand=City),Tenant&$orderby=Name desc,Identity";
ODataQueryOptions<CoreBuilding> options = ODataHelpers.GetODataQueryOptions<CoreBuilding>
(
query,
serviceProvider,
serviceProvider.GetRequiredService<IRouteBuilder>()
);
Test
(
await Get<CoreBuilding, TBuilding>
(
query,
options
)
);
void Test(ICollection<CoreBuilding> collection)
{
Assert.Null(options.Request.ODataFeature().TotalCount);
Assert.True(collection.Count == 1);
Assert.True(collection.First().Builder.City.Name == "London");
Assert.True(collection.First().Name == "One L1");
}
}
private async Task<ICollection<TModel>> Get<TModel, TData>(string query, ODataQueryOptions<TModel> options = null) where TModel : class where TData : class
{
return await DoGet
(
serviceProvider.GetRequiredService<IMapper>(),
serviceProvider.GetRequiredService<MyDbContext>()
);
async Task<ICollection<TModel>> DoGet(IMapper mapper, MyDbContext context)
{
return await context.Set<TData>().GetAsync
(
mapper,
options ?? ODataHelpers.GetODataQueryOptions<TModel>
(
query,
serviceProvider,
serviceProvider.GetRequiredService<IRouteBuilder>()
),
HandleNullPropagationOption.False
);
}
}
static void Seed_Database(MyDbContext context)
{
context.City.Add(new TCity { Name = "London" });
context.City.Add(new TCity { Name = "Leeds" });
context.SaveChanges();
List<TCity> cities = context.City.ToList();
context.Builder.Add(new TBuilder { Name = "Sam", CityId = cities.First(b => b.Name == "London").Id });
context.Builder.Add(new TBuilder { Name = "John", CityId = cities.First(b => b.Name == "London").Id });
context.Builder.Add(new TBuilder { Name = "Mark", CityId = cities.First(b => b.Name == "Leeds").Id });
context.SaveChanges();
List<TBuilder> builders = context.Builder.ToList();
context.MandatorSet.Add(new TMandator
{
Identity = Guid.NewGuid(),
Name = "One",
Buildings = new List<TBuilding>
{
new TBuilding { Identity = Guid.NewGuid(), LongName = "One L1", BuilderId = builders.First(b => b.Name == "Sam").Id },
new TBuilding { Identity = Guid.NewGuid(), LongName = "One L2", BuilderId = builders.First(b => b.Name == "Sam").Id }
}
});
context.MandatorSet.Add(new TMandator
{
Identity = Guid.NewGuid(),
Name = "Two",
Buildings = new List<TBuilding>
{
new TBuilding { Identity = Guid.NewGuid(), LongName = "Two L1", BuilderId = builders.First(b => b.Name == "John").Id },
new TBuilding { Identity = Guid.NewGuid(), LongName = "Two L2", BuilderId = builders.First(b => b.Name == "Mark").Id }
}
});
context.SaveChanges();
}
}
public static class ODataHelpers
{
public static ODataQueryOptions<T> GetODataQueryOptions<T>(string queryString, IServiceProvider serviceProvider, IRouteBuilder routeBuilder) where T : class
{
ODataConventionModelBuilder builder = new ODataConventionModelBuilder(serviceProvider);
builder.EntitySet<T>(typeof(T).Name);
IEdmModel model = builder.GetEdmModel();
IEdmEntitySet entitySet = model.EntityContainer.FindEntitySet(typeof(T).Name);
ODataPath path = new ODataPath(new Microsoft.OData.UriParser.EntitySetSegment(entitySet));
routeBuilder.EnableDependencyInjection();
Uri uri = new Uri(BASEADDRES + queryString);
return new ODataQueryOptions<T>
(
new ODataQueryContext(model, typeof(T), path),
new DefaultHttpRequest(new DefaultHttpContext() { RequestServices = serviceProvider })
{
Method = "GET",
Host = new HostString(uri.Host, uri.Port),
Path = uri.LocalPath,
QueryString = new QueryString(uri.Query)
}
);
}
static readonly string BASEADDRES = "http://localhost:16324";
}
}