-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated the Testing package to support multi dbcontext with SQLite
- Loading branch information
Farshad DASHTI
authored and
Farshad DASHTI
committed
Oct 15, 2024
1 parent
2d72080
commit 076778d
Showing
3 changed files
with
57 additions
and
91 deletions.
There are no files selected for viewing
31 changes: 0 additions & 31 deletions
31
src/DfE.CoreLibs.Testing/AutoFixture/Customizations/DbContextCustomization.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,53 @@ | ||
using System.Data.Common; | ||
using System.Diagnostics.CodeAnalysis; | ||
using Microsoft.Data.Sqlite; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.EntityFrameworkCore.Infrastructure; | ||
using Microsoft.EntityFrameworkCore.Storage; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using System.Data.Common; | ||
using System.Diagnostics.CodeAnalysis; | ||
|
||
namespace DfE.CoreLibs.Testing.Helpers | ||
{ | ||
[ExcludeFromCodeCoverage] | ||
public static class DbContextHelper | ||
{ | ||
public static void CreateDbContext<TContext>(IServiceCollection services, Action<TContext>? seedTestData = null) where TContext : DbContext | ||
public static void CreateDbContext<TContext>( | ||
IServiceCollection services, | ||
DbConnection connection, | ||
Action<TContext>? seedTestData = null) where TContext : DbContext | ||
{ | ||
var connectionString = GetConnectionStringFromConfig(); | ||
ConfigureDbContext<TContext>(services, connection); | ||
InitializeDbContext(services, seedTestData); | ||
} | ||
|
||
if (string.IsNullOrEmpty(connectionString) || connectionString.Contains("DataSource=:memory:")) | ||
public static void ConfigureDbContext<TContext>( | ||
IServiceCollection services, | ||
DbConnection connection) where TContext : DbContext | ||
{ | ||
services.AddDbContext<TContext>((sp, options) => | ||
{ | ||
// Sqlite doesn't seem to allow multiple dbContexts added to the same connection | ||
// We are creating a separate in-memory database for each dbContext | ||
// Please feel free to update if you have a better/ more efficient solution | ||
var connection = new SqliteConnection("DataSource=:memory:"); | ||
|
||
connection.Open(); | ||
options.UseSqlite(connection); | ||
}); | ||
} | ||
|
||
services.AddSingleton(connection); | ||
private static void InitializeDbContext<TContext>( | ||
IServiceCollection services, | ||
Action<TContext>? seedTestData) where TContext : DbContext | ||
{ | ||
var serviceProvider = services.BuildServiceProvider(); | ||
using var scope = serviceProvider.CreateScope(); | ||
var dbContext = scope.ServiceProvider.GetRequiredService<TContext>(); | ||
|
||
services.AddDbContext<TContext>((sp, options) => | ||
{ | ||
options.UseSqlite(connection); | ||
}); | ||
var relationalDatabaseCreator = dbContext.Database.GetService<IRelationalDatabaseCreator>(); | ||
if (!dbContext.Database.CanConnect()) | ||
{ | ||
relationalDatabaseCreator.Create(); | ||
} | ||
else | ||
{ | ||
services.AddDbContext<TContext>(options => | ||
{ | ||
options.UseSqlServer(connectionString); | ||
}); | ||
relationalDatabaseCreator.CreateTables(); | ||
} | ||
|
||
var serviceProvider = services.BuildServiceProvider(); | ||
|
||
using var scope = serviceProvider.CreateScope(); | ||
var dbContext = scope.ServiceProvider.GetRequiredService<TContext>(); | ||
dbContext.Database.EnsureCreated(); | ||
|
||
seedTestData?.Invoke(dbContext); | ||
} | ||
|
||
private static string? GetConnectionStringFromConfig() | ||
{ | ||
var configuration = new ConfigurationBuilder() | ||
.SetBasePath(Directory.GetCurrentDirectory()) | ||
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) | ||
.Build(); | ||
|
||
return configuration.GetConnectionString("DefaultConnection"); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters