Ejemplo n.º 1
0
        public void TestPerfFillByKeyNormalVsExtensionMethod()
        {
            Helper.DropAllCollections();

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

            c.Save();

            Stopwatch timer = Stopwatch.StartNew();

            for (int i = 0; i < 1000000; i++)
            {
                var country = new Country();
                country.FillByKey("ES");
            }
            timer.Stop();
            Console.WriteLine(string.Format("Elapsed para ExtensionMethod: {0}", timer.Elapsed));
            //Elapsed para ExtensionMethod: 00:04:38.5479462

            timer = Stopwatch.StartNew();

            for (int i = 0; i < 1000000; i++)
            {
                MongoMapper <Country> .FindByKey("ES");
            }

            timer.Stop();
            Console.WriteLine(string.Format("Elapsed para StaticMethod: {0}", timer.Elapsed));
            //Elapsed para StaticMethod: 00:04:27.1441065
        }
Ejemplo n.º 2
0
        public void TestEvents()
        {
            Helper.DropAllCollections();

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

            c.OnBeforeInsert += (s, e) => { ((Country)s).Name = "Francia"; };
            c.Save();

            var c3 = MongoMapper <Country> .FindByKey("FR");

            Assert.AreEqual(c3.Name, "Francia");
        }
        public void TestUdpate()
        {
            Helper.DropAllCollections();

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

            c.Save();

            var c2 = MongoMapper <Country> .FindByKey("ES");

            c2.Name = "España Up";
            c2.Save();

            var c3 = MongoMapper <Country> .FindByKey("ES");

            Assert.AreEqual(c3.Name, "España Up");

            var Countries = CountryCollection.Instance;

            Countries.Find(x => x.Code, "ES");
            Assert.AreEqual(Countries.Count, 1);
        }
        public void TestRelations()
        {
            Helper.DropAllCollections();

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

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

            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"
            });
            p.Childs.Add(
                new Child {
                ID = 2, Age = 7, BirthDate = DateTime.Now.AddDays(57).AddYears(-7), Name = "Ana Perez"
            });

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

            c = MongoMapper <Country> .FindByKey("ES");

            try
            {
                c.Delete();
                Assert.Fail();
            }
            catch (ValidateDownRelationException ex)
            {
                Assert.AreEqual(ex.GetBaseException().GetType(), typeof(ValidateDownRelationException));
                List <Person> persons = new List <Person>();
                persons.MongoFind(C => C.Country, "ES");
                foreach (Person p2 in persons)
                {
                    p2.Country = "UK";
                    p2.Save();
                }
                c.Delete();
            }

            List <Person> personsInUk = new List <Person>();

            personsInUk.MongoFind(C => C.Country, "UK");
            foreach (Person personInUk in personsInUk)
            {
                Assert.AreEqual(personInUk.Country, "UK");
            }
        }