Skip to content

Commit

Permalink
Add Results notification test for updates on embedded object.
Browse files Browse the repository at this point in the history
  • Loading branch information
elle-j committed Jul 10, 2024
1 parent b800ad7 commit 14ca017
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
47 changes: 47 additions & 0 deletions Tests/Realm.Tests/Database/NotificationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,53 @@ public void ResultsShouldSendNotifications()
}
}

[Test]
public void Results_WhenEmbeddedObjectIsModified_Notifies()
{
var query = _realm.All<TestNotificationObject>();
var actualChanges = new List<ChangeSet?>();
void OnNotification(IRealmCollection<TestNotificationObject> collection, ChangeSet? changes) => actualChanges.Add(changes);

Assert.That(query.Count, Is.EqualTo(0));

using (query.SubscribeForNotifications(OnNotification))
{
_realm.Refresh();

// Notification from subscribing.
Assert.That(actualChanges.Count, Is.EqualTo(1));

var testObject = _realm.Write(() => _realm.Add(new TestNotificationObject()));

_realm.Refresh();
Assert.That(actualChanges.Count, Is.EqualTo(2));
Assert.That(actualChanges[1], Is.Not.Null);
Assert.That(actualChanges[1]!.InsertedIndices, Is.EquivalentTo(new[] { 0 }));

_realm.Write(() =>
{
testObject.EmbeddedObject = new EmbeddedIntPropertyObject { Int = 1 };
});

_realm.Refresh();
Assert.That(actualChanges.Count, Is.EqualTo(3));
Assert.That(actualChanges[2], Is.Not.Null);
Assert.That(actualChanges[2]!.NewModifiedIndices, Is.EquivalentTo(new[] { 0 }));
Assert.That(actualChanges[2]!.ModifiedIndices, Is.EquivalentTo(new[] { 0 }));

_realm.Write(() =>
{
testObject.EmbeddedObject!.Int++;
});

_realm.Refresh();
Assert.That(actualChanges.Count, Is.EqualTo(4));
Assert.That(actualChanges[3], Is.Not.Null);
Assert.That(actualChanges[3]!.NewModifiedIndices, Is.EquivalentTo(new[] { 0 }));
Assert.That(actualChanges[3]!.ModifiedIndices, Is.EquivalentTo(new[] { 0 }));
}
}

[Test]
public void ListShouldSendNotifications()
{
Expand Down
2 changes: 2 additions & 0 deletions Tests/Realm.Tests/Database/TestNotificationObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,7 @@ public partial class TestNotificationObject : TestRealmObject

[Backlink(nameof(LinkSameType))]
public IQueryable<TestNotificationObject> Backlink { get; } = null!;

public EmbeddedIntPropertyObject? EmbeddedObject { get; set; }
}
}

0 comments on commit 14ca017

Please sign in to comment.