mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 08:11:10 +00:00
Adds persistence for plugins and library metadata provider configurations
This commit is contained in:
1 parent
f33817f2d2
commit
f71e72a948
16 files changed
+3381
No files matched your search
+119
@@ -0,0 +1,119 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using EntityFrameworkCore.Testing.NSubstitute;
|
||||
using ErrorOr;
|
||||
using Lumina.Application.Common.DataAccess.Entities.Plugins;
|
||||
using Lumina.DataAccess.Core.Repositories.Plugins;
|
||||
using Lumina.DataAccess.Core.UoW;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.DataAccess.UnitTests.Core.Repositories.Plugins;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="LibraryMetadataProviderConfigurationRepository"/> class.
|
||||
/// </summary>
|
||||
public class LibraryMetadataProviderConfigurationRepositoryTests
|
||||
{
|
||||
private readonly LuminaDbContext _mockContext;
|
||||
private readonly LibraryMetadataProviderConfigurationRepository _sut;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="LibraryMetadataProviderConfigurationRepositoryTests"/> class.
|
||||
/// </summary>
|
||||
public LibraryMetadataProviderConfigurationRepositoryTests()
|
||||
{
|
||||
_mockContext = Create.MockedDbContextFor<LuminaDbContext>();
|
||||
_sut = new LibraryMetadataProviderConfigurationRepository(_mockContext);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetByLibraryIdAsync_WhenCalled_ShouldReturnOnlyConfigurationsOfTheLibrary()
|
||||
{
|
||||
// Arrange
|
||||
Guid libraryId = Guid.NewGuid();
|
||||
LibraryMetadataProviderConfigurationEntity configurationOfLibrary = CreateConfiguration(libraryId, Guid.NewGuid(), 1);
|
||||
LibraryMetadataProviderConfigurationEntity configurationOfAnotherLibrary = CreateConfiguration(Guid.NewGuid(), Guid.NewGuid(), 1);
|
||||
_mockContext.LibraryMetadataProviderConfigurations.AddRange(configurationOfLibrary, configurationOfAnotherLibrary);
|
||||
await _mockContext.SaveChangesAsync();
|
||||
|
||||
// Act
|
||||
ErrorOr<System.Collections.Generic.IReadOnlyList<LibraryMetadataProviderConfigurationEntity>> result = await _sut.GetByLibraryIdAsync(libraryId, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsError);
|
||||
LibraryMetadataProviderConfigurationEntity retrievedConfiguration = Assert.Single(result.Value);
|
||||
Assert.Equal(configurationOfLibrary.PluginId, retrievedConfiguration.PluginId);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetByLibraryAndPluginIdAsync_WhenConfigurationExists_ShouldReturnIt()
|
||||
{
|
||||
// Arrange
|
||||
LibraryMetadataProviderConfigurationEntity configuration = CreateConfiguration(Guid.NewGuid(), Guid.NewGuid(), 1);
|
||||
_mockContext.LibraryMetadataProviderConfigurations.Add(configuration);
|
||||
await _mockContext.SaveChangesAsync();
|
||||
|
||||
// Act
|
||||
ErrorOr<LibraryMetadataProviderConfigurationEntity?> result = await _sut.GetByLibraryAndPluginIdAsync(configuration.LibraryId, configuration.PluginId, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsError);
|
||||
Assert.Equal(configuration.Id, result.Value!.Id);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UpsertAsync_WhenConfigurationDoesNotExist_ShouldInsertIt()
|
||||
{
|
||||
// Arrange
|
||||
LibraryMetadataProviderConfigurationEntity configuration = CreateConfiguration(Guid.NewGuid(), Guid.NewGuid(), 1);
|
||||
|
||||
// Act
|
||||
ErrorOr<Updated> result = await _sut.UpsertAsync(configuration, CancellationToken.None);
|
||||
await _mockContext.SaveChangesAsync();
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsError);
|
||||
Assert.Single(_mockContext.LibraryMetadataProviderConfigurations);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UpsertAsync_WhenConfigurationExists_ShouldUpdateIt()
|
||||
{
|
||||
// Arrange
|
||||
LibraryMetadataProviderConfigurationEntity configuration = CreateConfiguration(Guid.NewGuid(), Guid.NewGuid(), 1);
|
||||
_mockContext.LibraryMetadataProviderConfigurations.Add(configuration);
|
||||
await _mockContext.SaveChangesAsync();
|
||||
|
||||
LibraryMetadataProviderConfigurationEntity updatedConfiguration = CreateConfiguration(configuration.LibraryId, configuration.PluginId, 5);
|
||||
updatedConfiguration.IsEnabled = true;
|
||||
|
||||
// Act
|
||||
ErrorOr<Updated> result = await _sut.UpsertAsync(updatedConfiguration, CancellationToken.None);
|
||||
await _mockContext.SaveChangesAsync();
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsError);
|
||||
LibraryMetadataProviderConfigurationEntity retrievedConfiguration = _mockContext.LibraryMetadataProviderConfigurations.Single();
|
||||
Assert.Equal(5, retrievedConfiguration.Rank);
|
||||
Assert.True(retrievedConfiguration.IsEnabled);
|
||||
}
|
||||
|
||||
private static LibraryMetadataProviderConfigurationEntity CreateConfiguration(Guid libraryId, Guid pluginId, int rank)
|
||||
{
|
||||
return new LibraryMetadataProviderConfigurationEntity
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
LibraryId = libraryId,
|
||||
PluginId = pluginId,
|
||||
IsEnabled = false,
|
||||
Rank = rank,
|
||||
CreatedOnUtc = DateTime.UtcNow,
|
||||
CreatedBy = default,
|
||||
UpdatedBy = default
|
||||
};
|
||||
}
|
||||
}
|
||||
+134
@@ -0,0 +1,134 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using EntityFrameworkCore.Testing.NSubstitute;
|
||||
using ErrorOr;
|
||||
using Lumina.Application.Common.DataAccess.Entities.Plugins;
|
||||
using Lumina.DataAccess.Core.Repositories.Plugins;
|
||||
using Lumina.DataAccess.Core.UoW;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.Plugins;
|
||||
using Lumina.Domain.SharedKernel.Common.Errors;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.DataAccess.UnitTests.Core.Repositories.Plugins;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="PluginRepository"/> class.
|
||||
/// </summary>
|
||||
public class PluginRepositoryTests
|
||||
{
|
||||
private readonly LuminaDbContext _mockContext;
|
||||
private readonly PluginRepository _sut;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="PluginRepositoryTests"/> class.
|
||||
/// </summary>
|
||||
public PluginRepositoryTests()
|
||||
{
|
||||
_mockContext = Create.MockedDbContextFor<LuminaDbContext>();
|
||||
_sut = new PluginRepository(_mockContext);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetByIdAsync_WhenPluginExists_ShouldReturnIt()
|
||||
{
|
||||
// Arrange
|
||||
PluginEntity plugin = CreatePluginEntity();
|
||||
_mockContext.Plugins.Add(plugin);
|
||||
await _mockContext.SaveChangesAsync();
|
||||
|
||||
// Act
|
||||
ErrorOr<PluginEntity?> result = await _sut.GetByIdAsync(plugin.Id, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsError);
|
||||
Assert.Equal(plugin.Id, result.Value!.Id);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UpsertAsync_WhenPluginDoesNotExist_ShouldInsertIt()
|
||||
{
|
||||
// Arrange
|
||||
PluginEntity plugin = CreatePluginEntity();
|
||||
|
||||
// Act
|
||||
ErrorOr<Updated> result = await _sut.UpsertAsync(plugin, CancellationToken.None);
|
||||
await _mockContext.SaveChangesAsync();
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsError);
|
||||
Assert.Single(_mockContext.Plugins);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UpsertAsync_WhenPluginExists_ShouldUpdateDetectionFieldsAndPreserveSettings()
|
||||
{
|
||||
// Arrange
|
||||
PluginEntity plugin = CreatePluginEntity();
|
||||
plugin.SettingsJson = """{"preferredLanguage":"en"}""";
|
||||
_mockContext.Plugins.Add(plugin);
|
||||
await _mockContext.SaveChangesAsync();
|
||||
|
||||
PluginEntity updatedPlugin = CreatePluginEntity(plugin.Id);
|
||||
updatedPlugin.Name = "Updated Name";
|
||||
updatedPlugin.SettingsJson = null; // the stored settings must be preserved
|
||||
|
||||
// Act
|
||||
ErrorOr<Updated> result = await _sut.UpsertAsync(updatedPlugin, CancellationToken.None);
|
||||
await _mockContext.SaveChangesAsync();
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsError);
|
||||
PluginEntity retrievedPlugin = _mockContext.Plugins.Single();
|
||||
Assert.Equal("Updated Name", retrievedPlugin.Name);
|
||||
Assert.Equal("""{"preferredLanguage":"en"}""", retrievedPlugin.SettingsJson);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UpdateSettingsAsync_WhenPluginExists_ShouldUpdateItsSettings()
|
||||
{
|
||||
// Arrange
|
||||
PluginEntity plugin = CreatePluginEntity();
|
||||
_mockContext.Plugins.Add(plugin);
|
||||
await _mockContext.SaveChangesAsync();
|
||||
|
||||
// Act
|
||||
ErrorOr<Updated> result = await _sut.UpdateSettingsAsync(plugin.Id, """{"preferredLanguage":"fr"}""", CancellationToken.None);
|
||||
await _mockContext.SaveChangesAsync();
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsError);
|
||||
Assert.Equal("""{"preferredLanguage":"fr"}""", _mockContext.Plugins.Single().SettingsJson);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UpdateSettingsAsync_WhenPluginDoesNotExist_ShouldReturnError()
|
||||
{
|
||||
// Act
|
||||
ErrorOr<Updated> result = await _sut.UpdateSettingsAsync(Guid.NewGuid(), null, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsError);
|
||||
Assert.Equal(Errors.Plugins.PluginNotFound, result.FirstError);
|
||||
}
|
||||
|
||||
private static PluginEntity CreatePluginEntity(Guid? id = null)
|
||||
{
|
||||
return new PluginEntity
|
||||
{
|
||||
Id = id ?? Guid.NewGuid(),
|
||||
Name = "Test Plugin",
|
||||
Author = "Lumina",
|
||||
Version = "1.0.0",
|
||||
Description = "A test plugin.",
|
||||
LoadStatus = PluginLoadStatus.Loaded,
|
||||
CreatedOnUtc = DateTime.UtcNow,
|
||||
CreatedBy = default,
|
||||
UpdatedOnUtc = DateTime.UtcNow,
|
||||
UpdatedBy = default
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user