Skip to content

Commit

Permalink
Fix DateTime comparison in DqtReportingService tests (#1045)
Browse files Browse the repository at this point in the history
  • Loading branch information
gunndabad authored Jan 3, 2024
1 parent 25dbcf7 commit 977b10e
Showing 1 changed file with 21 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,9 @@

namespace TeachingRecordSystem.Core.Dqt.CrmIntegrationTests.Services.DqtReporting;

public class DqtReportingServiceTests : IClassFixture<DqtReportingFixture>
public class DqtReportingServiceTests(DqtReportingFixture fixture) : IClassFixture<DqtReportingFixture>
{
private readonly DqtReportingFixture _fixture;

public DqtReportingServiceTests(DqtReportingFixture fixture)
{
_fixture = fixture;
}
private static readonly TimeSpan _dateTimeComparisonTolerance = TimeSpan.FromMilliseconds(500);

[Fact]
public async Task ProcessChangesForEntityType_WritesNewRecordToDatabase()
Expand All @@ -28,24 +23,24 @@ public async Task ProcessChangesForEntityType_WritesNewRecordToDatabase()
var newItem = new NewOrUpdatedItem(ChangeType.NewOrUpdated, newContact);

// Act
await _fixture.PublishChangedItemsAndConsume(newItem);
await fixture.PublishChangedItemsAndConsume(newItem);

// Assert
var row = await GetRowById(Contact.EntityLogicalName, contactId);
Assert.NotNull(row);
Assert.Equal(newContact.Id, row["Id"]);
Assert.Equal(newContact.FirstName, row["firstname"]);
Assert.Equal(newContact.LastName, row["lastname"]);
Assert.Equal(_fixture.Clock.UtcNow, row["__Inserted"]);
Assert.Equal(_fixture.Clock.UtcNow, row["__Updated"]);
Assert.Equal(fixture.Clock.UtcNow, (DateTime)row["__Inserted"]!, _dateTimeComparisonTolerance);
Assert.Equal(fixture.Clock.UtcNow, (DateTime)row["__Updated"]!, _dateTimeComparisonTolerance);
}

[Fact]
public async Task ProcessChangesForEntityType_WritesUpdatedRecordToDatabase()
{
// Arrange
var contactId = Guid.NewGuid();
var insertedTime = _fixture.Clock.UtcNow.AddDays(-10);
var insertedTime = fixture.Clock.UtcNow.AddDays(-10);

await InsertRow(Contact.EntityLogicalName, new Dictionary<string, object?>()
{
Expand All @@ -65,24 +60,24 @@ public async Task ProcessChangesForEntityType_WritesUpdatedRecordToDatabase()
var newItem = new NewOrUpdatedItem(ChangeType.NewOrUpdated, updatedContact);

// Act
await _fixture.PublishChangedItemsAndConsume(newItem);
await fixture.PublishChangedItemsAndConsume(newItem);

// Assert
var row = await GetRowById(Contact.EntityLogicalName, contactId);
Assert.NotNull(row);
Assert.Equal(updatedContact.Id, row["Id"]);
Assert.Equal(updatedContact.FirstName, row["firstname"]);
Assert.Equal(updatedContact.LastName, row["lastname"]);
Assert.Equal(insertedTime, row["__Inserted"]);
Assert.Equal(_fixture.Clock.UtcNow, row["__Updated"]);
Assert.Equal(insertedTime, (DateTime)row["__Inserted"]!, _dateTimeComparisonTolerance);
Assert.Equal(fixture.Clock.UtcNow, (DateTime)row["__Updated"]!, _dateTimeComparisonTolerance);
}

[Fact]
public async Task ProcessChangesForEntityType_DeletesRemovedRecordFromDatabase()
{
// Arrange
var contactId = Guid.NewGuid();
var insertedTime = _fixture.Clock.UtcNow.AddDays(-10);
var insertedTime = fixture.Clock.UtcNow.AddDays(-10);

await InsertRow(Contact.EntityLogicalName, new Dictionary<string, object?>()
{
Expand All @@ -97,12 +92,12 @@ public async Task ProcessChangesForEntityType_DeletesRemovedRecordFromDatabase()
new EntityReference(Contact.EntityLogicalName, contactId));

// Act
await _fixture.PublishChangedItemsAndConsume(removedItem);
await fixture.PublishChangedItemsAndConsume(removedItem);

// Assert
var row = await GetRowById(Contact.EntityLogicalName, contactId);
Assert.Null(row);
await AssertInDeleteLog(Contact.EntityLogicalName, contactId, expectedDeleted: _fixture.Clock.UtcNow);
await AssertInDeleteLog(Contact.EntityLogicalName, contactId, expectedDeleted: fixture.Clock.UtcNow);
}

[Theory]
Expand All @@ -120,7 +115,7 @@ public async Task ProcessChangesForEntityType_SameRecordMultipleTimesInBatch_Wri
{ "Id", contactId },
{ "firstname", Faker.Name.First() },
{ "lastname", Faker.Name.Last() },
{ "__Inserted", _fixture.Clock.UtcNow.Subtract(TimeSpan.FromHours(1)) }
{ "__Inserted", fixture.Clock.UtcNow.Subtract(TimeSpan.FromHours(1)) }
});
}

Expand All @@ -129,35 +124,35 @@ public async Task ProcessChangesForEntityType_SameRecordMultipleTimesInBatch_Wri
Id = contactId,
FirstName = Faker.Name.First(),
LastName = Faker.Name.Last(),
ModifiedOn = _fixture.Clock.UtcNow,
ModifiedOn = fixture.Clock.UtcNow,
};

var contact2 = new Contact()
{
Id = contactId,
FirstName = Faker.Name.First(),
LastName = Faker.Name.Last(),
ModifiedOn = _fixture.Clock.UtcNow.AddMinutes(1),
ModifiedOn = fixture.Clock.UtcNow.AddMinutes(1),
};

var newItem1 = new NewOrUpdatedItem(ChangeType.NewOrUpdated, contact1);
var newItem2 = new NewOrUpdatedItem(ChangeType.NewOrUpdated, contact2);

// Act
await _fixture.PublishChangedItemsAndConsume(newItem1, newItem2);
await fixture.PublishChangedItemsAndConsume(newItem1, newItem2);

// Assert
var row = await GetRowById(Contact.EntityLogicalName, contactId);
Assert.NotNull(row);
Assert.Equal(contactId, row["Id"]);
Assert.Equal(contact2.FirstName, row["firstname"]);
Assert.Equal(contact2.LastName, row["lastname"]);
Assert.Equal(_fixture.Clock.UtcNow, row["__Updated"]);
Assert.Equal(fixture.Clock.UtcNow, (DateTime)row["__Updated"]!, _dateTimeComparisonTolerance);
}

private async Task AssertInDeleteLog(string entityLogicalName, Guid entityId, DateTime expectedDeleted)
{
using var sqlConnection = new SqlConnection(_fixture.ReportingDbConnectionString);
using var sqlConnection = new SqlConnection(fixture.ReportingDbConnectionString);
await sqlConnection.OpenAsync();

var cmd = new SqlCommand("select Deleted from [__DeleteLog] where EntityId = @EntityId and EntityType = @EntityType");
Expand All @@ -173,12 +168,12 @@ private async Task AssertInDeleteLog(string entityLogicalName, Guid entityId, Da
}

var deleted = reader.GetDateTime(0);
Assert.Equal(expectedDeleted, deleted);
Assert.Equal(expectedDeleted, deleted, _dateTimeComparisonTolerance);
}

private async Task<IReadOnlyDictionary<string, object?>?> GetRowById(string tableName, Guid id)
{
using var sqlConnection = new SqlConnection(_fixture.ReportingDbConnectionString);
using var sqlConnection = new SqlConnection(fixture.ReportingDbConnectionString);
await sqlConnection.OpenAsync();

var cmd = new SqlCommand($"select * from {tableName} where id = @id");
Expand All @@ -202,7 +197,7 @@ private async Task AssertInDeleteLog(string entityLogicalName, Guid entityId, Da

private async Task InsertRow(string tableName, IReadOnlyDictionary<string, object?> columns)
{
using var sqlConnection = new SqlConnection(_fixture.ReportingDbConnectionString);
using var sqlConnection = new SqlConnection(fixture.ReportingDbConnectionString);
await sqlConnection.OpenAsync();

var cmd = new SqlCommand(
Expand Down

0 comments on commit 977b10e

Please sign in to comment.