Path: redb-route/tests/redb.Route.Tests.Sql/ServiceCollectionExtensionsTests.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/tests/redb.Route.Tests.Sql/ServiceCollectionExtensionsTests.cs
----
using Microsoft.Extensions.DependencyInjection;
using redb.Route.Core;
using redb.Route.Extensions;
using redb.Route.Abstractions;
using redb.Route.Sql;
using redb.Route.Sql.Connection;
using redb.Route.Tests.Sql.E2E.Infrastructure;
namespace redb.Route.Tests.Sql;
///
/// Tests for the DI registration extension method AddRedbRouteSql.
///
public class ServiceCollectionExtensionsTests
{
[Fact]
public void AddRedbRouteSql_RegistersSqlComponent()
{
var services = new ServiceCollection();
services.AddSingleton(Substitute.For());
services.AddRedbRouteSql();
var sp = services.BuildServiceProvider();
sp.GetService().Should().NotBeNull();
}
[Fact]
public void AddRedbRouteSql_RegistersNamedQueryRegistry()
{
var services = new ServiceCollection();
services.AddSingleton(Substitute.For());
services.AddRedbRouteSql();
var sp = services.BuildServiceProvider();
sp.GetService().Should().NotBeNull();
}
[Fact]
public async Task AddDataSource_RegistersFactoryInContextRegistry()
{
var services = new ServiceCollection();
services.AddRedbRouteSql(sql =>
{
sql.AddDataSource("test", Substitute.For());
});
await using var sp = services.BuildServiceProvider();
await using var context = new RouteContext();
foreach (var configurator in sp.GetServices())
configurator.Configure(context);
context.GetFromRegistry("test").Should().NotBeNull();
}
[Fact]
public async Task AddDataSource_WithFactory_RegistersInContextRegistry()
{
var mockFactory = Substitute.For();
var services = new ServiceCollection();
services.AddRedbRouteSql(sql =>
{
sql.AddDataSource("custom", mockFactory);
});
await using var sp = services.BuildServiceProvider();
await using var context = new RouteContext();
foreach (var configurator in sp.GetServices())
configurator.Configure(context);
context.GetFromRegistry("custom").Should().BeSameAs(mockFactory);
}
[Fact]
public async Task AddRedbRouteSql_Twice_KeepsNamedQueriesAndDataSourcesOfBothCalls()
{
var services = new ServiceCollection();
services.AddRedbRouteSql(sql => sql.AddNamedQuery("q1", "SELECT 1").AddDataSource("a", Substitute.For()));
services.AddRedbRouteSql(sql => sql.AddNamedQuery("q2", "SELECT 2").AddDataSource("b", Substitute.For()));
await using var sp = services.BuildServiceProvider();
await using var context = new RouteContext();
foreach (var configurator in sp.GetServices())
configurator.Configure(context);
var registry = context.GetService();
registry.Should().NotBeNull();
registry!.Resolve("q1").Should().Be("SELECT 1", "a second registration (another module) must not drop the first one's queries");
registry.Resolve("q2").Should().Be("SELECT 2");
context.GetFromRegistry("a").Should().NotBeNull();
context.GetFromRegistry("b").Should().NotBeNull();
context.HasComponent("sql").Should().BeTrue();
}
[Fact]
public void AddNamedQuery_RegistersQuery()
{
var services = new ServiceCollection();
services.AddSingleton(Substitute.For());
services.AddRedbRouteSql(sql =>
{
sql.AddNamedQuery("getOrders", "SELECT * FROM orders");
});
var sp = services.BuildServiceProvider();
var registry = sp.GetRequiredService();
registry.Resolve("getOrders").Should().Be("SELECT * FROM orders");
}
[Fact]
public async Task AddMultipleDataSources_AllRegisteredInContext()
{
var services = new ServiceCollection();
services.AddRedbRouteSql(sql =>
{
sql.AddDataSource("db1", Substitute.For());
sql.AddDataSource("db2", Substitute.For());
});
await using var sp = services.BuildServiceProvider();
await using var context = new RouteContext();
foreach (var configurator in sp.GetServices())
configurator.Configure(context);
context.GetFromRegistry("db1").Should().NotBeNull();
context.GetFromRegistry("db2").Should().NotBeNull();
}
[Fact]
public void SqlConfigurationBuilder_FluentChaining()
{
var services = new ServiceCollection();
services.AddSingleton(Substitute.For());
services.AddRedbRouteSql(sql =>
{
var result = sql
.AddDataSource("ds", Substitute.For())
.AddNamedQuery("q1", "SELECT 1")
.AddNamedQuery("q2", "SELECT 2");
result.Should().BeSameAs(sql);
});
}
[Fact]
public void Configurator_IsRegistered()
{
var services = new ServiceCollection();
services.AddRedbRouteSql();
// The startup hook RouteHostedService applies must be present
var descriptor = services.FirstOrDefault(d => d.ServiceType == typeof(IRouteContextConfigurator));
descriptor.Should().NotBeNull();
descriptor!.Lifetime.Should().Be(ServiceLifetime.Singleton);
}
[Fact]
public void AddRedbRouteSql_NullConfigure_Works()
{
var services = new ServiceCollection();
services.AddSingleton(Substitute.For());
// Should not throw
services.AddRedbRouteSql(null);
var sp = services.BuildServiceProvider();
sp.GetService().Should().NotBeNull();
}
[Fact]
public void AddRedbRouteSql_NoConfigure_Works()
{
var services = new ServiceCollection();
services.AddSingleton(Substitute.For());
services.AddRedbRouteSql();
var sp = services.BuildServiceProvider();
sp.GetService().Should().NotBeNull();
}
[Fact]
public async Task Configurator_AddsComponentToContext()
{
var services = new ServiceCollection();
services.AddRedbRouteSql(sql =>
{
sql.AddDataSource("main", Substitute.For());
});
await using var sp = services.BuildServiceProvider();
await using var context = new RouteContext();
foreach (var configurator in sp.GetServices())
configurator.Configure(context);
context.HasComponent("sql").Should().BeTrue();
}
// ── Named queries through the host path ─────────────────────────
[Fact]
public async Task AddNamedQuery_RefResolvesInProducer()
{
using var db = NamedItemsDatabase();
await using var sp = NamedQueryServices(db).BuildServiceProvider();
await using var context = ConfiguredContext(sp);
var endpoint = RefEndpoint(sp, new() { ["mode"] = "Execute", ["outputType"] = "SelectList" });
var exchange = new Exchange(new Message("untouched"));
var thrown = await Outcome.Of(() => endpoint.CreateProducer().Process(exchange, CancellationToken.None));
thrown.Should().BeNull(Outcome.Describe(thrown));
exchange.In.Body.Should().BeAssignableTo>>()
.Which.Select(r => r["val"]).Should().Equal("a", "b");
}
[Fact]
public async Task AddNamedQuery_RefResolvesInPollConsumer()
{
using var db = NamedItemsDatabase();
await using var sp = NamedQueryServices(db).BuildServiceProvider();
await using var context = ConfiguredContext(sp);
var endpoint = RefEndpoint(sp, new() { ["mode"] = "Poll", ["repeatCount"] = "1" });
var received = new List