-
Notifications
You must be signed in to change notification settings - Fork 290
Expand file tree
/
Copy pathTestBase.cs
More file actions
715 lines (604 loc) · 24.5 KB
/
TestBase.cs
File metadata and controls
715 lines (604 loc) · 24.5 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
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
using System.Net;
using System.Security.Claims;
using Ardalis.GuardClauses;
using BuildingBlocks.Core.Event;
using BuildingBlocks.Core.Model;
using BuildingBlocks.EFCore;
using BuildingBlocks.Mongo;
using BuildingBlocks.PersistMessageProcessor;
using BuildingBlocks.Web;
using Duende.IdentityServer.EntityFramework.Entities;
using EasyNetQ.Management.Client;
using Grpc.Net.Client;
using MassTransit;
using MassTransit.Testing;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using MongoDB.Driver;
using NSubstitute;
using Respawn;
using Respawn.Graph;
using WebMotions.Fake.Authentication.JwtBearer;
using Xunit;
using Xunit.Abstractions;
namespace BuildingBlocks.TestBase;
using System.Globalization;
using Npgsql;
using Testcontainers.EventStoreDb;
using Testcontainers.MongoDb;
using Testcontainers.PostgreSql;
using Testcontainers.RabbitMq;
public class TestFixture<TEntryPoint> : IAsyncLifetime
where TEntryPoint : class
{
private readonly WebApplicationFactory<TEntryPoint> _factory;
private int Timeout => 120; // Second
public ITestHarness TestHarness => ServiceProvider?.GetTestHarness();
private Action<IServiceCollection> TestRegistrationServices { get; set; }
private PostgreSqlContainer PostgresTestcontainer;
private PostgreSqlContainer PostgresPersistTestContainer;
public RabbitMqContainer RabbitMqTestContainer;
public MongoDbContainer MongoDbTestContainer;
public EventStoreDbContainer EventStoreDbTestContainer;
public CancellationTokenSource CancellationTokenSource;
public PersistMessageBackgroundService PersistMessageBackgroundService =>
ServiceProvider.GetRequiredService<PersistMessageBackgroundService>();
public HttpClient HttpClient
{
get
{
var claims = new Dictionary<string, object>
{
{ ClaimTypes.Name, "test@sample.com" },
{ ClaimTypes.Role, "admin" },
{ "scope", "flight-api" },
};
var httpClient = _factory.CreateClient();
httpClient.SetFakeBearerToken(claims); // Uses FakeJwtBearer
return httpClient;
}
}
public GrpcChannel Channel =>
GrpcChannel.ForAddress(HttpClient.BaseAddress!, new GrpcChannelOptions { HttpClient = HttpClient });
public IServiceProvider ServiceProvider => _factory?.Services;
public IConfiguration Configuration => _factory?.Services.GetRequiredService<IConfiguration>();
public ILogger Logger { get; set; }
protected TestFixture()
{
_factory = new WebApplicationFactory<TEntryPoint>().WithWebHostBuilder(builder =>
{
builder.ConfigureAppConfiguration(AddCustomAppSettings);
builder.UseEnvironment("test");
builder.ConfigureServices(services =>
{
TestRegistrationServices?.Invoke(services);
services.ReplaceSingleton(AddHttpContextAccessorMock);
services.AddSingleton<PersistMessageBackgroundService>();
services.RemoveHostedService<PersistMessageBackgroundService>();
// Register all ITestDataSeeder implementations dynamically
services.Scan(scan =>
scan.FromApplicationDependencies() // Scan the current app and its dependencies
.AddClasses(classes => classes.AssignableTo<ITestDataSeeder>()) // Find classes that implement ITestDataSeeder
.AsImplementedInterfaces()
.WithScopedLifetime()
);
// Add Fake JWT Authentication - we can use SetAdminUser method to set authenticate user to existing HttContextAccessor
// https://github.com/webmotions/fake-authentication-jwtbearer
// https://github.com/webmotions/fake-authentication-jwtbearer/issues/14
services
.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = FakeJwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = FakeJwtBearerDefaults.AuthenticationScheme;
})
.AddFakeJwtBearer();
// Mock Authorization Policies
services.AddAuthorization(options =>
{
options.AddPolicy(
nameof(ApiScope),
policy =>
{
policy.AddAuthenticationSchemes(FakeJwtBearerDefaults.AuthenticationScheme);
policy.RequireAuthenticatedUser();
policy.RequireClaim("scope", "flight-api"); // Test-specific scope
}
);
});
});
});
}
public async Task InitializeAsync()
{
CancellationTokenSource = new CancellationTokenSource();
await StartTestContainerAsync();
await TestHarness.Start();
}
public async Task DisposeAsync()
{
await TestHarness.Stop();
await StopTestContainerAsync();
await _factory.DisposeAsync();
await CancellationTokenSource.CancelAsync();
}
public virtual void RegisterServices(Action<IServiceCollection> services)
{
TestRegistrationServices += services;
}
// ref: https://github.com/trbenning/serilog-sinks-xunit
public ILogger CreateLogger(ITestOutputHelper output)
{
if (output == null)
return null;
var loggerFactory = LoggerFactory.Create(builder =>
{
builder.AddXunit(output);
builder.SetMinimumLevel(LogLevel.Debug);
});
return loggerFactory.CreateLogger("TestLogger");
}
protected async Task ExecuteScopeAsync(Func<IServiceProvider, Task> action)
{
using var scope = ServiceProvider.CreateScope();
await action(scope.ServiceProvider);
}
protected async Task<T> ExecuteScopeAsync<T>(Func<IServiceProvider, Task<T>> action)
{
using var scope = ServiceProvider.CreateScope();
var result = await action(scope.ServiceProvider);
return result;
}
public Task<TResponse> SendAsync<TResponse>(IRequest<TResponse> request)
{
return ExecuteScopeAsync(sp =>
{
var mediator = sp.GetRequiredService<IMediator>();
return mediator.Send(request);
});
}
public Task SendAsync(IRequest request)
{
return ExecuteScopeAsync(sp =>
{
var mediator = sp.GetRequiredService<IMediator>();
return mediator.Send(request);
});
}
public async Task Publish<TMessage>(TMessage message, CancellationToken cancellationToken = default)
where TMessage : class, IEvent
{
// Use harness bus to ensure publish happens only after the bus is started.
await TestHarness.Bus.Publish(message, cancellationToken);
}
public async Task<bool> WaitForPublishing<TMessage>(CancellationToken cancellationToken = default)
where TMessage : class, IEvent
{
var result = await WaitUntilConditionMet(
async () =>
{
var published = await TestHarness.Published.Any<TMessage>(cancellationToken);
return published;
},
cancellationToken: cancellationToken
);
return result;
}
public Task<bool> WaitUntilAsync(
Func<Task<bool>> condition,
TimeSpan? timeout = null,
TimeSpan? pollInterval = null,
CancellationToken cancellationToken = default
)
{
var effectiveTimeout = timeout ?? TimeSpan.FromSeconds(Timeout);
var effectivePollInterval = pollInterval ?? TimeSpan.FromMilliseconds(200);
return WaitUntilConditionMet(
conditionToMet: async () =>
{
cancellationToken.ThrowIfCancellationRequested();
return await condition();
},
timeoutSecond: (int)Math.Ceiling(effectiveTimeout.TotalSeconds),
pollInterval: effectivePollInterval,
cancellationToken: cancellationToken
);
}
// Ref: https://tech.energyhelpline.com/in-memory-testing-with-masstransit/
private async Task<bool> WaitUntilConditionMet(
Func<Task<bool>> conditionToMet,
int? timeoutSecond = null,
TimeSpan? pollInterval = null,
CancellationToken cancellationToken = default
)
{
var time = timeoutSecond ?? Timeout;
var delay = pollInterval ?? TimeSpan.FromMilliseconds(100);
var startTime = DateTime.UtcNow;
while (DateTime.UtcNow - startTime <= TimeSpan.FromSeconds(time))
{
cancellationToken.ThrowIfCancellationRequested();
if (await conditionToMet.Invoke())
return true;
await Task.Delay(delay, cancellationToken);
}
return false;
}
private async Task StartTestContainerAsync()
{
PostgresTestcontainer = TestContainers.PostgresTestContainer();
PostgresPersistTestContainer = TestContainers.PostgresPersistTestContainer();
RabbitMqTestContainer = TestContainers.RabbitMqTestContainer();
MongoDbTestContainer = TestContainers.MongoTestContainer();
EventStoreDbTestContainer = TestContainers.EventStoreTestContainer();
await Task.WhenAll(
MongoDbTestContainer.StartAsync(),
PostgresTestcontainer.StartAsync(),
PostgresPersistTestContainer.StartAsync(),
EventStoreDbTestContainer.StartAsync()
);
await RabbitMqTestContainer.StartAsync();
}
private async Task StopTestContainerAsync()
{
await PostgresTestcontainer.StopAsync();
await PostgresPersistTestContainer.StopAsync();
await RabbitMqTestContainer.StopAsync();
await MongoDbTestContainer.StopAsync();
await EventStoreDbTestContainer.StopAsync();
}
private void AddCustomAppSettings(IConfigurationBuilder configuration)
{
//todo: provide better approach for reading `PostgresOptions`
configuration.AddInMemoryCollection(
new KeyValuePair<string, string>[]
{
new("PostgresOptions:ConnectionString", PostgresTestcontainer.GetConnectionString()),
new("PostgresOptions:ConnectionString:Flight", PostgresTestcontainer.GetConnectionString()),
new("PostgresOptions:ConnectionString:Identity", PostgresTestcontainer.GetConnectionString()),
new("PostgresOptions:ConnectionString:Passenger", PostgresTestcontainer.GetConnectionString()),
new("PersistMessageOptions:ConnectionString", PostgresPersistTestContainer.GetConnectionString()),
new("RabbitMqOptions:HostName", "127.0.0.1"),
new("RabbitMqOptions:UserName", TestContainers.RabbitMqContainerConfiguration.UserName),
new("RabbitMqOptions:Password", TestContainers.RabbitMqContainerConfiguration.Password),
new(
"RabbitMqOptions:Port",
RabbitMqTestContainer
.GetMappedPublicPort(TestContainers.RabbitMqContainerConfiguration.Port)
.ToString(NumberFormatInfo.InvariantInfo)
),
new("MongoOptions:ConnectionString", MongoDbTestContainer.GetConnectionString()),
new("MongoOptions:DatabaseName", TestContainers.MongoContainerConfiguration.Name),
new("EventStoreOptions:ConnectionString", EventStoreDbTestContainer.GetConnectionString()),
}
);
}
private IHttpContextAccessor AddHttpContextAccessorMock(IServiceProvider serviceProvider)
{
var httpContextAccessorMock = Substitute.For<IHttpContextAccessor>();
using var scope = serviceProvider.CreateScope();
httpContextAccessorMock.HttpContext = new DefaultHttpContext { RequestServices = scope.ServiceProvider };
httpContextAccessorMock.HttpContext.Request.Host = new HostString("localhost", 6012);
httpContextAccessorMock.HttpContext.Request.Scheme = "http";
return httpContextAccessorMock;
}
}
public class TestWriteFixture<TEntryPoint, TWContext> : TestFixture<TEntryPoint>
where TEntryPoint : class
where TWContext : DbContext
{
public Task ExecuteDbContextAsync(Func<TWContext, Task> action)
{
return ExecuteScopeAsync(sp => action(sp.GetService<TWContext>()));
}
public Task ExecuteDbContextAsync(Func<TWContext, ValueTask> action)
{
return ExecuteScopeAsync(sp => action(sp.GetService<TWContext>()).AsTask());
}
public Task ExecuteDbContextAsync(Func<TWContext, IMediator, Task> action)
{
return ExecuteScopeAsync(sp => action(sp.GetService<TWContext>(), sp.GetService<IMediator>()));
}
public Task<T> ExecuteDbContextAsync<T>(Func<TWContext, Task<T>> action)
{
return ExecuteScopeAsync(sp => action(sp.GetService<TWContext>()));
}
public Task<T> ExecuteDbContextAsync<T>(Func<TWContext, ValueTask<T>> action)
{
return ExecuteScopeAsync(sp => action(sp.GetService<TWContext>()).AsTask());
}
public Task<T> ExecuteDbContextAsync<T>(Func<TWContext, IMediator, Task<T>> action)
{
return ExecuteScopeAsync(sp => action(sp.GetService<TWContext>(), sp.GetService<IMediator>()));
}
public Task InsertAsync<T>(params T[] entities)
where T : class
{
return ExecuteDbContextAsync(db =>
{
foreach (var entity in entities)
{
db.Set<T>().Add(entity);
}
return db.SaveChangesAsync();
});
}
public async Task InsertAsync<TEntity>(TEntity entity)
where TEntity : class
{
await ExecuteDbContextAsync(db =>
{
db.Set<TEntity>().Add(entity);
return db.SaveChangesAsync();
});
}
public Task InsertAsync<TEntity, TEntity2>(TEntity entity, TEntity2 entity2)
where TEntity : class
where TEntity2 : class
{
return ExecuteDbContextAsync(db =>
{
db.Set<TEntity>().Add(entity);
db.Set<TEntity2>().Add(entity2);
return db.SaveChangesAsync();
});
}
public Task InsertAsync<TEntity, TEntity2, TEntity3>(TEntity entity, TEntity2 entity2, TEntity3 entity3)
where TEntity : class
where TEntity2 : class
where TEntity3 : class
{
return ExecuteDbContextAsync(db =>
{
db.Set<TEntity>().Add(entity);
db.Set<TEntity2>().Add(entity2);
db.Set<TEntity3>().Add(entity3);
return db.SaveChangesAsync();
});
}
public Task InsertAsync<TEntity, TEntity2, TEntity3, TEntity4>(
TEntity entity,
TEntity2 entity2,
TEntity3 entity3,
TEntity4 entity4
)
where TEntity : class
where TEntity2 : class
where TEntity3 : class
where TEntity4 : class
{
return ExecuteDbContextAsync(db =>
{
db.Set<TEntity>().Add(entity);
db.Set<TEntity2>().Add(entity2);
db.Set<TEntity3>().Add(entity3);
db.Set<TEntity4>().Add(entity4);
return db.SaveChangesAsync();
});
}
public Task<T> FindAsync<T, TKey>(TKey id)
where T : class, IEntity
{
return ExecuteDbContextAsync(db => db.Set<T>().FindAsync(id).AsTask());
}
public Task<T> FirstOrDefaultAsync<T>()
where T : class, IEntity
{
return ExecuteDbContextAsync(db => db.Set<T>().FirstOrDefaultAsync());
}
}
public class TestReadFixture<TEntryPoint, TRContext> : TestFixture<TEntryPoint>
where TEntryPoint : class
where TRContext : MongoDbContext
{
public Task ExecuteReadContextAsync(Func<TRContext, Task> action)
{
return ExecuteScopeAsync(sp => action(sp.GetRequiredService<TRContext>()));
}
public Task<T> ExecuteReadContextAsync<T>(Func<TRContext, Task<T>> action)
{
return ExecuteScopeAsync(sp => action(sp.GetRequiredService<TRContext>()));
}
public async Task InsertMongoDbContextAsync<T>(string collectionName, params T[] entities)
where T : class
{
await ExecuteReadContextAsync(async db =>
{
await db.GetCollection<T>(collectionName).InsertManyAsync(entities.ToList());
});
}
}
public class TestFixture<TEntryPoint, TWContext, TRContext> : TestWriteFixture<TEntryPoint, TWContext>
where TEntryPoint : class
where TWContext : DbContext
where TRContext : MongoDbContext
{
public Task ExecuteReadContextAsync(Func<TRContext, Task> action)
{
return ExecuteScopeAsync(sp => action(sp.GetRequiredService<TRContext>()));
}
public Task<T> ExecuteReadContextAsync<T>(Func<TRContext, Task<T>> action)
{
return ExecuteScopeAsync(sp => action(sp.GetRequiredService<TRContext>()));
}
public async Task InsertMongoDbContextAsync<T>(string collectionName, params T[] entities)
where T : class
{
await ExecuteReadContextAsync(async db =>
{
await db.GetCollection<T>(collectionName).InsertManyAsync(entities.ToList());
});
}
}
public class TestFixtureCore<TEntryPoint> : IAsyncLifetime
where TEntryPoint : class
{
private Respawner _reSpawnerDefaultDb;
private Respawner _reSpawnerPersistDb;
private NpgsqlConnection DefaultDbConnection { get; set; }
private NpgsqlConnection PersistDbConnection { get; set; }
private Type _dbContextType;
public TestFixtureCore(
TestFixture<TEntryPoint> integrationTestFixture,
ITestOutputHelper outputHelper,
Type dbContextType = null
)
{
Fixture = integrationTestFixture;
integrationTestFixture.RegisterServices(RegisterTestsServices);
integrationTestFixture.Logger = integrationTestFixture.CreateLogger(outputHelper);
_dbContextType = dbContextType;
}
public TestFixture<TEntryPoint> Fixture { get; }
public async Task InitializeAsync()
{
await InitPostgresAsync();
}
public async Task DisposeAsync()
{
await ResetPostgresAsync();
await ResetMongoAsync();
await ResetRabbitMqAsync();
}
private async Task InitPostgresAsync()
{
var postgresOptions = Fixture.ServiceProvider.GetService<PostgresOptions>();
var persistOptions = Fixture.ServiceProvider.GetService<PersistMessageOptions>();
if (!string.IsNullOrEmpty(persistOptions?.ConnectionString))
{
PersistDbConnection = new NpgsqlConnection(persistOptions.ConnectionString);
await PersistDbConnection.OpenAsync();
using var scope = Fixture.ServiceProvider.CreateScope();
var dbContext = scope.ServiceProvider.GetRequiredService<PersistMessageDbContext>();
await dbContext.Database.EnsureCreatedAsync();
await Fixture.PersistMessageBackgroundService.StartAsync(Fixture.CancellationTokenSource.Token);
_reSpawnerPersistDb = await Respawner.CreateAsync(
PersistDbConnection,
new RespawnerOptions { DbAdapter = DbAdapter.Postgres }
);
}
if (!string.IsNullOrEmpty(postgresOptions?.ConnectionString) && _dbContextType != null)
{
DefaultDbConnection = new NpgsqlConnection(postgresOptions.ConnectionString);
await DefaultDbConnection.OpenAsync();
using var scope = Fixture.ServiceProvider.CreateScope();
if (scope.ServiceProvider.GetRequiredService(_dbContextType) is DbContext dbContext)
{
await dbContext.Database.EnsureCreatedAsync();
}
_reSpawnerDefaultDb = await Respawner.CreateAsync(
DefaultDbConnection,
new RespawnerOptions { DbAdapter = DbAdapter.Postgres, TablesToIgnore = ["__EFMigrationsHistory"] }
);
await SeedDataAsync();
}
}
private async Task ResetPostgresAsync()
{
if (PersistDbConnection is not null)
{
await _reSpawnerPersistDb.ResetAsync(PersistDbConnection);
await Fixture.PersistMessageBackgroundService.StopAsync(Fixture.CancellationTokenSource.Token);
}
if (DefaultDbConnection is not null)
{
await _reSpawnerDefaultDb.ResetAsync(DefaultDbConnection);
}
}
private async Task ResetMongoAsync(CancellationToken cancellationToken = default)
{
//https://stackoverflow.com/questions/3366397/delete-everything-in-a-mongodb-database
var dbClient = new MongoClient(Fixture.MongoDbTestContainer?.GetConnectionString());
var collections = await dbClient
.GetDatabase(TestContainers.MongoContainerConfiguration.Name)
.ListCollectionsAsync(cancellationToken: cancellationToken);
foreach (var collection in collections.ToList())
{
await dbClient
.GetDatabase(TestContainers.MongoContainerConfiguration.Name)
.DropCollectionAsync(collection["name"].AsString, cancellationToken);
}
}
private async Task ResetRabbitMqAsync(CancellationToken cancellationToken = default)
{
var port =
Fixture.RabbitMqTestContainer?.GetMappedPublicPort(TestContainers.RabbitMqContainerConfiguration.ApiPort)
?? TestContainers.RabbitMqContainerConfiguration.ApiPort;
var managementClient = new ManagementClient(
Fixture.RabbitMqTestContainer?.Hostname,
TestContainers.RabbitMqContainerConfiguration?.UserName,
TestContainers.RabbitMqContainerConfiguration?.Password,
port
);
var bd = await managementClient.GetBindingsAsync(cancellationToken);
var bindings = bd.Where(x => !string.IsNullOrEmpty(x.Source) && !string.IsNullOrEmpty(x.Destination));
foreach (var binding in bindings)
{
await managementClient.DeleteBindingAsync(binding, cancellationToken);
}
var queues = await managementClient.GetQueuesAsync(cancellationToken: cancellationToken);
foreach (var queue in queues)
{
await managementClient.PurgeAsync(queue, cancellationToken);
}
}
protected virtual void RegisterTestsServices(IServiceCollection services) { }
private async Task SeedDataAsync()
{
using var scope = Fixture.ServiceProvider.CreateScope();
var seedManager = scope.ServiceProvider.GetService<ISeedManager>();
await seedManager.ExecuteTestSeedAsync();
}
}
public abstract class TestReadBase<TEntryPoint, TRContext> : TestFixtureCore<TEntryPoint>
// ,IClassFixture<IntegrationTestFactory<TEntryPoint, TWContext>>
where TEntryPoint : class
where TRContext : MongoDbContext
{
protected TestReadBase(
TestReadFixture<TEntryPoint, TRContext> integrationTestFixture,
ITestOutputHelper outputHelper = null
)
: base(integrationTestFixture, outputHelper)
{
Fixture = integrationTestFixture;
}
public TestReadFixture<TEntryPoint, TRContext> Fixture { get; }
}
public abstract class TestWriteBase<TEntryPoint, TWContext> : TestFixtureCore<TEntryPoint>
//,IClassFixture<IntegrationTestFactory<TEntryPoint, TWContext>>
where TEntryPoint : class
where TWContext : DbContext
{
protected TestWriteBase(
TestWriteFixture<TEntryPoint, TWContext> integrationTestFixture,
ITestOutputHelper outputHelper = null
)
: base(integrationTestFixture, outputHelper, typeof(TWContext))
{
Fixture = integrationTestFixture;
}
public TestWriteFixture<TEntryPoint, TWContext> Fixture { get; }
}
public abstract class TestBase<TEntryPoint, TWContext, TRContext> : TestFixtureCore<TEntryPoint>
//,IClassFixture<IntegrationTestFactory<TEntryPoint, TWContext, TRContext>>
where TEntryPoint : class
where TWContext : DbContext
where TRContext : MongoDbContext
{
protected TestBase(
TestFixture<TEntryPoint, TWContext, TRContext> integrationTestFixture,
ITestOutputHelper outputHelper = null
)
: base(integrationTestFixture, outputHelper, typeof(TWContext))
{
Fixture = integrationTestFixture;
}
public TestFixture<TEntryPoint, TWContext, TRContext> Fixture { get; }
}