Skip to content

Commit

Permalink
Add to-be-generated 'set' accessor.
Browse files Browse the repository at this point in the history
  • Loading branch information
elle-j committed Jul 9, 2024
1 parent be4fe42 commit fe1d4fc
Showing 1 changed file with 62 additions and 1 deletion.
63 changes: 62 additions & 1 deletion Tests/Realm.Tests/Database/FlexibleSchemaPocTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, RealmValue>());

Assert.That(() => dog.Name, Throws.TypeOf<ArgumentException>().And.Message.Contains($"A property with name '{nameof(dog.Name)}' does not exist on '{nameof(Dog)}'."));
Assert.That(() => dog.BarkCount, Throws.TypeOf<ArgumentException>().And.Message.Contains($"A property with name '{nameof(dog.BarkCount)}' does not exist on '{nameof(Dog)}'."));

var bird = new Bird();
bird.SetBackingStorage(new Dictionary<string, RealmValue>());

Assert.That(() => bird.Name, Throws.TypeOf<ArgumentException>().And.Message.Contains($"A property with name '{nameof(bird.Name)}' does not exist on '{nameof(Bird)}'."));
Assert.That(() => bird.CanFly, Throws.TypeOf<ArgumentException>().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<FlexibleSchemaPocContainer>().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<FlexibleSchemaPocContainer>().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()
{
Expand Down Expand Up @@ -196,11 +243,13 @@ public partial class Dog : INotifyPropertyChanged
public string Name
{
get => Get<string>(nameof(Name));
set => Set(nameof(Name), value);
}

public int BarkCount
{
get => Get<int>(nameof(BarkCount));
set => Set(nameof(BarkCount), value);
}

private T Get<T>(string propertyName)
Expand All @@ -210,6 +259,11 @@ private T Get<T>(string propertyName)
return value.As<T>();
}

private void Set(string propertyName, RealmValue value)
{
_backingStorage[propertyName] = value;
}

public void SetBackingStorage(IDictionary<string, RealmValue> dictionary)
{
_backingStorage = dictionary;
Expand Down Expand Up @@ -297,20 +351,27 @@ public partial class Bird : INotifyPropertyChanged
public string Name
{
get => Get<string>(nameof(Name));
set => Set(nameof(Name), value);
}

public bool CanFly
{
get => Get<bool>(nameof(CanFly));
set => Set(nameof(CanFly), value);
}

private T Get<T>(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<T>();
}

private void Set(string propertyName, RealmValue value)
{
_backingStorage[propertyName] = value;
}

public void SetBackingStorage(IDictionary<string, RealmValue> dictionary)
{
_backingStorage = dictionary;
Expand Down

0 comments on commit fe1d4fc

Please sign in to comment.