Skip to content

Commit

Permalink
Added fixes while testing against CRM build environment data
Browse files Browse the repository at this point in the history
  • Loading branch information
hortha committed Dec 16, 2024
1 parent dbc2969 commit c778f0f
Show file tree
Hide file tree
Showing 6 changed files with 202 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public async Task ExecuteAsync(bool createMigratedEvent, bool dryRun, Cancellati

var serviceClient = _crmServiceClientProvider.GetClient(TrsDataSyncService.CrmClientName);
var columns = new ColumnSet(TrsDataSyncHelper.GetEntityInfoForModelType(TrsDataSyncHelper.ModelTypes.Induction).AttributeNames);
var filter = new FilterExpression();
filter.AddCondition(Contact.Fields.dfeta_InductionStatus, ConditionOperator.NotNull);

var query = new QueryExpression(Contact.EntityLogicalName)
{
Expand All @@ -43,7 +45,9 @@ public async Task ExecuteAsync(bool createMigratedEvent, bool dryRun, Cancellati
{
Count = pageSize,
PageNumber = 1
}
},
Criteria = filter

};

while (true)
Expand All @@ -70,6 +74,11 @@ await _trsDataSyncHelper.SyncInductionsAsync(

if (result.MoreRecords)
{
if (query.PageInfo.PageNumber == 100)
{
break;
}

query.PageInfo.PageNumber++;
query.PageInfo.PagingCookie = result.PagingCookie;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,8 @@ public async Task<int> SyncInductionsAsync(
dfeta_induction.Fields.dfeta_InductionStatus,
dfeta_induction.Fields.CreatedOn,
dfeta_induction.Fields.CreatedBy,
dfeta_induction.Fields.ModifiedOn
dfeta_induction.Fields.ModifiedOn,
dfeta_induction.Fields.StateCode
};

var inductions = await GetEntitiesAsync<dfeta_induction>(
Expand Down Expand Up @@ -644,6 +645,7 @@ private EntityVersionInfo<TEntity>[] GetEntityVersions<TEntity>(TEntity latest,
.OfType<AttributeAuditDetail>()
.Select(a => (AuditDetail: a, AuditRecord: a.AuditRecord.ToEntity<Audit>()))
.OrderBy(a => a.AuditRecord.CreatedOn)
.ThenBy(a => a.AuditRecord.Action == Audit_Action.Create ? 0 : 1)
.ToArray();

if (ordered.Length == 0)
Expand Down Expand Up @@ -1012,7 +1014,21 @@ RETURNING t.person_id
{
Contact.PrimaryIdAttribute,
Contact.Fields.dfeta_InductionStatus,
Contact.Fields.dfeta_qtlsdate
Contact.Fields.dfeta_qtlsdate,
Contact.Fields.CreatedOn,
Contact.Fields.CreatedBy,
Contact.Fields.StateCode,
Contact.Fields.ModifiedOn,
Contact.Fields.dfeta_TRN,
Contact.Fields.FirstName,
Contact.Fields.MiddleName,
Contact.Fields.LastName,
Contact.Fields.dfeta_StatedFirstName,
Contact.Fields.dfeta_StatedMiddleName,
Contact.Fields.dfeta_StatedLastName,
Contact.Fields.BirthDate,
Contact.Fields.dfeta_NINumber,
Contact.Fields.EMailAddress1,
};

Action<NpgsqlBinaryImporter, InductionInfo> writeRecord = (writer, induction) =>
Expand Down Expand Up @@ -1225,7 +1241,8 @@ private static List<Person> MapPersons(IEnumerable<Contact> contacts) => contact
dfeta_induction.Fields.dfeta_InductionExemptionReason,
dfeta_induction.Fields.dfeta_StartDate,
dfeta_induction.Fields.dfeta_InductionStatus,
dfeta_induction.Fields.ModifiedOn
dfeta_induction.Fields.ModifiedOn,
dfeta_induction.Fields.StateCode
};

if (auditDetails.TryGetValue(induction!.Id, out var inductionAudits))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using Microsoft.Extensions.Options;
using TeachingRecordSystem.Core.Dqt.Models;
using TeachingRecordSystem.Core.Jobs;
using TeachingRecordSystem.Core.Services.TrsDataSync;
using TeachingRecordSystem.Core.Tests.Services.TrsDataSync;

namespace TeachingRecordSystem.Core.Tests.Jobs;

[CollectionDefinition(nameof(TrsDataSyncTestCollection), DisableParallelization = true)]
public class SyncAllInductionsFromCrmJobTests : SyncFromCrmJobTestBase, IAsyncLifetime
{
public SyncAllInductionsFromCrmJobTests(SyncFromCrmJobFixture jobFixture) : base(jobFixture)
{
}

[Fact]
public async Task SyncInductionsAsync_WithExistingDqtInduction_UpdatesPersonRecord()
{
// Arrange
var inductionStatus = dfeta_InductionStatus.Pass;
var inductionStartDate = Clock.Today.AddYears(-1);
var inductionCompletedDate = Clock.Today.AddDays(-5);
var options = Options.Create(new TrsDataSyncServiceOptions()
{
CrmConnectionString = "dummy",
ModelTypes = [TrsDataSyncHelper.ModelTypes.Person],
PollIntervalSeconds = 60,
IgnoreInvalidData = false,
RunService = false
});

var person = await TestData.CreatePersonAsync(
p => p.WithTrn()
.WithQts()
.WithDqtInduction(inductionStatus, null, inductionStartDate, inductionCompletedDate)
.WithSyncOverride(false));

// Act
var job = new SyncAllInductionsFromCrmJob(
CrmServiceClientProvider,
Helper,
options);

await job.ExecuteAsync(createMigratedEvent: false, dryRun: false, CancellationToken.None);

// Assert
await DbFixture.WithDbContextAsync(async dbContext =>
{
var updatedPerson = await dbContext.Persons.SingleOrDefaultAsync(p => p.DqtContactId == person.ContactId);
Assert.Equal(inductionStatus.ToInductionStatus(), updatedPerson!.InductionStatus);
Assert.Equal(inductionStartDate, updatedPerson.InductionStartDate);
Assert.Equal(inductionCompletedDate, updatedPerson.InductionCompletedDate);
});
}

Task IAsyncLifetime.DisposeAsync() => Task.CompletedTask;

Task IAsyncLifetime.InitializeAsync() => JobFixture.DbFixture.DbHelper.ClearDataAsync();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using Microsoft.PowerPlatform.Dataverse.Client;
using TeachingRecordSystem.Core.Dqt;
using TeachingRecordSystem.Core.Services.TrsDataSync;

namespace TeachingRecordSystem.Core.Tests.Jobs;

public class SyncFromCrmJobFixture : IAsyncLifetime
{
public SyncFromCrmJobFixture(
DbFixture dbFixture,
IOrganizationServiceAsync2 organizationService,
ReferenceDataCache referenceDataCache,
FakeTrnGenerator trnGenerator)
{
DbFixture = dbFixture;
Clock = new();

Helper = new TrsDataSyncHelper(
dbFixture.GetDataSource(),
organizationService,
referenceDataCache,
Clock);

TestData = new TestData(
dbFixture.GetDbContextFactory(),
organizationService,
referenceDataCache,
Clock,
trnGenerator,
TestDataSyncConfiguration.Sync(Helper));

CrmServiceClientProvider = new TestCrmServiceClientProvider(organizationService);
}

public TestableClock Clock { get; }

public DbFixture DbFixture { get; }

public TrsDataSyncHelper Helper { get; }

public TestData TestData { get; }

public ICrmServiceClientProvider CrmServiceClientProvider { get; }

Task IAsyncLifetime.DisposeAsync() => Task.CompletedTask;

Task IAsyncLifetime.InitializeAsync() => DbFixture.DbHelper.ClearDataAsync();

private class TestCrmServiceClientProvider : ICrmServiceClientProvider
{
private readonly IOrganizationServiceAsync2 _organizationService;

public TestCrmServiceClientProvider(IOrganizationServiceAsync2 organizationService)
{
_organizationService = organizationService;
}

public IOrganizationServiceAsync2 GetClient(string name)
{
return _organizationService;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using TeachingRecordSystem.Core.Dqt;
using TeachingRecordSystem.Core.Services.TrsDataSync;

namespace TeachingRecordSystem.Core.Tests.Jobs;

public abstract class SyncFromCrmJobTestBase : IClassFixture<SyncFromCrmJobFixture>
{
public SyncFromCrmJobTestBase(SyncFromCrmJobFixture jobFixture)
{
JobFixture = jobFixture;
}

public SyncFromCrmJobFixture JobFixture { get; }

protected TestableClock Clock => JobFixture.Clock;

protected DbFixture DbFixture => JobFixture.DbFixture;

protected TrsDataSyncHelper Helper => JobFixture.Helper;

protected TestData TestData => JobFixture.TestData;

public ICrmServiceClientProvider CrmServiceClientProvider => JobFixture.CrmServiceClientProvider;
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,32 @@ await DbFixture.WithDbContextAsync(async dbContext =>
});
}

[Fact]
public async Task SyncInductionsAsync_WithExistingDqtInduction_UpdatesPersonRecord()
{
// Arrange
var inductionStatus = dfeta_InductionStatus.InProgress;
var inductionStartDate = Clock.Today.AddYears(-1);

var person = await TestData.CreatePersonAsync(
p => p.WithTrn()
.WithQts()
.WithDqtInduction(inductionStatus, null, inductionStartDate, null)
.WithSyncOverride(false));

// Act
await Helper.SyncInductionsAsync([person.Contact], ignoreInvalid: true, createMigratedEvent: false, dryRun: false, CancellationToken.None);

// Assert
await DbFixture.WithDbContextAsync(async dbContext =>
{
var updatedPerson = await dbContext.Persons.SingleOrDefaultAsync(p => p.DqtContactId == person.ContactId);
Assert.Equal(inductionStatus.ToInductionStatus(), updatedPerson!.InductionStatus);
Assert.Equal(inductionStartDate, updatedPerson.InductionStartDate);

});
}

[Fact]
public async Task SyncInductionsAsync_WithQtlsButNotExemptAndIgnoreInvalidSetToFalse_ThrowsException()
{
Expand Down

0 comments on commit c778f0f

Please sign in to comment.