mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-02 23:11:10 +00:00
Adapts the tests and fixtures to the refactored domain and persistence layers
This commit is contained in:
1 parent
e6f0c63feb
commit
ef913f5c9e
99 files changed
+612
-171
No files matched your search
+132
@@ -0,0 +1,132 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.DataAccess.Entities.MediaLibrary.Management;
|
||||
using Lumina.Application.Common.DataAccess.Entities.UsersManagement;
|
||||
using Lumina.Application.Common.Infrastructure.Authentication;
|
||||
using Lumina.Application.Common.Infrastructure.Time;
|
||||
using Lumina.Application.Fixtures.Common.DataAccess.Entities.MediaLibrary.Management;
|
||||
using Lumina.Application.Fixtures.Common.DataAccess.Entities.UsersManagement;
|
||||
using Lumina.DataAccess.Common.Interceptors;
|
||||
using Lumina.DataAccess.Core.Repositories.Libraries;
|
||||
using Lumina.DataAccess.Core.UoW;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using Microsoft.Data.Sqlite;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using NSubstitute;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.DataAccess.IntegrationTests.Core.Repositories.Libraries;
|
||||
|
||||
/// <summary>
|
||||
/// Contains integration tests for the <see cref="LibraryRepository"/> class, exercising it against a real SQLite database.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class LibraryRepositoryTests
|
||||
{
|
||||
private readonly LibraryEntityFixture _libraryEntityFixture = new();
|
||||
private readonly UserEntityFixture _userEntityFixture = new();
|
||||
|
||||
[Fact]
|
||||
public async Task UpdateAsync_WhenNothingChanged_ShouldNotChangeTheLibraryAudit()
|
||||
{
|
||||
// Arrange
|
||||
using SqliteConnection anchorConnection = new($"Data Source=luminadataccess-libraryrepo-update-noop-{Guid.NewGuid()};Mode=Memory;Cache=Shared");
|
||||
anchorConnection.Open();
|
||||
Guid userId = Guid.NewGuid();
|
||||
DateTime utcNow = new(2025, 1, 1, 12, 0, 0, DateTimeKind.Utc);
|
||||
using LuminaDbContext context = CreateAuditedContext(anchorConnection, userId, utcNow);
|
||||
LibraryRepository sut = new(context);
|
||||
context.Users.Add(_userEntityFixture.Create(id: userId));
|
||||
LibraryEntity library = _libraryEntityFixture.Create(userId: userId, contentLocations: ["/media/one", "/media/two"]);
|
||||
context.Libraries.Add(library);
|
||||
await context.SaveChangesAsync();
|
||||
LibraryEntity incoming = await LoadDetachedLibraryAsync(context, library.Id);
|
||||
|
||||
// Act
|
||||
Result<Updated> result = await sut.UpdateAsync(incoming, CancellationToken.None);
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
LibraryEntity? storedLibrary = await context.Libraries.AsNoTracking().Include(candidate => candidate.ContentLocations).FirstOrDefaultAsync(candidate => candidate.Id == library.Id);
|
||||
Assert.NotNull(storedLibrary);
|
||||
Assert.Null(storedLibrary!.UpdatedOnUtc);
|
||||
Assert.Equal(2, storedLibrary.ContentLocations.Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UpdateAsync_WhenContentLocationsChange_ShouldApplyOnlyTheDeltasAndKeepTheUnchangedOnes()
|
||||
{
|
||||
// Arrange
|
||||
using SqliteConnection anchorConnection = new($"Data Source=luminadataccess-libraryrepo-update-locations-{Guid.NewGuid()};Mode=Memory;Cache=Shared");
|
||||
anchorConnection.Open();
|
||||
Guid userId = Guid.NewGuid();
|
||||
DateTime utcNow = new(2025, 1, 1, 12, 0, 0, DateTimeKind.Utc);
|
||||
using LuminaDbContext context = CreateAuditedContext(anchorConnection, userId, utcNow);
|
||||
LibraryRepository sut = new(context);
|
||||
context.Users.Add(_userEntityFixture.Create(id: userId));
|
||||
LibraryEntity library = _libraryEntityFixture.Create(userId: userId, contentLocations: ["/media/one", "/media/two", "/media/keep"]);
|
||||
context.Libraries.Add(library);
|
||||
await context.SaveChangesAsync();
|
||||
Guid keptLocationId = context.Entry(library.ContentLocations.Single(location => location.Path == "/media/keep")).Property<Guid>("Id").CurrentValue;
|
||||
Guid retainedLocationId = context.Entry(library.ContentLocations.Single(location => location.Path == "/media/one")).Property<Guid>("Id").CurrentValue;
|
||||
LibraryEntity incoming = await LoadDetachedLibraryAsync(context, library.Id);
|
||||
incoming.ContentLocations.Clear();
|
||||
foreach (string path in new[] { "/media/one", "/media/keep", "/media/three" })
|
||||
incoming.ContentLocations.Add(new LibraryContentLocationEntity { Path = path });
|
||||
|
||||
// Act
|
||||
Result<Updated> result = await sut.UpdateAsync(incoming, CancellationToken.None);
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
LibraryEntity? storedLibrary = await context.Libraries.AsNoTracking().Include(candidate => candidate.ContentLocations).FirstOrDefaultAsync(candidate => candidate.Id == library.Id);
|
||||
Assert.NotNull(storedLibrary);
|
||||
Assert.Equal(3, storedLibrary!.ContentLocations.Count);
|
||||
Assert.Equal(["/media/keep", "/media/one", "/media/three"], [.. storedLibrary.ContentLocations.Select(location => location.Path).OrderBy(path => path)]);
|
||||
// The two locations that did not change must keep their stored row identity, instead of being deleted and re-inserted.
|
||||
Assert.Equal(retainedLocationId, context.Entry(library.ContentLocations.Single(location => location.Path == "/media/one")).Property<Guid>("Id").CurrentValue);
|
||||
Assert.Equal(keptLocationId, context.Entry(library.ContentLocations.Single(location => location.Path == "/media/keep")).Property<Guid>("Id").CurrentValue);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a real SQLite backed context with the auditing interceptor attached, so that audit columns behave exactly like in production.
|
||||
/// </summary>
|
||||
/// <param name="anchorConnection">The open in-memory SQLite connection that keeps the database alive for the duration of the test.</param>
|
||||
/// <param name="userId">The Id of the user reported as the current user.</param>
|
||||
/// <param name="utcNow">The current UTC time reported by the time provider.</param>
|
||||
/// <returns>The created context.</returns>
|
||||
private static LuminaDbContext CreateAuditedContext(SqliteConnection anchorConnection, Guid userId, DateTime utcNow)
|
||||
{
|
||||
ICurrentUserService currentUserService = Substitute.For<ICurrentUserService>();
|
||||
currentUserService.UserId.Returns(userId);
|
||||
IDateTimeProvider dateTimeProvider = Substitute.For<IDateTimeProvider>();
|
||||
dateTimeProvider.UtcNow.Returns(utcNow);
|
||||
LuminaDbContext context = new(new DbContextOptionsBuilder<LuminaDbContext>()
|
||||
.UseSqlite(anchorConnection.ConnectionString)
|
||||
.AddInterceptors(new UpdateAuditableEntitiesInterceptor(currentUserService, dateTimeProvider))
|
||||
.Options);
|
||||
context.Database.EnsureCreated();
|
||||
return context;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads a detached copy of a stored library, with its content locations, so that it can be handed to the repository as the desired state of an edit.
|
||||
/// </summary>
|
||||
/// <param name="context">The context that tracks the stored library.</param>
|
||||
/// <param name="libraryId">The Id of the library to load.</param>
|
||||
/// <returns>The detached copy of the library.</returns>
|
||||
private static async Task<LibraryEntity> LoadDetachedLibraryAsync(LuminaDbContext context, Guid libraryId)
|
||||
{
|
||||
return await context.Libraries
|
||||
.AsNoTracking()
|
||||
.Include(library => library.ContentLocations)
|
||||
.FirstAsync(library => library.Id == libraryId);
|
||||
}
|
||||
}
|
||||
+15
-1
@@ -47,7 +47,7 @@ public class LuminaDbContextTests : IDisposable
|
||||
|
||||
// Assert
|
||||
// The count assertion fails when a table is added or removed, reminding to update the assertions below.
|
||||
Assert.Equal(32, tableNames.Length);
|
||||
Assert.Equal(46, tableNames.Length);
|
||||
Assert.Contains("Books", tableNames);
|
||||
Assert.Contains("Users", tableNames);
|
||||
Assert.Contains("UserSettings", tableNames);
|
||||
@@ -80,6 +80,20 @@ public class LuminaDbContextTests : IDisposable
|
||||
Assert.Contains("ScheduledJobs", tableNames);
|
||||
Assert.Contains("ScheduledJobExecutions", tableNames);
|
||||
Assert.Contains("SchedulerDisplayPreferences", tableNames);
|
||||
Assert.Contains("Artists", tableNames);
|
||||
Assert.Contains("Albums", tableNames);
|
||||
Assert.Contains("Tracks", tableNames);
|
||||
Assert.Contains("ArtistContributors", tableNames);
|
||||
Assert.Contains("AlbumContributors", tableNames);
|
||||
Assert.Contains("AlbumGenres", tableNames);
|
||||
Assert.Contains("AlbumRatings", tableNames);
|
||||
Assert.Contains("AlbumTags", tableNames);
|
||||
Assert.Contains("TrackContributors", tableNames);
|
||||
Assert.Contains("TrackGenres", tableNames);
|
||||
Assert.Contains("TrackIsrcs", tableNames);
|
||||
Assert.Contains("TrackMoods", tableNames);
|
||||
Assert.Contains("TrackRatings", tableNames);
|
||||
Assert.Contains("TrackTags", tableNames);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
||||
-1
@@ -25,7 +25,6 @@ public class GetDrivesEndpointTests : IClassFixture<AuthenticatedLuminaApiFactor
|
||||
private readonly AuthenticatedLuminaApiFactory _apiFactory;
|
||||
private readonly JsonSerializerOptions _jsonOptions = new()
|
||||
{
|
||||
ReferenceHandler = ReferenceHandler.Preserve,
|
||||
PropertyNameCaseInsensitive = true,
|
||||
Converters = { new JsonStringEnumConverter(JsonNamingPolicy.CamelCase) }
|
||||
};
|
||||
|
||||
-1
@@ -24,7 +24,6 @@ public class GetTypeEndpointTests : IClassFixture<AuthenticatedLuminaApiFactory>
|
||||
private readonly AuthenticatedLuminaApiFactory _apiFactory;
|
||||
private readonly JsonSerializerOptions _jsonOptions = new()
|
||||
{
|
||||
ReferenceHandler = ReferenceHandler.Preserve,
|
||||
PropertyNameCaseInsensitive = true,
|
||||
Converters = { new JsonStringEnumConverter(JsonNamingPolicy.CamelCase) }
|
||||
};
|
||||
|
||||
-1
@@ -25,7 +25,6 @@ public class CombinePathEndpointTests : IClassFixture<AuthenticatedLuminaApiFact
|
||||
private readonly AuthenticatedLuminaApiFactory _apiFactory;
|
||||
private readonly JsonSerializerOptions _jsonOptions = new()
|
||||
{
|
||||
ReferenceHandler = ReferenceHandler.Preserve,
|
||||
PropertyNameCaseInsensitive = true,
|
||||
Converters = { new JsonStringEnumConverter(JsonNamingPolicy.CamelCase) }
|
||||
};
|
||||
|
||||
-1
@@ -27,7 +27,6 @@ public class GetPathParentEndpointTests : IClassFixture<AuthenticatedLuminaApiFa
|
||||
private readonly AuthenticatedLuminaApiFactory _apiFactory;
|
||||
private readonly JsonSerializerOptions _jsonOptions = new()
|
||||
{
|
||||
ReferenceHandler = ReferenceHandler.Preserve,
|
||||
PropertyNameCaseInsensitive = true,
|
||||
Converters = { new JsonStringEnumConverter(JsonNamingPolicy.CamelCase) }
|
||||
};
|
||||
|
||||
-1
@@ -25,7 +25,6 @@ public class GetPathRootEndpointTests : IClassFixture<AuthenticatedLuminaApiFact
|
||||
private readonly AuthenticatedLuminaApiFactory _apiFactory;
|
||||
private readonly JsonSerializerOptions _jsonOptions = new()
|
||||
{
|
||||
ReferenceHandler = ReferenceHandler.Preserve,
|
||||
PropertyNameCaseInsensitive = true,
|
||||
Converters = { new JsonStringEnumConverter(JsonNamingPolicy.CamelCase) }
|
||||
};
|
||||
|
||||
-1
@@ -23,7 +23,6 @@ public class GetPathSeparatorEndpointTests : IClassFixture<AuthenticatedLuminaAp
|
||||
private readonly AuthenticatedLuminaApiFactory _apiFactory;
|
||||
private readonly JsonSerializerOptions _jsonOptions = new()
|
||||
{
|
||||
ReferenceHandler = ReferenceHandler.Preserve,
|
||||
PropertyNameCaseInsensitive = true,
|
||||
Converters = { new JsonStringEnumConverter(JsonNamingPolicy.CamelCase) }
|
||||
};
|
||||
|
||||
-1
@@ -26,7 +26,6 @@ public class SplitPathEndpointTests : IClassFixture<AuthenticatedLuminaApiFactor
|
||||
private readonly AuthenticatedLuminaApiFactory _apiFactory;
|
||||
private readonly JsonSerializerOptions _jsonOptions = new()
|
||||
{
|
||||
ReferenceHandler = ReferenceHandler.Preserve,
|
||||
PropertyNameCaseInsensitive = true,
|
||||
Converters = { new JsonStringEnumConverter(JsonNamingPolicy.CamelCase) }
|
||||
};
|
||||
|
||||
-1
@@ -25,7 +25,6 @@ public class ValidatePathEndpointTests : IClassFixture<AuthenticatedLuminaApiFac
|
||||
private readonly AuthenticatedLuminaApiFactory _apiFactory;
|
||||
private readonly JsonSerializerOptions _jsonOptions = new()
|
||||
{
|
||||
ReferenceHandler = ReferenceHandler.Preserve,
|
||||
PropertyNameCaseInsensitive = true,
|
||||
Converters = { new JsonStringEnumConverter(JsonNamingPolicy.CamelCase) }
|
||||
};
|
||||
|
||||
-1
@@ -25,7 +25,6 @@ public class GetThumbnailEndpointTests : IClassFixture<AuthenticatedLuminaApiFac
|
||||
private readonly AuthenticatedLuminaApiFactory _apiFactory;
|
||||
private readonly JsonSerializerOptions _jsonOptions = new()
|
||||
{
|
||||
ReferenceHandler = ReferenceHandler.Preserve,
|
||||
PropertyNameCaseInsensitive = true,
|
||||
Converters = { new JsonStringEnumConverter(JsonNamingPolicy.CamelCase) }
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user