From 03a8736161c1b633b968cb0d876fec3b9d8daff7 Mon Sep 17 00:00:00 2001 From: TekH Date: Thu, 21 May 2026 12:53:58 +0200 Subject: [PATCH] Add TryResolveProfileIdAsync method for test profiles Introduced the `TryResolveProfileIdAsync` method in the `RecApplicationTestBase` class to resolve a usable profile ID for tests. The method prioritizes a configured `FakeProfileId` and falls back to querying the database for the first available profile. Added necessary namespaces (`System.Linq`, `System.Threading.Tasks`, and `MediatR`) to support LINQ, async operations, and the `ISender` interface. Implemented dependency injection for querying profiles and added error handling to ensure robustness. --- .../Application/RecApplicationTestBase.cs | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/tests/ReC.Tests/Application/RecApplicationTestBase.cs b/tests/ReC.Tests/Application/RecApplicationTestBase.cs index abc4c47..90796de 100644 --- a/tests/ReC.Tests/Application/RecApplicationTestBase.cs +++ b/tests/ReC.Tests/Application/RecApplicationTestBase.cs @@ -1,10 +1,14 @@ using System; using System.IO; +using System.Linq; +using System.Threading.Tasks; +using MediatR; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using ReC.Application; using ReC.Application.Common.Options; +using ReC.Application.Profile.Queries; using ReC.Infrastructure; namespace ReC.Tests.Application; @@ -21,6 +25,31 @@ public abstract class RecApplicationTestBase : IDisposable protected IServiceProvider ServiceProvider { get; } + /// + /// Resolves a usable profile id for tests that require an existing profile in the database. + /// Prefers the configured FakeProfileId value; otherwise asks the server for the first + /// available profile via the standard read query. Returns null when no profile is + /// configured and none can be discovered. + /// + protected async Task TryResolveProfileIdAsync() + { + var configured = Configuration.GetValue("FakeProfileId"); + if (configured is > 0) + return configured; + + try + { + using var scope = ServiceProvider.CreateScope(); + var sender = scope.ServiceProvider.GetRequiredService(); + var profiles = await sender.Send(new ReadProfileViewQuery { IncludeActions = false }); + return profiles?.FirstOrDefault()?.Id; + } + catch + { + return null; + } + } + private static IConfiguration BuildConfiguration() { var appSettingsPath = LocateApiAppSettings();