diff --git a/Tests/Realm.Tests/Database/FlexibleSchemaPocTests.cs b/Tests/Realm.Tests/Database/FlexibleSchemaPocTests.cs index e6d095cf89..5e6b76e307 100644 --- a/Tests/Realm.Tests/Database/FlexibleSchemaPocTests.cs +++ b/Tests/Realm.Tests/Database/FlexibleSchemaPocTests.cs @@ -94,6 +94,53 @@ public void AccessMappedTypeProperties_ReadsValuesFromBackingStorage() Assert.That(bird.CanFly, Is.True); } + [Test] + public void AccessMappedTypeProperties_WhenNonExistent_Throws() + { + var dog = new Dog(); + dog.SetBackingStorage(new Dictionary()); + + Assert.That(() => dog.Name, Throws.TypeOf().And.Message.Contains($"A property with name '{nameof(dog.Name)}' does not exist on '{nameof(Dog)}'.")); + Assert.That(() => dog.BarkCount, Throws.TypeOf().And.Message.Contains($"A property with name '{nameof(dog.BarkCount)}' does not exist on '{nameof(Dog)}'.")); + + var bird = new Bird(); + bird.SetBackingStorage(new Dictionary()); + + Assert.That(() => bird.Name, Throws.TypeOf().And.Message.Contains($"A property with name '{nameof(bird.Name)}' does not exist on '{nameof(Bird)}'.")); + Assert.That(() => bird.CanFly, Throws.TypeOf().And.Message.Contains($"A property with name '{nameof(bird.CanFly)}' does not exist on '{nameof(Bird)}'.")); + } + + [Test] + public void UpdateMappedTypeProperties_WritesValuesToBackingStorage() + { + AddData(); + var dogContainer = _realm.All().First(c => c.ContainedObjectType == nameof(Dog)); + + var dog = new Dog(); + dog.SetBackingStorage(dogContainer.MixedDict); + + _realm.Write(() => + { + dog.Name = "Updated Fido"; + dog.BarkCount++; + }); + Assert.That(dog.Name, Is.EqualTo("Updated Fido")); + Assert.That(dog.BarkCount, Is.EqualTo(6)); + + var birdContainer = _realm.All().First(c => c.ContainedObjectType == nameof(Bird)); + + var bird = new Bird(); + bird.SetBackingStorage(birdContainer.MixedDict); + + _realm.Write(() => + { + bird.Name = "Updated Tweety"; + bird.CanFly = false; + }); + Assert.That(bird.Name, Is.EqualTo("Updated Tweety")); + Assert.That(bird.CanFly, Is.False); + } + [Test] public void NotifyPropertyChanged_NotifiesForModifications() { @@ -196,11 +243,13 @@ public partial class Dog : INotifyPropertyChanged public string Name { get => Get(nameof(Name)); + set => Set(nameof(Name), value); } public int BarkCount { get => Get(nameof(BarkCount)); + set => Set(nameof(BarkCount), value); } private T Get(string propertyName) @@ -210,6 +259,11 @@ private T Get(string propertyName) return value.As(); } + private void Set(string propertyName, RealmValue value) + { + _backingStorage[propertyName] = value; + } + public void SetBackingStorage(IDictionary dictionary) { _backingStorage = dictionary; @@ -297,20 +351,27 @@ public partial class Bird : INotifyPropertyChanged public string Name { get => Get(nameof(Name)); + set => Set(nameof(Name), value); } public bool CanFly { get => Get(nameof(CanFly)); + set => Set(nameof(CanFly), value); } private T Get(string propertyName) { - Argument.Ensure(_backingStorage.TryGetValue(propertyName, out var value), $"A property with name '{propertyName}' does not exist on '{nameof(Dog)}'.", nameof(propertyName)); + Argument.Ensure(_backingStorage.TryGetValue(propertyName, out var value), $"A property with name '{propertyName}' does not exist on '{nameof(Bird)}'.", nameof(propertyName)); return value.As(); } + private void Set(string propertyName, RealmValue value) + { + _backingStorage[propertyName] = value; + } + public void SetBackingStorage(IDictionary dictionary) { _backingStorage = dictionary;