Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 70 additions & 2 deletions test/Effort/ClinicalImportServiceTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Threading.Channels;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -331,8 +332,10 @@ public async Task GetPreviewAsync_SyncMode_CalculatesCountsCorrectly()

#region ExecuteImportAsync - EnsureEffortPerson/Course Tests

[Fact]
public async Task ExecuteImportAsync_CreatesEffortPerson_WhenMissing()
[Theory]
[InlineData(false, null)]
[InlineData(true, EffortConstants.CahfsReportUnit)]
public async Task ExecuteImportAsync_CreatesEffortPerson_WhenMissing(bool hasCahfsJob, string? expectedReportUnit)
{
// Arrange - term, source data, AAUD record, VIPER person — but NO EffortPerson
await SeedTermAndCourseAsync();
Expand Down Expand Up @@ -382,6 +385,10 @@ public async Task ExecuteImportAsync_CreatesEffortPerson_WhenMissing()
});
await _aaudContext.SaveChangesAsync(TestContext.Current.CancellationToken);

_instructorServiceMock
.GetCahfsMothraIdsAsync(Arg.Any<IReadOnlyCollection<string>>(), TermCode, Arg.Any<CancellationToken>())
.Returns(hasCahfsJob ? new HashSet<string> { "INST0020" } : new HashSet<string>());

// Act
var result = await _service.ExecuteImportAsync(TermCode, ClinicalImportMode.AddNewOnly, modifiedBy: 1, TestContext.Current.CancellationToken);

Expand All @@ -394,6 +401,67 @@ public async Task ExecuteImportAsync_CreatesEffortPerson_WhenMissing()
Assert.Equal("CLINICAL", effortPerson.LastName);
Assert.Equal("VME", effortPerson.EffortDept);
Assert.Equal("1234", effortPerson.EffortTitleCode);
Assert.Equal(expectedReportUnit, effortPerson.ReportUnit);
await _instructorServiceMock.Received(1).GetCahfsMothraIdsAsync(
Arg.Any<IReadOnlyCollection<string>>(), TermCode, Arg.Any<CancellationToken>());
await _instructorServiceMock.DidNotReceiveWithAnyArgs().HasCahfsAppointmentAsync(default!, default, TestContext.Current.CancellationToken);
}

[Theory]
[InlineData(null, true, false, false, EffortConstants.CahfsReportUnit)]
[InlineData("", true, false, false, EffortConstants.CahfsReportUnit)]
[InlineData(null, false, false, false, null)]
[InlineData("PMI", true, false, false, "PMI")]
[InlineData(null, true, true, false, EffortConstants.CahfsReportUnit)]
[InlineData(null, true, true, true, EffortConstants.CahfsReportUnit)]
public async Task ExecuteImportAsync_TagsExistingPersonCahfs_OnlyWhenReportUnitUnset(
string? existingReportUnit, bool hasCahfsJob, bool hasExistingRecord, bool withProgress, string? expectedReportUnit)
{
// Arrange - instructor already exists for the term (e.g. added manually)
await SeedTermAndCourseAsync();
await SeedSourceDataAsync("INST0022", "DVM", "453", weekCount: 2);
_context.ViperPersons.Add(new ViperPerson
{
PersonId = 22,
FirstName = "Existing",
LastName = "Clinical",
MothraId = "INST0022"
});
_context.Persons.Add(new EffortPerson
{
PersonId = 22,
TermCode = TermCode,
FirstName = "EXISTING",
LastName = "CLINICAL",
EffortDept = "VME",
EffortTitleCode = "1234",
ReportUnit = existingReportUnit
});
await _context.SaveChangesAsync(TestContext.Current.CancellationToken);

if (hasExistingRecord)
{
// First import creates the clinical record, so the repeat import skips it
await _service.ExecuteImportAsync(TermCode, ClinicalImportMode.AddNewOnly, modifiedBy: 1, TestContext.Current.CancellationToken);
_auditServiceMock.ClearReceivedCalls();
}

_instructorServiceMock
.GetCahfsMothraIdsAsync(Arg.Any<IReadOnlyCollection<string>>(), TermCode, Arg.Any<CancellationToken>())
.Returns(hasCahfsJob ? new HashSet<string> { "INST0022" } : new HashSet<string>());

// Act
var result = withProgress
? await _service.ExecuteImportWithProgressAsync(TermCode, ClinicalImportMode.AddNewOnly, modifiedBy: 1,
Channel.CreateUnbounded<ClinicalImportProgressEvent>().Writer, TestContext.Current.CancellationToken)
: await _service.ExecuteImportAsync(TermCode, ClinicalImportMode.AddNewOnly, modifiedBy: 1, TestContext.Current.CancellationToken);

// Assert
Assert.True(result.Success);
var effortPerson = await _context.Persons.AsNoTracking().SingleAsync(p => p.PersonId == 22 && p.TermCode == TermCode, TestContext.Current.CancellationToken);
Assert.Equal(expectedReportUnit, effortPerson.ReportUnit);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
Comment thread
rlorenzo marked this conversation as resolved.
Resolved
_auditServiceMock.Received(expectedReportUnit != existingReportUnit ? 1 : 0).AddPersonChangeAudit(
22, TermCode, EffortAuditActions.UpdatePerson, Arg.Any<object>(), Arg.Any<object>());
}

[Fact]
Expand Down
Loading
Loading