-
Notifications
You must be signed in to change notification settings - Fork 1
Experiment using Test containers #175
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
Open
markusrt
wants to merge
4
commits into
master
Choose a base branch
from
test-containers
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,97 @@ | ||
| using System; | ||
| using System.Linq; | ||
| using System.Net.Http; | ||
| using System.Net.Http.Headers; | ||
| using FluentAssertions.Common; | ||
| using Microsoft.AspNetCore.Authentication; | ||
| using Microsoft.AspNetCore.Mvc.Testing; | ||
| using Microsoft.AspNetCore.TestHost; | ||
| using Microsoft.EntityFrameworkCore; | ||
| using Microsoft.EntityFrameworkCore.Diagnostics; | ||
| using Microsoft.Extensions.Configuration; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.Logging; | ||
| using NRZMyk.Server; | ||
| using NRZMyk.Services.Configuration; | ||
| using NRZMyk.Services.Data; | ||
| using Testcontainers.MsSql; | ||
|
|
||
| namespace Api.Integration.Tests | ||
| { | ||
| public static class TestcontainerDbClientFactory | ||
| { | ||
| private static readonly WebApplicationFactory<Program> _factory; | ||
| public static IServiceProvider ServiceProvider => _factory.Services; | ||
|
|
||
| private static readonly MsSqlContainer _dbContainer = new MsSqlBuilder() | ||
| .WithImage("mcr.microsoft.com/mssql/server:2022-latest") | ||
| .WithPassword("Strong_password_123!") | ||
| .Build(); | ||
|
|
||
| static TestcontainerDbClientFactory() | ||
| { | ||
| _dbContainer.StartAsync().Wait(); | ||
|
|
||
| _factory = new WebApplicationFactory<Program>().WithWebHostBuilder(builder => | ||
Check warningCode scanning / CodeQL Missing Dispose call on local IDisposable
Disposable 'WebApplicationFactory<Program>' is created but not disposed.
|
||
| { | ||
| builder.ConfigureTestServices(services => | ||
| { | ||
| services.AddAuthentication(TestAuthHandler.AuthenticationScheme) | ||
| .AddScheme<AuthenticationSchemeOptions, TestAuthHandler>( | ||
| TestAuthHandler.AuthenticationScheme, _ => { }); | ||
|
|
||
| var descriptor = services.SingleOrDefault( | ||
| d => d.ServiceType == | ||
| typeof(DbContextOptions<ApplicationDbContext>)); | ||
| if (descriptor != null) | ||
| { | ||
| services.Remove(descriptor); | ||
| } | ||
|
|
||
| services.AddDbContext<ApplicationDbContext>(options => | ||
| { | ||
| options.UseSqlServer(_dbContainer.GetConnectionString()); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| using (var scope = _factory.Services.CreateScope()) | ||
| { | ||
| var services = scope.ServiceProvider; | ||
| var loggerFactory = services.GetRequiredService<ILoggerFactory>(); | ||
| var configuration = services.GetRequiredService<IConfiguration>(); | ||
| var seedSettings = configuration.Get<DatabaseSeedSettings>(); | ||
|
|
||
| try | ||
| { | ||
| var catalogContext = services.GetRequiredService<ApplicationDbContext>(); | ||
| ApplicationDbContextSeed.SeedAsync(catalogContext, loggerFactory, seedSettings.DatabaseSeed).Wait(); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| var logger = loggerFactory.CreateLogger<Program>(); | ||
| logger.LogError(ex, "An error occurred seeding the DB."); | ||
| } | ||
|
Comment on lines
+70
to
+74
Check noticeCode scanning / CodeQL Generic catch clause
Generic catch clause.
|
||
| } | ||
|
|
||
| AppDomain.CurrentDomain.ProcessExit += Dispose; | ||
| } | ||
|
|
||
| private static void Dispose(object? sender, EventArgs e) { | ||
| var disposeContainerTask = _dbContainer.DisposeAsync().AsTask(); | ||
| disposeContainerTask.Wait(); | ||
| _factory.Dispose(); | ||
| } | ||
|
|
||
| public static HttpClient CreateClient() | ||
| { | ||
| var client = _factory.CreateClient(new WebApplicationFactoryClientOptions | ||
| { | ||
| AllowAutoRedirect = false, | ||
| }); | ||
|
|
||
| client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(TestAuthHandler.AuthenticationScheme); | ||
| return client; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check failure
Code scanning / CodeQL
Hard-coded credentials