public void AddOrUpdate_WhenListHasPK_ShouldUpdateListItemsSingleWrite() { PrimaryKeyWithPKList first = null; _realm.Write(() => { for (var i = 0; i < 10; i++) { first = new PrimaryKeyWithPKList { Id = 1, StringValue = "first" }; first.ListValue.Add(new PrimaryKeyObject { Id = 1, StringValue = "child" }); first.ListValue.Add(new PrimaryKeyObject { Id = 2, StringValue = "secondChild" }); _realm.Add(first, update: true); } }); Assert.That(first.ListValue.Count, Is.EqualTo(2)); // did the Add keep adding dups? Assert.That(_realm.Find <PrimaryKeyWithPKList>(1).ListValue.Count, Is.EqualTo(2)); Assert.That(_realm.Find <PrimaryKeyObject>(1).StringValue, Is.EqualTo("child")); Assert.That(_realm.Find <PrimaryKeyObject>(2).StringValue, Is.EqualTo("secondChild")); }
public void AddOrUpdate_WhenNewListIsNull_ShouldNotThrow() { var first = new PrimaryKeyWithPKList { Id = 1 }; first.ListValue.Add(new PrimaryKeyObject { Id = 1 }); _realm.Write(() => _realm.Add(first)); var second = new PrimaryKeyWithPKList { Id = 1 }; // second.listValue is null, because the getter is never invoked. _realm.Write(() => _realm.Add(second, update: true)); Assert.That(first.ListValue, Is.EquivalentTo(second.ListValue)); // Verify that the original list was cleared Assert.That(first.ListValue.Count, Is.EqualTo(0)); // Verify that clearing the list hasn't deleted the item from the Realm. Assert.That(_realm.All <PrimaryKeyObject>().Count(), Is.EqualTo(1)); }
public void AddOrUpdate_WhenListIsNull_ShouldNotThrow() { var first = new PrimaryKeyWithPKList { Id = 42 }; Assert.That(() => { _realm.Write(() => _realm.Add(first, update: true)); }, Throws.Nothing); }
public void AddOrUpdate_WhenListHasPK_ShouldAddNewAndUpdateOldItems() { var first = new PrimaryKeyWithPKList { Id = 1, StringValue = "first" }; first.ListValue.Add(new PrimaryKeyObject { Id = 1, StringValue = "child" }); _realm.Write(() => { _realm.Add(first, update: true); }); var updatedFirst = new PrimaryKeyWithPKList { Id = 1, StringValue = "updated first" }; updatedFirst.ListValue.Add(new PrimaryKeyObject { Id = 1, StringValue = "updated child" }); updatedFirst.ListValue.Add(new PrimaryKeyObject { Id = 2, StringValue = "new child" }); _realm.Write(() => { _realm.Add(updatedFirst, update: true); }); Assert.That(_realm.All <PrimaryKeyObject>().Count(), Is.EqualTo(2)); Assert.That(_realm.Find <PrimaryKeyObject>(1).StringValue, Is.EqualTo("updated child")); Assert.That(_realm.Find <PrimaryKeyObject>(2).StringValue, Is.EqualTo("new child")); }
public void AddOrUpdate_WhenListObjectsHavePK_ShouldOverwriteList() { // Original object - has 2 elements in the list var first = new PrimaryKeyWithPKList { Id = 1 }; first.ListValue.Add(new PrimaryKeyObject { Id = 1 }); first.ListValue.Add(new PrimaryKeyObject { Id = 2 }); _realm.Write(() => _realm.Add(first)); // Object to update with - has 1 element in the list var second = new PrimaryKeyWithPKList { Id = 1 }; second.ListValue.Add(new PrimaryKeyObject { Id = 3 }); _realm.Write(() => _realm.Add(second, update: true)); Assert.That(first.ListValue, Is.EquivalentTo(second.ListValue)); // Verify that the original list was replaced with the new one. Assert.That(first.ListValue.Count, Is.EqualTo(1)); // Verify that the list's sole element has the correct Id. Assert.That(first.ListValue[0].Id, Is.EqualTo(3)); // Verify that overwriting the list hasn't deleted any elements from the Realm. Assert.That(_realm.All <PrimaryKeyObject>().Count(), Is.EqualTo(3)); }
public void AddOrUpdate_WhenObjectHasNonEmptyList_ShouldNotThrow() { /* This test verifies that updating an object that has PK and non-empty list will not throw an exception. * The reason it *could* throw is a limitation on core in table.cpp: Table::check_lists_are_empty. * The comment states: * FIXME: Due to a limitation in Sync, it is not legal to change the primary * key of a row that contains lists (including linklists) after those lists * have been populated. This limitation may be lifted in the future, but for * now it is necessary to ensure that all lists are empty before setting a * primary key (by way of set_int_unique() or set_string_unique() or set_null_unique()). * * So if we set the Primary Key unnecessarily in the .NET binding, we could trigger that case. */ var first = new PrimaryKeyWithPKList { Id = 42, StringValue = "value1" }; first.ListValue.Add(new PrimaryKeyObject { Id = 1 }); _realm.Write(() => _realm.Add(first)); var second = new PrimaryKeyWithPKList { Id = 42, StringValue = "value2" }; second.ListValue.Add(new PrimaryKeyObject { Id = 1 }); Assert.That(() => { _realm.Write(() => _realm.Add(second, update: true)); }, Throws.Nothing); }
public void AddOrUpdate_WhenListHasPK_ShouldUpdateListItems() { var first = new PrimaryKeyWithPKList { Id = 1, StringValue = "first" }; first.ListValue.Add(new PrimaryKeyObject { Id = 1, StringValue = "child" }); _realm.Write(() => { _realm.Add(first, update: true); }); var second = new PrimaryKeyWithPKList { Id = 2, StringValue = "second" }; second.ListValue.Add(new PrimaryKeyObject { Id = 1, StringValue = "secondChild" }); _realm.Write(() => { _realm.Add(second, update: true); }); Assert.That(first.ListValue, Is.EqualTo(second.ListValue)); Assert.That(_realm.All <PrimaryKeyObject>().Count(), Is.EqualTo(1)); Assert.That(_realm.Find <PrimaryKeyObject>(1).StringValue, Is.EqualTo("secondChild")); }
public void AddOrUpdate_WhenNewListIsNull_ShouldNotThrow() { var first = new PrimaryKeyWithPKList { Id = 1 }; first.ListValue.Add(new PrimaryKeyObject { Id = 1 }); _realm.Write(() => _realm.Add(first)); var second = new PrimaryKeyWithPKList { Id = 1 }; // second.listValue is null, because the getter is never invoked. _realm.Write(() => _realm.Add(second, update: true)); Assert.That(first.ListValue, Is.EquivalentTo(second.ListValue)); // Verify that the original list was cleared Assert.That(first.ListValue.Count, Is.EqualTo(0)); // Verify that clearing the list hasn't deleted the item from the Realm. Assert.That(_realm.All<PrimaryKeyObject>().Count(), Is.EqualTo(1)); }
public void AddOrUpdate_WhenListObjectsHavePK_ShouldOverwriteList() { // Original object - has 2 elements in the list var first = new PrimaryKeyWithPKList { Id = 1 }; first.ListValue.Add(new PrimaryKeyObject { Id = 1 }); first.ListValue.Add(new PrimaryKeyObject { Id = 2 }); _realm.Write(() => _realm.Add(first)); // Object to update with - has 1 element in the list var second = new PrimaryKeyWithPKList { Id = 1 }; second.ListValue.Add(new PrimaryKeyObject { Id = 3 }); _realm.Write(() => _realm.Add(second, update: true)); Assert.That(first.ListValue, Is.EquivalentTo(second.ListValue)); // Verify that the original list was replaced with the new one. Assert.That(first.ListValue.Count, Is.EqualTo(1)); // Verify that the list's sole element has the correct Id. Assert.That(first.ListValue[0].Id, Is.EqualTo(3)); // Verify that overwriting the list hasn't deleted any elements from the Realm. Assert.That(_realm.All<PrimaryKeyObject>().Count(), Is.EqualTo(3)); }
public void AddOrUpdate_WhenObjectHasNonEmptyList_ShouldNotThrow() { // This test verifies that updating an object that has PK and non-empty list will not throw an exception. // The reason it *could* throw is a limitation on core in table.cpp: Table::check_lists_are_empty. // The comment states: // FIXME: Due to a limitation in Sync, it is not legal to change the primary // key of a row that contains lists (including linklists) after those lists // have been populated. This limitation may be lifted in the future, but for // now it is necessary to ensure that all lists are empty before setting a // primary key (by way of set_int_unique() or set_string_unique() or set_null_unique()). // // So if we set the Primary Key unnecessarily in the .NET binding, we could trigger that case. var first = new PrimaryKeyWithPKList { Id = 42, StringValue = "value1" }; first.ListValue.Add(new PrimaryKeyObject { Id = 1 }); _realm.Write(() => _realm.Add(first)); var second = new PrimaryKeyWithPKList { Id = 42, StringValue = "value2" }; second.ListValue.Add(new PrimaryKeyObject { Id = 1 }); Assert.That(() => { _realm.Write(() => _realm.Add(second, update: true)); }, Throws.Nothing); }
public void AddOrUpdate_WhenListHasPK_ShouldAddNewAndUpdateOldItems() { var first = new PrimaryKeyWithPKList { Id = 1, StringValue = "first" }; first.ListValue.Add(new PrimaryKeyObject { Id = 1, StringValue = "child" }); _realm.Write(() => { _realm.Add(first, update: true); }); var updatedFirst = new PrimaryKeyWithPKList { Id = 1, StringValue = "updated first" }; updatedFirst.ListValue.Add(new PrimaryKeyObject { Id = 1, StringValue = "updated child" }); updatedFirst.ListValue.Add(new PrimaryKeyObject { Id = 2, StringValue = "new child" }); _realm.Write(() => { _realm.Add(updatedFirst, update: true); }); Assert.That(_realm.All<PrimaryKeyObject>().Count(), Is.EqualTo(2)); Assert.That(_realm.Find<PrimaryKeyObject>(1).StringValue, Is.EqualTo("updated child")); Assert.That(_realm.Find<PrimaryKeyObject>(2).StringValue, Is.EqualTo("new child")); }
public void AddOrUpdate_WhenListHasPK_ShouldUpdateListItemsSingleWrite() { PrimaryKeyWithPKList first = null; _realm.Write(() => { for (var i = 0; i < 10; i++) { first = new PrimaryKeyWithPKList { Id = 1, StringValue = "first" }; first.ListValue.Add(new PrimaryKeyObject { Id = 1, StringValue = "child" }); first.ListValue.Add(new PrimaryKeyObject { Id = 2, StringValue = "secondChild" }); _realm.Add(first, update: true); } }); Assert.That(first.ListValue.Count(), Is.EqualTo(2)); // did the Add keep adding dups? Assert.That(_realm.Find<PrimaryKeyWithPKList>(1).ListValue.Count(), Is.EqualTo(2)); Assert.That(_realm.Find<PrimaryKeyObject>(1).StringValue, Is.EqualTo("child")); Assert.That(_realm.Find<PrimaryKeyObject>(2).StringValue, Is.EqualTo("secondChild")); }
public void AddOrUpdate_WhenListHasPK_ShouldUpdateListItems() { var first = new PrimaryKeyWithPKList { Id = 1, StringValue = "first" }; first.ListValue.Add(new PrimaryKeyObject { Id = 1, StringValue = "child" }); _realm.Write(() => { _realm.Add(first, update: true); }); var second = new PrimaryKeyWithPKList { Id = 2, StringValue = "second" }; second.ListValue.Add(new PrimaryKeyObject { Id = 1, StringValue = "secondChild" }); _realm.Write(() => { _realm.Add(second, update: true); }); Assert.That(first.ListValue, Is.EqualTo(second.ListValue)); Assert.That(_realm.All<PrimaryKeyObject>().Count(), Is.EqualTo(1)); Assert.That(_realm.Find<PrimaryKeyObject>(1).StringValue, Is.EqualTo("secondChild")); }