Repository navigation
Feature/11 api postgres #43
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
base: develop
Are you sure you want to change the base?
Changes from all commits
c2d4584
4be3b27
0059579
b8e802b
d606f29
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| using Microsoft.EntityFrameworkCore; | ||
|
|
||
| namespace MoissonMontreal.Api.Data; | ||
|
|
||
| public class MoissonDbContext(DbContextOptions<MoissonDbContext> options) : DbContext(options) | ||
| { | ||
| // Les tables (DbSet<Don>, DbSet<Recu>, ...) seront ajoutées avec l'issue du schéma. | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,4 @@ | ||
| @MoissonMontreal.Api_HostAddress = http://localhost:5297 | ||
|
|
||
| GET {{MoissonMontreal.Api_HostAddress}}/weatherforecast/ | ||
| Accept: application/json | ||
|
|
||
| GET {{MoissonMontreal.Api_HostAddress}}/health | ||
| ### |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,20 @@ | ||
| using Microsoft.EntityFrameworkCore; | ||
| using MoissonMontreal.Api.Data; | ||
|
|
||
|
|
||
| var builder = WebApplication.CreateBuilder(args); | ||
|
|
||
| var connectionString = builder.Configuration.GetConnectionString("Moisson") | ||
| ?? throw new InvalidOperationException( | ||
| "La chaîne de connexion 'Moisson' est absente. Définissez ConnectionStrings__Moisson " + | ||
| "ou utilisez 'dotnet user-secrets'."); | ||
|
|
||
|
|
||
| // Add services to the container. | ||
| builder.Services.AddDbContext<MoissonDbContext>(options => options.UseNpgsql(connectionString)); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Heads-up for the migrations issue: because the connection string is read and validated at startup (line 7), |
||
|
|
||
| builder.Services.AddHealthChecks() | ||
| .AddDbContextCheck<MoissonDbContext>("database"); | ||
|
|
||
| builder.Services.AddControllers(); | ||
| // Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi | ||
|
|
@@ -20,4 +34,6 @@ | |
|
|
||
| app.MapControllers(); | ||
|
|
||
| app.MapHealthChecks("/health"); | ||
|
Comment on lines
14
to
+37
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. MVC configuration is currently pointless, but we will most likely add controllers soon. We can keep them for now
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| app.Run(); | ||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| using System.Net; | ||
| using Microsoft.AspNetCore.Hosting; | ||
| using Microsoft.AspNetCore.Mvc.Testing; | ||
| using Testcontainers.PostgreSql; | ||
|
|
||
| namespace MoissonMontreal.Tests.Api; | ||
|
|
||
| public class HealthEndpointTests : IAsyncLifetime | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These tests require Docker to be running, otherwise |
||
| { | ||
| private readonly PostgreSqlContainer _postgres = new PostgreSqlBuilder("postgres:17").Build(); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. xUnit creates a new instance of the class per test, so this starts a new Postgres container for each test (including the "unreachable" one, which doesn't use it). Moving the container into a shared |
||
|
|
||
| public Task InitializeAsync() => _postgres.StartAsync(); | ||
|
|
||
| public Task DisposeAsync() => _postgres.DisposeAsync().AsTask(); | ||
|
|
||
| [Fact] | ||
| public async Task Health_RetourneHealthy_QuandLaBaseRepond() | ||
| { | ||
| await using var factory = CreerFactory(_postgres.GetConnectionString()); | ||
| using var client = factory.CreateClient(); | ||
|
|
||
| var response = await client.GetAsync("/health"); | ||
|
|
||
| Assert.Equal(HttpStatusCode.OK, response.StatusCode); | ||
| Assert.Equal("Healthy", await response.Content.ReadAsStringAsync()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task Health_RetourneUnhealthy_QuandLaBaseEstInjoignable() | ||
| { | ||
| // Rien n'écoute sur le port 1 : la connexion échoue immédiatement. | ||
| await using var factory = CreerFactory("Host=localhost;Port=1;Database=inexistante;Username=test;Timeout=2"); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor: |
||
| using var client = factory.CreateClient(); | ||
|
|
||
| var response = await client.GetAsync("/health"); | ||
|
|
||
| Assert.Equal(HttpStatusCode.ServiceUnavailable, response.StatusCode); | ||
| Assert.Equal("Unhealthy", await response.Content.ReadAsStringAsync()); | ||
| } | ||
|
|
||
| private static WebApplicationFactory<Program> CreerFactory(string connectionString) => | ||
| new WebApplicationFactory<Program>().WithWebHostBuilder(builder => builder | ||
| .UseEnvironment("Testing") | ||
| .UseSetting("ConnectionStrings:Moisson", connectionString)); | ||
| } | ||
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.
Cloning the repo causes a crash on
dotnet run:ConnectionStrings:Moissonisn't defined anywhere, even thoughcompose.yamlalready sets the dev credentials. Could we add the local dev connection string toappsettings.Development.json?These are local-only Docker credentials, so committing them is fine. User-secrets/env vars can still override them.