Inheritance: RealmObject
        public void UnmanagedObject()
        {
            AsyncContext.Run(async delegate
            {
                string notifiedPropertyName = null;
                var person = new Person();

                var handler = new PropertyChangedEventHandler((sender, e) =>
                {
                    notifiedPropertyName = e.PropertyName;
                });
                person.PropertyChanged += handler;

                // Subscribed - should trigger
                person.FirstName = "Peter";
                await Task.Yield();
                Assert.That(notifiedPropertyName, Is.EqualTo(nameof(Person.FirstName)));

                notifiedPropertyName = null;
                person.PropertyChanged -= handler;

                // Unsubscribed - should not trigger
                person.FirstName = "George";
                await Task.Yield();
                Assert.That(notifiedPropertyName, Is.Null);
            });
        }
Example #2
0
        public void RemoveObject_WhenStandalone_ShouldThrow()
        {
            var person = new Person();

            Assert.That(() => _realm.Write(() => _realm.Remove(person)), Throws.TypeOf<ArgumentException>());
        }
Example #3
0
        public void TestCircularRelationshipsFromStandaloneTwoStage()
        {
            var sally = new Person
            {
                FullName = "Sally",
                Friends =
                {
                    new Person { FullName = "Alice" },
                    new Person { FullName = "Joan" }
                }
            };
            var joanFriend = new Person
            {
                FullName = "Krystal",
                Friends = { sally }
            };

            using (var trans = realm.BeginWrite())
            {
                realm.Add(sally); // top person Manages entire tree
                sally.Friends[1].Friends.Add(joanFriend);

                trans.Commit();
            }

            Assert.That(realm.All<Person>().ToList().Select(p => p.FirstName),
                        Is.EquivalentTo(new[] { "Alice", "Joan", "Krystal", "Sally" }));
        }
Example #4
0
        public void TestManagingStandaloneThreeLevelRelationship()
        {
            var sally = new Person
            {
                FullName = "Sally",
                Friends = // see NoteOnListInit above
                {
                    new Person { FullName = "Alice" },
                    new Person
                    {
                        FullName = "Joan",
                        Friends = // see NoteOnListInit above
                        {
                            new Person
                            {
                                FullName = "Krystal",
                                Friends = { new Person { FullName = "Sally" } } // Manages a second Sally
                            }
                        }
                    }
                }
            };

            using (var trans = realm.BeginWrite())
            {
                realm.Add(sally); // top person Manages entire tree
                trans.Commit();
            }

            Assert.That(realm.All<Person>().ToList().Select(p => p.FirstName),
                        Is.EquivalentTo(new[] { "Alice", "Joan", "Krystal", "Sally", "Sally" }));
        }
Example #5
0
        public void TestManagingStandaloneTwoLevelRelationship()
        {
            var person = new Person
            {
                FullName = "Jack Thorne",
                Friends = // see NoteOnListInit above
                {
                    new Person { FullName = "Christian Molehound" },
                    new Person { FullName = "Frederick Van Whatnot" }
                }
            };

            Assert.That(person.Friends is List<Person>);

            using (var trans = realm.BeginWrite())
            {
                realm.Add(person);
                trans.Commit();
            }

#if ENABLE_INTERNAL_NON_PCL_TESTS
            Assert.That(person.Friends is RealmList<Person>);
#else
            Assert.That(person.Friends.GetType().ToString() == "Realms.RealmList`1[IntegrationTests.Person]");
#endif
            Assert.That(realm.All<Person>().ToList().Select(p => p.FirstName),
                        Is.EquivalentTo(new[] { "Jack", "Christian", "Frederick" }));
        }
        public void ManagedObject_MultipleProperties()
        {
            var notifiedPropertyNames = new List<string>();
            var person = new Person();
            _realm.Write(() =>
            {
                _realm.Add(person);
            });

            person.PropertyChanged += (sender, e) =>
            {
                notifiedPropertyNames.Add(e.PropertyName);
            };

            _realm.Write(() =>
            {
                person.FirstName = "Peter";
            });

            Assert.That(notifiedPropertyNames, Is.EquivalentTo(new[] { nameof(Person.FirstName) }));

            _realm.Write(() =>
            {
                person.LastName = "Smith";
            });

            Assert.That(notifiedPropertyNames, Is.EquivalentTo(new[] { nameof(Person.FirstName), nameof(Person.LastName) }));

            _realm.Write(() =>
            {
                person.Score = 3.5f;
            });

            Assert.That(notifiedPropertyNames, Is.EquivalentTo(new[] { nameof(Person.FirstName), nameof(Person.LastName), nameof(Person.Score) }));
        }
 public void SetUp()
 {
     _person = new Person();
     Realm.DeleteRealm(RealmConfiguration.DefaultConfiguration);
 }
Example #8
0
        public void DateTimeOffsetShouldStoreFullPrecision()
        {
            // Arrange
            const long Ticks = 636059331339132912;
            var p = new Person { Birthday = new DateTimeOffset(Ticks, TimeSpan.Zero) };

            // Act
            _realm.Write(() => { _realm.Add(p); });

            // Assert
            Assert.That(p.Birthday.Ticks, Is.EqualTo(Ticks));
        }
        private async Task TestManaged(Func<Person, string, Task> writeFirstNameAction)
        {
            var notifiedPropertyNames = new List<string>();
            var person = new Person();
            _realm.Write(() =>
            {
                _realm.Add(person);
            });
            var handler = new PropertyChangedEventHandler((sender, e) =>
            {
                notifiedPropertyNames.Add(e.PropertyName);
            });
            person.PropertyChanged += handler;

            // Subscribed - regular set should trigger
            await writeFirstNameAction(person, "Peter");
            await Task.Yield();
            Assert.That(notifiedPropertyNames, Is.EquivalentTo(new[] { nameof(Person.FirstName) }));

            // Subscribed - setting the same value for the property should trigger again
            // This is different from .NET's usual behavior, but is a limitation due to the fact that we don't
            // check the previous value of the property before setting it.
            await writeFirstNameAction(person, "Peter");
            await Task.Yield();
            Assert.That(notifiedPropertyNames, Is.EquivalentTo(new[] { nameof(Person.FirstName), nameof(Person.FirstName) }));

            notifiedPropertyNames.Clear();
            person.PropertyChanged -= handler;

            // Unsubscribed - should not trigger
            await writeFirstNameAction(person, "George");
            await Task.Yield();
            Assert.That(notifiedPropertyNames, Is.Empty);
        }
        public void ManagedObject_WhenHandleIsReleased_ShouldNotReceiveNotifications()
        {
            AsyncContext.Run(async delegate
            {
                var notifiedPropertyNames = new List<string>();
                WeakReference personReference = null;
                new Action(() =>
                {
                    var person = new Person();
                    _realm.Write(() => _realm.Add(person));

                    person.PropertyChanged += (sender, e) =>
                    {
                        notifiedPropertyNames.Add(e.PropertyName);
                    };

                    personReference = new WeakReference(person);

                    _realm.Write(() => person.FirstName = "Peter");

                    // Sanity check
                    Assert.That(notifiedPropertyNames, Is.EquivalentTo(new[] { nameof(Person.FirstName) }));
                })();

                notifiedPropertyNames.Clear();

                while (personReference.IsAlive)
                {
                    await Task.Yield();
                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                }

                Assert.That(personReference.IsAlive, Is.False);

                _realm.Write(() =>
                {
                    var peter = _realm.All<Person>().Single();
                    Assert.That(peter.FirstName, Is.EqualTo("Peter"));
                    peter.FirstName = "George";
                });

                // person was garbage collected, so we should not be notified and no exception should be thrown.
                Assert.That(notifiedPropertyNames, Is.Empty);
            });
        }
        public void UnmanagedObject_WhenMappedTo_ShouldUsePropertyName()
        {
            var notifiedPropertyNames = new List<string>();
            var person = new Person();

            person.PropertyChanged += (sender, e) =>
            {
                Assert.That(sender, Is.EqualTo(person));
                notifiedPropertyNames.Add(e.PropertyName);
            };

            person.Email = "*****@*****.**";

            Assert.That(notifiedPropertyNames, Is.EquivalentTo(new[] { "Email_" }));
        }
        public void ManagedObject_MultipleSubscribers()
        {
            var subscriber1Properties = new List<string>();
            var subscriber2Properties = new List<string>();
            var person = new Person();
            _realm.Write(() =>
            {
                _realm.Add(person);
            });

            var handler1 = new PropertyChangedEventHandler((sender, e) =>
            {
                Assert.That(sender, Is.EqualTo(person));
                subscriber1Properties.Add(e.PropertyName);
            });
            person.PropertyChanged += handler1;

            person.PropertyChanged += (sender, e) =>
            {
                Assert.That(sender, Is.EqualTo(person));
                subscriber2Properties.Add(e.PropertyName);
            };

            _realm.Write(() =>
            {
                person.Birthday = new DateTimeOffset(1985, 1, 5, 8, 2, 3, TimeSpan.FromHours(3));
            });

            Assert.That(subscriber1Properties, Is.EquivalentTo(new[] { nameof(Person.Birthday) }));
            Assert.That(subscriber2Properties, Is.EquivalentTo(new[] { nameof(Person.Birthday) }));

            person.PropertyChanged -= handler1;

            _realm.Write(() =>
            {
                person.IsInteresting = true;
            });

            Assert.That(subscriber1Properties, Is.EquivalentTo(new[] { nameof(Person.Birthday) }));
            Assert.That(subscriber2Properties, Is.EquivalentTo(new[] { nameof(Person.Birthday), nameof(Person.IsInteresting) }));
        }
        public void ManagedObject_AfterSubscribe_CanRemove()
        {
            var notifiedPropertyNames = new List<string>();
            var person = new Person();
            _realm.Write(() =>
            {
                _realm.Add(person);
            });

            person.PropertyChanged += (sender, e) =>
            {
                Assert.That(sender, Is.EqualTo(person));
                notifiedPropertyNames.Add(e.PropertyName);
            };

            _realm.Write(() =>
            {
                person.FirstName = "Peter";
            });

            Assert.That(notifiedPropertyNames, Is.EquivalentTo(new[] { nameof(Person.FirstName) }));

            _realm.Write(() =>
            {
                _realm.Remove(person);
            });

            Assert.That(_realm.All<Person>().Count(), Is.EqualTo(0));
        }
        public void MultipleManagedObjects()
        {
            var firstNotifiedPropertyNames = new List<string>();
            var secondNotifiedPropertyNames = new List<string>();
            var first = new Person();
            var second = new Person();
            _realm.Write(() =>
            {
                _realm.Add(first);
                _realm.Add(second);
            });

            first.PropertyChanged += (sender, e) =>
            {
                firstNotifiedPropertyNames.Add(e.PropertyName);
            };

            second.PropertyChanged += (sender, e) =>
            {
                secondNotifiedPropertyNames.Add(e.PropertyName);
            };

            _realm.Write(() =>
            {
                first.IsAmbivalent = true;
            });

            Assert.That(firstNotifiedPropertyNames, Is.EquivalentTo(new[] { nameof(Person.IsAmbivalent) }));
            Assert.That(secondNotifiedPropertyNames, Is.Empty);

            _realm.Write(() =>
            {
                second.Latitude = 4.6;
                second.Longitude = 5.6;
            });

            Assert.That(firstNotifiedPropertyNames, Is.EquivalentTo(new[] { nameof(Person.IsAmbivalent) }));
            Assert.That(secondNotifiedPropertyNames, Is.EquivalentTo(new[] { nameof(Person.Latitude), nameof(Person.Longitude) }));
        }
 public void AddOutsideTransactionShouldFail()
 {
     var obj = new Person();
     Assert.That(() => _realm.Add(obj), Throws.TypeOf<RealmInvalidTransactionException>());
 }
        public void IsValidReturnsTrueWhenObjectIsAttached()
        {
            _realm.Write(() =>
            {
                var p1 = _realm.Add(new Person());
                Assert.That(p1.IsValid);

                _realm.Remove(p1);
                Assert.That(p1.IsValid, Is.False);
            });

            // IsValid should always return true for standalone objects
            var p2 = new Person();
            Assert.That(p2.IsValid);
        }
        private async Task TestManagedRollback(Action<Person, string> writeFirstNameAction, Func<Transaction> transactionFactory)
        {
            var notifiedPropertyNames = new List<string>();
            var person = new Person();
            _realm.Write(() =>
            {
                _realm.Add(person);
            });

            person.PropertyChanged += (sender, e) =>
            {
                notifiedPropertyNames.Add(e.PropertyName);
            };

            using (var transaction = transactionFactory())
            {
                writeFirstNameAction(person, "Peter");

                await Task.Yield();
                Assert.That(notifiedPropertyNames, Is.EquivalentTo(new[] { nameof(Person.FirstName) }));

                transaction.Rollback();
            }

            await Task.Yield();
            Assert.That(notifiedPropertyNames, Is.EquivalentTo(new[] { nameof(Person.FirstName), nameof(Person.FirstName) }));
        }
Example #18
0
        public void Add_ShouldReturnPassedInObject()
        {
            var first = new Person
            {
                FirstName = "Peter"
            };

            Person added = null;
            _realm.Write(() =>
            {
                added = _realm.Add(first, update: false);
            });

            Assert.That(added, Is.SameAs(first));
        }
        public void UnmanagedObject_AfterAdd_ShouldContinueTriggering()
        {
            AsyncContext.Run(async delegate
            {
                var notifications = 0;
                var person = new Person();

                person.PropertyChanged += (sender, e) =>
                {
                    if (e.PropertyName == nameof(Person.FirstName))
                    {
                        notifications++;
                    }
                };

                person.FirstName = "Peter";
                await Task.Yield();
                Assert.That(notifications, Is.EqualTo(1));

                _realm.Write(() =>
                {
                    _realm.Add(person);
                });

                // When calling Realm.Add, all properties are persisted, which causes a notification to be sent out.
                // We're using SomeClass because Person has nullable properties, which are set always, so it interferes with the test.
                await Task.Yield();
                Assert.That(notifications, Is.EqualTo(2));

                _realm.Write(() =>
                {
                    person.FirstName = "George";
                });

                await Task.Yield();
                Assert.That(notifications, Is.EqualTo(3));
            });
        }
        public void RealmObject_WhenStandalone_ShouldHaveDefaultEqualsImplementation()
        {
            var otherPerson = new Person();

            Assert.DoesNotThrow(() => _person.Equals(otherPerson));
        }
        public void ManagedObject_WhenAnotherThreadInstanceTransactionRollback()
        {
            AsyncContext.Run(async delegate
            {
                var notifiedPropertyNames = new List<string>();
                var person = new Person();
                _realm.Write(() =>
                {
                    _realm.Add(person);
                });

                person.PropertyChanged += (sender, e) =>
                {
                    notifiedPropertyNames.Add(e.PropertyName);
                };

                await Task.Run(() =>
                {
                    using (var otherRealm = Realm.GetInstance(_databasePath))
                    using (var transaction = otherRealm.BeginWrite())
                    {
                        var otherInstance = otherRealm.All<Person>().First();
                        otherInstance.FirstName = "Peter";

                        Assert.That(notifiedPropertyNames, Is.Empty);

                        transaction.Rollback();
                    }
                });

                await Task.Yield();
                Assert.That(notifiedPropertyNames, Is.Empty);
            });
        }