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

            for (int i = 0; i < 50; i++)
            {
                Country c = new Country();
                c.Code = "C" + i.ToString();
                c.Name = "Name" + i.ToString();
                c.Save();
            }

            CountryCollection col = new CountryCollection();

            col.Find().Sort(col.Sort.Descending("Code"));

            Console.WriteLine(col.Count);

            foreach (var c in col)
            {
                Console.WriteLine(c.Code);
            }

            col.AddIncludeFields("Code");
            col.AddExcludeFields("Name");
            col.Find(C => C.Name, "Name%");

            foreach (var c in col)
            {
                Console.WriteLine(c.Code + " | " + c.Name);
            }
        }
        public void Cursor()
        {

            Helper.DropAllCollections();

            for (int i = 0; i < 50; i++)
            {
                Country c = new Country();
                c.Code = "C" + i.ToString();
                c.Name = "Name" + i.ToString();
                c.Save();
            }

            CountryCollection col = new CountryCollection();
            col.Find().Sort(col.Sort.Descending("Code"));
       
            Console.WriteLine(col.Count);

            foreach (var c in col)
            {
                Console.WriteLine(c.Code);
            }

            col.AddIncludeFields("Code");
            col.AddExcludeFields("Name");
            col.Find(C=>C.Name, "Name%");

            foreach (var c in col)
            {
                Console.WriteLine(c.Code + " | " + c.Name);
            }

        }
        public void TestDelete()
        {
            Helper.DropAllCollections();

            var c = new Country {
                Code = "NL", Name = "Holanda"
            };

            c.Save();

            var countries = new CountryCollection();

            countries.Find(X => X.Code, "NL");
            Assert.AreEqual(1, countries.Count);

            foreach (Country country in countries)
            {
                country.Delete();
            }

            //TODO: Pruebas Replica Set
            //System.Threading.Thread.Sleep(5000);

            countries.Find(X => X.Code, "NL");
            Assert.AreEqual(0, countries.Count);
        }
Ejemplo n.º 4
0
        public void Test()
        {
            Helper.DropAllCollections();

            var country = new Country {
                Code = "NL", Name = "Holanda"
            };

            country.Save();
            country = new Country {
                Code = "UK", Name = "Reino Unido"
            };
            country.Save();
            country = new Country {
                Code = "ES", Name = "España"
            };
            country.Save();

            var col = new CountryCollection {
                FromPrimary = false
            };

            col.Find();

            foreach (Country c in col)
            {
                Console.WriteLine(c.Name);
            }

            col.Find().Limit(1);

            //Console.WriteLine(col.Cursor.Explain().ToJson());

            Assert.AreEqual(1, col.Count);
            Assert.AreEqual(3, col.Total);

            col = new CountryCollection {
                FromPrimary = true
            };
            col.Find().Limit(3).Sort(col.Sort.Ascending(C => C.Name));

            Assert.AreEqual(3, col.Count);
            Assert.AreEqual("ES", col.First().Code);

            col.Find(MongoQuery <Country> .Eq(C => C.Code, "NL"));

            Assert.AreEqual("NL", col.First().Code);
            //TODO: No esta implementado el last Assert.AreEqual("NL", col.Last().Code);
        }
Ejemplo n.º 5
0
        public void TestPop()
        {
            Helper.DropAllCollections();

            var c = new Country {
                Code = "PT", Name = "Portugal"
            };

            c.Save();
            c = new Country {
                Code = "ES", Name = "España"
            };
            c.Save();
            c = new Country {
                Code = "UK", Name = "Reino Unido"
            };
            c.Save();
            c = new Country {
                Code = "US", Name = "Estados Unidos"
            };
            c.Save();

            var countries = new CountryCollection();

            countries.Find();

            Assert.AreEqual(4, countries.Count);

            c = countries.Pop();

            Assert.AreEqual(c.Code, "PT");

            Assert.AreEqual(3, countries.Count);

            c = countries.Pop();

            Assert.AreEqual(c.Code, "ES");

            Assert.AreEqual(2, countries.Count);

            c = countries.Pop();

            Assert.AreEqual(c.Code, "UK");

            Assert.AreEqual(1, countries.Count);

            c = countries.Pop();

            Assert.AreEqual(c.Code, "US");

            Assert.AreEqual(0, countries.Count);

            c = countries.Pop();

            Assert.IsNull(c);
        }
Ejemplo n.º 6
0
        public void TestPopCustomSort()
        {
            Helper.DropAllCollections();

            var c = new Country {
                Code = "A", Name = "A"
            };

            c.Save();
            c = new Country {
                Code = "B", Name = "B"
            };
            c.Save();
            c = new Country {
                Code = "C", Name = "C"
            };
            c.Save();
            c = new Country {
                Code = "D", Name = "D"
            };
            c.Save();

            var countries = new CountryCollection();

            countries.Find();

            Assert.AreEqual(4, countries.Count);

            c = countries.Pop(new BsonDocument(), countries.Sort.Ascending(C => C.Code));

            Assert.AreEqual(c.Code, "A");

            Assert.AreEqual(3, countries.Count);

            c = countries.Pop(new BsonDocument(), countries.Sort.Ascending(C => C.Code));

            Assert.AreEqual(c.Code, "B");

            Assert.AreEqual(2, countries.Count);

            c = countries.Pop(new BsonDocument(), countries.Sort.Ascending(C => C.Code));

            Assert.AreEqual(c.Code, "C");

            Assert.AreEqual(1, countries.Count);

            c = countries.Pop(new BsonDocument(), countries.Sort.Ascending(C => C.Code));

            Assert.AreEqual(c.Code, "D");

            Assert.AreEqual(0, countries.Count);

            c = countries.Pop(new BsonDocument(), countries.Sort.Ascending(C => C.Code));

            Assert.IsNull(c);
        }
        public void Test()
        {
            Helper.DropAllCollections();

            var country = new Country { Code = "NL", Name = "Holanda" };
            country.Save();
            country = new Country { Code = "UK", Name = "Reino Unido" };
            country.Save();
            country = new Country { Code = "ES", Name = "España" };
            country.Save();

            var col = new CountryCollection {FromPrimary = false};
            col.Find();

            foreach (Country c in col)
            {
                Console.WriteLine(c.Name);
            }

            col.Find().Limit(1);

            //Console.WriteLine(col.Cursor.Explain().ToJson());

            Assert.AreEqual(1, col.Count);
            Assert.AreEqual(3, col.Total);
       
            col = new CountryCollection {FromPrimary = true};
            col.Find().Limit(3).Sort(col.Sort.Ascending(C=>C.Name));
        
            Assert.AreEqual(3, col.Count);
            Assert.AreEqual("ES", col.First().Code);

            col.Find(MongoQuery<Country>.Eq(C => C.Code, "NL"));

            Assert.AreEqual("NL", col.First().Code);
            //TODO: No esta implementado el last Assert.AreEqual("NL", col.Last().Code);

       

        }
        public void TestDelete()
        {
            Helper.DropAllCollections();

            var c = new Country {Code = "NL", Name = "Holanda"};
            c.Save();

            var countries = new CountryCollection();
            countries.Find(X=>X.Code, "NL");
            Assert.AreEqual(1,countries.Count);

            foreach (Country country in countries)
            {
                country.Delete();
            }

            //TODO: Pruebas Replica Set
            //System.Threading.Thread.Sleep(5000);

            countries.Find(X=>X.Code, "NL");
            Assert.AreEqual(0, countries.Count);
        }
Ejemplo n.º 9
0
        public void TestPopCustomSort()
        {

            Helper.DropAllCollections();

            var c = new Country { Code = "A", Name = "A" };
            c.Save();
            c = new Country { Code = "B", Name = "B" };
            c.Save();
            c = new Country { Code = "C", Name = "C" };
            c.Save();
            c = new Country { Code = "D", Name = "D" };
            c.Save();

            var countries = new CountryCollection();

            countries.Find();

            Assert.AreEqual(4, countries.Count);

            c = countries.Pop(Query.Null, SortBy<Country>.Ascending(C=>C.Code));

            Assert.AreEqual(c.Code, "A");

            Assert.AreEqual(3, countries.Count);

            c = countries.Pop(Query.Null, SortBy<Country>.Ascending(C=>C.Code));

            Assert.AreEqual(c.Code, "B");

            Assert.AreEqual(2, countries.Count);

            c = countries.Pop(Query.Null, SortBy<Country>.Ascending(C=>C.Code));

            Assert.AreEqual(c.Code, "C");

            Assert.AreEqual(1, countries.Count);

            c = countries.Pop(Query.Null, SortBy<Country>.Ascending(C=>C.Code));

            Assert.AreEqual(c.Code, "D");

            Assert.AreEqual(0, countries.Count);

            c = countries.Pop(Query.Null, SortBy<Country>.Ascending(C=>C.Code));

            Assert.IsNull(c);


        }
Ejemplo n.º 10
0
        public void TestPopCustomSortCustomQuery()
        {
            Helper.DropAllCollections();

            var c = new Country {
                Code = "A", Name = "1"
            };

            c.Save();
            c = new Country {
                Code = "B", Name = "1"
            };
            c.Save();
            c = new Country {
                Code = "C", Name = "C"
            };
            c.Save();
            c = new Country {
                Code = "D", Name = "1"
            };
            c.Save();

            var countries = new CountryCollection();

            countries.Find();

            Assert.AreEqual(4, countries.Count);

            c = countries.Pop(MongoQuery <Country> .Eq(C => C.Name, "1"), countries.Sort.Descending(C => C.Code));

            Assert.AreEqual(c.Code, "D");

            Assert.AreEqual(3, countries.Count);

            c = countries.Pop(MongoQuery <Country> .Eq(C => C.Name, "1"), countries.Sort.Descending(C => C.Code));

            Assert.AreEqual(c.Code, "B");

            Assert.AreEqual(2, countries.Count);

            c = countries.Pop(MongoQuery <Country> .Eq(C => C.Name, "1"), countries.Sort.Descending(C => C.Code));

            Assert.AreEqual(c.Code, "A");

            Assert.AreEqual(1, countries.Count);

            c = countries.Pop(MongoQuery <Country> .Eq(C => C.Name, "1"), countries.Sort.Descending(C => C.Code));

            Assert.IsNull(c);
        }
Ejemplo n.º 11
0
        public void TestPop()
        {

            Helper.DropAllCollections();

            var c = new Country { Code = "PT", Name = "Portugal" };
            c.Save();
            c = new Country { Code = "ES", Name = "España"};
            c.Save();
            c = new Country { Code = "UK", Name = "Reino Unido"};
            c.Save();                     
            c = new Country { Code = "US", Name = "Estados Unidos"};
            c.Save();

            var countries = new CountryCollection();

            countries.Find();

            Assert.AreEqual(4 , countries.Count);

            c = countries.Pop();

            Assert.AreEqual(c.Code, "PT");

            Assert.AreEqual(3, countries.Count);

            c = countries.Pop();

            Assert.AreEqual(c.Code, "ES");

            Assert.AreEqual(2, countries.Count);

            c = countries.Pop();

            Assert.AreEqual(c.Code, "UK");

            Assert.AreEqual(1, countries.Count);

            c = countries.Pop();

            Assert.AreEqual(c.Code, "US");

            Assert.AreEqual(0, countries.Count);

            c = countries.Pop();

            Assert.IsNull(c);


        }
        public void Test()
        {
            Helper.DropAllCollections();

            var country = new Country { Code = "NL", Name = "Holanda" };
            country.Save();
            country = new Country { Code = "UK", Name = "Reino Unido" };
            country.Save();
            country = new Country { Code = "ES", Name = "España" };
            country.Save();

            var col = new CountryCollection {FromPrimary = false};
            col.Find().SetLimit(1);

            Console.WriteLine(col.Cursor.Explain().ToJson());

            Assert.AreEqual(1, col.Count);
            Assert.AreEqual(3, col.Total);

            col = new CountryCollection {FromPrimary = true};
            col.Find().SetLimit(3).SetSortOrder(SortBy<Country>.Ascending(C=>C.Name));              

            Assert.AreEqual(3, col.Count);
            Assert.AreEqual("ES", col.First().Code);

            col.Find(Query<Country>.EQ(C => C.Code, "NL"));

            Assert.AreEqual("NL", col.First().Code);
            Assert.AreEqual("NL", col.Last().Code);

            foreach (Country c in col)
            {

            }

        }
Ejemplo n.º 13
0
        public void TestPerfMongoFindNormalVsExtensionMethods()
        {
            Helper.DropAllCollections();

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

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

            Stopwatch timer = Stopwatch.StartNew();

            for (int i = 0; i < 1000000; i++)
            {
                var countries = new List <Country>();
                countries.MongoFind(
                    Builders <Country> .Filter.Or(MongoQuery <Country> .Eq(co => co.Code, "ES"), MongoQuery <Country> .Eq(co => co.Code, "UK")));
            }
            timer.Stop();
            Console.WriteLine(string.Format("Elapsed para ExtensionMethod: {0}", timer.Elapsed));
            //Elapsed para ExtensionMethod: 00:04:29.8042031

            timer = Stopwatch.StartNew();

            for (int i = 0; i < 1000000; i++)
            {
                CountryCollection mongoCol = new CountryCollection();
                mongoCol.Find(
                    mongoCol.Filter.Or(MongoQuery <Country> .Eq(co => co.Code, "ES"), MongoQuery <Country> .Eq(co => co.Code, "UK")));
                mongoCol.ToList();
            }

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

            //Insert de Paises
            var c = new Country {Code = "ES", Name = "España"};
            c.Save();
            c = new Country {Code = "UK", Name = "Reino Unido"};
            c.Save();
            c = new Country {Code = "US", Name = "Estados Unidos"};
            c.Save();

            Stopwatch timer = Stopwatch.StartNew();

            for (int i = 0; i < 1000000; i++)
            {
                var countries = new List<Country>();
                countries.MongoFind(
                    Builders<Country>.Filter.Or(MongoQuery<Country>.Eq(co => co.Code, "ES"), MongoQuery<Country>.Eq(co => co.Code, "UK")));
            }
            timer.Stop();
            Console.WriteLine(string.Format("Elapsed para ExtensionMethod: {0}", timer.Elapsed));
            //Elapsed para ExtensionMethod: 00:04:29.8042031

            timer = Stopwatch.StartNew();

            for (int i = 0; i < 1000000; i++)
            {
                CountryCollection mongoCol = new CountryCollection();
                mongoCol.Find(
                    mongoCol.Filter.Or(MongoQuery<Country>.Eq(co=>co.Code, "ES"), MongoQuery<Country>.Eq(co => co.Code, "UK")));
                mongoCol.ToList();
            }

            timer.Stop();
            Console.WriteLine(string.Format("Elapsed para StaticMethod: {0}", timer.Elapsed));
            //Elapsed para StaticMethod: 00:04:10.1821050
        }
Ejemplo n.º 15
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);
        }
Ejemplo n.º 16
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);
        }
        public void TestInsert()
        {
            Helper.DropAllCollections();


            //Paris
            var geoArea = new GeoArea();
            geoArea.type = "Polygon";            
            var coordinates = new List<double[]>();
            coordinates.Add(new double[] { 48.979766324449706, 2.098388671875 });
            coordinates.Add(new double[] { 48.972555195463336, 2.5982666015625 });
            coordinates.Add(new double[] { 48.683254235765325, 2.603759765625 });
            coordinates.Add(new double[] { 48.66874533279169, 2.120361328125 });
            coordinates.Add(new double[] { 48.979766324449706, 2.098388671875 });
            geoArea.coordinates = new [] {coordinates.ToArray()};

            //Insert de Paises
            var c = new Country {Code = "es", Name = "España", Area = geoArea};
            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", Area = geoArea};
            c.Save();

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

            c = new Country { Code = "US", Name = "Estados Unidos", Area = geoArea };
            c.Save();

            var countries = new CountryCollection();

            countries.Find(x=>x.Code, "ES");
            Assert.AreEqual(countries.Count, 1);

            countries.Find(x=>x.Code, "UK");
            Assert.AreEqual(countries.Count, 1);

            countries.Find(x=>x.Code, "US");
            Assert.AreEqual(countries.Count, 1);

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

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

            p.Save();

            p = new Person
                {
                    Name = "Juanito Sanchez",
                    Age = 25,
                    BirthDate = DateTime.Now.AddDays(52).AddYears(-38),
                    Married = true,
                    Country = "ES",
                    BankBalance = decimal.Parse("1500,00")
                };

            p.Childs.Add(
                new Child {ID = 1, Age = 5, BirthDate = DateTime.Now.AddDays(7).AddYears(-5), Name = "Toni Sanchez"});

            p.Save();

            p = new Person
                {
                    Name = "Andres Perez",
                    Age = 25,
                    BirthDate = DateTime.Now.AddDays(25).AddYears(-25),
                    Married = false,
                    Country = "ES",
                    BankBalance = decimal.Parse("500,00")
                };

            p.Save();

            p = new Person
                {
                    Name = "Marta Serrano",
                    Age = 28,
                    BirthDate = DateTime.Now.AddDays(28).AddYears(-28),
                    Married = false,
                    Country = "ES",
                    BankBalance = decimal.Parse("9500,00")
                };

            p.Childs.Add(
                new Child {ID = 1, Age = 2, BirthDate = DateTime.Now.AddDays(2).AddYears(-2), Name = "Toni Serrano"});
            p.Save();

            p = new Person
                {
                    Name = "Jonh Smith",
                    Age = 21,
                    BirthDate = DateTime.Now.AddDays(21).AddYears(-21),
                    Married = false,
                    Country = "US",
                    BankBalance = decimal.Parse("10000,00")
                };

            p.Save();

            var persons = new List<Person>();
            persons.MongoFind();

            Assert.AreEqual(persons.Count, 5);
        }
Ejemplo n.º 18
0
        public void TestPopCustomSortCustomQuery()
        {

            Helper.DropAllCollections();

            var c = new Country { Code = "A", Name = "1" };
            c.Save();
            c = new Country { Code = "B", Name = "1" };
            c.Save();
            c = new Country { Code = "C", Name = "C" };
            c.Save();
            c = new Country { Code = "D", Name = "1" };
            c.Save();

            var countries = new CountryCollection();

            countries.Find();

            Assert.AreEqual(4, countries.Count);

            c = countries.Pop(MongoQuery<Country>.Eq(C => C.Name, "1"),countries.Sort.Descending(C=>C.Code));

            Assert.AreEqual(c.Code, "D");

            Assert.AreEqual(3, countries.Count);

            c = countries.Pop(MongoQuery<Country>.Eq(C => C.Name, "1"),countries.Sort.Descending(C => C.Code));

            Assert.AreEqual(c.Code, "B");

            Assert.AreEqual(2, countries.Count);

            c = countries.Pop(MongoQuery<Country>.Eq(C => C.Name, "1"),countries.Sort.Descending(C => C.Code));

            Assert.AreEqual(c.Code, "A");

            Assert.AreEqual(1, countries.Count);

            c = countries.Pop(MongoQuery<Country>.Eq(C => C.Name, "1"),countries.Sort.Descending(C => C.Code));

            Assert.IsNull(c);


        }
        public void TestInsert()
        {
            Helper.DropAllCollections();


            //Paris
            var geoArea = new GeoArea();

            geoArea.type = "Polygon";
            var coordinates = new List <double[]>();

            coordinates.Add(new double[] { 48.979766324449706, 2.098388671875 });
            coordinates.Add(new double[] { 48.972555195463336, 2.5982666015625 });
            coordinates.Add(new double[] { 48.683254235765325, 2.603759765625 });
            coordinates.Add(new double[] { 48.66874533279169, 2.120361328125 });
            coordinates.Add(new double[] { 48.979766324449706, 2.098388671875 });
            geoArea.coordinates = new [] { coordinates.ToArray() };

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

            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", Area = geoArea
            };
            c.Save();

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

            c = new Country {
                Code = "US", Name = "Estados Unidos", Area = geoArea
            };
            c.Save();

            var countries = new CountryCollection();

            countries.Find(x => x.Code, "ES");
            Assert.AreEqual(countries.Count, 1);

            countries.Find(x => x.Code, "UK");
            Assert.AreEqual(countries.Count, 1);

            countries.Find(x => x.Code, "US");
            Assert.AreEqual(countries.Count, 1);

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

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

            p.Save();

            p = new Person
            {
                Name        = "Juanito Sanchez",
                Age         = 25,
                BirthDate   = DateTime.Now.AddDays(52).AddYears(-38),
                Married     = true,
                Country     = "ES",
                BankBalance = decimal.Parse("1500,00")
            };

            p.Childs.Add(
                new Child {
                ID = 1, Age = 5, BirthDate = DateTime.Now.AddDays(7).AddYears(-5), Name = "Toni Sanchez"
            });

            p.Save();

            p = new Person
            {
                Name        = "Andres Perez",
                Age         = 25,
                BirthDate   = DateTime.Now.AddDays(25).AddYears(-25),
                Married     = false,
                Country     = "ES",
                BankBalance = decimal.Parse("500,00")
            };

            p.Save();

            p = new Person
            {
                Name        = "Marta Serrano",
                Age         = 28,
                BirthDate   = DateTime.Now.AddDays(28).AddYears(-28),
                Married     = false,
                Country     = "ES",
                BankBalance = decimal.Parse("9500,00")
            };

            p.Childs.Add(
                new Child {
                ID = 1, Age = 2, BirthDate = DateTime.Now.AddDays(2).AddYears(-2), Name = "Toni Serrano"
            });
            p.Save();

            p = new Person
            {
                Name        = "Jonh Smith",
                Age         = 21,
                BirthDate   = DateTime.Now.AddDays(21).AddYears(-21),
                Married     = false,
                Country     = "US",
                BankBalance = decimal.Parse("10000,00")
            };

            p.Save();

            var persons = new List <Person>();

            persons.MongoFind();

            Assert.AreEqual(persons.Count, 5);
        }