Esempio n. 1
0
        public void TestServerUdpate()
        {
            Helper.DropAllCollections();

            //Insert de Paises
            var c = new Country {
                Code = "ES", Name = "España"
            };

            c.Save();
            c.ServerUpdate(c.Update.Set(MongoMapperHelper.ConvertFieldName("Country", "Name"),
                                        "España 22"));


            Assert.AreEqual(c.Name, "España 22");

            //Insert de personas
            var p = new Person
            {
                Name        = "Pepito Perez",
                Age         = 35,
                BirthDate   = DateTime.Now.AddDays(57).AddYears(-35),
                Married     = true,
                Country     = "ES",
                BankBalance = decimal.Parse("3500,00")
            };

            p.Childs.Add(
                new Child {
                ID = 1, Age = 10, BirthDate = DateTime.Now.AddDays(57).AddYears(-10), Name = "Juan Perez"
            });
            p.Save();

            p.ServerUpdate(
                p.Update.Push(
                    MongoMapperHelper.ConvertFieldName("Person", "Childs"),
                    new Child
            {
                ID = 2, Age = 3, BirthDate = DateTime.Now.AddDays(57).AddYears(-17), Name = "Laura Perez"
            }));

            Assert.AreEqual(p.Childs.Count, 2);
            Assert.AreEqual(p.Childs[1].Name, "Laura Perez");
        }
Esempio n. 2
0
        public void TestServerUdpate()
        {
            Helper.DropAllCollections();

            //Insert de Paises
            var c = new Country {Code = "ES", Name = "España"};
            c.Save();
            c.ServerUpdate(c.Update.Set(MongoMapperHelper.ConvertFieldName("Country", "Name"),
                "España 22"));
                                

            Assert.AreEqual(c.Name, "España 22");

            //Insert de personas
            var p = new Person
                {
                    Name = "Pepito Perez",
                    Age = 35,
                    BirthDate = DateTime.Now.AddDays(57).AddYears(-35),
                    Married = true,
                    Country = "ES",
                    BankBalance = decimal.Parse("3500,00")
                };

            p.Childs.Add(
                new Child {ID = 1, Age = 10, BirthDate = DateTime.Now.AddDays(57).AddYears(-10), Name = "Juan Perez"});
            p.Save();

            p.ServerUpdate(
                p.Update.Push(
                    MongoMapperHelper.ConvertFieldName("Person","Childs"),
                    new Child
                        {ID = 2, Age = 3, BirthDate = DateTime.Now.AddDays(57).AddYears(-17), Name = "Laura Perez"}));

            Assert.AreEqual(p.Childs.Count, 2);
            Assert.AreEqual(p.Childs[1].Name, "Laura Perez");
        }
        public void Test()
        {
            Helper.DropAllCollections();

            ConfigManager.Out = Console.Out;

            var c = new Country {Code = "es", Name = "España"};
            try
            {
                c.Save();
                Assert.Fail();
            }
            catch (ValidatePropertyException ex)
            {
                Assert.AreEqual(ex.GetBaseException().GetType(), typeof (ValidatePropertyException));
                c.Code = "ES";
                c.Save();
            }

            c = new Country {Code = "UK", Name = "Reino Unido"};
            c.Save();

            c = new Country {Code = "UK", Name = "Reino Unido"};
            try
            {
                c.Save();
                Assert.Fail();
            }
            catch (DuplicateKeyException ex)
            {
                Assert.AreEqual(ex.GetBaseException().GetType(), typeof (DuplicateKeyException));
            }

            using (var t = new MongoMapperTransaction())
            {
                var c2 = new Country {Code = "US", Name = "Francia"};
                c2.OnBeforeInsert += (s, e) => { ((Country) s).Name = "Estados Unidos"; };
                c2.Save();

                t.Commit();
            }

            var c3 = new Country();
            c3.FillByKey("US");
            Assert.AreEqual(c3.Name, "Estados Unidos");

            if (!c3.IsLastVersion())
                c3.FillFromLastVersion();

            var countries = new CountryCollection();
            countries.Find();
            Assert.AreEqual(countries.Count, 3);

            countries.Find().Limit(2).Sort(countries.Sort.Ascending(C=>C.Name));
            Assert.AreEqual(countries.Count, 2);
            Assert.AreEqual(countries.Total, 3);

            countries.Find(
                countries.Filter.Or(MongoQuery<Country>.Eq(co => co.Code, "ES"), MongoQuery<Country>.Eq(co => co.Code, "UK")));
            Assert.AreEqual(countries.Count, 2);

            var p = new Person
                {
                    Name = "Pepito Perez",
                    Age = 35,
                    BirthDate = DateTime.Now.AddDays(57).AddYears(-35),
                    Married = true,
                    Country = "XXXXX",
                    BankBalance = decimal.Parse("3500,00")
                };

            p.Childs.Add(
                new Child {ID = 1, Age = 10, BirthDate = DateTime.Now.AddDays(57).AddYears(-10), Name = "Juan Perez"});

            try
            {
                p.Save();
                Assert.Fail();
            }
            catch (ValidateUpRelationException ex)
            {
                Assert.AreEqual(ex.GetBaseException().GetType(), typeof (ValidateUpRelationException));
                p.Country = "ES";
                p.Save();
            }

            p.ServerUpdate(
                p.Update.Push(
                    MongoMapperHelper.ConvertFieldName("Person","Childs"),
                    new Child {ID = 2, Age = 2, BirthDate = DateTime.Now.AddDays(57).AddYears(-7), Name = "Ana Perez"}));

            var persons = new List<Person>();

            persons.MongoFind();
            
            persons.MongoFind("Childs.Age", 2);
            Assert.AreEqual(1, persons.Count);
        }
Esempio n. 4
0
        public void Test()
        {
            Helper.DropAllCollections();

            ConfigManager.Out = Console.Out;

            var c = new Country {
                Code = "es", Name = "España"
            };

            try
            {
                c.Save();
                Assert.Fail();
            }
            catch (ValidatePropertyException ex)
            {
                Assert.AreEqual(ex.GetBaseException().GetType(), typeof(ValidatePropertyException));
                c.Code = "ES";
                c.Save();
            }

            c = new Country {
                Code = "UK", Name = "Reino Unido"
            };
            c.Save();

            c = new Country {
                Code = "UK", Name = "Reino Unido"
            };
            try
            {
                c.Save();
                Assert.Fail();
            }
            catch (DuplicateKeyException ex)
            {
                Assert.AreEqual(ex.GetBaseException().GetType(), typeof(DuplicateKeyException));
            }

            using (var t = new MongoMapperTransaction())
            {
                var c2 = new Country {
                    Code = "US", Name = "Francia"
                };
                c2.OnBeforeInsert += (s, e) => { ((Country)s).Name = "Estados Unidos"; };
                c2.Save();

                t.Commit();
            }

            var c3 = new Country();

            c3.FillByKey("US");
            Assert.AreEqual(c3.Name, "Estados Unidos");

            if (!c3.IsLastVersion())
            {
                c3.FillFromLastVersion();
            }

            var countries = new CountryCollection();

            countries.Find();
            Assert.AreEqual(countries.Count, 3);

            countries.Find().Limit(2).Sort(countries.Sort.Ascending(C => C.Name));
            Assert.AreEqual(countries.Count, 2);
            Assert.AreEqual(countries.Total, 3);

            countries.Find(
                countries.Filter.Or(MongoQuery <Country> .Eq(co => co.Code, "ES"), MongoQuery <Country> .Eq(co => co.Code, "UK")));
            Assert.AreEqual(countries.Count, 2);

            var p = new Person
            {
                Name        = "Pepito Perez",
                Age         = 35,
                BirthDate   = DateTime.Now.AddDays(57).AddYears(-35),
                Married     = true,
                Country     = "XXXXX",
                BankBalance = decimal.Parse("3500,00")
            };

            p.Childs.Add(
                new Child {
                ID = 1, Age = 10, BirthDate = DateTime.Now.AddDays(57).AddYears(-10), Name = "Juan Perez"
            });

            try
            {
                p.Save();
                Assert.Fail();
            }
            catch (ValidateUpRelationException ex)
            {
                Assert.AreEqual(ex.GetBaseException().GetType(), typeof(ValidateUpRelationException));
                p.Country = "ES";
                p.Save();
            }

            p.ServerUpdate(
                p.Update.Push(
                    MongoMapperHelper.ConvertFieldName("Person", "Childs"),
                    new Child {
                ID = 2, Age = 2, BirthDate = DateTime.Now.AddDays(57).AddYears(-7), Name = "Ana Perez"
            }));

            var persons = new List <Person>();

            persons.MongoFind();

            persons.MongoFind("Childs.Age", 2);
            Assert.AreEqual(1, persons.Count);
        }