mirror of
https://github.com/meysamhadeli/booking-microservices.git
synced 2026-04-10 17:59:38 +08:00
fix: fix ci failed
This commit is contained in:
parent
e259b64476
commit
20e49770b2
@ -39,7 +39,7 @@ using Testcontainers.PostgreSql;
|
||||
using Testcontainers.RabbitMq;
|
||||
|
||||
public class TestFixture<TEntryPoint> : IAsyncLifetime
|
||||
where TEntryPoint : class
|
||||
where TEntryPoint : class
|
||||
{
|
||||
private readonly WebApplicationFactory<TEntryPoint> _factory;
|
||||
private int Timeout => 120; // Second
|
||||
@ -60,11 +60,11 @@ where TEntryPoint : class
|
||||
get
|
||||
{
|
||||
var claims = new Dictionary<string, object>
|
||||
{
|
||||
{ ClaimTypes.Name, "test@sample.com" },
|
||||
{ ClaimTypes.Role, "admin" },
|
||||
{ "scope", "flight-api" }
|
||||
};
|
||||
{
|
||||
{ ClaimTypes.Name, "test@sample.com" },
|
||||
{ ClaimTypes.Role, "admin" },
|
||||
{ "scope", "flight-api" },
|
||||
};
|
||||
|
||||
var httpClient = _factory.CreateClient();
|
||||
httpClient.SetFakeBearerToken(claims); // Uses FakeJwtBearer
|
||||
@ -73,9 +73,7 @@ where TEntryPoint : class
|
||||
}
|
||||
|
||||
public GrpcChannel Channel =>
|
||||
GrpcChannel.ForAddress(
|
||||
HttpClient.BaseAddress!,
|
||||
new GrpcChannelOptions { HttpClient = HttpClient });
|
||||
GrpcChannel.ForAddress(HttpClient.BaseAddress!, new GrpcChannelOptions { HttpClient = HttpClient });
|
||||
|
||||
public IServiceProvider ServiceProvider => _factory?.Services;
|
||||
public IConfiguration Configuration => _factory?.Services.GetRequiredService<IConfiguration>();
|
||||
@ -83,64 +81,76 @@ where TEntryPoint : class
|
||||
|
||||
protected TestFixture()
|
||||
{
|
||||
_factory = new WebApplicationFactory<TEntryPoint>()
|
||||
.WithWebHostBuilder(
|
||||
builder =>
|
||||
_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 =>
|
||||
{
|
||||
builder.ConfigureAppConfiguration(AddCustomAppSettings);
|
||||
|
||||
builder.UseEnvironment("test");
|
||||
|
||||
builder.ConfigureServices(
|
||||
services =>
|
||||
options.AddPolicy(
|
||||
nameof(ApiScope),
|
||||
policy =>
|
||||
{
|
||||
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
|
||||
});
|
||||
});
|
||||
});
|
||||
policy.AddAuthenticationSchemes(FakeJwtBearerDefaults.AuthenticationScheme);
|
||||
policy.RequireAuthenticatedUser();
|
||||
policy.RequireClaim("scope", "flight-api"); // Test-specific scope
|
||||
}
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
public async Task InitializeAsync()
|
||||
{
|
||||
CancellationTokenSource = new CancellationTokenSource();
|
||||
await StartTestContainerAsync();
|
||||
|
||||
if (ServiceProvider.GetService<ITestHarness>() is { } harness)
|
||||
{
|
||||
await harness.Start();
|
||||
await Task.Delay(1000);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task DisposeAsync()
|
||||
{
|
||||
if (ServiceProvider.GetService<ITestHarness>() is { } harness)
|
||||
{
|
||||
await harness.Stop();
|
||||
}
|
||||
|
||||
await StopTestContainerAsync();
|
||||
await _factory.DisposeAsync();
|
||||
await CancellationTokenSource.CancelAsync();
|
||||
@ -158,10 +168,10 @@ where TEntryPoint : class
|
||||
return null;
|
||||
|
||||
var loggerFactory = LoggerFactory.Create(builder =>
|
||||
{
|
||||
builder.AddXunit(output);
|
||||
builder.SetMinimumLevel(LogLevel.Debug);
|
||||
});
|
||||
{
|
||||
builder.AddXunit(output);
|
||||
builder.SetMinimumLevel(LogLevel.Debug);
|
||||
});
|
||||
return loggerFactory.CreateLogger("TestLogger");
|
||||
}
|
||||
|
||||
@ -180,67 +190,53 @@ where TEntryPoint : class
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
public Task<TResponse> SendAsync<TResponse>(IRequest<TResponse> request)
|
||||
{
|
||||
return ExecuteScopeAsync(
|
||||
sp =>
|
||||
{
|
||||
var mediator = sp.GetRequiredService<IMediator>();
|
||||
return ExecuteScopeAsync(sp =>
|
||||
{
|
||||
var mediator = sp.GetRequiredService<IMediator>();
|
||||
|
||||
return mediator.Send(request);
|
||||
});
|
||||
return mediator.Send(request);
|
||||
});
|
||||
}
|
||||
|
||||
public Task SendAsync(IRequest request)
|
||||
{
|
||||
return ExecuteScopeAsync(
|
||||
sp =>
|
||||
{
|
||||
var mediator = sp.GetRequiredService<IMediator>();
|
||||
return mediator.Send(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
|
||||
public async Task Publish<TMessage>(TMessage message, CancellationToken cancellationToken = default)
|
||||
where TMessage : class, IEvent
|
||||
{
|
||||
await TestHarness.Bus.Publish(message, cancellationToken);
|
||||
}
|
||||
|
||||
public async Task<bool> WaitForPublishing<TMessage>(
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
where TMessage : class, IEvent
|
||||
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);
|
||||
var result = await WaitUntilConditionMet(async () =>
|
||||
{
|
||||
var published = await TestHarness.Published.Any<TMessage>(cancellationToken);
|
||||
|
||||
return published;
|
||||
});
|
||||
return published;
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public async Task<bool> WaitForConsuming<TMessage>(
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
where TMessage : class, IEvent
|
||||
public async Task<bool> WaitForConsuming<TMessage>(CancellationToken cancellationToken = default)
|
||||
where TMessage : class, IEvent
|
||||
{
|
||||
var result = await WaitUntilConditionMet(
|
||||
async () =>
|
||||
{
|
||||
var consumed =
|
||||
await TestHarness.Consumed.Any<TMessage>(cancellationToken);
|
||||
var result = await WaitUntilConditionMet(async () =>
|
||||
{
|
||||
var consumed = await TestHarness.Consumed.Any<TMessage>(cancellationToken);
|
||||
|
||||
return consumed;
|
||||
});
|
||||
return consumed;
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
@ -248,44 +244,31 @@ where TEntryPoint : class
|
||||
public async Task<bool> ShouldProcessedPersistInternalCommand<TInternalCommand>(
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
where TInternalCommand : class, IInternalCommand
|
||||
where TInternalCommand : class, IInternalCommand
|
||||
{
|
||||
var result = await WaitUntilConditionMet(
|
||||
async () =>
|
||||
{
|
||||
return await ExecuteScopeAsync(
|
||||
async sp =>
|
||||
{
|
||||
var persistMessageProcessor =
|
||||
sp.GetService<IPersistMessageProcessor>();
|
||||
var result = await WaitUntilConditionMet(async () =>
|
||||
{
|
||||
return await ExecuteScopeAsync(async sp =>
|
||||
{
|
||||
var persistMessageProcessor = sp.GetService<IPersistMessageProcessor>();
|
||||
|
||||
Guard.Against.Null(
|
||||
persistMessageProcessor,
|
||||
nameof(persistMessageProcessor));
|
||||
Guard.Against.Null(persistMessageProcessor, nameof(persistMessageProcessor));
|
||||
|
||||
var filter =
|
||||
await persistMessageProcessor.GetByFilterAsync(
|
||||
x =>
|
||||
x.DeliveryType ==
|
||||
MessageDeliveryType.Internal &&
|
||||
typeof(TInternalCommand).ToString() ==
|
||||
x.DataType);
|
||||
var filter = await persistMessageProcessor.GetByFilterAsync(x =>
|
||||
x.DeliveryType == MessageDeliveryType.Internal && typeof(TInternalCommand).ToString() == x.DataType
|
||||
);
|
||||
|
||||
var res = filter.Any(
|
||||
x => x.MessageStatus == MessageStatus.Processed);
|
||||
var res = filter.Any(x => x.MessageStatus == MessageStatus.Processed);
|
||||
|
||||
return res;
|
||||
});
|
||||
});
|
||||
return res;
|
||||
});
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// Ref: https://tech.energyhelpline.com/in-memory-testing-with-masstransit/
|
||||
private async Task<bool> WaitUntilConditionMet(
|
||||
Func<Task<bool>> conditionToMet,
|
||||
int? timeoutSecond = null
|
||||
)
|
||||
private async Task<bool> WaitUntilConditionMet(Func<Task<bool>> conditionToMet, int? timeoutSecond = null)
|
||||
{
|
||||
var time = timeoutSecond ?? Timeout;
|
||||
|
||||
@ -338,39 +321,25 @@ where TEntryPoint : class
|
||||
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("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", RabbitMqTestContainer.Hostname),
|
||||
new(
|
||||
"RabbitMqOptions:UserName",
|
||||
TestContainers.RabbitMqContainerConfiguration.UserName),
|
||||
new(
|
||||
"RabbitMqOptions:Password",
|
||||
TestContainers.RabbitMqContainerConfiguration.Password),
|
||||
new("RabbitMqOptions:UserName", TestContainers.RabbitMqContainerConfiguration.UserName),
|
||||
new("RabbitMqOptions:Password", TestContainers.RabbitMqContainerConfiguration.Password),
|
||||
new(
|
||||
"RabbitMqOptions:Port",
|
||||
RabbitMqTestContainer.GetMappedPublicPort(
|
||||
TestContainers.RabbitMqContainerConfiguration.Port)
|
||||
.ToString(NumberFormatInfo.InvariantInfo)),
|
||||
RabbitMqTestContainer
|
||||
.GetMappedPublicPort(TestContainers.RabbitMqContainerConfiguration.Port)
|
||||
.ToString(NumberFormatInfo.InvariantInfo)
|
||||
),
|
||||
new("MongoOptions:ConnectionString", MongoDbTestContainer.GetConnectionString()),
|
||||
new("MongoOptions:DatabaseName", TestContainers.MongoContainerConfiguration.Name),
|
||||
new(
|
||||
"EventStoreOptions:ConnectionString",
|
||||
EventStoreDbTestContainer.GetConnectionString())
|
||||
});
|
||||
new("EventStoreOptions:ConnectionString", EventStoreDbTestContainer.GetConnectionString()),
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
private IHttpContextAccessor AddHttpContextAccessorMock(IServiceProvider serviceProvider)
|
||||
@ -378,8 +347,7 @@ where TEntryPoint : class
|
||||
var httpContextAccessorMock = Substitute.For<IHttpContextAccessor>();
|
||||
using var scope = serviceProvider.CreateScope();
|
||||
|
||||
httpContextAccessorMock.HttpContext = new DefaultHttpContext
|
||||
{ RequestServices = scope.ServiceProvider };
|
||||
httpContextAccessorMock.HttpContext = new DefaultHttpContext { RequestServices = scope.ServiceProvider };
|
||||
|
||||
httpContextAccessorMock.HttpContext.Request.Host = new HostString("localhost", 6012);
|
||||
httpContextAccessorMock.HttpContext.Request.Scheme = "http";
|
||||
@ -389,8 +357,8 @@ where TEntryPoint : class
|
||||
}
|
||||
|
||||
public class TestWriteFixture<TEntryPoint, TWContext> : TestFixture<TEntryPoint>
|
||||
where TEntryPoint : class
|
||||
where TWContext : DbContext
|
||||
where TEntryPoint : class
|
||||
where TWContext : DbContext
|
||||
{
|
||||
public Task ExecuteDbContextAsync(Func<TWContext, Task> action)
|
||||
{
|
||||
@ -404,8 +372,7 @@ where TWContext : DbContext
|
||||
|
||||
public Task ExecuteDbContextAsync(Func<TWContext, IMediator, Task> action)
|
||||
{
|
||||
return ExecuteScopeAsync(
|
||||
sp => action(sp.GetService<TWContext>(), sp.GetService<IMediator>()));
|
||||
return ExecuteScopeAsync(sp => action(sp.GetService<TWContext>(), sp.GetService<IMediator>()));
|
||||
}
|
||||
|
||||
public Task<T> ExecuteDbContextAsync<T>(Func<TWContext, Task<T>> action)
|
||||
@ -420,69 +387,60 @@ where TWContext : DbContext
|
||||
|
||||
public Task<T> ExecuteDbContextAsync<T>(Func<TWContext, IMediator, Task<T>> action)
|
||||
{
|
||||
return ExecuteScopeAsync(
|
||||
sp => action(sp.GetService<TWContext>(), sp.GetService<IMediator>()));
|
||||
return ExecuteScopeAsync(sp => action(sp.GetService<TWContext>(), sp.GetService<IMediator>()));
|
||||
}
|
||||
|
||||
public Task InsertAsync<T>(params T[] entities)
|
||||
where T : class
|
||||
where T : class
|
||||
{
|
||||
return ExecuteDbContextAsync(
|
||||
db =>
|
||||
return ExecuteDbContextAsync(db =>
|
||||
{
|
||||
foreach (var entity in entities)
|
||||
{
|
||||
foreach (var entity in entities)
|
||||
{
|
||||
db.Set<T>().Add(entity);
|
||||
}
|
||||
db.Set<T>().Add(entity);
|
||||
}
|
||||
|
||||
return db.SaveChangesAsync();
|
||||
});
|
||||
return db.SaveChangesAsync();
|
||||
});
|
||||
}
|
||||
|
||||
public async Task InsertAsync<TEntity>(TEntity entity)
|
||||
where TEntity : class
|
||||
where TEntity : class
|
||||
{
|
||||
await ExecuteDbContextAsync(
|
||||
db =>
|
||||
{
|
||||
db.Set<TEntity>().Add(entity);
|
||||
await ExecuteDbContextAsync(db =>
|
||||
{
|
||||
db.Set<TEntity>().Add(entity);
|
||||
|
||||
return db.SaveChangesAsync();
|
||||
});
|
||||
return db.SaveChangesAsync();
|
||||
});
|
||||
}
|
||||
|
||||
public Task InsertAsync<TEntity, TEntity2>(TEntity entity, TEntity2 entity2)
|
||||
where TEntity : class
|
||||
where TEntity2 : class
|
||||
where TEntity : class
|
||||
where TEntity2 : class
|
||||
{
|
||||
return ExecuteDbContextAsync(
|
||||
db =>
|
||||
{
|
||||
db.Set<TEntity>().Add(entity);
|
||||
db.Set<TEntity2>().Add(entity2);
|
||||
return ExecuteDbContextAsync(db =>
|
||||
{
|
||||
db.Set<TEntity>().Add(entity);
|
||||
db.Set<TEntity2>().Add(entity2);
|
||||
|
||||
return db.SaveChangesAsync();
|
||||
});
|
||||
return db.SaveChangesAsync();
|
||||
});
|
||||
}
|
||||
|
||||
public Task InsertAsync<TEntity, TEntity2, TEntity3>(
|
||||
TEntity entity,
|
||||
TEntity2 entity2,
|
||||
TEntity3 entity3
|
||||
)
|
||||
where TEntity : class
|
||||
where TEntity2 : class
|
||||
where TEntity3 : class
|
||||
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 ExecuteDbContextAsync(db =>
|
||||
{
|
||||
db.Set<TEntity>().Add(entity);
|
||||
db.Set<TEntity2>().Add(entity2);
|
||||
db.Set<TEntity3>().Add(entity3);
|
||||
|
||||
return db.SaveChangesAsync();
|
||||
});
|
||||
return db.SaveChangesAsync();
|
||||
});
|
||||
}
|
||||
|
||||
public Task InsertAsync<TEntity, TEntity2, TEntity3, TEntity4>(
|
||||
@ -491,39 +449,38 @@ where TWContext : DbContext
|
||||
TEntity3 entity3,
|
||||
TEntity4 entity4
|
||||
)
|
||||
where TEntity : class
|
||||
where TEntity2 : class
|
||||
where TEntity3 : class
|
||||
where TEntity4 : class
|
||||
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 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();
|
||||
});
|
||||
return db.SaveChangesAsync();
|
||||
});
|
||||
}
|
||||
|
||||
public Task<T> FindAsync<T, TKey>(TKey id)
|
||||
where T : class, IEntity
|
||||
where T : class, IEntity
|
||||
{
|
||||
return ExecuteDbContextAsync(db => db.Set<T>().FindAsync(id).AsTask());
|
||||
}
|
||||
|
||||
public Task<T> FirstOrDefaultAsync<T>()
|
||||
where T : class, IEntity
|
||||
where T : class, IEntity
|
||||
{
|
||||
return ExecuteDbContextAsync(db => db.Set<T>().FirstOrDefaultAsync());
|
||||
}
|
||||
}
|
||||
|
||||
public class TestReadFixture<TEntryPoint, TRContext> : TestFixture<TEntryPoint>
|
||||
where TEntryPoint : class
|
||||
where TRContext : MongoDbContext
|
||||
where TEntryPoint : class
|
||||
where TRContext : MongoDbContext
|
||||
{
|
||||
public Task ExecuteReadContextAsync(Func<TRContext, Task> action)
|
||||
{
|
||||
@ -536,21 +493,19 @@ where TRContext : MongoDbContext
|
||||
}
|
||||
|
||||
public async Task InsertMongoDbContextAsync<T>(string collectionName, params T[] entities)
|
||||
where T : class
|
||||
where T : class
|
||||
{
|
||||
await ExecuteReadContextAsync(
|
||||
async db =>
|
||||
{
|
||||
await db.GetCollection<T>(collectionName).InsertManyAsync(entities.ToList());
|
||||
});
|
||||
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 class TestFixture<TEntryPoint, TWContext, TRContext> : TestWriteFixture<TEntryPoint, TWContext>
|
||||
where TEntryPoint : class
|
||||
where TWContext : DbContext
|
||||
where TRContext : MongoDbContext
|
||||
{
|
||||
public Task ExecuteReadContextAsync(Func<TRContext, Task> action)
|
||||
{
|
||||
@ -563,18 +518,17 @@ where TRContext : MongoDbContext
|
||||
}
|
||||
|
||||
public async Task InsertMongoDbContextAsync<T>(string collectionName, params T[] entities)
|
||||
where T : class
|
||||
where T : class
|
||||
{
|
||||
await ExecuteReadContextAsync(
|
||||
async db =>
|
||||
{
|
||||
await db.GetCollection<T>(collectionName).InsertManyAsync(entities.ToList());
|
||||
});
|
||||
await ExecuteReadContextAsync(async db =>
|
||||
{
|
||||
await db.GetCollection<T>(collectionName).InsertManyAsync(entities.ToList());
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public class TestFixtureCore<TEntryPoint> : IAsyncLifetime
|
||||
where TEntryPoint : class
|
||||
where TEntryPoint : class
|
||||
{
|
||||
private Respawner _reSpawnerDefaultDb;
|
||||
private Respawner _reSpawnerPersistDb;
|
||||
@ -596,7 +550,6 @@ where TEntryPoint : class
|
||||
|
||||
public TestFixture<TEntryPoint> Fixture { get; }
|
||||
|
||||
|
||||
public async Task InitializeAsync()
|
||||
{
|
||||
await InitPostgresAsync();
|
||||
@ -623,12 +576,12 @@ where TEntryPoint : class
|
||||
var dbContext = scope.ServiceProvider.GetRequiredService<PersistMessageDbContext>();
|
||||
await dbContext.Database.EnsureCreatedAsync();
|
||||
|
||||
await Fixture.PersistMessageBackgroundService.StartAsync(
|
||||
Fixture.CancellationTokenSource.Token);
|
||||
await Fixture.PersistMessageBackgroundService.StartAsync(Fixture.CancellationTokenSource.Token);
|
||||
|
||||
_reSpawnerPersistDb = await Respawner.CreateAsync(
|
||||
PersistDbConnection,
|
||||
new RespawnerOptions { DbAdapter = DbAdapter.Postgres });
|
||||
PersistDbConnection,
|
||||
new RespawnerOptions { DbAdapter = DbAdapter.Postgres }
|
||||
);
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(postgresOptions?.ConnectionString) && _dbContextType != null)
|
||||
@ -644,12 +597,9 @@ where TEntryPoint : class
|
||||
}
|
||||
|
||||
_reSpawnerDefaultDb = await Respawner.CreateAsync(
|
||||
DefaultDbConnection,
|
||||
new RespawnerOptions
|
||||
{
|
||||
DbAdapter = DbAdapter.Postgres,
|
||||
TablesToIgnore = ["__EFMigrationsHistory",]
|
||||
});
|
||||
DefaultDbConnection,
|
||||
new RespawnerOptions { DbAdapter = DbAdapter.Postgres, TablesToIgnore = ["__EFMigrationsHistory"] }
|
||||
);
|
||||
|
||||
await SeedDataAsync();
|
||||
}
|
||||
@ -661,8 +611,7 @@ where TEntryPoint : class
|
||||
{
|
||||
await _reSpawnerPersistDb.ResetAsync(PersistDbConnection);
|
||||
|
||||
await Fixture.PersistMessageBackgroundService.StopAsync(
|
||||
Fixture.CancellationTokenSource.Token);
|
||||
await Fixture.PersistMessageBackgroundService.StopAsync(Fixture.CancellationTokenSource.Token);
|
||||
}
|
||||
|
||||
if (DefaultDbConnection is not null)
|
||||
@ -677,31 +626,33 @@ where TEntryPoint : class
|
||||
var dbClient = new MongoClient(Fixture.MongoDbTestContainer?.GetConnectionString());
|
||||
|
||||
var collections = await dbClient
|
||||
.GetDatabase(TestContainers.MongoContainerConfiguration.Name)
|
||||
.ListCollectionsAsync(cancellationToken: cancellationToken);
|
||||
.GetDatabase(TestContainers.MongoContainerConfiguration.Name)
|
||||
.ListCollectionsAsync(cancellationToken: cancellationToken);
|
||||
|
||||
foreach (var collection in collections.ToList())
|
||||
{
|
||||
await dbClient.GetDatabase(TestContainers.MongoContainerConfiguration.Name)
|
||||
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 port =
|
||||
Fixture.RabbitMqTestContainer?.GetMappedPublicPort(TestContainers.RabbitMqContainerConfiguration.ApiPort)
|
||||
?? TestContainers.RabbitMqContainerConfiguration.ApiPort;
|
||||
|
||||
var managementClient = new ManagementClient(Fixture.RabbitMqTestContainer?.Hostname,
|
||||
var managementClient = new ManagementClient(
|
||||
Fixture.RabbitMqTestContainer?.Hostname,
|
||||
TestContainers.RabbitMqContainerConfiguration?.UserName,
|
||||
TestContainers.RabbitMqContainerConfiguration?.Password, port);
|
||||
TestContainers.RabbitMqContainerConfiguration?.Password,
|
||||
port
|
||||
);
|
||||
|
||||
var bd = await managementClient.GetBindingsAsync(cancellationToken);
|
||||
|
||||
var bindings = bd.Where(
|
||||
x => !string.IsNullOrEmpty(x.Source) && !string.IsNullOrEmpty(x.Destination));
|
||||
var bindings = bd.Where(x => !string.IsNullOrEmpty(x.Source) && !string.IsNullOrEmpty(x.Destination));
|
||||
|
||||
foreach (var binding in bindings)
|
||||
{
|
||||
@ -716,9 +667,7 @@ where TEntryPoint : class
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual void RegisterTestsServices(IServiceCollection services)
|
||||
{
|
||||
}
|
||||
protected virtual void RegisterTestsServices(IServiceCollection services) { }
|
||||
|
||||
private async Task SeedDataAsync()
|
||||
{
|
||||
@ -730,14 +679,15 @@ where TEntryPoint : class
|
||||
}
|
||||
|
||||
public abstract class TestReadBase<TEntryPoint, TRContext> : TestFixtureCore<TEntryPoint>
|
||||
// ,IClassFixture<IntegrationTestFactory<TEntryPoint, TWContext>>
|
||||
where TEntryPoint : class
|
||||
where TRContext : MongoDbContext
|
||||
// ,IClassFixture<IntegrationTestFactory<TEntryPoint, TWContext>>
|
||||
where TEntryPoint : class
|
||||
where TRContext : MongoDbContext
|
||||
{
|
||||
protected TestReadBase(
|
||||
TestReadFixture<TEntryPoint, TRContext> integrationTestFixture,
|
||||
ITestOutputHelper outputHelper = null
|
||||
) : base(integrationTestFixture, outputHelper)
|
||||
)
|
||||
: base(integrationTestFixture, outputHelper)
|
||||
{
|
||||
Fixture = integrationTestFixture;
|
||||
}
|
||||
@ -746,14 +696,15 @@ where TRContext : MongoDbContext
|
||||
}
|
||||
|
||||
public abstract class TestWriteBase<TEntryPoint, TWContext> : TestFixtureCore<TEntryPoint>
|
||||
//,IClassFixture<IntegrationTestFactory<TEntryPoint, TWContext>>
|
||||
where TEntryPoint : class
|
||||
where TWContext : DbContext
|
||||
//,IClassFixture<IntegrationTestFactory<TEntryPoint, TWContext>>
|
||||
where TEntryPoint : class
|
||||
where TWContext : DbContext
|
||||
{
|
||||
protected TestWriteBase(
|
||||
TestWriteFixture<TEntryPoint, TWContext> integrationTestFixture,
|
||||
ITestOutputHelper outputHelper = null
|
||||
) : base(integrationTestFixture, outputHelper, typeof(TWContext))
|
||||
)
|
||||
: base(integrationTestFixture, outputHelper, typeof(TWContext))
|
||||
{
|
||||
Fixture = integrationTestFixture;
|
||||
}
|
||||
@ -762,19 +713,19 @@ where TWContext : DbContext
|
||||
}
|
||||
|
||||
public abstract class TestBase<TEntryPoint, TWContext, TRContext> : TestFixtureCore<TEntryPoint>
|
||||
//,IClassFixture<IntegrationTestFactory<TEntryPoint, TWContext, TRContext>>
|
||||
where TEntryPoint : class
|
||||
where TWContext : DbContext
|
||||
where TRContext : MongoDbContext
|
||||
//,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))
|
||||
)
|
||||
: base(integrationTestFixture, outputHelper, typeof(TWContext))
|
||||
{
|
||||
Fixture = integrationTestFixture;
|
||||
}
|
||||
|
||||
public TestFixture<TEntryPoint, TWContext, TRContext> Fixture { get; }
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user