Beispiel #1
0
        public void TestManyToManyCircularReadOnly()
        {
            // In this test we will create a many to many relationship between instances of the same class
            // including inverse relationship

            // This is the hierarchy that we're going to implement
            //                     [1]
            //                     / \
            //                   [2] [3]
            //                  /  \ /  \
            //                 4    5    6
            //
            // To implement it, only children relationshipd of objects [1], [2] and [3] are going to be persisted,
            // the inverse relationships will be discovered automatically

            var conn = Utils.CreateConnection();

            conn.DropTable <M2MClassH>();
            conn.DropTable <ClassHClassH>();
            conn.CreateTable <M2MClassH>();
            conn.CreateTable <ClassHClassH>();

            var object1 = new M2MClassH {
                Name = "Object 1"
            };
            var object2 = new M2MClassH {
                Name = "Object 2"
            };
            var object3 = new M2MClassH {
                Name = "Object 3"
            };
            var object4 = new M2MClassH {
                Name = "Object 4"
            };
            var object5 = new M2MClassH {
                Name = "Object 5"
            };
            var object6 = new M2MClassH {
                Name = "Object 6"
            };

            var objects = new List <M2MClassH> {
                object1, object2, object3, object4, object5, object6
            };

            conn.InsertAll(objects);

            object1.Children = new ObservableCollection <M2MClassH> {
                object2, object3
            };
            conn.UpdateWithChildren(object1);

            object2.Children = new ObservableCollection <M2MClassH> {
                object4, object5
            };
            conn.UpdateWithChildren(object2);

            object3.Children = new ObservableCollection <M2MClassH> {
                object5, object6
            };
            conn.UpdateWithChildren(object3);

            // These relationships are discovered on runtime, assign them to check for correctness below
            object2.Parents = new List <M2MClassH> {
                object1
            };
            object3.Parents = new List <M2MClassH> {
                object1
            };
            object4.Parents = new List <M2MClassH> {
                object2
            };
            object5.Parents = new List <M2MClassH> {
                object2, object3
            };
            object6.Parents = new List <M2MClassH> {
                object3
            };

            foreach (var expected in objects)
            {
                var obtained = conn.GetWithChildren <M2MClassH>(expected.Id);

                Assert.AreEqual(expected.Name, obtained.Name);
                Assert.AreEqual((expected.Children ?? new ObservableCollection <M2MClassH>()).Count, (obtained.Children ?? new ObservableCollection <M2MClassH>()).Count, obtained.Name);
                Assert.AreEqual((expected.Parents ?? new List <M2MClassH>()).Count, (obtained.Parents ?? new List <M2MClassH>()).Count, obtained.Name);

                foreach (var child in expected.Children ?? Enumerable.Empty <M2MClassH>())
                {
                    Assert.IsTrue(obtained.Children.Any(c => c.Id == child.Id && c.Name == child.Name), obtained.Name);
                }

                foreach (var parent in expected.Parents ?? Enumerable.Empty <M2MClassH>())
                {
                    Assert.IsTrue(obtained.Parents.Any(p => p.Id == parent.Id && p.Name == parent.Name), obtained.Name);
                }
            }
        }
        public void TestManyToManyCircularReadOnly() {
            // In this test we will create a many to many relationship between instances of the same class
            // including inverse relationship

            // This is the hierarchy that we're going to implement
            //                     [1]
            //                     / \
            //                   [2] [3]
            //                  /  \ /  \
            //                 4    5    6
            //
            // To implement it, only children relationshipd of objects [1], [2] and [3] are going to be persisted,
            // the inverse relationships will be discovered automatically

            var conn = Utils.CreateConnection();
            conn.DropTable<M2MClassH>();
            conn.DropTable<ClassHClassH>();
            conn.CreateTable<M2MClassH>();
            conn.CreateTable<ClassHClassH>();

            var object1 = new M2MClassH { Name = "Object 1" };
            var object2 = new M2MClassH { Name = "Object 2" };
            var object3 = new M2MClassH { Name = "Object 3" };
            var object4 = new M2MClassH { Name = "Object 4" };
            var object5 = new M2MClassH { Name = "Object 5" };
            var object6 = new M2MClassH { Name = "Object 6" };

            var objects = new List<M2MClassH>{ object1, object2, object3, object4, object5, object6 };
            conn.InsertAll(objects);

            object1.Children = new ObservableCollection<M2MClassH>{ object2, object3 };
            conn.UpdateWithChildren(object1);

            object2.Children = new ObservableCollection<M2MClassH>{ object4, object5 };
            conn.UpdateWithChildren(object2);

            object3.Children = new ObservableCollection<M2MClassH>{ object5, object6 };
            conn.UpdateWithChildren(object3);

            // These relationships are discovered on runtime, assign them to check for correctness below
            object2.Parents = new List<M2MClassH>{ object1 };
            object3.Parents = new List<M2MClassH>{ object1 };
            object4.Parents = new List<M2MClassH>{ object2 };
            object5.Parents = new List<M2MClassH>{ object2, object3 };
            object6.Parents = new List<M2MClassH>{ object3 };

            foreach (var expected in objects)
            {
                var obtained = conn.GetWithChildren<M2MClassH>(expected.Id);

                Assert.AreEqual(expected.Name, obtained.Name);
                Assert.AreEqual((expected.Children ?? new ObservableCollection<M2MClassH>()).Count, (obtained.Children ?? new ObservableCollection<M2MClassH>()).Count, obtained.Name);
                Assert.AreEqual((expected.Parents ?? new List<M2MClassH>()).Count, (obtained.Parents ?? new List<M2MClassH>()).Count, obtained.Name);

                foreach (var child in expected.Children ?? Enumerable.Empty<M2MClassH>())
                    Assert.IsTrue(obtained.Children.Any(c => c.Id == child.Id && c.Name == child.Name), obtained.Name);

                foreach (var parent in expected.Parents ?? Enumerable.Empty<M2MClassH>())
                    Assert.IsTrue(obtained.Parents.Any(p => p.Id == parent.Id && p.Name == parent.Name), obtained.Name);
            }
        }