|
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Aspire.MongoDB.Driver.Tests;
using Aspire.TestUtilities;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using OpenTelemetry.Trace;
using Xunit;
namespace Aspire.MongoDB.EntityFrameworkCore.Tests;
public class EnrichMongoDbTests : ConformanceTests
{
public EnrichMongoDbTests(MongoDbContainerFixture? containerFixture)
: base(containerFixture)
{
}
protected override void RegisterComponent(HostApplicationBuilder builder, Action<MongoDBEntityFrameworkCoreSettings>? configure = null, string? key = null)
{
builder.Services.AddDbContextPool<TestDbContext>(options => options.UseMongoDB(ConnectionString, DatabaseName));
builder.EnrichMongoDbContext<TestDbContext>(configure);
}
[Fact]
public void ShouldThrowIfDbContextIsNotRegistered()
{
HostApplicationBuilder builder = Host.CreateEmptyApplicationBuilder(settings: null);
var exception = Assert.Throws<InvalidOperationException>(() => builder.EnrichMongoDbContext<TestDbContext>());
Assert.Equal("DbContext<TestDbContext> was not registered. Ensure you have registered the DbContext in DI before calling EnrichMongoDbContext.", exception.Message);
}
[Fact]
public void ShouldNotThrowIfDbContextIsRegistered()
{
HostApplicationBuilder builder = Host.CreateEmptyApplicationBuilder(settings: null);
builder.Services.AddDbContext<TestDbContext>(options => options.UseMongoDB(ConnectionString, DatabaseName));
builder.EnrichMongoDbContext<TestDbContext>();
}
protected override void SetupConnectionInformationIsDelayValidated()
{
Assert.Skip("Enrich doesn't use ConnectionString");
}
[Fact]
public void EnrichSupportServiceType()
{
var builder = Host.CreateEmptyApplicationBuilder(null);
builder.Services.AddDbContextPool<ITestDbContext, TestDbContext>(optionsBuilder =>
{
optionsBuilder.UseMongoDB(ConnectionString, DatabaseName);
});
builder.EnrichMongoDbContext<TestDbContext>();
using var host = builder.Build();
var context = host.Services.GetRequiredService<ITestDbContext>() as TestDbContext;
Assert.NotNull(context);
}
[Fact]
public void EnrichSupportCustomOptionsLifetime()
{
var builder = Host.CreateEmptyApplicationBuilder(null);
builder.Services.AddDbContext<ITestDbContext, TestDbContext>(optionsBuilder =>
{
optionsBuilder.UseMongoDB(ConnectionString, DatabaseName);
}, contextLifetime: ServiceLifetime.Singleton);
builder.EnrichMongoDbContext<TestDbContext>();
var optionsDescriptor = builder.Services.FirstOrDefault(sd => sd.ServiceType == typeof(DbContextOptions<TestDbContext>));
Assert.NotNull(optionsDescriptor);
Assert.Equal(ServiceLifetime.Singleton, optionsDescriptor.Lifetime);
using var host = builder.Build();
var context = host.Services.GetRequiredService<ITestDbContext>() as TestDbContext;
Assert.NotNull(context);
}
[Fact]
public void EnrichWithNamedAndNonNamedUsesBoth()
{
var builder = Host.CreateEmptyApplicationBuilder(null);
builder.Configuration.AddInMemoryCollection([
new KeyValuePair<string, string?>("Aspire:MongoDB:EntityFrameworkCore:DisableTracing", "false"),
new KeyValuePair<string, string?>("Aspire:MongoDB:EntityFrameworkCore:TestDbContext:DisableTracing", "true")
]);
builder.Services.AddDbContextPool<TestDbContext>(optionsBuilder =>
{
optionsBuilder.UseMongoDB(ConnectionString, DatabaseName);
});
builder.EnrichMongoDbContext<TestDbContext>();
using var host = builder.Build();
var tracerProvider = host.Services.GetService<TracerProvider>();
Assert.Null(tracerProvider);
}
}
|