예제 #1
0
        public void Many_Serialize_Deserialize()
        {
            var realm = GetRealm();

            var ref1 = new RealmRef
            {
                Text = "123",
                Id   = "1",
            };
            var ref2 = new RealmRef
            {
                Text = "456",
                Id   = "2",
            };
            var refParent = new RealmManyRef
            {
                Id   = "3",
                Text = "zxc",
            };

            refParent.Children.Add(ref1);
            refParent.Children.Add(ref2);

            realm.Write(
                () =>
            {
                realm.Add(ref1);
                realm.Add(ref2);
                realm.Add(refParent);
            });

            var s1 = _realmiusSyncService.SerializeObject(ref1);

            s1.Should().BeEquivalentTo($"{{\"Id\":\"1\",\"Text\":\"123\",\"Parent\":null,\"MobilePrimaryKey\":\"1\"}}");

            var s2 = _realmiusSyncService.SerializeObject(refParent);

            s2.Should().BeEquivalentTo("{\"Id\":\"3\",\"Text\":\"zxc\",\"Children\":[\"1\",\"2\"],\"MobilePrimaryKey\":\"3\"}");

            realm.Write(
                () =>
            {
                realm.Remove(refParent);
            });

            var ref11 = new RealmManyRef();

            realm.Write(
                () =>
            {
                _realmiusSyncService.Populate(s2, ref11, realm);
            });

            ref11.Text.Should().Be("zxc");
            ref11.Id.Should().Be("3");
            ref11.Children.Should().NotBeNull();
            string.Join(", ", ref11.Children.Select(x => x.Id)).Should().BeEquivalentTo("1, 2");
        }
예제 #2
0
        public void Serialize_Deserialize()
        {
            var realm = GetRealm();

            var ref1 = new RealmRef
            {
                Text = "123",
                Id   = "1",
            };

            var ref2 = new RealmRef
            {
                Id     = "2",
                Text   = "zxc",
                Parent = ref1,
            };

            realm.Write(
                () =>
            {
                realm.Add(ref1);
                realm.Add(ref2);
            });

            var s1 = _realmiusSyncService.SerializeObject(ref1);

            s1.Should().BeEquivalentTo($"{{\"Id\":\"1\",\"Text\":\"123\",\"Parent\":null,\"MobilePrimaryKey\":\"1\"}}");

            var s2 = _realmiusSyncService.SerializeObject(ref2);

            s2.Should().BeEquivalentTo($"{{\"Id\":\"2\",\"Text\":\"zxc\",\"Parent\":\"1\",\"MobilePrimaryKey\":\"2\"}}");

            realm.Write(
                () =>
            {
                realm.Remove(ref1);
                realm.Remove(ref2);
            });

            var ref11 = new RealmRef();

            realm.Write(
                () =>
            {
                realm.Add(ref11);
                _realmiusSyncService.Populate(s1, ref11, realm);
            });

            ref11.Text.Should().Be("123");
            ref11.Id.Should().Be("1");
            ref11.Parent.Should().BeNull();

            var ref21 = new RealmRef();

            realm.Write(
                () =>
            {
                realm.Add(ref21);
                _realmiusSyncService.Populate(s2, ref21, realm);
            });

            ref21.Text.Should().Be("zxc");
            ref21.Id.Should().Be("2");
            ref21.Parent.Should().NotBeNull();
            ref21.Parent.Id.Should().Be("1");
        }