public void TestOriginalValue() { Helper.DropAllCollections(); var c = new Country {Code = "ES", Name = "España"}; c.Save(); 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.Name = "Juan Sin Miedo"; object originalName = p.GetOriginalObject().Name; Assert.AreEqual(originalName.ToString(), "Pepito Perez"); }
public void TestChildIncrementalId() { Helper.DropAllCollections(); var c = new Country {Code = "ES", Name = "España"}; c.Save(); //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(); var Persons = new List<Person>(); Persons.MongoFind(); long index = 1; foreach (Person person in Persons) { foreach (Child child in person.Childs) { Assert.AreEqual(child._id, index); index ++; } } }
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(); } catch (Exception ex) { Assert.AreEqual(ex.GetBaseException().GetType(), typeof (ValidateUpRelationException)); p.Country = "ES"; p.Save(); } c = MongoMapper.FindByKey<Country>("ES"); try { c.Delete(); } catch (Exception ex) { Assert.AreEqual(ex.GetBaseException().GetType(), typeof (ValidateDownRelationException)); List<Person> Persons = c.GetRelation<Person>("Person,Country"); foreach (Person p2 in Persons) { p2.Country = "UK"; p2.Save(); } c.Delete(); } c = MongoMapper.FindByKey<Country>("UK"); List<Person> PersonasEnUK = c.GetRelation<Person>("Person,Country"); foreach (Person PersonInUK in PersonasEnUK) { Assert.AreEqual(PersonInUK.Country, "UK"); Assert.AreEqual(PersonInUK.GetRelation<Country>("Country,Code").First().Code, "UK"); } }
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.Aggregate<Person>(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 (""); } }
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 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 TestInsert() { Helper.DropAllCollections(); //Insert de Paises 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)); } c = new Country {Code = "US", Name = "Estados Unidos"}; c.Save(); var Countries = new List<Country>(); Countries.MongoFind(C=>C.Code, "ES"); Assert.AreEqual(Countries.Count, 1); Countries.MongoFind(C=>C.Code, "UK"); Assert.AreEqual(Countries.Count, 1); Countries.MongoFind(C=>C.Code, "US"); Assert.AreEqual(Countries.Count, 1); Countries.MongoFind(); 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("100,00") }; p.Save(); var persons = new List<Person>(); persons.MongoFind(); Assert.AreEqual(persons.Count, 5); persons.MongoFind("Childs.Age", 2); Assert.AreEqual(persons.Count, 1); }
public void TestFillByKeyExtension() { Helper.DropAllCollections(); //Insert de Paises var c = new Country {Code = "ES", Name = "España"}; c.Save(); var country = new Country(); country.FillByKey("ES"); Assert.AreEqual(country.Code, "ES"); //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(); long id = p.m_id; p = new Person(); p.FillByKey(id); }
public void TestFindByPk() { //Llenamos datos Helper.DropAllCollections(); ConfigManager.Out = Console.Out; var c = new Country {Code = "ES", Name = "España"}; c.Save(); //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 = "ES", BankBalance = decimal.Parse("100,00") }; p.Save(); List<Person> plist = MongoMapperCollection<Person>.Instance.Find().ToList(); var p2 = new Person(); p2.FillByKey(plist[0].m_id); p2.Name = "FindBYKey Name"; p2.Save(); var Persons = MongoMapperCollection<Person>.Instance; plist = Persons.Find(x=>x.m_id, p2.m_id).ToList(); Assert.AreEqual(1, plist.Count); Assert.AreEqual(plist[0].Name, "FindBYKey Name"); c = new Country(); //Assert.Throws(typeof(FindByKeyNotFoundException), c.FillByKey("XXXXXX")); }
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); }