Skip to content

Commit 841ac86

Browse files
author
Adrian Hall
committed
(#173) Fixes to the AzureSQL and PgSQL test suite to accommodate changes
1 parent b5f11b8 commit 841ac86

File tree

3 files changed

+25
-9
lines changed

3 files changed

+25
-9
lines changed

tests/CommunityToolkit.Datasync.Server.EntityFrameworkCore.Test/AzureSqlEntityTableRepository_Tests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public AzureSqlEntityTableRepository_Tests(DatabaseFixture fixture, ITestOutputH
3636
protected override bool CanRunLiveTests() => !string.IsNullOrEmpty(this.connectionString);
3737

3838
protected override async Task<AzureSqlEntityMovie> GetEntityAsync(string id)
39-
=> await Context.Movies.AsNoTracking().SingleOrDefaultAsync(m => m.Id == id);
39+
=> (await Context.Movies.AsNoTracking().SingleOrDefaultAsync(m => m.Id == id)).Clone();
4040

4141
protected override Task<int> GetEntityCountAsync()
4242
=> Task.FromResult(Context.Movies.Count());

tests/CommunityToolkit.Datasync.TestCommon/FluentExtensions/HttpExceptionAssertions.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,16 @@ public static class FluentHttpExceptionAssertions
1414
/// <summary>
1515
/// An extension to FluentAssertions to validate the payload of a <see cref="HttpException"/>.
1616
/// </summary>
17-
public static AndConstraint<ExceptionAssertions<HttpException>> WithPayload(this ExceptionAssertions<HttpException> current, object payload, string because = "", params object[] becauseArgs)
17+
public static AndConstraint<ExceptionAssertions<HttpException>> WithPayload(this ExceptionAssertions<HttpException> current, object expectedPayload, string because = "", params object[] becauseArgs)
1818
{
19-
current.Subject.First().Payload.Should().NotBeNull().And.BeEquivalentTo(payload, because, becauseArgs);
19+
object actualPayload = current.Subject.First().Payload;
20+
actualPayload.Should().NotBeNull();
21+
22+
// The UpdatedAt field should be within 1msec of the subject.
23+
actualPayload.Should().BeEquivalentTo(expectedPayload, options => options
24+
.Using<DateTimeOffset>(ctx => ctx.Subject.Should().BeCloseTo(ctx.Expectation, TimeSpan.FromMilliseconds(1)))
25+
.WhenTypeIs<DateTimeOffset>(), because, becauseArgs);
26+
2027
return new AndConstraint<ExceptionAssertions<HttpException>>(current);
2128
}
2229

tests/CommunityToolkit.Datasync.TestCommon/RepositoryTests.cs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,17 +125,19 @@ public async Task CreateAsync_CreatesNewEntity_WithSpecifiedId()
125125
{
126126
Skip.IfNot(CanRunLiveTests());
127127

128+
DateTimeOffset dto = DateTimeOffset.Now;
128129
IRepository<TEntity> Repository = await GetPopulatedRepositoryAsync();
129130
string id = await GetRandomEntityIdAsync(false);
130131
TEntity addition = TestData.Movies.OfType<TEntity>(TestData.Movies.BlackPanther, id);
131132
TEntity sut = addition.Clone();
132133
await Repository.CreateAsync(sut);
134+
133135
TEntity actual = await GetEntityAsync(id);
134136

135137
actual.Should().BeEquivalentTo<IMovie>(addition);
136138
actual.Should().NotBeEquivalentTo<ITableData>(addition).And.HaveEquivalentMetadataTo(sut);
137139
actual.Id.Should().Be(id);
138-
actual.UpdatedAt.Should().BeAfter(StartTime);
140+
actual.UpdatedAt?.Ticks.Should().BeGreaterThan(dto.Ticks);
139141
actual.Version.Should().NotBeNullOrEmpty();
140142
}
141143

@@ -146,6 +148,7 @@ public async Task CreateAsync_CreatesNewEntity_WithNullId(string id)
146148
{
147149
Skip.IfNot(CanRunLiveTests());
148150

151+
DateTimeOffset dto = DateTimeOffset.Now;
149152
IRepository<TEntity> Repository = await GetPopulatedRepositoryAsync();
150153
TEntity addition = TestData.Movies.OfType<TEntity>(TestData.Movies.BlackPanther);
151154
addition.Id = id;
@@ -154,7 +157,7 @@ public async Task CreateAsync_CreatesNewEntity_WithNullId(string id)
154157
TEntity actual = await GetEntityAsync(sut.Id);
155158

156159
actual.Should().BeEquivalentTo<IMovie>(addition);
157-
actual.UpdatedAt.Should().BeAfter(StartTime);
160+
actual.UpdatedAt?.Ticks.Should().BeGreaterThan(dto.Ticks);
158161
}
159162

160163
[SkippableFact]
@@ -177,6 +180,7 @@ public async Task CreateAsync_UpdatesMetadata()
177180
{
178181
Skip.IfNot(CanRunLiveTests());
179182

183+
DateTimeOffset dto = DateTimeOffset.Now;
180184
IRepository<TEntity> Repository = await GetPopulatedRepositoryAsync();
181185
string id = await GetRandomEntityIdAsync(false);
182186
TEntity addition = TestData.Movies.OfType<TEntity>(TestData.Movies.BlackPanther, id);
@@ -190,7 +194,7 @@ public async Task CreateAsync_UpdatesMetadata()
190194
actual.Should().BeEquivalentTo<IMovie>(addition);
191195
actual.Should().NotBeEquivalentTo<ITableData>(addition).And.HaveEquivalentMetadataTo(sut);
192196
actual.Id.Should().Be(id);
193-
actual.UpdatedAt.Should().BeAfter(StartTime);
197+
actual.UpdatedAt?.Ticks.Should().BeGreaterThan(dto.Ticks);
194198
actual.Version.Should().NotBeEquivalentTo(expectedVersion);
195199
}
196200

@@ -295,7 +299,9 @@ public async Task ReadAsync_ReturnsDisconnectedEntity()
295299
TEntity expected = await GetEntityAsync(id);
296300
TEntity actual = await Repository.ReadAsync(id);
297301

298-
actual.Should().BeEquivalentTo(expected).And.NotBeSameAs(expected);
302+
actual.Should().BeEquivalentTo<IMovie>(expected);
303+
actual.Should().HaveEquivalentMetadataTo(expected);
304+
actual.Should().NotBeSameAs(expected);
299305
}
300306

301307
[SkippableTheory]
@@ -373,6 +379,7 @@ public async Task ReplaceAsync_Replaces_OnVersionMatch()
373379
{
374380
Skip.IfNot(CanRunLiveTests());
375381

382+
DateTimeOffset dto = DateTimeOffset.Now;
376383
IRepository<TEntity> Repository = await GetPopulatedRepositoryAsync();
377384
string id = await GetRandomEntityIdAsync(true);
378385
TEntity replacement = TestData.Movies.OfType<TEntity>(TestData.Movies.BlackPanther, id);
@@ -383,14 +390,15 @@ public async Task ReplaceAsync_Replaces_OnVersionMatch()
383390

384391
actual.Should().BeEquivalentTo<IMovie>(replacement).And.NotBeEquivalentTo<ITableData>(expected);
385392
actual.Version.Should().NotBeEquivalentTo(version);
386-
actual.UpdatedAt.Should().BeAfter(StartTime);
393+
actual.UpdatedAt?.Ticks.Should().BeGreaterThan(dto.Ticks);
387394
}
388395

389396
[SkippableFact]
390397
public async Task ReplaceAsync_Replaces_OnNoVersion()
391398
{
392399
Skip.IfNot(CanRunLiveTests());
393400

401+
DateTimeOffset dto = DateTimeOffset.Now;
394402
IRepository<TEntity> Repository = await GetPopulatedRepositoryAsync();
395403
string id = await GetRandomEntityIdAsync(true);
396404
TEntity replacement = TestData.Movies.OfType<TEntity>(TestData.Movies.BlackPanther, id);
@@ -401,7 +409,8 @@ public async Task ReplaceAsync_Replaces_OnNoVersion()
401409

402410
actual.Should().BeEquivalentTo<IMovie>(replacement).And.NotBeEquivalentTo<ITableData>(expected);
403411
actual.Version.Should().NotBeEquivalentTo(version);
404-
actual.UpdatedAt.Should().BeAfter(StartTime);
412+
actual.UpdatedAt?.Ticks.Should().BeGreaterThan(dto.Ticks);
413+
405414
}
406415
#endregion
407416
}

0 commit comments

Comments
 (0)