Persists the activated theme without a redundant update

This commit is contained in:
BinaryAlley committed 2026-11-30 21:20:44 +02:00
1 parent 68143dff25
commit 2a90022aa8
4 files changed
+145 -74

No files matched your search

@@ -0,0 +1,92 @@
#region ========================================================================= USING =====================================================================================
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Nemera.Application.Common.DataAccess.Entities.Themes;
using Nemera.Application.Common.Infrastructure.Models.DTO.Themes;
using Nemera.Application.Common.Infrastructure.Themes;
using Nemera.Application.Common.Infrastructure.Time;
using Nemera.Application.Fixtures.Common.Infrastructure.Models.DTO.Themes;
using Nemera.DataAccess.Core.UoW;
using Nemera.Domain.Common.Primitives;
using Nemera.Infrastructure.Core.Themes;
using NSubstitute;
using System;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Nemera.Infrastructure.IntegrationTests.Core.Themes;
/// <summary>
/// Contains integration tests for the <see cref="ThemeSynchronizer"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class ThemeSynchronizerTests : IDisposable
{
private readonly DbContextOptions<NemeraDbContext> _dbContextOptions;
private readonly NemeraDbContext _dbContext;
private readonly UnitOfWork _unitOfWork;
private readonly IThemeService _mockThemeService;
private readonly ILogger _mockLogger;
private readonly ThemeManifestDtoFixture _themeManifestDtoFixture = new();
private readonly string _archivePath;
/// <summary>
/// Initializes a new instance of the <see cref="ThemeSynchronizerTests"/> class.
/// </summary>
public ThemeSynchronizerTests()
{
_dbContextOptions = new DbContextOptionsBuilder<NemeraDbContext>()
.UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
.Options;
_dbContext = new NemeraDbContext(_dbContextOptions);
_unitOfWork = new UnitOfWork(_dbContext, Substitute.For<IDateTimeProvider>());
_mockThemeService = Substitute.For<IThemeService>();
_mockLogger = Substitute.For<ILogger>();
_mockLogger.IsEnabled(Arg.Any<LogLevel>()).Returns(true);
_archivePath = Path.Combine(Path.GetTempPath(), $"nemera-theme-sync-{Guid.NewGuid():N}.zip");
}
[Fact]
public async Task SynchronizeAsync_WhenANewBundledThemeIsInstalled_ShouldPersistTheActivatedTheme()
{
// Arrange
ThemeManifestDto manifest = _themeManifestDtoFixture.Create();
File.WriteAllText(_archivePath, "theme-pack");
_mockThemeService.GetBundledThemeArchivePaths().Returns([_archivePath]);
_mockThemeService.ReadManifestFromArchiveAsync(_archivePath, Arg.Any<CancellationToken>()).Returns(Result.From(manifest));
_mockThemeService.InstallAsync(Arg.Any<Stream>(), Arg.Any<CancellationToken>()).Returns(Result.From(manifest));
_mockThemeService.HasThemePack(Arg.Any<string>()).Returns(true);
_mockThemeService.DefaultThemeId.Returns(manifest.Id);
// Act
await ThemeSynchronizer.SynchronizeAsync(_mockThemeService, _unitOfWork, _mockLogger, CancellationToken.None);
// Assert
// Activating an inserted theme must not attempt an update, which the repository would report as a failed activation warning.
_mockLogger.DidNotReceive().Log(
LogLevel.Warning,
Arg.Any<EventId>(),
Arg.Any<object>(),
Arg.Any<Exception?>(),
Arg.Any<Func<object, Exception?, string>>());
// Read through a fresh context so the assertion proves SaveChanges wrote the activation, not only that the entity was tracked.
using (NemeraDbContext verificationContext = new(_dbContextOptions))
{
ThemeEntity persistedTheme = Assert.Single(verificationContext.Themes);
Assert.True(persistedTheme.IsCurrent);
}
}
/// <summary>
/// Releases the database context and removes the temporary archive file.
/// </summary>
public void Dispose()
{
_dbContext.Dispose();
if (File.Exists(_archivePath))
File.Delete(_archivePath);
}
}
@@ -24,6 +24,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.AspNetCore.Http.Features" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="NSubstitute" />
@@ -40,6 +41,7 @@
<ItemGroup>
<ProjectReference Include="..\..\..\src\Nemera.Infrastructure\Nemera.Infrastructure.csproj" />
<ProjectReference Include="..\..\..\src\Nemera.DataAccess\Nemera.DataAccess.csproj" />
<ProjectReference Include="..\..\Common\Nemera.Domain.Fixtures\Nemera.Domain.Fixtures.csproj" />
<ProjectReference Include="..\..\Common\Nemera.Application.Fixtures\Nemera.Application.Fixtures.csproj" />
<ProjectReference Include="..\..\Common\Nemera.Contracts.Fixtures\Nemera.Contracts.Fixtures.csproj" />