Files
PowderCoatingLogix/tests/PowderCoating.UnitTests/PlatformSettingsServiceTests.cs
T
spouliot dbe4170986 Add unit tests for 9 new services/controllers and expand existing test coverage
116 tests passing: JobPhotoService, MeasurementConversionService, PlatformSettingsService,
QuoteApprovalController, QuotePhotoService, ShopCapabilityCalculator, StorageMigrationService,
TenantContext, UsageQuotaController — plus expanded PricingCalculation, Registration, and
Subscription tests.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-25 18:27:30 -04:00

101 lines
3.2 KiB
C#

using Microsoft.EntityFrameworkCore;
using PowderCoating.Core.Entities;
using PowderCoating.Infrastructure.Data;
using PowderCoating.Infrastructure.Services;
namespace PowderCoating.UnitTests;
public class PlatformSettingsServiceTests
{
[Fact]
public async Task GetAsync_ReturnsStoredValue()
{
await using var context = CreateContext();
context.PlatformSettings.Add(new PlatformSetting
{
Key = "BrandName",
Value = "Powder Coating Pro"
});
await context.SaveChangesAsync();
var service = new PlatformSettingsService(context);
var value = await service.GetAsync("BrandName");
Assert.Equal("Powder Coating Pro", value);
}
[Fact]
public async Task GetAsync_WhenMissing_ReturnsNull()
{
await using var context = CreateContext();
var service = new PlatformSettingsService(context);
var value = await service.GetAsync("MissingKey");
Assert.Null(value);
}
[Fact]
public async Task SetAsync_WhenSettingExists_UpdatesValueAndAuditFields()
{
await using var context = CreateContext();
context.PlatformSettings.Add(new PlatformSetting
{
Key = "TrialsEnabled",
Value = "true",
UpdatedBy = "old-user"
});
await context.SaveChangesAsync();
var service = new PlatformSettingsService(context);
await service.SetAsync("TrialsEnabled", "false", "superadmin@example.com");
var setting = await context.PlatformSettings.SingleAsync();
Assert.Equal("false", setting.Value);
Assert.Equal("superadmin@example.com", setting.UpdatedBy);
Assert.True(setting.UpdatedAt.HasValue);
}
[Fact]
public async Task SetAsync_WhenSettingMissing_InsertsRow()
{
await using var context = CreateContext();
var service = new PlatformSettingsService(context);
await service.SetAsync("SupportEmail", "help@example.com", "setup");
var setting = await context.PlatformSettings.SingleAsync();
Assert.Equal("SupportEmail", setting.Key);
Assert.Equal("help@example.com", setting.Value);
Assert.Equal("setup", setting.UpdatedBy);
}
[Fact]
public async Task GetAllAsync_OrdersByGroupThenKey()
{
await using var context = CreateContext();
context.PlatformSettings.AddRange(
new PlatformSetting { Key = "Zeta", GroupName = "Billing", Value = "1" },
new PlatformSetting { Key = "Alpha", GroupName = "Billing", Value = "2" },
new PlatformSetting { Key = "Bravo", GroupName = "Alerts", Value = "3" });
await context.SaveChangesAsync();
var service = new PlatformSettingsService(context);
var settings = await service.GetAllAsync();
Assert.Equal(new[] { "Bravo", "Alpha", "Zeta" }, settings.Select(s => s.Key).ToArray());
}
private static ApplicationDbContext CreateContext()
{
var options = new DbContextOptionsBuilder<ApplicationDbContext>()
.UseInMemoryDatabase(Guid.NewGuid().ToString())
.Options;
return new ApplicationDbContext(options);
}
}