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 Filter() { var Fields = new string[] { "Description" }; var OrderBy = new string[] { "lmd", "CurrencyCode" }; string Name = "%"; int Skip = 25; int Limit = 25; var col = MongoMapperCollection <CurrencyType> .Instance; Fields = MongoMapperHelper.ConvertFieldName("CurrencyType", Fields.ToList()).ToArray(); OrderBy = MongoMapperHelper.ConvertFieldName("CurrencyType", OrderBy.ToList()).ToArray(); var sortList = new List <SortDefinition <CurrencyType> >(); if (!OrderBy.Any() || string.IsNullOrEmpty(OrderBy.First())) { sortList.Add(col.Sort.Ascending("$natural")); } sortList.AddRange(OrderBy.Where(S => !string.IsNullOrEmpty(S)).Select(Field => col.Sort.Ascending(Field))); var order = col.Sort.Combine(sortList); col.AddIncludeFields(Fields); col.Find(col.Filter.And(MongoQuery <CurrencyType> .Eq(D => D.Name, Name))).Limit(Limit).Skip(Skip).Sort(order); Console.WriteLine(col.Count); foreach (var currencyType in col) { Console.WriteLine(currencyType.CurrencyCode); Console.WriteLine(currencyType.Description); } }
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 TestGroupSum() { Helper.DropAllCollections(); var c = new Country { Code = "NL", Name = "Holanda" }; c.Save(); c = new Country { Code = "ES", Name = "SPAIN" }; c.Save(); for (int i = 0; i < 10; i++) { var p = new Person { Name = i.ToString(), Age = i, BirthDate = DateTime.Now.AddDays(57).AddYears(-35), Married = true, Country = "ES", BankBalance = decimal.Parse("3500,00") }; p.Save(); } for (int i = 0; i < 5; i++) { var p = new Person { Name = i.ToString(), Age = i, BirthDate = DateTime.Now.AddDays(57).AddYears(-35), Married = true, Country = "NL", BankBalance = decimal.Parse("3500,00") }; p.Save(); } var operations = new[] { new BsonDocument { { //$Country es $c "$group", new BsonDocument { { "_id", "$" + MongoMapperHelper.ConvertFieldName("Person", "Country") }, { "Total", new BsonDocument { { "$sum", 1 } } }, } } } }; var mongoresult = MongoMapper <Person> .Aggregate(operations); foreach (var document in mongoresult) { if (document["_id"].ToString() == "ES") { Assert.AreEqual(10, int.Parse(document["Total"].ToString())); } else if (document["_id"].ToString() == "NL") { Assert.AreEqual(5, int.Parse(document["Total"].ToString())); } else { throw new InconclusiveException(""); } } }