using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc.Testing; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using StellaOps.Graph.Api.Services; namespace StellaOps.Graph.Api.Tests; public sealed class GraphRuntimeRepositoryRegistrationTests : IClassFixture { private readonly GraphApiPostgresFixture _fixture; public GraphRuntimeRepositoryRegistrationTests(GraphApiPostgresFixture fixture) { _fixture = fixture; } [Fact] [Trait("Category", "Integration")] [Trait("Intent", "Registration")] public void Resolves_PostgresRuntimeRepository_WhenConnectionStringIsConfigured() { using var factory = CreateFactory(_fixture.ConnectionString); using var scope = factory.Services.CreateScope(); var repository = scope.ServiceProvider.GetRequiredService(); Assert.IsType(repository); } [Fact] [Trait("Category", "Integration")] [Trait("Intent", "Registration")] public void Resolves_EmptyInMemoryRuntimeRepository_WhenConnectionStringIsMissing() { using var factory = CreateFactory(string.Empty); using var scope = factory.Services.CreateScope(); var repository = scope.ServiceProvider.GetRequiredService(); var inMemoryRepository = Assert.IsType(repository); Assert.Empty(inMemoryRepository.GetCompatibilityNodes("acme")); Assert.Empty(inMemoryRepository.GetCompatibilityEdges("acme")); } private static WebApplicationFactory CreateFactory(string? connectionString) { return new WebApplicationFactory() .WithWebHostBuilder(builder => { builder.UseEnvironment("Development"); builder.ConfigureAppConfiguration((_, configurationBuilder) => { configurationBuilder.AddInMemoryCollection(new Dictionary { ["Postgres:Graph:ConnectionString"] = connectionString, ["Postgres:Graph:SchemaName"] = "graph", }); }); }); } }