public void Test_Corner_Cases_on_Live_Ef_to_Poco() { var db = new EfDbMapper(connectionString); var repo = new EF.Repository<Order>(db); var customerStub = new EF.Repository<Customer>(db); Customer cx = customerStub.LoadStub(1); Order oPoco = repo.Get(1); { System.Reflection.PropertyInfo px = oPoco.GetType().GetProperty("Customer", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance); oPoco.GetType().InvokeMember("Customer", System.Reflection.BindingFlags.GetProperty, null, oPoco, new object[] { }); var a = oPoco.OrderLines; var b = a[0].Order; object x = oPoco.Customer; var languages = oPoco.Customer.Country.Languages; // To avoid circular reference // http://connect.microsoft.com/VisualStudio/feedback/details/679399/entity-framework-4-1-using-lazyloading-with-notracking-option-causes-invalidoperationexception // object countries = languages[0].Countries; // Do these instead: int lid = languages.First().LanguageId; Language firstLanguage = new EF.Repository<Language>(db).All.SingleOrDefault(l => l.LanguageId == lid); object countries = firstLanguage.Countries; } Assert.AreEqual("Michael", oPoco.Customer.CustomerName); Assert.AreEqual("Philippines", oPoco.Customer.Country.CountryName); Assert.AreEqual(1, oPoco.OrderLines[0].Product.ProductId); OrderDto oDto = Mapper.ToDto<Order, OrderDto>(oPoco); Assert.AreEqual("Michael", oDto.CustomerName); Assert.AreEqual(3, oDto.OrderLines.Count); }
public void Test_Dto_To_Live_Ef_Poco() { // Arrange OrderDto oDto = new OrderDto { OrderDate = new DateTime(2012, 07, 22), OrderDescription = "Hey Apple!", CustomerId = 1, CustomerName = "Hey", OrderLines = new[] { new OrderLineDto { ProductoId = 1, Quantity = 7, Price = 6, Amount = 42 }, new OrderLineDto { ProductoId = 2, Quantity = 3, Price = 6, Amount = 18, FreebieId = 1, Koments = new[] { new CommentDto { TheComment = "Lovely" }, new CommentDto { TheComment = "View" } } }, new OrderLineDto { ProductoId = 1, Quantity = 4, Price = 5, Amount = 20, FreebieId = 2 }, } }; // Act Order oPoco = Mapper.ToPoco<OrderDto,Order>(oDto); var db = new EfDbMapper(connectionString); // can use this too: /* db.AssignStub(oPoco); db.Set<Order>().Add(oPoco); db.SaveChanges(); */ db.AssignStub(oPoco); var repoOrder = new EF.Repository<Order>(db); repoOrder.Save(oPoco); Assert.AreNotEqual("", oPoco.Customer.CustomerName); Assert.IsNotNull(oPoco.Customer); // oPoco = repoOrder.Get(oPoco.OrderId); repoOrder.Save(oPoco); // Assert Assert.AreNotEqual(0, oPoco.OrderId); Assert.IsNotNull(oPoco.Customer); Assert.AreNotEqual("", oPoco.Customer.CustomerName); }
public void Test_nested_Live_Ef_SaveGraph_Two_Times() { var date = new DateTime(1976, 11, 05); var db = new EfDbMapper(connectionString); var repo = new EF.Repository<Order>(db); Order oPoco = repo.Get(1); Assert.AreEqual(date, oPoco.OrderDate); OrderDto oDto = Mapper.ToDto<Order, OrderDto>(oPoco); // OrderDate is not mapped to OrderDate, it is mapped to DummyDate, // hence should not be equal Assert.AreNotEqual(date, oDto.OrderDate); Order pPoco = Mapper.ToPoco<OrderDto, Order>(oDto); pPoco.OrderDate = date; Assert.AreEqual(date, pPoco.OrderDate); db.AssignStub(pPoco); Assert.AreEqual(date, pPoco.OrderDate); int count = pPoco.OrderLines.Count; // pPoco.OrderLines.RemoveAt(0); /*db.Entry(pPoco).State = System.Data.EntityState.Modified; db.SaveChanges();*/ repo.SaveGraph(pPoco); Order qPoco = repo.Get(1); // Assert.AreNotEqual(count, qPoco.OrderLines.Count); }
public void Test_nested_Live_Ef_Dto_to_Poco() { // Arrange int customerId = 1; string orderDesc = "Superb"; OrderDto oDto = new OrderDto { CustomerId = customerId, CustomerName = "Miguel", OrderDescription = orderDesc, OrderDate = new DateTime(2076, 11, 05), OrderLines = new[] { new OrderLineDto { ProductoId = 1, Quantity = 8, Price = 6, Amount = 48 }, new OrderLineDto { ProductoId = 2, Quantity = 3, Price = 6, Amount = 18 } } }; // Act Order oPoco = Mapper.ToPoco<OrderDto, Order>(oDto); // Assert Assert.AreNotSame(oDto, oPoco); Assert.IsNotNull(oPoco.Customer); Assert.AreEqual(customerId, oPoco.Customer.CustomerId); // Even we have a Customer object. it's just a stub object. Expect other properties to be null or zero, i.e. in their default value Assert.IsNull(oPoco.Customer.CustomerName); // And so is this Assert.IsNull(oPoco.Customer.Country); EfDbMapper db = new EfDbMapper(connectionString); db.AssignStub(oPoco); var repo = new EF.Repository<Order>(db); repo.SaveGraph(oPoco); /*db.Set<Order>().Add(oPoco); db.SaveChanges();*/ Assert.AreEqual(2, oPoco.OrderLines.Count); int retId = oPoco.OrderId; oPoco = db.Set<Order>().AsNoTracking().Single(x => x.OrderId == retId); Customer cl = db.Set<Customer>().AsNoTracking().Single(x => x.CustomerId == 2); Assert.AreEqual("Lennon", cl.CustomerName); Customer c = db.Set<Customer>().AsNoTracking().Single(x => x.CustomerId == 1); Assert.AreEqual("Michael", c.CustomerName); Assert.AreNotEqual(0, oPoco.OrderId); Assert.AreEqual(oDto.OrderDescription, oPoco.OrderDescription); // the customer name from DTO would not cascade to POCO. referential integrity is maintained Assert.AreEqual("Michael", oPoco.Customer.CustomerName); Assert.AreEqual("Philippines", oPoco.Customer.Country.CountryName); Assert.AreEqual(1976, oPoco.Customer.MemberYear); Assert.IsNotNull(oPoco.Customer.Address1); }
public void Test_Live_EF_Poco_To_Dto() { // Arrange var db = new EfDbMapper(connectionString); DateTime expectedDate = new DateTime(1976, 11, 5); // Act Order o = db.Set<Order>().AsNoTracking() .Include("OrderLines") .Include("OrderLines.Product") .Include("OrderLines.Comments") .OrderBy(x => x.OrderId).First(); // Assert OrderDto odto = Mapper.ToDto<Order, OrderDto>(o); Assert.AreEqual("Hello", odto.OrderDescription); Assert.AreEqual(expectedDate, odto.DummyDate); Assert.AreEqual(3, odto.OrderLines.Count); Assert.AreEqual(1, odto.OrderLines[0].ProductoId); Assert.AreEqual(2, odto.OrderLines[0].FreebieId); Assert.AreEqual("Mouse", odto.OrderLines[0].ProductDescription); Assert.AreEqual(2, odto.OrderLines[1].ProductoId); Assert.AreEqual(0, odto.OrderLines[1].FreebieId); Assert.AreEqual("Keyboard", odto.OrderLines[1].ProductDescription); Assert.AreEqual(1, odto.OrderLines[2].ProductoId); Assert.AreEqual("Mouse", odto.OrderLines[2].ProductDescription); Assert.AreEqual(2, odto.OrderLines[1].Koments.Count); Assert.AreEqual("Nice", odto.OrderLines[1].Koments[0].TheComment); Assert.AreEqual("Great", odto.OrderLines[1].Koments[1].TheComment); }
public void Test_Live_Ef_Language_Country_Poco_to_Dto_NullableCountry() { // Arrange var db = new EfDbMapper(connectionString); int orderId = 2; // Act int c = db.Set<Order>().Where(x => x.OrderId == orderId) .SelectMany(x => x.Customer.Country.Languages).Count(); var langs = db.Set<Order>().Where(x => x.OrderId == orderId).SelectMany(x => x.Customer.Country.Languages).OrderBy(x => x.LanguageName); Order oPoco = db.Set<Order>().Single(x => x.OrderId == orderId); OrderDto oDto = Mapper.ToDto<Order, OrderDto>(oPoco); // Assert Assert.AreEqual(0, c); Assert.IsNotNull(oPoco.Customer); Assert.IsNull(oPoco.Customer.Country); // go back // Assert.IsNull(oDto.PossibleLanguages); }
public void Test_Live_Ef_Language_Country_Poco_to_Dto() { // Arrange var db = new EfDbMapper(connectionString); // Act int c = db.Set<Order>().Where(x => x.OrderId == 1) .SelectMany(x => x.Customer.Country.Languages).Count(); var langs = db.Set<Order>().Where(x => x.OrderId == 1).SelectMany(x => x.Customer.Country.Languages).OrderBy(x => x.LanguageName); Order oPoco = db.Set<Order>().Single(x => x.OrderId == 1); OrderDto oDto = Mapper.ToDto<Order,OrderDto>(oPoco); Assert.AreEqual(2, oPoco.Customer.Country.Languages.Count()); Assert.IsNotNull(oDto); // go back // Assert.IsNotNull(oDto.PossibleLanguages); // var dtoLang = oDto.PossibleLanguages.OrderBy(x => x.LanguageName).ToArray(); // Assert Assert.AreEqual(2, c); Assert.AreEqual("English", langs.ToArray()[0].LanguageName); Assert.AreEqual("Tagalog", langs.ToArray()[1].LanguageName); // go back /* Assert.AreNotSame(oPoco.Customer.Country.Languages, oDto.PossibleLanguages); Assert.AreEqual("English", dtoLang[0].LanguageName); Assert.AreEqual("Tagalog", dtoLang[1].LanguageName); */ }
public void Test_Live_Ef_Language_Country() { // Arrange var db = new EfDbMapper(connectionString); // Act int c = db.Set<Order>().Where(x => x.OrderId == 1) .SelectMany(x => x.Customer.Country.Languages).Count(); var langs = db.Set<Order>().Where(x => x.OrderId == 1).SelectMany(x => x.Customer.Country.Languages).OrderBy(x => x.LanguageName); // Assert Assert.AreEqual(2, c); Assert.AreEqual("English", langs.ToArray()[0].LanguageName); Assert.AreEqual("Tagalog", langs.ToArray()[1].LanguageName); }