Skip to content

Commit

Permalink
improved context connectionstring initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
TheTrigger committed Nov 14, 2024
1 parent fe49e85 commit dbb2d67
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 85 deletions.
2 changes: 1 addition & 1 deletion F2.Repository.Tests/F2.Repository.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="F2.Testing" Version="8.0.11" />
<PackageReference Include="F2.Testing" Version="8.0.11.4" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="Testcontainers.PostgreSql" Version="4.0.0" />
<PackageReference Include="xunit" Version="2.9.2" />
Expand Down
49 changes: 26 additions & 23 deletions F2.Repository.Tests/TestContainerApplicationFactory.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
using DotNet.Testcontainers.Builders;
using F2.Repository.Demo;
using F2.Repository.Demo.Models;
using F2.Testing;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.TestHost;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using System.Linq;
using Microsoft.Extensions.Configuration;
using System.Collections.Generic;
using System.Threading.Tasks;
using Testcontainers.PostgreSql;
using Xunit;

namespace F2.Repository.Tests;

public class TestContainerApplicationFactory : ServerFixture<Startup>, IAsyncLifetime
public class TestContainerApplicationFactory : ServerFixture<Startup>
{
private const ushort _port = 5432;

Expand All @@ -38,37 +35,43 @@ protected override void ConfigureWebHost(IWebHostBuilder builder)
{
base.ConfigureWebHost(builder);
builder
.ConfigureTestServices(services =>
.ConfigureAppConfiguration((hostingContext, config) =>
{
var descriptor = services.SingleOrDefault(d => d.ServiceType == typeof(DbContextOptions<LibraryContext>));
services?.Remove(descriptor);
services.AddDbContext<LibraryContext>(options =>
var newConfigs = new Dictionary<string, string>
{
// connection string from container
options.UseNpgsql(_applicationDatabase.GetConnectionString(),
npgsqlOptions => npgsqlOptions.MigrationsAssembly(typeof(LibraryContext).Assembly.FullName))
.EnableSensitiveDataLogging(true)
.EnableDetailedErrors()
;
{ "ConnectionStrings:Demo", _applicationDatabase.GetConnectionString() }
};

}, ServiceLifetime.Scoped);
config.AddInMemoryCollection(newConfigs);
})
.ConfigureAppConfiguration((hostingContext, config) =>
.ConfigureTestServices(services =>
{
//var descriptor = services.SingleOrDefault(d => d.ServiceType == typeof(DbContextOptions<LibraryContext>));
//services?.Remove(descriptor);
//services.AddDbContext<LibraryContext>(options =>
//{
// // connection string from container
// options.UseNpgsql(_applicationDatabase.GetConnectionString(),
// npgsqlOptions => npgsqlOptions.MigrationsAssembly(typeof(LibraryContext).Assembly.FullName))
// .EnableSensitiveDataLogging(true)
// .EnableDetailedErrors()
// ;

//}, ServiceLifetime.Scoped);
})

;
}

Task IAsyncLifetime.InitializeAsync()
public override async Task InitializeAsync()
{
return _applicationDatabase.StartAsync();
await _applicationDatabase.StartAsync().ConfigureAwait(false);
await base.InitializeAsync().ConfigureAwait(false);
}


async Task IAsyncLifetime.DisposeAsync()
public async override ValueTask DisposeAsync()
{
await _applicationDatabase.StopAsync().ConfigureAwait(false);

await base.DisposeAsync().ConfigureAwait(false);
}
}
119 changes: 59 additions & 60 deletions F2.Repository.Tests/TestControllerApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,82 +6,81 @@
using System.Threading.Tasks;
using Xunit;

namespace F2.Repository.Tests
namespace F2.Repository.Tests;

public class TestControllerApi : IClassFixture<TestContainerApplicationFactory>
{
public class TestControllerApi : IClassFixture<TestContainerApplicationFactory>
private readonly TestContainerApplicationFactory _serverFixture;
private readonly IServiceScope _scope;
private readonly LibraryDbScope _libraryScope;
public TestControllerApi(TestContainerApplicationFactory serverFixture)
{
private readonly TestContainerApplicationFactory _serverFixture;
private readonly IServiceScope _scope;
private readonly LibraryDbScope _libraryScope;
public TestControllerApi(TestContainerApplicationFactory serverFixture)
{
_serverFixture = serverFixture;
_scope = _serverFixture.Server.Services.CreateScope();
_libraryScope = _scope.ServiceProvider.GetRequiredService<LibraryDbScope>();
}
_serverFixture = serverFixture;
_scope = _serverFixture.Server.Services.CreateScope();
_libraryScope = _scope.ServiceProvider.GetRequiredService<LibraryDbScope>();
}

[Fact]
public async Task CreateExampleDataIfNotExists()
{
var a1 = new Author { Name = "William Shakespeare" };
var a2 = new Author { Name = "Oibi.dev" };
[Fact]
public async Task CreateExampleDataIfNotExists()
{
var a1 = new Author { Name = "William Shakespeare" };
var a2 = new Author { Name = "Oibi.dev" };

_libraryScope.AuthorRepository.Create(a1);
_libraryScope.AuthorRepository.Create(a2);
_libraryScope.AuthorRepository.Create(a1);
_libraryScope.AuthorRepository.Create(a2);

var affectedRows = await _libraryScope.SaveChangesAsync();
Assert.NotEqual(Guid.Empty, a1.Id);
Assert.NotEqual(default, affectedRows);
var affectedRows = await _libraryScope.SaveChangesAsync();
Assert.NotEqual(Guid.Empty, a1.Id);
Assert.NotEqual(default, affectedRows);

var b1 = new Book { Title = "Hamlet", Isbn = "1234567890123" };
var b2 = new Book { Title = "King Lear", Isbn = "0987654321045" };
var b3 = new Book { Id = Guid.NewGuid(), Title = "Random Othello w/ no authors" };
var b1 = new Book { Title = "Hamlet", Isbn = "1234567890123" };
var b2 = new Book { Title = "King Lear", Isbn = "0987654321045" };
var b3 = new Book { Id = Guid.NewGuid(), Title = "Random Othello w/ no authors" };

_libraryScope.BookRepository.Create(b1);
_libraryScope.BookRepository.Create(b2);
_libraryScope.BookRepository.Create(b3);
_libraryScope.BookRepository.Create(b1);
_libraryScope.BookRepository.Create(b2);
_libraryScope.BookRepository.Create(b3);

a1.Books = [b1, b2];
a2.Books = [b2, b3];
a1.Books = [b1, b2];
a2.Books = [b2, b3];

affectedRows = await _libraryScope.SaveChangesAsync();
Assert.NotEqual(default, affectedRows);
affectedRows = await _libraryScope.SaveChangesAsync();
Assert.NotEqual(default, affectedRows);

var results = await _libraryScope.AuthorRepository
.Include(i => i.Books)
.ToListAsync();
var results = await _libraryScope.AuthorRepository
.Include(i => i.Books)
.ToListAsync();

Assert.NotEmpty(results);
foreach (var author in results)
{
Assert.NotEmpty(author.Name);
Assert.NotEmpty(author.Books);
}
Assert.NotEmpty(results);
foreach (var author in results)
{
Assert.NotEmpty(author.Name);
Assert.NotEmpty(author.Books);
}
}

[Fact]
public async Task ToListAsync()
{
var b1 = new Book { Title = "Hamlet", Isbn = "1234567890123" };
_libraryScope.BookRepository.Create(b1);
var affectedRows = await _libraryScope.SaveChangesAsync();
Assert.Equal(1, affectedRows);
[Fact]
public async Task ToListAsync()
{
var b1 = new Book { Title = "Hamlet", Isbn = "1234567890123" };
_libraryScope.BookRepository.Create(b1);
var affectedRows = await _libraryScope.SaveChangesAsync();
Assert.Equal(1, affectedRows);

var results = await _libraryScope.BookRepository.ToListAsync();
Assert.NotNull(results);
}
var results = await _libraryScope.BookRepository.ToListAsync();
Assert.NotNull(results);
}

[Fact]
public async Task CanCreateAndDelete()
{
var a1 = new Author { Name = "William Shakespeare" };
_libraryScope.AuthorRepository.Create(a1);
await _libraryScope.SaveChangesAsync(default);
[Fact]
public async Task CanCreateAndDelete()
{
var a1 = new Author { Name = "William Shakespeare" };
_libraryScope.AuthorRepository.Create(a1);
await _libraryScope.SaveChangesAsync(default);

_libraryScope.AuthorRepository.RemoveRange(a1);
await _libraryScope.SaveChangesAsync(default);
_libraryScope.AuthorRepository.RemoveRange(a1);
await _libraryScope.SaveChangesAsync(default);

Assert.NotEqual(Guid.Empty, a1.Id);
}
Assert.NotEqual(Guid.Empty, a1.Id);
}
}
2 changes: 1 addition & 1 deletion F2.Repository/F2.Repository.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<TargetFramework>net8.0</TargetFramework>
<Title>F2.Repository</Title>
<UserSecretsId>320f5e09-36cb-4c5b-8f02-72cbf1003ad1</UserSecretsId>
<Version>8.0.11</Version>
<Version>8.0.11.1</Version>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit dbb2d67

Please sign in to comment.