generated from frankhaugen/DotnetRepoTemplate
-
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.
Update Logging and Testing projects, and add EntityFrameworkCore project
Multiple changes have been made: An EntityFrameworkCore project has been introduced with a DbContextBuilder class, useful for constructing an instance of DbContext. The Logging project has been updated with the introduction of TestLoggerSettings to better control the logging level. In addition, various updates have been made to our Testing project, including modifications in test run timing and in the output format of our logs.
- Loading branch information
1 parent
42a7ce7
commit 255ae84
Showing
15 changed files
with
229 additions
and
12 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.Infrastructure; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Frank.Testing.EntityFrameworkCore; | ||
|
||
/// <summary> | ||
/// Builder class for constructing an instance of DbContext. | ||
/// </summary> | ||
/// <typeparam name="T">The type of DbContext.</typeparam> | ||
public class DbContextBuilder<T> where T : DbContext | ||
{ | ||
private readonly IServiceCollection _serviceCollection = new ServiceCollection(); | ||
|
||
private Action<DbContextOptionsBuilder>? _configuredOptions; | ||
|
||
private ILoggerFactory? _loggerFactory; | ||
private string _sqliteConnectionString = "Data Source=:memory:"; | ||
|
||
public DbContextBuilder<T> WithLoggerProvider(ILoggerProvider loggerProvider) | ||
{ | ||
_loggerFactory = LoggerFactory.Create(builder => builder.ClearProviders().AddProvider(loggerProvider)); | ||
return this; | ||
} | ||
|
||
public DbContextBuilder<T> WithSqliteConnectionString(string sqliteConnectionString) | ||
{ | ||
_sqliteConnectionString = sqliteConnectionString; | ||
return this; | ||
} | ||
|
||
public DbContextBuilder<T> WithService<TService>(Action<IServiceCollection> configureService) | ||
{ | ||
configureService(_serviceCollection); | ||
return this; | ||
} | ||
|
||
public DbContextBuilder<T> WithOptions(Action<DbContextOptionsBuilder<T>> configureOptions) | ||
{ | ||
_configuredOptions = configureOptions as Action<DbContextOptionsBuilder>; | ||
return this; | ||
} | ||
|
||
public T Build() | ||
{ | ||
_serviceCollection.AddDbContext<T>(OptionsAction); | ||
var context = _serviceCollection.BuildServiceProvider().GetRequiredService<T>(); | ||
context.Database.EnsureDeleted(); | ||
context.Database.EnsureCreated(); | ||
return context; | ||
} | ||
|
||
private void OptionsAction(IServiceProvider arg1, DbContextOptionsBuilder arg2) | ||
{ | ||
_configuredOptions?.Invoke(arg2); | ||
if (_loggerFactory != null) | ||
arg2.UseLoggerFactory(_loggerFactory); | ||
|
||
arg2.EnableSensitiveDataLogging(); | ||
arg2.EnableDetailedErrors(); | ||
arg2.UseSqlite(_sqliteConnectionString); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Frank.Testing.EntityFrameworkCore/Frank.Testing.EntityFrameworkCore.csproj
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<Description>xUnit.net helpers and extensions for writing automated tests</Description> | ||
<PackageTags>xunit test testing TDD BDD</PackageTags> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.1" /> | ||
</ItemGroup> | ||
</Project> |
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
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
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
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
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
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
111 changes: 111 additions & 0 deletions
111
Frank.Testing.Tests/EntityFramworkCoreTests/DbContextBuilderTests.cs
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 |
---|---|---|
@@ -0,0 +1,111 @@ | ||
using Frank.PulseFlow; | ||
using Frank.PulseFlow.Logging; | ||
using Frank.Testing.EntityFrameworkCore; | ||
using Frank.Testing.Logging; | ||
using Frank.Testing.Tests.TestingInfrastructure; | ||
|
||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.Infrastructure; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
using Xunit.Abstractions; | ||
|
||
namespace Frank.Testing.Tests.EntityFramworkCoreTests; | ||
|
||
public class DbContextBuilderTests(ITestOutputHelper outputHelper) | ||
{ | ||
[Fact] | ||
public void Build_WithLoggerProvider_UsesLoggerProvider() | ||
{ | ||
var conduit = new TestConduit(); | ||
var loggerProvider = new TestLoggerProvider(conduit, new TestLoggerSettings()); | ||
var dbContext = new DbContextBuilder<DbContext>() | ||
.WithLoggerProvider(loggerProvider) | ||
.Build(); | ||
dbContext.Database.EnsureCreated(); | ||
dbContext.Database.ExecuteSqlRaw("SELECT 1"); | ||
dbContext.Dispose(); | ||
|
||
outputHelper.WriteJson(conduit.Logs); | ||
// var log = conduit.Logs.Single(); | ||
// Assert.Equal("SELECT 1", log.Message); | ||
} | ||
|
||
[Fact] | ||
public void Build_WithService_UsesService() | ||
{ | ||
var dbContext = new DbContextBuilder<DbContext>() | ||
.WithService<ITestService>(services => services.AddSingleton<ITestService, TestService>()) | ||
.Build(); | ||
Assert.NotNull(dbContext.GetService<ITestService>()); | ||
} | ||
|
||
[Fact] | ||
public void Build_WithOptions_UsesOptions() | ||
{ | ||
var dbContext = new DbContextBuilder<DbContext>() | ||
.WithOptions(options => options.UseSqlite("Data Source=:memory:")) | ||
.Build(); | ||
dbContext.Database.EnsureCreated(); | ||
dbContext.Database.ExecuteSqlRaw("SELECT 1"); | ||
dbContext.Dispose(); | ||
} | ||
|
||
[Fact] | ||
public void Build_WithLoggerProviderAndService_UsesLoggerProviderAndService() | ||
{ | ||
var conduit = new TestConduit(); | ||
var loggerProvider = new TestLoggerProvider(conduit, new TestLoggerSettings()); | ||
var dbContext = new DbContextBuilder<TestDbContext>() | ||
.WithLoggerProvider(loggerProvider) | ||
.WithSqliteConnectionString("Data Source=MyTestDatabase.db") | ||
.WithService<ITestService>(services => services.AddSingleton<ITestService, TestService>()) | ||
.Build(); | ||
dbContext.Database.EnsureCreated(); | ||
dbContext.Persons.Add(new TestPerson() { Name = "Frank" }); | ||
dbContext.SaveChanges(); | ||
dbContext.Dispose(); | ||
} | ||
|
||
public class TestService : ITestService | ||
{ | ||
public async Task DoSomethingAsync() | ||
{ | ||
await Task.CompletedTask; | ||
} | ||
} | ||
|
||
public interface ITestService | ||
{ | ||
Task DoSomethingAsync(); | ||
} | ||
|
||
[Fact] | ||
public void Build_WithLoggerProviderAndOptions_UsesLoggerProviderAndOptions() | ||
{ | ||
var conduit = new TestConduit(); | ||
var loggerProvider = new TestLoggerProvider(conduit, new TestLoggerSettings()); | ||
var dbContext = new DbContextBuilder<TestDbContext>() | ||
.WithSqliteConnectionString("Data Source=MyTestDatabase.db") | ||
.WithLoggerProvider(loggerProvider) | ||
.Build(); | ||
dbContext.Database.EnsureCreated(); | ||
dbContext.Persons.Add(new TestPerson() { Name = "Frank" }); | ||
dbContext.SaveChanges(); | ||
dbContext.Database.ExecuteSqlRaw("SELECT 1"); | ||
dbContext.Dispose(); | ||
|
||
outputHelper.WriteJson(conduit.Logs); | ||
} | ||
|
||
public class TestConduit : IConduit | ||
{ | ||
public async Task SendAsync(IPulse message) | ||
{ | ||
await Task.CompletedTask; | ||
Logs.Add(message as LogPulse ?? throw new InvalidOperationException()); | ||
} | ||
|
||
public List<LogPulse> Logs { get; } = new(); | ||
} | ||
} |
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
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
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
21 changes: 21 additions & 0 deletions
21
Frank.Testing.Tests/TestingInfrastructure/TestDbContext.cs
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace Frank.Testing.Tests.TestingInfrastructure; | ||
|
||
public class TestDbContext : DbContext | ||
{ | ||
public TestDbContext(DbContextOptions<TestDbContext> options) : base(options) | ||
{ | ||
} | ||
|
||
public DbSet<TestPerson> Persons { get; set; } | ||
|
||
public DbSet<TestAddress> Addresses { get; set; } | ||
|
||
protected override void OnModelCreating(ModelBuilder modelBuilder) | ||
{ | ||
modelBuilder.Entity<TestPerson>().HasKey(e => e.Id); | ||
modelBuilder.Entity<TestAddress>().HasKey(e => e.Id); | ||
base.OnModelCreating(modelBuilder); | ||
} | ||
} |
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
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