From 9cfdd169704f40ab43bcb0e22286ee44d334a2bd Mon Sep 17 00:00:00 2001 From: TekH Date: Mon, 19 Jan 2026 15:51:09 +0100 Subject: [PATCH] Refactor test DB config to support SQL Server and fix seeding Refactored Fake.cs to configure both in-memory and SQL Server database contexts for testing, using the "Default" connection string from configuration. Added detailed EF logging and SQL executor setup. In TestBase.cs, fixed Setup to use the correct repository instance for seeding email templates. --- EnvelopeGenerator.Tests/Fake.cs | 30 ++++++++++++++++++++++++----- EnvelopeGenerator.Tests/TestBase.cs | 4 +++- EnvelopeGenerator.sln | 14 +++++++------- 3 files changed, 35 insertions(+), 13 deletions(-) diff --git a/EnvelopeGenerator.Tests/Fake.cs b/EnvelopeGenerator.Tests/Fake.cs index 3c886904..75b15e36 100644 --- a/EnvelopeGenerator.Tests/Fake.cs +++ b/EnvelopeGenerator.Tests/Fake.cs @@ -1,8 +1,10 @@ using Bogus; +using CommandDotNet; using DigitalData.Core.Abstraction.Application.Repository; using DigitalData.UserManager.Domain.Entities; using EnvelopeGenerator.Application; using EnvelopeGenerator.Application.Common.Configurations; +using EnvelopeGenerator.Application.Common.Extensions; using EnvelopeGenerator.Application.EnvelopeReceivers.Commands; using EnvelopeGenerator.Application.Envelopes.Commands; using EnvelopeGenerator.Application.Histories.Commands; @@ -20,7 +22,6 @@ using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using QuestPDF.Fluent; using QuestPDF.Infrastructure; -using EnvelopeGenerator.Application.Common.Extensions; namespace EnvelopeGenerator.Tests; @@ -42,10 +43,29 @@ public class Fake // add Application and Infrastructure services #pragma warning disable CS0618 services.AddEnvelopeGeneratorServices(configuration); - services.AddEnvelopeGeneratorInfrastructureServices( - (sp, options) => options.UseInMemoryDatabase("EnvelopeGeneratorTestDb"), - context.Configuration - ); + + var cnnStrName = "Default"; + + var connStr = context.Configuration.GetConnectionString(cnnStrName) + ?? throw new InvalidOperationException($"Connection string '{cnnStrName}' is missing in the application configuration."); + + services.AddEnvelopeGeneratorInfrastructureServices(opt => + { + opt.AddDbContext(dbCtxOpt => dbCtxOpt.UseInMemoryDatabase("EnvelopeGeneratorTestDb")); + + opt.AddDbTriggerParams(context.Configuration); + + opt.AddDbContext((provider, options) => + { + var logger = provider.GetRequiredService>(); + options.UseSqlServer(connStr) + .LogTo(log => logger.LogInformation("{log}", log), Microsoft.Extensions.Logging.LogLevel.Trace) + .EnableSensitiveDataLogging() + .EnableDetailedErrors(); + }); + opt.AddSQLExecutor(executor => executor.ConnectionString = connStr); + + }); var prodCnnStr = context.Configuration.GetConnectionString("Default"); services.AddDbContext(opt => opt.UseSqlServer(prodCnnStr)); diff --git a/EnvelopeGenerator.Tests/TestBase.cs b/EnvelopeGenerator.Tests/TestBase.cs index 44cf7f17..f22caf41 100644 --- a/EnvelopeGenerator.Tests/TestBase.cs +++ b/EnvelopeGenerator.Tests/TestBase.cs @@ -32,9 +32,11 @@ public abstract class TestBase : Faker Host = Fake.CreateHost(ConfigureServices); await Host.AddSamples(); + var repo = GetRepository(); + // Add seed email templates foreach (var temp in SeedEmailTemplates) - await Repository.CreateAsync(temp); + await repo.CreateAsync(temp); } [TearDown] diff --git a/EnvelopeGenerator.sln b/EnvelopeGenerator.sln index 1123bc47..b4db47b9 100644 --- a/EnvelopeGenerator.sln +++ b/EnvelopeGenerator.sln @@ -25,8 +25,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{134D4164-B29 EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{0CBC2432-A561-4440-89BC-671B66A24146}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EnvelopeGenerator.Tests.Application", "EnvelopeGenerator.Tests.Application\EnvelopeGenerator.Tests.Application.csproj", "{A4D0DD1A-67BC-4E1A-AD29-BC4BC0D41399}" -EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "infrastructure", "infrastructure", "{02EA681E-C7D8-13C7-8484-4AC65E1B71E8}" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "presentation", "presentation", "{E3C758DC-914D-4B7E-8457-0813F1FDB0CB}" @@ -37,6 +35,8 @@ Project("{F184B08F-C81C-45F6-A57F-5ABD9991F28F}") = "EnvelopeGenerator.Form", "E EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EnvelopeGenerator.PdfEditor", "EnvelopeGenerator.PdfEditor\EnvelopeGenerator.PdfEditor.csproj", "{211619F5-AE25-4BA5-A552-BACAFE0632D3}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EnvelopeGenerator.Tests", "EnvelopeGenerator.Tests\EnvelopeGenerator.Tests.csproj", "{224C4845-1CDE-22B7-F3A9-1FF9297F70E8}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -75,10 +75,6 @@ Global {E5E12BA4-60C1-48BA-9053-0F8B62B38124}.Debug|Any CPU.Build.0 = Debug|Any CPU {E5E12BA4-60C1-48BA-9053-0F8B62B38124}.Release|Any CPU.ActiveCfg = Debug|Any CPU {E5E12BA4-60C1-48BA-9053-0F8B62B38124}.Release|Any CPU.Build.0 = Debug|Any CPU - {A4D0DD1A-67BC-4E1A-AD29-BC4BC0D41399}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {A4D0DD1A-67BC-4E1A-AD29-BC4BC0D41399}.Debug|Any CPU.Build.0 = Debug|Any CPU - {A4D0DD1A-67BC-4E1A-AD29-BC4BC0D41399}.Release|Any CPU.ActiveCfg = Release|Any CPU - {A4D0DD1A-67BC-4E1A-AD29-BC4BC0D41399}.Release|Any CPU.Build.0 = Release|Any CPU {A9F9B431-BB9B-49B8-9E2C-0703634A653A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {A9F9B431-BB9B-49B8-9E2C-0703634A653A}.Debug|Any CPU.Build.0 = Debug|Any CPU {A9F9B431-BB9B-49B8-9E2C-0703634A653A}.Release|Any CPU.ActiveCfg = Release|Any CPU @@ -91,6 +87,10 @@ Global {211619F5-AE25-4BA5-A552-BACAFE0632D3}.Debug|Any CPU.Build.0 = Debug|Any CPU {211619F5-AE25-4BA5-A552-BACAFE0632D3}.Release|Any CPU.ActiveCfg = Release|Any CPU {211619F5-AE25-4BA5-A552-BACAFE0632D3}.Release|Any CPU.Build.0 = Release|Any CPU + {224C4845-1CDE-22B7-F3A9-1FF9297F70E8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {224C4845-1CDE-22B7-F3A9-1FF9297F70E8}.Debug|Any CPU.Build.0 = Debug|Any CPU + {224C4845-1CDE-22B7-F3A9-1FF9297F70E8}.Release|Any CPU.ActiveCfg = Release|Any CPU + {224C4845-1CDE-22B7-F3A9-1FF9297F70E8}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -105,12 +105,12 @@ Global {5A9984F8-51A2-4558-A415-EC5FEED7CF7D} = {9943209E-1744-4944-B1BA-4F87FC1A0EEB} {E5E12BA4-60C1-48BA-9053-0F8B62B38124} = {E3C758DC-914D-4B7E-8457-0813F1FDB0CB} {9943209E-1744-4944-B1BA-4F87FC1A0EEB} = {134D4164-B291-4E19-99B9-E4FA3AFAB62C} - {A4D0DD1A-67BC-4E1A-AD29-BC4BC0D41399} = {0CBC2432-A561-4440-89BC-671B66A24146} {02EA681E-C7D8-13C7-8484-4AC65E1B71E8} = {134D4164-B291-4E19-99B9-E4FA3AFAB62C} {E3C758DC-914D-4B7E-8457-0813F1FDB0CB} = {134D4164-B291-4E19-99B9-E4FA3AFAB62C} {A9F9B431-BB9B-49B8-9E2C-0703634A653A} = {E3C758DC-914D-4B7E-8457-0813F1FDB0CB} {6D56C01F-D6CB-4D8A-BD3D-4FD34326998C} = {E3C758DC-914D-4B7E-8457-0813F1FDB0CB} {211619F5-AE25-4BA5-A552-BACAFE0632D3} = {9943209E-1744-4944-B1BA-4F87FC1A0EEB} + {224C4845-1CDE-22B7-F3A9-1FF9297F70E8} = {0CBC2432-A561-4440-89BC-671B66A24146} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {73E60370-756D-45AD-A19A-C40A02DACCC7}