fix: test infrastructure with in-memory DB, SMTP mocking, and thread-safe billing configuration
- Add in-memory database support for test isolation in WebServerFixture - Implement TestMailSender fake SMTP provider for email test support - Add thread synchronization to billing service registration to prevent race conditions - Make RegisterBilling<T> idempotent to safely handle reconfiguration - Configure test environment via in-memory settings (UseTestEmailSender, UseInMemoryDatabase) - Add regression tests for billing module idempotency and duplicate registration detection - Fix tests: EMaillingTests.SendEMailSynchrone, BillingServiceTests (2 tests), HaveConfigurationRoot (3 tests) All core test infrastructure tests now passing.
This commit is contained in:
parent
be7df3d054
commit
87d62791b8
10 changed files with 223 additions and 58 deletions
48
test/yavscTests/NonRegression/BillingServiceTests.cs
Normal file
48
test/yavscTests/NonRegression/BillingServiceTests.cs
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
using Microsoft.EntityFrameworkCore;
|
||||
using Xunit;
|
||||
using Yavsc;
|
||||
using Yavsc.Abstract.Workflow;
|
||||
using Yavsc.Helpers;
|
||||
using Yavsc.Models;
|
||||
using Yavsc.Models.Billing;
|
||||
using Yavsc.Models.Haircut;
|
||||
using Yavsc.Services;
|
||||
|
||||
namespace yavscTests
|
||||
{
|
||||
[Trait("regression", "II")]
|
||||
public class BillingServiceTests
|
||||
{
|
||||
[Fact]
|
||||
public void ConfigureBillingService_CanBeCalledTwiceWithoutThrowing()
|
||||
{
|
||||
// First initialization should populate the billing registry.
|
||||
WorkflowHelpers.ConfigureBillingService();
|
||||
|
||||
int firstBillingCount = BillingService.Billing.Count;
|
||||
int firstSettingsCount = BillingService.UserSettings.Count;
|
||||
int firstProfileTypesCount = Config.ProfileTypes.Count;
|
||||
|
||||
// Second call should be idempotent and not throw.
|
||||
WorkflowHelpers.ConfigureBillingService();
|
||||
|
||||
Assert.Equal(firstBillingCount, BillingService.Billing.Count);
|
||||
Assert.Equal(firstSettingsCount, BillingService.UserSettings.Count);
|
||||
Assert.Equal(firstProfileTypesCount, Config.ProfileTypes.Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RegisterBilling_DuplicateRegistrationThrowsInvalidOperationException()
|
||||
{
|
||||
WorkflowHelpers.ConfigureBillingService();
|
||||
|
||||
var firstRegistrar = new Func<ApplicationDbContext, long, IDecidableQuery>((db, id) =>
|
||||
db.HairCutQueries.Include(q => q.Prestation).Include(q => q.Regularisation).Single(q => q.Id == id));
|
||||
|
||||
const string testCode = "TestBrush";
|
||||
|
||||
Assert.Throws<InvalidOperationException>(() =>
|
||||
WorkflowHelpers.RegisterBilling<HairCutQuery>(testCode, firstRegistrar));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -28,7 +28,7 @@ namespace isnd.tests
|
|||
|
||||
private SiteSettings siteSettings;
|
||||
|
||||
public IConfigurationRoot Configuration { get; private set; }
|
||||
public IConfiguration Configuration { get; private set; }
|
||||
|
||||
private WebApplication app;
|
||||
public string TestClientId { get; private set; }
|
||||
|
|
@ -73,12 +73,22 @@ namespace isnd.tests
|
|||
{
|
||||
|
||||
var builder = WebApplication.CreateBuilder();
|
||||
builder.Environment.EnvironmentName = "Development";
|
||||
ConfigureLogger();
|
||||
Configuration = builder.Configuration
|
||||
.AddJsonFile("appsettings.json")
|
||||
.AddJsonFile($"appsettings.{builder.Environment.EnvironmentName}.json", optional: true)
|
||||
builder.Configuration
|
||||
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: false)
|
||||
.AddJsonFile($"appsettings.{builder.Environment.EnvironmentName}.json", optional: true, reloadOnChange: false)
|
||||
.AddEnvironmentVariables()
|
||||
.Build();
|
||||
.AddInMemoryCollection(new Dictionary<string, string?>
|
||||
{
|
||||
["UseInMemoryDatabase"] = "true",
|
||||
["UseTestEmailSender"] = "true",
|
||||
["Smtp:Host"] = "localhost",
|
||||
["Smtp:Port"] = "25",
|
||||
["Smtp:SenderName"] = "Yavsc Test",
|
||||
["Smtp:SenderEmail"] = "test@example.com"
|
||||
});
|
||||
Configuration = builder.Configuration;
|
||||
|
||||
this.app = builder.ConfigureWebAppServices();
|
||||
Services = app.Services;
|
||||
|
|
|
|||
|
|
@ -41,7 +41,7 @@
|
|||
}
|
||||
},
|
||||
"ConnectionStrings": {
|
||||
"YavscConnection": "Server=lame-NpgsqlHostName;Port=5432;Database=lame-DataBase;Username=lame-Username;Password=lame-dbPassword;",
|
||||
"YavscConnection": "Server=lame-NpgsqlHostName;Port=5432;Database=lame-DataBase;Username=lame-Username;Password=lame-dbPassword;"
|
||||
},
|
||||
"DataProtection": {
|
||||
"Keys": {
|
||||
|
|
|
|||
|
|
@ -24,6 +24,11 @@
|
|||
<PackageReference Include="xunit" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Content Include="appsettings.json">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</Content>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\Yavsc.Org\Yavsc.Org.csproj" />
|
||||
<ProjectReference Include="..\..\src\Yavsc.Abstract\Yavsc.Abstract.csproj" />
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue