Skip to content

Commit cd10d8b

Browse files
committed
add test
1 parent 704529d commit cd10d8b

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

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

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,22 @@ public abstract class BaseTest
2525
/// </summary>
2626
protected static TestDbContext CreateContext(Action<DbContextOptionsBuilder<TestDbContext>> configureOptions = null)
2727
{
28-
SqliteConnection connection = new("Data Source=:memory:");
29-
connection.Open();
28+
SqliteConnection connection = CreateAndOpenConnection();
29+
DbContextOptionsBuilder<TestDbContext> optionsBuilder = new DbContextOptionsBuilder<TestDbContext>()
30+
.UseSqlite(connection);
31+
configureOptions?.Invoke(optionsBuilder);
32+
TestDbContext context = new(optionsBuilder.Options) { Connection = connection };
33+
34+
// Ensure the database is created.
35+
context.Database.EnsureCreated();
36+
return context;
37+
}
38+
39+
/// <summary>
40+
/// Creates a version of the TestDbContext backed by the specified SQLite connection.
41+
/// </summary>
42+
protected static TestDbContext CreateContext(SqliteConnection connection, Action<DbContextOptionsBuilder<TestDbContext>> configureOptions = null)
43+
{
3044
DbContextOptionsBuilder<TestDbContext> optionsBuilder = new DbContextOptionsBuilder<TestDbContext>()
3145
.UseSqlite(connection);
3246
configureOptions?.Invoke(optionsBuilder);
@@ -37,6 +51,16 @@ protected static TestDbContext CreateContext(Action<DbContextOptionsBuilder<Test
3751
return context;
3852
}
3953

54+
/// <summary>
55+
/// Creates and opens an in-memory SQLite database connection.
56+
/// </summary>
57+
protected static SqliteConnection CreateAndOpenConnection()
58+
{
59+
SqliteConnection connection = new("Data Source=:memory:");
60+
connection.Open();
61+
return connection;
62+
}
63+
4064
/// <summary>
4165
/// Creates a response message based on code and content.
4266
/// </summary>

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using CommunityToolkit.Datasync.TestCommon.Databases;
1010
using Microsoft.EntityFrameworkCore;
1111
using System.Net;
12+
using Microsoft.Data.Sqlite;
1213
using TestData = CommunityToolkit.Datasync.TestCommon.TestData;
1314

1415
namespace CommunityToolkit.Datasync.Client.Test.Offline;
@@ -39,7 +40,7 @@ public void GetEntityMap_Works()
3940
public async Task GetExistingOperationAsync_InvalidId_Throws()
4041
{
4142
ClientMovie movie = new() { Id = "###" };
42-
Func<Task> act = async () => _ = await queueManager.GetExistingOperationAsync(movie);
43+
Func<Task> act = async () => _ = await queueManager.GetExistingOperationAsync(context.Entry(movie));
4344
await act.Should().ThrowAsync<DatasyncException>();
4445
}
4546
#endregion
@@ -482,5 +483,29 @@ public async Task LLP_PushAsync_Replacement_Works()
482483

483484
llpContext.DatasyncOperationsQueue.Should().BeEmpty();
484485
}
486+
487+
[Fact]
488+
public async Task LLP_ModifyAfterInsertInNewContext_NoPush_ShouldUpdateOperationsQueue()
489+
{
490+
// Arrange
491+
await using SqliteConnection connection = CreateAndOpenConnection();
492+
string id = Guid.NewGuid().ToString("N");
493+
await using (TestDbContext llpContext = CreateContext(connection, x => x.UseLazyLoadingProxies()))
494+
{
495+
ClientMovie clientMovie = new(TestData.Movies.BlackPanther) { Id = id };
496+
llpContext.Movies.Add(clientMovie);
497+
llpContext.SaveChanges();
498+
}
499+
500+
// Act
501+
await using TestDbContext newLlpContext = CreateContext(connection, x => x.UseLazyLoadingProxies());
502+
503+
ClientMovie storedClientMovie = newLlpContext.Movies.First(m => m.Id == id);
504+
storedClientMovie.Title = TestData.Movies.MovieList[0].Title;
505+
newLlpContext.SaveChanges();
506+
507+
// Assert
508+
newLlpContext.DatasyncOperationsQueue.Should().ContainSingle(op => op.ItemId == id);
509+
}
485510
#endregion
486511
}

0 commit comments

Comments
 (0)