示例#1
0
        public void TestUpdateSetOneToManyListWithInverseGuidId()
        {
            var conn = Utils.CreateConnection();

            conn.DropTable <O2MClassG>();
            conn.DropTable <O2MClassH>();
            conn.CreateTable <O2MClassG>();
            conn.CreateTable <O2MClassH>();

            // Use standard SQLite-Net API to create the objects
            var objectsH = new List <O2MClassH>
            {
                new O2MClassH {
                    Guid = Guid.NewGuid(),
                    Foo  = string.Format("1- Foo String {0}", new Random().Next(100))
                },
                new O2MClassH {
                    Guid = Guid.NewGuid(),
                    Foo  = string.Format("2- Foo String {0}", new Random().Next(100))
                },
                new O2MClassH {
                    Guid = Guid.NewGuid(),
                    Foo  = string.Format("3- Foo String {0}", new Random().Next(100))
                },
                new O2MClassH {
                    Guid = Guid.NewGuid(),
                    Foo  = string.Format("4- Foo String {0}", new Random().Next(100))
                }
            };

            conn.InsertAll(objectsH);

            var objectG = new O2MClassG {
                Guid = Guid.NewGuid()
            };

            conn.Insert(objectG);

            Assert.Null(objectG.HObjects);

            objectG.HObjects = new ObservableCollection <O2MClassH>(objectsH);

            foreach (var objectD in objectsH)
            {
                Assert.AreEqual(Guid.Empty, objectD.ClassGKey, "Foreign keys shouldn't have been updated yet");
            }


            conn.UpdateWithChildren(objectG);

            foreach (var objectH in objectG.HObjects)
            {
                Assert.AreEqual(objectG.Guid, objectH.ClassGKey, "Foreign keys haven't been updated yet");
                Assert.AreSame(objectG, objectH.ObjectG, "Inverse relationship hasn't been set");

                // Check database values
                var newObjectH = conn.Get <O2MClassH>(objectH.Guid);
                Assert.AreEqual(objectG.Guid, newObjectH.ClassGKey, "Database stored value is not correct");
            }
        }
        public void TestUpdateSetOneToManyListWithInverseGuidId()
        {
            var conn = Utils.CreateConnection();
            conn.DropTable<O2MClassG>();
            conn.DropTable<O2MClassH>();
            conn.CreateTable<O2MClassG>();
            conn.CreateTable<O2MClassH>();

            // Use standard SQLite-Net API to create the objects
            var objectsH = new List<O2MClassH>
            {
                new O2MClassH {
                    Guid = Guid.NewGuid(),
                    Foo = string.Format("1- Foo String {0}", new Random().Next(100))
                },
                new O2MClassH {
                    Guid = Guid.NewGuid(),
                    Foo = string.Format("2- Foo String {0}", new Random().Next(100))
                },
                new O2MClassH {
                    Guid = Guid.NewGuid(),
                    Foo = string.Format("3- Foo String {0}", new Random().Next(100))
                },
                new O2MClassH {
                    Guid = Guid.NewGuid(),
                    Foo = string.Format("4- Foo String {0}", new Random().Next(100))
                }
            };
            conn.InsertAll(objectsH);

            var objectG = new O2MClassG { Guid = Guid.NewGuid() };
            conn.Insert(objectG);

            Assert.Null(objectG.HObjects);

            objectG.HObjects = new ObservableCollection<O2MClassH>(objectsH);

            foreach (var objectD in objectsH)
            {
                Assert.AreEqual(Guid.Empty, objectD.ClassGKey, "Foreign keys shouldn't have been updated yet");
            }


            conn.UpdateWithChildren(objectG);

            foreach (var objectH in objectG.HObjects)
            {
                Assert.AreEqual(objectG.Guid, objectH.ClassGKey, "Foreign keys haven't been updated yet");
                Assert.AreSame(objectG, objectH.ObjectG, "Inverse relationship hasn't been set");

                // Check database values
                var newObjectH = conn.Get<O2MClassH>(objectH.Guid);
                Assert.AreEqual(objectG.Guid, newObjectH.ClassGKey, "Database stored value is not correct");
            }

        }
示例#3
0
        public void TestGetOneToManyListWithInverseGuidId()
        {
            var conn = Utils.CreateConnection();

            conn.DropTable <O2MClassG>();
            conn.DropTable <O2MClassH>();
            conn.CreateTable <O2MClassG>();
            conn.CreateTable <O2MClassH>();

            // Use standard SQLite-Net API to create the objects
            var objectsD = new List <O2MClassH>
            {
                new O2MClassH {
                    Guid = Guid.NewGuid(),
                    Foo  = string.Format("1- Foo String {0}", new Random().Next(100))
                },
                new O2MClassH {
                    Guid = Guid.NewGuid(),
                    Foo  = string.Format("2- Foo String {0}", new Random().Next(100))
                },
                new O2MClassH {
                    Guid = Guid.NewGuid(),
                    Foo  = string.Format("3- Foo String {0}", new Random().Next(100))
                },
                new O2MClassH {
                    Guid = Guid.NewGuid(),
                    Foo  = string.Format("4- Foo String {0}", new Random().Next(100))
                }
            };

            conn.InsertAll(objectsD);

            var objectC = new O2MClassG {
                Guid = Guid.NewGuid()
            };

            conn.Insert(objectC);

            Assert.Null(objectC.HObjects);

            // Fetch (yet empty) the relationship
            conn.GetChildren(objectC);
            Assert.NotNull(objectC.HObjects);
            Assert.AreEqual(0, objectC.HObjects.Count);

            // Set the relationship using IDs
            foreach (var objectD in objectsD)
            {
                objectD.ClassGKey = objectC.Guid;
                conn.Update(objectD);
            }

            Assert.NotNull(objectC.HObjects);
            Assert.AreEqual(0, objectC.HObjects.Count);

            // Fetch the relationship
            conn.GetChildren(objectC);

            Assert.NotNull(objectC.HObjects);
            Assert.AreEqual(objectsD.Count, objectC.HObjects.Count);
            var foos = objectsD.Select(objectB => objectB.Foo).ToList();

            foreach (var objectD in objectC.HObjects)
            {
                Assert.IsTrue(foos.Contains(objectD.Foo));
                Assert.AreEqual(objectC.Guid, objectD.ObjectG.Guid);
                Assert.AreEqual(objectC.Bar, objectD.ObjectG.Bar);
                Assert.AreSame(objectC, objectD.ObjectG); // Not only equal, they are the same!
            }
        }
        public void TestGetOneToManyListWithInverseGuidId()
        {
            var conn = Utils.CreateConnection();
            conn.DropTable<O2MClassG>();
            conn.DropTable<O2MClassH>();
            conn.CreateTable<O2MClassG>();
            conn.CreateTable<O2MClassH>();

            // Use standard SQLite-Net API to create the objects
            var objectsD = new List<O2MClassH>
            {
                new O2MClassH {
                    Guid = Guid.NewGuid(),
                    Foo = string.Format("1- Foo String {0}", new Random().Next(100))
                },
                new O2MClassH {
                    Guid = Guid.NewGuid(),
                    Foo = string.Format("2- Foo String {0}", new Random().Next(100))
                },
                new O2MClassH {
                    Guid = Guid.NewGuid(),
                    Foo = string.Format("3- Foo String {0}", new Random().Next(100))
                },
                new O2MClassH {
                    Guid = Guid.NewGuid(),
                    Foo = string.Format("4- Foo String {0}", new Random().Next(100))
                }
            };
            conn.InsertAll(objectsD);

            var objectC = new O2MClassG { Guid = Guid.NewGuid() };
            conn.Insert(objectC);

            Assert.Null(objectC.HObjects);

            // Fetch (yet empty) the relationship
            conn.GetChildren(objectC);
            Assert.NotNull(objectC.HObjects);
            Assert.AreEqual(0, objectC.HObjects.Count);

            // Set the relationship using IDs
            foreach (var objectD in objectsD)
            {
                objectD.ClassGKey = objectC.Guid;
                conn.Update(objectD);
            }

            Assert.NotNull(objectC.HObjects);
            Assert.AreEqual(0, objectC.HObjects.Count);

            // Fetch the relationship
            conn.GetChildren(objectC);

            Assert.NotNull(objectC.HObjects);
            Assert.AreEqual(objectsD.Count, objectC.HObjects.Count);
            var foos = objectsD.Select(objectB => objectB.Foo).ToList();
            foreach (var objectD in objectC.HObjects)
            {
                Assert.IsTrue(foos.Contains(objectD.Foo));
                Assert.AreEqual(objectC.Guid, objectD.ObjectG.Guid);
                Assert.AreEqual(objectC.Bar, objectD.ObjectG.Bar);
                Assert.AreSame(objectC, objectD.ObjectG); // Not only equal, they are the same!
            }
        }