mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-02 14:01:11 +00:00
Covers the SSD content setting with tests
Adds domain, mapping, endpoint, persistence, discovery, and hashing coverage, including parallel and sequential read behavior.
This commit is contained in:
1 parent
fc800afe03
commit
45c1d1bd44
21 files changed
+354
-22
No files matched your search
+25
@@ -148,6 +148,31 @@ public class LibraryRepositoryTests
|
||||
Assert.Null(storedLibrary.CompletionThresholdPercentage);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task InsertAsync_WhenLibraryContentIsOnSsd_ShouldPersistTheSetting()
|
||||
{
|
||||
// Arrange
|
||||
using SqliteConnection anchorConnection = new($"Data Source=luminadataccess-libraryrepo-insert-ssd-{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, isContentOnSsd: true, contentLocations: []);
|
||||
|
||||
// Act
|
||||
Result<Created> result = await sut.InsertAsync(library, CancellationToken.None);
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(Result.Created, result.Value);
|
||||
LibraryEntity? storedLibrary = await context.Libraries.AsNoTracking().FirstOrDefaultAsync(candidate => candidate.Id == library.Id);
|
||||
Assert.NotNull(storedLibrary);
|
||||
Assert.True(storedLibrary!.IsContentOnSsd);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UpdateAsync_WhenConsumptionSettingsAreEnabled_ShouldPersistTheTrackingAndThreshold()
|
||||
{
|
||||
|
||||
+3
-2
@@ -81,7 +81,7 @@ public class BooksFileSystemDiscoveryJobTests
|
||||
_scanId = _scanIdFixture.Create();
|
||||
_userId = _userIdFixture.Create();
|
||||
_libraryId = _libraryIdFixture.Create();
|
||||
_sut = new BooksFileSystemDiscoveryJob(_mockServiceScopeFactory)
|
||||
_sut = new BooksFileSystemDiscoveryJob(_mockServiceScopeFactory, Substitute.For<Microsoft.Extensions.Logging.ILogger<BooksFileSystemDiscoveryJob>>())
|
||||
{
|
||||
ScanId = _scanId,
|
||||
UserId = _userId,
|
||||
@@ -105,7 +105,8 @@ public class BooksFileSystemDiscoveryJobTests
|
||||
title: "Test Library",
|
||||
libraryType: LibraryType.Book,
|
||||
contentLocations: [temporaryLibraryDirectory.Path],
|
||||
shouldSkipUnchangedDirectoriesDuringScan: false);
|
||||
shouldSkipUnchangedDirectoriesDuringScan: false,
|
||||
isContentOnSsd: true);
|
||||
_mockLibraryRepository.GetByIdAsync(_libraryId.Value, cancellationToken: Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<LibraryEntity?>(libraryEntity));
|
||||
_mockStagingResultsRepository.InsertRangeAsync(Arg.Any<IReadOnlyCollection<LibraryScanStagingResultsEntity>>(), Arg.Any<CancellationToken>())
|
||||
|
||||
+39
-7
@@ -40,6 +40,7 @@ public class FileHashServiceTests
|
||||
// Act
|
||||
List<HashedFileSystemFileDto> result = await _sut.HashFilesAsync(
|
||||
inputFiles,
|
||||
shouldParallelizeReads: true,
|
||||
() =>
|
||||
{
|
||||
callbackInvocationCount++;
|
||||
@@ -66,7 +67,7 @@ public class FileHashServiceTests
|
||||
List<HashedFileSystemFileDto> inputFiles = [_hashedFileSystemFileDtoFixture.Create(path: filePath, size: new FileInfo(filePath).Length, currentHash: 0, oldHash: 0, ticks: 0)];
|
||||
|
||||
// Act
|
||||
List<HashedFileSystemFileDto> result = await _sut.HashFilesAsync(inputFiles, () => Task.CompletedTask, CancellationToken.None);
|
||||
List<HashedFileSystemFileDto> result = await _sut.HashFilesAsync(inputFiles, shouldParallelizeReads: true, () => Task.CompletedTask, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
HashedFileSystemFileDto hashedFile = Assert.Single(result);
|
||||
@@ -83,10 +84,10 @@ public class FileHashServiceTests
|
||||
List<HashedFileSystemFileDto> firstInputFiles = [_hashedFileSystemFileDtoFixture.Create(path: filePath, size: new FileInfo(filePath).Length, currentHash: 0, oldHash: 0, ticks: 0)];
|
||||
|
||||
// Act
|
||||
List<HashedFileSystemFileDto> firstResult = await _sut.HashFilesAsync(firstInputFiles, () => Task.CompletedTask, CancellationToken.None);
|
||||
List<HashedFileSystemFileDto> firstResult = await _sut.HashFilesAsync(firstInputFiles, shouldParallelizeReads: true, () => Task.CompletedTask, CancellationToken.None);
|
||||
await File.WriteAllTextAsync(filePath, "changed content");
|
||||
List<HashedFileSystemFileDto> secondInputFiles = [_hashedFileSystemFileDtoFixture.Create(path: filePath, size: new FileInfo(filePath).Length, currentHash: 0, oldHash: 0, ticks: 0)];
|
||||
List<HashedFileSystemFileDto> secondResult = await _sut.HashFilesAsync(secondInputFiles, () => Task.CompletedTask, CancellationToken.None);
|
||||
List<HashedFileSystemFileDto> secondResult = await _sut.HashFilesAsync(secondInputFiles, shouldParallelizeReads: true, () => Task.CompletedTask, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.NotEqual(firstResult[0].CurrentHash, secondResult[0].CurrentHash);
|
||||
@@ -100,7 +101,7 @@ public class FileHashServiceTests
|
||||
List<HashedFileSystemFileDto> inputFiles = [_hashedFileSystemFileDtoFixture.Create(path: nonExistentFilePath, size: 100, currentHash: 0, oldHash: 0, ticks: 0)];
|
||||
|
||||
// Act
|
||||
List<HashedFileSystemFileDto> result = await _sut.HashFilesAsync(inputFiles, () => Task.CompletedTask, CancellationToken.None);
|
||||
List<HashedFileSystemFileDto> result = await _sut.HashFilesAsync(inputFiles, shouldParallelizeReads: true, () => Task.CompletedTask, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.Empty(result);
|
||||
@@ -119,7 +120,7 @@ public class FileHashServiceTests
|
||||
];
|
||||
|
||||
// Act
|
||||
List<HashedFileSystemFileDto> result = await _sut.HashFilesAsync(inputFiles, () => Task.CompletedTask, CancellationToken.None);
|
||||
List<HashedFileSystemFileDto> result = await _sut.HashFilesAsync(inputFiles, shouldParallelizeReads: true, () => Task.CompletedTask, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
HashedFileSystemFileDto hashedFile = Assert.Single(result);
|
||||
@@ -127,6 +128,37 @@ public class FileHashServiceTests
|
||||
Assert.NotEqual(0UL, hashedFile.CurrentHash);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HashFilesAsync_WhenReadsAreNotParallelized_ShouldHashAllFilesSequentially()
|
||||
{
|
||||
// Arrange
|
||||
using TemporaryTestDirectory temporaryTestDirectory = new();
|
||||
string filePath1 = temporaryTestDirectory.CreateFile("book1.pdf", "content of the first book");
|
||||
string filePath2 = temporaryTestDirectory.CreateFile("book2.pdf", "content of the second book");
|
||||
List<HashedFileSystemFileDto> inputFiles =
|
||||
[
|
||||
_hashedFileSystemFileDtoFixture.Create(path: filePath1, size: new FileInfo(filePath1).Length, currentHash: 0, oldHash: 0, ticks: 0),
|
||||
_hashedFileSystemFileDtoFixture.Create(path: filePath2, size: new FileInfo(filePath2).Length, currentHash: 0, oldHash: 0, ticks: 0)
|
||||
];
|
||||
int callbackInvocationCount = 0;
|
||||
|
||||
// Act
|
||||
List<HashedFileSystemFileDto> result = await _sut.HashFilesAsync(
|
||||
inputFiles,
|
||||
shouldParallelizeReads: false,
|
||||
() =>
|
||||
{
|
||||
callbackInvocationCount++;
|
||||
return Task.CompletedTask;
|
||||
},
|
||||
CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.Equal(2, result.Count);
|
||||
Assert.Equal(2, callbackInvocationCount);
|
||||
Assert.All(result, file => Assert.NotEqual(0UL, file.CurrentHash));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HashFilesAsync_WhenCancellationRequested_ShouldCancelOperation()
|
||||
{
|
||||
@@ -138,7 +170,7 @@ public class FileHashServiceTests
|
||||
cancellationTokenSource.Cancel();
|
||||
|
||||
// Act
|
||||
Task<List<HashedFileSystemFileDto>> operation = _sut.HashFilesAsync(inputFiles, () => Task.CompletedTask, cancellationTokenSource.Token);
|
||||
Task<List<HashedFileSystemFileDto>> operation = _sut.HashFilesAsync(inputFiles, shouldParallelizeReads: true, () => Task.CompletedTask, cancellationTokenSource.Token);
|
||||
|
||||
// Assert
|
||||
await Assert.ThrowsAnyAsync<OperationCanceledException>(() => operation);
|
||||
@@ -182,7 +214,7 @@ public class FileHashServiceTests
|
||||
List<HashedFileSystemFileDto> inputFiles = [_hashedFileSystemFileDtoFixture.Create(path: filePath, size: new FileInfo(filePath).Length, currentHash: 0, oldHash: 0, ticks: 0)];
|
||||
|
||||
// Act
|
||||
List<HashedFileSystemFileDto> result = await _sut.HashFilesAsync(inputFiles, () => Task.CompletedTask, CancellationToken.None);
|
||||
List<HashedFileSystemFileDto> result = await _sut.HashFilesAsync(inputFiles, shouldParallelizeReads: true, () => Task.CompletedTask, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
HashedFileSystemFileDto hashedFile = Assert.Single(result);
|
||||
|
||||
Reference in new issue
Block a user