Ejemplo n.º 1
0
        public void TestManyToManyCircular()
        {
            // 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 relationshipd of objects [2] and [3] are going to be persisted,
            // the inverse relationships will be discovered automatically

            var conn = Utils.CreateConnection();

            conn.DropTable <M2MClassG>();
            conn.DropTable <ClassGClassG>();
            conn.CreateTable <M2MClassG>();
            conn.CreateTable <ClassGClassG>();

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

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

            conn.InsertAll(objects);

            object2.Parents = new ObservableCollection <M2MClassG> {
                object1
            };
            object2.Children = new List <M2MClassG> {
                object4, object5
            };
            conn.UpdateWithChildren(object2);

            object3.Parents = new ObservableCollection <M2MClassG> {
                object1
            };
            object3.Children = new List <M2MClassG> {
                object5, object6
            };
            conn.UpdateWithChildren(object3);

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

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

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

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

                foreach (var parent in expected.Parents ?? Enumerable.Empty <M2MClassG>())
                {
                    Assert.IsTrue(obtained.Parents.Any(p => p.Id == parent.Id && p.Name == parent.Name), obtained.Name);
                }
            }
        }
        public void TestManyToManyCircular() {
            // 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 relationshipd of objects [2] and [3] are going to be persisted,
            // the inverse relationships will be discovered automatically

            var conn = Utils.CreateConnection();
            conn.DropTable<M2MClassG>();
            conn.DropTable<ClassGClassG>();
            conn.CreateTable<M2MClassG>();
            conn.CreateTable<ClassGClassG>();

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

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

            object2.Parents = new ObservableCollection<M2MClassG>{ object1 };
            object2.Children = new List<M2MClassG>{ object4, object5 };
            conn.UpdateWithChildren(object2);

            object3.Parents = new ObservableCollection<M2MClassG>{ object1 };
            object3.Children = new List<M2MClassG>{ object5, object6 };
            conn.UpdateWithChildren(object3);

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

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

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

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

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