-
Couldn't load subscription status.
- Fork 131
Feature: Add EnrichSqliteDatabaseDbContext extension method for WebApplicationBuilder #837
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
2e63b5d
9c4c326
436d2da
af1deff
54e6e77
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,76 @@ | ||||||
| using Microsoft.AspNetCore.Builder; | ||||||
| using Microsoft.EntityFrameworkCore; | ||||||
| using Microsoft.Extensions.Configuration; | ||||||
| using Microsoft.Extensions.DependencyInjection; | ||||||
| using Microsoft.Extensions.Hosting; | ||||||
|
|
||||||
| namespace CommunityToolkit.Aspire.Microsoft.EntityFrameworkCore.Sqlite.Tests; | ||||||
|
|
||||||
| public class EnrichSqliteDatabaseDbContextTests | ||||||
| { | ||||||
| [Fact] | ||||||
| public void EnrichSqliteDatabaseDbContext_RegistersDbContext() | ||||||
| { | ||||||
| // Arrange | ||||||
| var builder = WebApplication.CreateBuilder(); | ||||||
| builder.Configuration.AddInMemoryCollection([ | ||||||
| new KeyValuePair<string, string?>("ConnectionStrings:DefaultConnection", "Data Source=:memory:") | ||||||
| ]); | ||||||
|
|
||||||
| // Act | ||||||
| builder.EnrichSqliteDatabaseDbContext<TestDbContext>(); | ||||||
|
|
||||||
| // Assert | ||||||
| var app = builder.Build(); | ||||||
| var dbContext = app.Services.GetRequiredService<TestDbContext>(); | ||||||
| Assert.NotNull(dbContext); | ||||||
| } | ||||||
|
|
||||||
| [Fact] | ||||||
| public void EnrichSqliteDatabaseDbContext_ThrowsWhenBuilderIsNull() | ||||||
| { | ||||||
| // Act & Assert | ||||||
| Assert.Throws<ArgumentNullException>(() => | ||||||
| AspireEFSqliteExtensions.EnrichSqliteDatabaseDbContext<TestDbContext>(null!)); | ||||||
| } | ||||||
|
|
||||||
| [Fact] | ||||||
| public void EnrichSqliteDatabaseDbContext_DisablesOpenTelemetryWhenFalse() | ||||||
| { | ||||||
| // Arrange | ||||||
| var builder = WebApplication.CreateBuilder(); | ||||||
| builder.Configuration.AddInMemoryCollection([ | ||||||
| new KeyValuePair<string, string?>("ConnectionStrings:DefaultConnection", "Data Source=:memory:") | ||||||
| ]); | ||||||
|
|
||||||
| // Act | ||||||
| builder.EnrichSqliteDatabaseDbContext<TestDbContext>(settings => settings.DisableTracing = true); | ||||||
|
||||||
|
|
||||||
| // Assert - The test passes if no exceptions are thrown and DbContext is registered | ||||||
| var app = builder.Build(); | ||||||
| var dbContext = app.Services.GetRequiredService<TestDbContext>(); | ||||||
| Assert.NotNull(dbContext); | ||||||
| } | ||||||
|
|
||||||
| [Fact] | ||||||
| public void EnrichSqliteDatabaseDbContext_EnablesOpenTelemetryByDefault() | ||||||
| { | ||||||
| // Arrange | ||||||
| var builder = WebApplication.CreateBuilder(); | ||||||
| builder.Configuration.AddInMemoryCollection([ | ||||||
| new KeyValuePair<string, string?>("ConnectionStrings:DefaultConnection", "Data Source=:memory:") | ||||||
| ]); | ||||||
|
|
||||||
| // Act | ||||||
| builder.EnrichSqliteDatabaseDbContext<TestDbContext>(settings => settings.DisableTracing = false); | ||||||
|
||||||
| builder.EnrichSqliteDatabaseDbContext<TestDbContext>(settings => settings.DisableTracing = false); | |
| builder.EnrichSqliteDatabaseDbContext<TestDbContext>(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The connection string is not validated before use. Unlike the existing
AddSqliteDbContextmethod which validates the connection string, this new method directly passessettings.ConnectionStringtoUseSqlitewithout checking if it's null or empty.