Adds the scheduling module tests and centralizes domain event test fixtures

This commit is contained in:
BinaryAlley committed 2026-09-04 00:16:41 -07:00
1 parent 237be9e2c8
commit 99a6159f7f
135 files changed
+10203 -165

No files matched your search

@@ -0,0 +1,237 @@
#region ========================================================================= USING =====================================================================================
using EntityFrameworkCore.Testing.NSubstitute;
using Lumina.Application.Common.DataAccess.Entities.Scheduling;
using Lumina.Application.Fixtures.Common.DataAccess.Entities.Scheduling;
using Lumina.DataAccess.Core.Repositories.Scheduling;
using Lumina.DataAccess.Core.UoW;
using Lumina.Domain.Common.Errors;
using Lumina.Domain.Common.Primitives;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.DataAccess.UnitTests.Core.Repositories.Scheduling;
/// <summary>
/// Contains unit tests for the <see cref="ScheduledJobExecutionRepository"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class ScheduledJobExecutionRepositoryTests
{
private readonly LuminaDbContext _mockContext;
private readonly ScheduledJobExecutionRepository _sut;
private readonly ScheduledJobExecutionEntityFixture _scheduledJobExecutionEntityFixture = new();
/// <summary>
/// Initializes a new instance of the <see cref="ScheduledJobExecutionRepositoryTests"/> class.
/// </summary>
public ScheduledJobExecutionRepositoryTests()
{
_mockContext = Create.MockedDbContextFor<LuminaDbContext>();
_sut = new ScheduledJobExecutionRepository(_mockContext);
}
[Fact]
public async Task GetByIdAsync_WhenExecutionExists_ShouldReturnExecution()
{
// Arrange
ScheduledJobExecutionEntity execution = _scheduledJobExecutionEntityFixture.Create();
_mockContext.ScheduledJobExecutions.Add(execution);
await _mockContext.SaveChangesAsync();
// Act
Result<ScheduledJobExecutionEntity?> result = await _sut.GetByIdAsync(execution.Id, CancellationToken.None);
// Assert
Assert.False(result.IsFailure);
Assert.NotNull(result.Value);
Assert.Equal(execution.Id, result.Value!.Id);
Assert.Equal(execution.ScheduledJobId, result.Value.ScheduledJobId);
}
[Fact]
public async Task GetByIdAsync_WhenExecutionDoesNotExist_ShouldReturnNull()
{
// Act
Result<ScheduledJobExecutionEntity?> result = await _sut.GetByIdAsync(Guid.NewGuid(), CancellationToken.None);
// Assert
Assert.False(result.IsFailure);
Assert.Null(result.Value);
}
[Fact]
public async Task InsertAsync_WhenExecutionDoesNotExist_ShouldAddExecutionToContextAndReturnCreated()
{
// Arrange
ScheduledJobExecutionEntity execution = _scheduledJobExecutionEntityFixture.Create();
// Act
Result<Created> result = await _sut.InsertAsync(execution, CancellationToken.None);
// Assert
Assert.False(result.IsFailure);
Assert.Equal(Result.Created, result.Value);
EntityEntry<ScheduledJobExecutionEntity>? addedExecution = _mockContext.ChangeTracker.Entries<ScheduledJobExecutionEntity>()
.FirstOrDefault(entry => entry.State == EntityState.Added && entry.Entity.Id == execution.Id);
Assert.NotNull(addedExecution);
}
[Fact]
public async Task InsertAsync_WhenExecutionAlreadyExists_ShouldReturnError()
{
// Arrange
ScheduledJobExecutionEntity execution = _scheduledJobExecutionEntityFixture.Create();
_mockContext.ScheduledJobExecutions.Add(execution);
await _mockContext.SaveChangesAsync();
// Act
Result<Created> result = await _sut.InsertAsync(execution, CancellationToken.None);
// Assert
Assert.True(result.IsFailure);
Assert.Equal(Errors.Scheduling.ScheduledJobExecutionAlreadyExists, result.FirstError);
}
[Fact]
public async Task UpdateAsync_WhenExecutionExists_ShouldUpdateItsPropertiesAndReturnUpdated()
{
// Arrange
DateTime completedOnUtc = DateTime.UtcNow;
ScheduledJobExecutionEntity existingExecution = _scheduledJobExecutionEntityFixture.Create(completedOnUtc: null);
_mockContext.ScheduledJobExecutions.Add(existingExecution);
await _mockContext.SaveChangesAsync();
ScheduledJobExecutionEntity updatedExecution = _scheduledJobExecutionEntityFixture.Create(
id: existingExecution.Id,
scheduledJobId: existingExecution.ScheduledJobId,
isCycleRun: existingExecution.IsCycleRun,
startedOnUtc: existingExecution.StartedOnUtc,
completedOnUtc: completedOnUtc);
// Act
Result<Updated> result = await _sut.UpdateAsync(updatedExecution, CancellationToken.None);
// Assert
Assert.False(result.IsFailure);
Assert.Equal(Result.Updated, result.Value);
ScheduledJobExecutionEntity? modifiedExecution = await _mockContext.ScheduledJobExecutions.FirstOrDefaultAsync(execution => execution.Id == existingExecution.Id);
Assert.NotNull(modifiedExecution);
Assert.Equal(completedOnUtc, modifiedExecution!.CompletedOnUtc);
}
[Fact]
public async Task UpdateAsync_WhenExecutionDoesNotExist_ShouldReturnError()
{
// Arrange
ScheduledJobExecutionEntity execution = _scheduledJobExecutionEntityFixture.Create();
// Act
Result<Updated> result = await _sut.UpdateAsync(execution, CancellationToken.None);
// Assert
Assert.True(result.IsFailure);
Assert.Equal(Errors.Scheduling.ScheduledJobExecutionNotFound, result.FirstError);
}
[Fact]
public async Task GetOpenByScheduledJobIdAsync_WhenAnOpenExecutionExists_ShouldReturnTheMostRecentOne()
{
// Arrange
Guid scheduledJobId = Guid.NewGuid();
DateTime mostRecentStart = DateTime.UtcNow.AddMinutes(-1);
ScheduledJobExecutionEntity openExecution = _scheduledJobExecutionEntityFixture.Create(scheduledJobId: scheduledJobId, startedOnUtc: mostRecentStart, completedOnUtc: null);
ScheduledJobExecutionEntity olderOpenExecution = _scheduledJobExecutionEntityFixture.Create(scheduledJobId: scheduledJobId, startedOnUtc: mostRecentStart.AddMinutes(-10), completedOnUtc: null);
ScheduledJobExecutionEntity closedExecution = _scheduledJobExecutionEntityFixture.Create(scheduledJobId: scheduledJobId, startedOnUtc: mostRecentStart.AddMinutes(-5), completedOnUtc: DateTime.UtcNow);
_mockContext.ScheduledJobExecutions.AddRange(openExecution, olderOpenExecution, closedExecution);
await _mockContext.SaveChangesAsync();
// Act
Result<ScheduledJobExecutionEntity?> result = await _sut.GetOpenByScheduledJobIdAsync(scheduledJobId, CancellationToken.None);
// Assert
Assert.False(result.IsFailure);
Assert.NotNull(result.Value);
Assert.Equal(openExecution.Id, result.Value!.Id);
}
[Fact]
public async Task GetOpenByScheduledJobIdAsync_WhenNoOpenExecutionExists_ShouldReturnNull()
{
// Arrange
Guid scheduledJobId = Guid.NewGuid();
ScheduledJobExecutionEntity closedExecution = _scheduledJobExecutionEntityFixture.Create(scheduledJobId: scheduledJobId, completedOnUtc: DateTime.UtcNow);
_mockContext.ScheduledJobExecutions.Add(closedExecution);
await _mockContext.SaveChangesAsync();
// Act
Result<ScheduledJobExecutionEntity?> result = await _sut.GetOpenByScheduledJobIdAsync(scheduledJobId, CancellationToken.None);
// Assert
Assert.False(result.IsFailure);
Assert.Null(result.Value);
}
[Fact]
public async Task GetByTimeRangeAsync_WhenExecutionsStartedInTheRange_ShouldReturnThemOrderedByStartTime()
{
// Arrange
DateTime fromUtc = DateTime.UtcNow.AddHours(-2);
DateTime toUtc = DateTime.UtcNow;
ScheduledJobExecutionEntity inRange1 = _scheduledJobExecutionEntityFixture.Create(startedOnUtc: fromUtc.AddMinutes(1));
ScheduledJobExecutionEntity inRange2 = _scheduledJobExecutionEntityFixture.Create(startedOnUtc: fromUtc.AddMinutes(30));
ScheduledJobExecutionEntity beforeRange = _scheduledJobExecutionEntityFixture.Create(startedOnUtc: fromUtc.AddMinutes(-10));
ScheduledJobExecutionEntity afterRange = _scheduledJobExecutionEntityFixture.Create(startedOnUtc: toUtc.AddMinutes(10));
_mockContext.ScheduledJobExecutions.AddRange(inRange1, inRange2, beforeRange, afterRange);
await _mockContext.SaveChangesAsync();
// Act
Result<IEnumerable<ScheduledJobExecutionEntity>> result = await _sut.GetByTimeRangeAsync(fromUtc, toUtc, CancellationToken.None);
// Assert
Assert.False(result.IsFailure);
List<ScheduledJobExecutionEntity> executions = [.. result.Value];
Assert.Equal(2, executions.Count);
Assert.Equal(inRange1.Id, executions[0].Id);
Assert.Equal(inRange2.Id, executions[1].Id);
}
[Fact]
public async Task DeleteByScheduledJobIdAsync_WhenExecutionsExist_ShouldRemoveThemAllAndReturnSuccess()
{
// Arrange
Guid scheduledJobId = Guid.NewGuid();
ScheduledJobExecutionEntity execution1 = _scheduledJobExecutionEntityFixture.Create(scheduledJobId: scheduledJobId);
ScheduledJobExecutionEntity execution2 = _scheduledJobExecutionEntityFixture.Create(scheduledJobId: scheduledJobId);
ScheduledJobExecutionEntity otherExecution = _scheduledJobExecutionEntityFixture.Create();
_mockContext.ScheduledJobExecutions.AddRange(execution1, execution2, otherExecution);
await _mockContext.SaveChangesAsync();
// Act
Result<Success> result = await _sut.DeleteByScheduledJobIdAsync(scheduledJobId, CancellationToken.None);
// Assert
Assert.False(result.IsFailure);
Assert.Equal(Result.Success, result.Value);
Assert.Equal(2, _mockContext.ChangeTracker.Entries<ScheduledJobExecutionEntity>().Count(entry => entry.State == EntityState.Deleted));
Assert.Contains(_mockContext.ChangeTracker.Entries<ScheduledJobExecutionEntity>(), entry => entry.State == EntityState.Deleted && entry.Entity.Id == execution1.Id);
Assert.Contains(_mockContext.ChangeTracker.Entries<ScheduledJobExecutionEntity>(), entry => entry.State == EntityState.Deleted && entry.Entity.Id == execution2.Id);
}
[Fact]
public async Task DeleteByScheduledJobIdAsync_WhenNoExecutionsExist_ShouldReturnSuccess()
{
// Act
Result<Success> result = await _sut.DeleteByScheduledJobIdAsync(Guid.NewGuid(), CancellationToken.None);
// Assert
Assert.False(result.IsFailure);
Assert.Equal(Result.Success, result.Value);
}
}
@@ -0,0 +1,211 @@
#region ========================================================================= USING =====================================================================================
using EntityFrameworkCore.Testing.NSubstitute;
using Lumina.Application.Common.DataAccess.Entities.Scheduling;
using Lumina.Application.Fixtures.Common.DataAccess.Entities.Scheduling;
using Lumina.DataAccess.Core.Repositories.Scheduling;
using Lumina.DataAccess.Core.UoW;
using Lumina.Domain.Common.Errors;
using Lumina.Domain.Common.Primitives;
using Lumina.Domain.SharedKernel.Common.Enums.Scheduling;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.DataAccess.UnitTests.Core.Repositories.Scheduling;
/// <summary>
/// Contains unit tests for the <see cref="ScheduledJobRepository"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class ScheduledJobRepositoryTests
{
private readonly LuminaDbContext _mockContext;
private readonly ScheduledJobRepository _sut;
private readonly ScheduledJobEntityFixture _scheduledJobEntityFixture = new();
/// <summary>
/// Initializes a new instance of the <see cref="ScheduledJobRepositoryTests"/> class.
/// </summary>
public ScheduledJobRepositoryTests()
{
_mockContext = Create.MockedDbContextFor<LuminaDbContext>();
_sut = new ScheduledJobRepository(_mockContext);
}
[Fact]
public async Task GetByIdAsync_WhenScheduledJobExists_ShouldReturnScheduledJob()
{
// Arrange
ScheduledJobEntity scheduledJob = _scheduledJobEntityFixture.Create();
_mockContext.ScheduledJobs.Add(scheduledJob);
await _mockContext.SaveChangesAsync();
// Act
Result<ScheduledJobEntity?> result = await _sut.GetByIdAsync(scheduledJob.Id, CancellationToken.None);
// Assert
Assert.False(result.IsFailure);
Assert.NotNull(result.Value);
Assert.Equal(scheduledJob.Id, result.Value!.Id);
Assert.Equal(scheduledJob.Name, result.Value.Name);
}
[Fact]
public async Task GetByIdAsync_WhenScheduledJobDoesNotExist_ShouldReturnNull()
{
// Act
Result<ScheduledJobEntity?> result = await _sut.GetByIdAsync(Guid.NewGuid(), CancellationToken.None);
// Assert
Assert.False(result.IsFailure);
Assert.Null(result.Value);
}
[Fact]
public async Task GetAllAsync_WhenScheduledJobsExist_ShouldReturnAllScheduledJobs()
{
// Arrange
ScheduledJobEntity scheduledJob1 = _scheduledJobEntityFixture.Create();
ScheduledJobEntity scheduledJob2 = _scheduledJobEntityFixture.Create();
_mockContext.ScheduledJobs.AddRange(scheduledJob1, scheduledJob2);
await _mockContext.SaveChangesAsync();
// Act
Result<IEnumerable<ScheduledJobEntity>> result = await _sut.GetAllAsync(CancellationToken.None);
// Assert
Assert.False(result.IsFailure);
Assert.Equal(2, result.Value.Count());
}
[Fact]
public async Task GetActiveOrRunningAsync_WhenScheduledJobsAreActiveOrRunning_ShouldReturnOnlyThem()
{
// Arrange
ScheduledJobEntity activeJob = _scheduledJobEntityFixture.Create(status: ScheduledJobStatus.Active);
ScheduledJobEntity runningJob = _scheduledJobEntityFixture.Create(status: ScheduledJobStatus.Running);
ScheduledJobEntity addedJob = _scheduledJobEntityFixture.Create(status: ScheduledJobStatus.Added);
ScheduledJobEntity completedJob = _scheduledJobEntityFixture.Create(status: ScheduledJobStatus.Completed);
_mockContext.ScheduledJobs.AddRange(activeJob, runningJob, addedJob, completedJob);
await _mockContext.SaveChangesAsync();
// Act
Result<IEnumerable<ScheduledJobEntity>> result = await _sut.GetActiveOrRunningAsync(CancellationToken.None);
// Assert
Assert.False(result.IsFailure);
List<ScheduledJobEntity> scheduledJobs = result.Value.ToList();
Assert.Equal(2, scheduledJobs.Count);
Assert.Contains(scheduledJobs, scheduledJob => scheduledJob.Id == activeJob.Id);
Assert.Contains(scheduledJobs, scheduledJob => scheduledJob.Id == runningJob.Id);
}
[Fact]
public async Task InsertAsync_WhenScheduledJobDoesNotExist_ShouldAddScheduledJobToContextAndReturnCreated()
{
// Arrange
ScheduledJobEntity scheduledJob = _scheduledJobEntityFixture.Create();
// Act
Result<Created> result = await _sut.InsertAsync(scheduledJob, CancellationToken.None);
// Assert
Assert.False(result.IsFailure);
Assert.Equal(Result.Created, result.Value);
EntityEntry<ScheduledJobEntity>? addedScheduledJob = _mockContext.ChangeTracker.Entries<ScheduledJobEntity>()
.FirstOrDefault(entry => entry.State == EntityState.Added && entry.Entity.Id == scheduledJob.Id);
Assert.NotNull(addedScheduledJob);
Assert.Equal(scheduledJob.Name, addedScheduledJob!.Entity.Name);
}
[Fact]
public async Task InsertAsync_WhenScheduledJobAlreadyExists_ShouldReturnError()
{
// Arrange
ScheduledJobEntity scheduledJob = _scheduledJobEntityFixture.Create();
_mockContext.ScheduledJobs.Add(scheduledJob);
await _mockContext.SaveChangesAsync();
// Act
Result<Created> result = await _sut.InsertAsync(scheduledJob, CancellationToken.None);
// Assert
Assert.True(result.IsFailure);
Assert.Equal(Errors.Scheduling.ScheduledJobAlreadyExists, result.FirstError);
}
[Fact]
public async Task UpdateAsync_WhenScheduledJobExists_ShouldUpdateItsPropertiesAndReturnUpdated()
{
// Arrange
ScheduledJobEntity existingScheduledJob = _scheduledJobEntityFixture.Create(status: ScheduledJobStatus.Added);
_mockContext.ScheduledJobs.Add(existingScheduledJob);
await _mockContext.SaveChangesAsync();
ScheduledJobEntity updatedScheduledJob = _scheduledJobEntityFixture.Create(
id: existingScheduledJob.Id,
name: "Updated name",
status: ScheduledJobStatus.Active);
// Act
Result<Updated> result = await _sut.UpdateAsync(updatedScheduledJob, CancellationToken.None);
// Assert
Assert.False(result.IsFailure);
Assert.Equal(Result.Updated, result.Value);
ScheduledJobEntity? modifiedScheduledJob = await _mockContext.ScheduledJobs.FirstOrDefaultAsync(scheduledJob => scheduledJob.Id == existingScheduledJob.Id);
Assert.NotNull(modifiedScheduledJob);
Assert.Equal("Updated name", modifiedScheduledJob!.Name);
Assert.Equal(ScheduledJobStatus.Active, modifiedScheduledJob.Status);
}
[Fact]
public async Task UpdateAsync_WhenScheduledJobDoesNotExist_ShouldReturnError()
{
// Arrange
ScheduledJobEntity scheduledJob = _scheduledJobEntityFixture.Create();
// Act
Result<Updated> result = await _sut.UpdateAsync(scheduledJob, CancellationToken.None);
// Assert
Assert.True(result.IsFailure);
Assert.Equal(Errors.Scheduling.ScheduledJobNotFound, result.FirstError);
}
[Fact]
public async Task DeleteByIdAsync_WhenScheduledJobExists_ShouldRemoveItAndReturnDeleted()
{
// Arrange
ScheduledJobEntity scheduledJob = _scheduledJobEntityFixture.Create();
_mockContext.ScheduledJobs.Add(scheduledJob);
await _mockContext.SaveChangesAsync();
// Act
Result<Deleted> result = await _sut.DeleteByIdAsync(scheduledJob.Id, CancellationToken.None);
// Assert
Assert.False(result.IsFailure);
Assert.Equal(Result.Deleted, result.Value);
EntityEntry<ScheduledJobEntity>? deletedScheduledJob = _mockContext.ChangeTracker.Entries<ScheduledJobEntity>()
.FirstOrDefault(entry => entry.State == EntityState.Deleted && entry.Entity.Id == scheduledJob.Id);
Assert.NotNull(deletedScheduledJob);
}
[Fact]
public async Task DeleteByIdAsync_WhenScheduledJobDoesNotExist_ShouldReturnError()
{
// Act
Result<Deleted> result = await _sut.DeleteByIdAsync(Guid.NewGuid(), CancellationToken.None);
// Assert
Assert.True(result.IsFailure);
Assert.Equal(Errors.Scheduling.ScheduledJobNotFound, result.FirstError);
}
}
@@ -0,0 +1,123 @@
#region ========================================================================= USING =====================================================================================
using EntityFrameworkCore.Testing.NSubstitute;
using Lumina.Application.Common.DataAccess.Entities.Scheduling;
using Lumina.Application.Fixtures.Common.DataAccess.Entities.Scheduling;
using Lumina.DataAccess.Core.Repositories.Scheduling;
using Lumina.DataAccess.Core.UoW;
using Lumina.Domain.Common.Primitives;
using Lumina.Domain.SharedKernel.Common.Enums.Scheduling;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using System;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.DataAccess.UnitTests.Core.Repositories.Scheduling;
/// <summary>
/// Contains unit tests for the <see cref="SchedulerDisplayPreferencesRepository"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class SchedulerDisplayPreferencesRepositoryTests
{
private readonly LuminaDbContext _mockContext;
private readonly SchedulerDisplayPreferencesRepository _sut;
private readonly SchedulerDisplayPreferencesEntityFixture _schedulerDisplayPreferencesEntityFixture = new();
/// <summary>
/// Initializes a new instance of the <see cref="SchedulerDisplayPreferencesRepositoryTests"/> class.
/// </summary>
public SchedulerDisplayPreferencesRepositoryTests()
{
_mockContext = Create.MockedDbContextFor<LuminaDbContext>();
_sut = new SchedulerDisplayPreferencesRepository(_mockContext);
}
[Fact]
public async Task GetByUserIdAsync_WhenDisplayPreferencesExist_ShouldReturnThem()
{
// Arrange
SchedulerDisplayPreferencesEntity displayPreferences = _schedulerDisplayPreferencesEntityFixture.Create();
_mockContext.SchedulerDisplayPreferences.Add(displayPreferences);
await _mockContext.SaveChangesAsync();
// Act
Result<SchedulerDisplayPreferencesEntity?> result = await _sut.GetByUserIdAsync(displayPreferences.UserId, CancellationToken.None);
// Assert
Assert.False(result.IsFailure);
Assert.NotNull(result.Value);
Assert.Equal(displayPreferences.Id, result.Value!.Id);
Assert.Equal(displayPreferences.UserId, result.Value.UserId);
Assert.Equal(displayPreferences.DisplayTimeSpan, result.Value.DisplayTimeSpan);
}
[Fact]
public async Task GetByUserIdAsync_WhenDisplayPreferencesDoNotExist_ShouldReturnNull()
{
// Act
Result<SchedulerDisplayPreferencesEntity?> result = await _sut.GetByUserIdAsync(Guid.NewGuid(), CancellationToken.None);
// Assert
Assert.False(result.IsFailure);
Assert.Null(result.Value);
}
[Fact]
public async Task UpsertAsync_WhenDisplayPreferencesDoNotExist_ShouldAddThemToContextAndReturnUpdated()
{
// Arrange
SchedulerDisplayPreferencesEntity displayPreferences = _schedulerDisplayPreferencesEntityFixture.Create(
displayTimeSpan: 30,
displayTimeUnit: SchedulerDisplayTimeUnit.Minutes);
// Act
Result<Updated> result = await _sut.UpsertAsync(displayPreferences, CancellationToken.None);
// Assert
Assert.False(result.IsFailure);
Assert.Equal(Result.Updated, result.Value);
EntityEntry<SchedulerDisplayPreferencesEntity>? addedPreferences = _mockContext.ChangeTracker.Entries<SchedulerDisplayPreferencesEntity>()
.FirstOrDefault(entry => entry.State == EntityState.Added && entry.Entity.UserId == displayPreferences.UserId);
Assert.NotNull(addedPreferences);
}
[Fact]
public async Task UpsertAsync_WhenDisplayPreferencesAlreadyExist_ShouldUpdateTheMutableFieldsAndReturnUpdated()
{
// Arrange
Guid userId = Guid.NewGuid();
SchedulerDisplayPreferencesEntity existingPreferences = _schedulerDisplayPreferencesEntityFixture.Create(
userId: userId,
jobTypeFilter: ScheduledTaskType.ScanMediaLibraries,
displayTimeSpan: 10,
displayTimeUnit: SchedulerDisplayTimeUnit.Hours);
_mockContext.SchedulerDisplayPreferences.Add(existingPreferences);
await _mockContext.SaveChangesAsync();
SchedulerDisplayPreferencesEntity updatedPreferences = _schedulerDisplayPreferencesEntityFixture.Create(
userId: userId,
jobTypeFilter: ScheduledTaskType.CleanTemporaryFiles,
displayTimeSpan: 60,
displayTimeUnit: SchedulerDisplayTimeUnit.Days);
// Act
Result<Updated> result = await _sut.UpsertAsync(updatedPreferences, CancellationToken.None);
// Assert
Assert.False(result.IsFailure);
Assert.Equal(Result.Updated, result.Value);
SchedulerDisplayPreferencesEntity? modifiedPreferences = await _mockContext.SchedulerDisplayPreferences
.FirstOrDefaultAsync(preferences => preferences.UserId == userId);
Assert.NotNull(modifiedPreferences);
// The Id and the ownership of the existing preferences are preserved.
Assert.Equal(existingPreferences.Id, modifiedPreferences!.Id);
Assert.Equal(ScheduledTaskType.CleanTemporaryFiles, modifiedPreferences.JobTypeFilter);
Assert.Equal(60, modifiedPreferences.DisplayTimeSpan);
Assert.Equal(SchedulerDisplayTimeUnit.Days, modifiedPreferences.DisplayTimeUnit);
Assert.Single(_mockContext.ChangeTracker.Entries<SchedulerDisplayPreferencesEntity>());
}
}