Files
2026-03-24 15:26:03 -05:00

383 lines
9.0 KiB
C#

using LCU.Api.Internal.Dispatcher.Configurations;
using LCU.Api.Internal.Dispatcher.Services;
using LCU.Api.Internal.Dispatcher.Services.DispatcherOperationServices;
using LCU.Api.Internal.DispatcherLib.Repositories;
using LCU.Library.Internal.DispatcherModels.Models.Results;
using System.Collections.Generic;
namespace LCU.Api.Internal.DispatcherTests.ServiceTests.DispatcherOperationServices;
public class CleanUpJobsServiceTest
{
private readonly Mock<IJobService> _mockJobService;
private readonly Mock<ICleanUpJobSettingService> _mockCleanUpJobSettingService;
private readonly Mock<IUnitOfWork> _mockUnitOfWork;
private readonly CleanUpJobsService _cleanUpJobsService;
public CleanUpJobsServiceTest()
{
var apiConfiguration = new ApiConfiguration()
{
CleanUpJobsDaysToDelete = -20
};
_mockJobService = new Mock<IJobService>();
_mockCleanUpJobSettingService = new Mock<ICleanUpJobSettingService>();
_mockUnitOfWork = new Mock<IUnitOfWork>();
_cleanUpJobsService = new CleanUpJobsService(_mockJobService.Object,
_mockCleanUpJobSettingService.Object,
_mockUnitOfWork.Object,
apiConfiguration);
}
[Fact]
public void Basic_Construct_Test()
{
Assert.NotNull(_cleanUpJobsService);
}
[Fact]
public void ShouldSoftDeleteByDate_True()
{
// Arrange
var setting = new CleanUpJobSetting()
{
CompletionDaysPast = -15
};
var execution = new WorkItemExecution()
{
WorkItemExecutionId = 1,
WorkItemId = 2,
CompletedTimestamp = DateTime.Now.AddDays(-30),
StartedTimestamp = DateTime.Now.AddDays(-30).AddMinutes(-1)
};
var workItem = new WorkItem()
{
JobId = 1,
WorkItemId = 2,
WorkItemExecutions = new List<WorkItemExecution>()
{
execution
},
Order = 1
};
var job = new Job()
{
JobId = 1,
WorkItems = new List<WorkItem>()
{
workItem
},
};
var result = new CleanUpJobBatchResult();
// Act
var shouldDelete = _cleanUpJobsService.ShouldSoftDeleteByDate(setting, job, result);
// Assert
Assert.True(shouldDelete);
}
[Fact]
public void ShouldSoftDeleteByDate_False()
{
// Arrange
var setting = new CleanUpJobSetting()
{
CompletionDaysPast = -45
};
var execution = new WorkItemExecution()
{
WorkItemExecutionId = 1,
WorkItemId = 2,
CompletedTimestamp = DateTime.Now.AddDays(-30),
StartedTimestamp = DateTime.Now.AddDays(-30).AddMinutes(-1)
};
var workItem = new WorkItem()
{
JobId = 1,
WorkItemId = 2,
WorkItemExecutions = new List<WorkItemExecution>()
{
execution
},
Order = 1
};
var job = new Job()
{
JobId = 1,
WorkItems = new List<WorkItem>()
{
workItem
},
};
var result = new CleanUpJobBatchResult();
// Act
var shouldDelete = _cleanUpJobsService.ShouldSoftDeleteByDate(setting, job, result);
// Assert
Assert.False(shouldDelete);
}
[Fact]
public void ShouldSoftDeleteByDate_NoWorkItemExecutions_False()
{
// Arrange
var setting = new CleanUpJobSetting()
{
CompletionDaysPast = -45
};
var workItem = new WorkItem()
{
JobId = 1,
WorkItemId = 2,
WorkItemExecutions = new List<WorkItemExecution>(),
Order = 1
};
var job = new Job()
{
JobId = 1,
WorkItems = new List<WorkItem>()
{
workItem
},
};
var result = new CleanUpJobBatchResult();
// Act
var shouldDelete = _cleanUpJobsService.ShouldSoftDeleteByDate(setting, job, result);
// Assert
Assert.False(shouldDelete);
Assert.Equal(1, result.FailureCount);
}
[Fact]
public void ShouldSoftDeleteByDate_NoWorkItems_False()
{
// Arrange
var setting = new CleanUpJobSetting()
{
CompletionDaysPast = -45
};
var job = new Job()
{
JobId = 1,
WorkItems = new List<WorkItem>(),
};
var result = new CleanUpJobBatchResult();
// Act
var shouldDelete = _cleanUpJobsService.ShouldSoftDeleteByDate(setting, job, result);
// Assert
Assert.False(shouldDelete);
Assert.Equal(1, result.FailureCount);
}
[Fact]
public void ShouldSoftDeleteByTag_Okay()
{
// Arrange
var setting = new CleanUpJobSetting()
{
CleanUpJobTags = new List<CleanUpJobTag>()
{
new CleanUpJobTag()
{
TagId = 1
},
new CleanUpJobTag()
{
TagId = 3
},
new CleanUpJobTag()
{
TagId = 5
}
}
};
var job = new Job()
{
JobTags = new List<JobTag>()
{
new JobTag()
{
TagId = 1
},
new JobTag()
{
TagId = 3
},
new JobTag()
{
TagId = 5
}
}
};
// Act
var shouldDelete = _cleanUpJobsService.ShouldSoftDeleteByTag(setting, job);
// Assert
Assert.True(shouldDelete);
}
[Fact]
public void ShouldSoftDeleteByTag_Okay_UnorderedTagId()
{
// Arrange
var setting = new CleanUpJobSetting()
{
CleanUpJobTags = new List<CleanUpJobTag>()
{
new CleanUpJobTag()
{
TagId = 1
},
new CleanUpJobTag()
{
TagId = 3
},
new CleanUpJobTag()
{
TagId = 5
}
}
};
var job = new Job()
{
JobTags = new List<JobTag>()
{
new JobTag()
{
TagId = 3
},
new JobTag()
{
TagId = 5
},
new JobTag()
{
TagId = 1
}
}
};
// Act
var shouldDelete = _cleanUpJobsService.ShouldSoftDeleteByTag(setting, job);
// Assert
Assert.True(shouldDelete);
}
[Fact]
public void ShouldSoftDeleteByTag_False_NumberOfTags()
{
// Arrange
var setting = new CleanUpJobSetting()
{
CleanUpJobTags = new List<CleanUpJobTag>()
{
new CleanUpJobTag()
{
TagId = 1
},
new CleanUpJobTag()
{
TagId = 3
},
new CleanUpJobTag()
{
TagId = 5
}
}
};
var job = new Job()
{
JobTags = new List<JobTag>()
{
new JobTag()
{
TagId = 3
},
new JobTag()
{
TagId = 5
}
}
};
// Act
var shouldDelete = _cleanUpJobsService.ShouldSoftDeleteByTag(setting, job);
// Assert
Assert.False(shouldDelete);
}
[Fact]
public void ShouldSoftDeleteByTag_False_DifferentTags()
{
// Arrange
var setting = new CleanUpJobSetting()
{
CleanUpJobTags = new List<CleanUpJobTag>()
{
new CleanUpJobTag()
{
TagId = 1
},
new CleanUpJobTag()
{
TagId = 3
},
new CleanUpJobTag()
{
TagId = 5
}
}
};
var job = new Job()
{
JobTags = new List<JobTag>()
{
new JobTag()
{
TagId = 3
},
new JobTag()
{
TagId = 5
},
new JobTag()
{
TagId = 100
}
}
};
// Act
var shouldDelete = _cleanUpJobsService.ShouldSoftDeleteByTag(setting, job);
// Assert
Assert.False(shouldDelete);
}
}