Skip to content

Commit ccca249

Browse files
authored
(#146) Additional tests for lazy loading proxies. (#152)
1 parent 3f2b4c1 commit ccca249

File tree

4 files changed

+76
-1
lines changed

4 files changed

+76
-1
lines changed

Directory.Packages.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<PackageVersion Include="Microsoft.EntityFrameworkCore" Version="8.0.11" />
1515
<PackageVersion Include="Microsoft.EntityFrameworkCore.Cosmos" Version="8.0.11" />
1616
<PackageVersion Include="Microsoft.EntityFrameworkCore.InMemory" Version="8.0.11" />
17+
<PackageVersion Include="Microsoft.EntityFrameworkCore.Proxies" Version="8.0.11" />
1718
<PackageVersion Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.11" />
1819
<PackageVersion Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.11" />
1920
<PackageVersion Include="Microsoft.Extensions.Caching.Memory" Version="8.0.1" />

tests/CommunityToolkit.Datasync.Client.Test/CommunityToolkit.Datasync.Client.Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<ItemGroup>
33
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" />
4+
<PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" />
45
<PackageReference Include="System.Linq.Async" />
56
</ItemGroup>
67
<ItemGroup>

tests/CommunityToolkit.Datasync.Client.Test/Offline/Helpers/BaseTest.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,13 @@ public abstract class BaseTest
2323
/// <summary>
2424
/// Creates a version of the TestDbContext backed by SQLite.
2525
/// </summary>
26-
protected static TestDbContext CreateContext()
26+
protected static TestDbContext CreateContext(Action<DbContextOptionsBuilder<TestDbContext>> configureOptions = null)
2727
{
2828
SqliteConnection connection = new("Data Source=:memory:");
2929
connection.Open();
3030
DbContextOptionsBuilder<TestDbContext> optionsBuilder = new DbContextOptionsBuilder<TestDbContext>()
3131
.UseSqlite(connection);
32+
configureOptions?.Invoke(optionsBuilder);
3233
TestDbContext context = new(optionsBuilder.Options) { Connection = connection };
3334

3435
// Ensure the database is created.

tests/CommunityToolkit.Datasync.Client.Test/Offline/OperationsQueueManager_Tests.cs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,4 +411,76 @@ public void ToOperationKind_Invalid_Throws()
411411
act.Should().Throw<InvalidOperationException>();
412412
}
413413
#endregion
414+
415+
#region LazyLoadingProxies Support
416+
[Fact]
417+
public async Task LLP_PushAsync_Addition_Works()
418+
{
419+
TestDbContext llpContext = CreateContext(x => x.UseLazyLoadingProxies());
420+
ClientMovie clientMovie = new(TestData.Movies.BlackPanther) { Id = Guid.NewGuid().ToString("N") };
421+
string clientMovieJson = DatasyncSerializer.Serialize(clientMovie);
422+
llpContext.Movies.Add(clientMovie);
423+
llpContext.SaveChanges();
424+
425+
ClientMovie responseMovie = new(TestData.Movies.BlackPanther) { Id = clientMovie.Id, UpdatedAt = DateTimeOffset.UtcNow, Version = Guid.NewGuid().ToString() };
426+
string expectedJson = DatasyncSerializer.Serialize(responseMovie);
427+
llpContext.Handler.AddResponseContent(expectedJson, HttpStatusCode.Created);
428+
429+
PushResult results = await llpContext.QueueManager.PushAsync([typeof(ClientMovie)], new PushOptions());
430+
results.IsSuccessful.Should().BeTrue();
431+
results.CompletedOperations.Should().Be(1);
432+
results.FailedRequests.Should().BeEmpty();
433+
434+
llpContext.DatasyncOperationsQueue.Should().BeEmpty();
435+
436+
ClientMovie actualMovie = llpContext.Movies.SingleOrDefault(x => x.Id == clientMovie.Id);
437+
actualMovie.UpdatedAt!.Should().BeCloseTo((DateTimeOffset)responseMovie.UpdatedAt, TimeSpan.FromMicroseconds(1000));
438+
actualMovie.Version.Should().Be(responseMovie.Version);
439+
}
440+
441+
[Fact]
442+
public async Task LLP_PushAsync_Removal_Works()
443+
{
444+
TestDbContext llpContext = CreateContext(x => x.UseLazyLoadingProxies());
445+
ClientMovie clientMovie = new(TestData.Movies.BlackPanther) { Id = Guid.NewGuid().ToString("N") };
446+
llpContext.Movies.Add(clientMovie);
447+
llpContext.SaveChanges(acceptAllChangesOnSuccess: true, addToQueue: false);
448+
449+
llpContext.Movies.Remove(clientMovie);
450+
llpContext.SaveChanges();
451+
llpContext.Handler.AddResponse(HttpStatusCode.NoContent);
452+
453+
PushResult results = await llpContext.QueueManager.PushAsync([typeof(ClientMovie)], new PushOptions());
454+
results.IsSuccessful.Should().BeTrue();
455+
results.CompletedOperations.Should().Be(1);
456+
results.FailedRequests.Should().BeEmpty();
457+
458+
llpContext.DatasyncOperationsQueue.Should().BeEmpty();
459+
llpContext.Movies.Find(clientMovie.Id).Should().BeNull();
460+
}
461+
462+
[Fact]
463+
public async Task LLP_PushAsync_Replacement_Works()
464+
{
465+
TestDbContext llpContext = CreateContext(x => x.UseLazyLoadingProxies());
466+
ClientMovie clientMovie = new(TestData.Movies.BlackPanther) { Id = Guid.NewGuid().ToString("N") };
467+
llpContext.Movies.Add(clientMovie);
468+
llpContext.SaveChanges(acceptAllChangesOnSuccess: true, addToQueue: false);
469+
470+
clientMovie.Title = "Foo";
471+
llpContext.Update(clientMovie);
472+
llpContext.SaveChanges();
473+
474+
ClientMovie responseMovie = new(TestData.Movies.BlackPanther) { Id = clientMovie.Id, UpdatedAt = DateTimeOffset.UtcNow, Version = Guid.NewGuid().ToString() };
475+
string expectedJson = DatasyncSerializer.Serialize(responseMovie);
476+
llpContext.Handler.AddResponseContent(expectedJson, HttpStatusCode.OK);
477+
478+
PushResult results = await llpContext.QueueManager.PushAsync([typeof(ClientMovie)], new PushOptions());
479+
results.IsSuccessful.Should().BeTrue();
480+
results.CompletedOperations.Should().Be(1);
481+
results.FailedRequests.Should().BeEmpty();
482+
483+
llpContext.DatasyncOperationsQueue.Should().BeEmpty();
484+
}
485+
#endregion
414486
}

0 commit comments

Comments
 (0)