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_Strongly_typed_mapper_for_Dto_To_Poco() { // Arrange OrderDto oDto = new OrderDto { OrderId = 76, OrderDate = new DateTime(2012, 07, 22), OrderDescription = "Hey Apple!", CustomerId = 5201314, CustomerName = "Hey", OrderLines = new[] { new OrderLineDto { OrderLineId = 7, ProductoId = 1, Quantity = 7, Price = 6, Amount = 42 }, new OrderLineDto { ProductoId = 2, Quantity = 3, Price = 6, Amount = 18, FreebieId = 9, Koments = new[] { new CommentDto { CommentId = 9, TheComment = "Great" }, new CommentDto { TheComment = "Nice" } } }, new OrderLineDto { ProductoId = 1, Quantity = 4, Price = 5, Amount = 20, FreebieId = 9 }, } }; // Act Order oPoco = Mapper.ToPoco<OrderDto, Order>(oDto); // Assert Assert.AreEqual(oDto.OrderId, oPoco.OrderId); Assert.AreEqual(oDto.CustomerId, oPoco.Customer.CustomerId); Assert.AreEqual(oDto.OrderDate, oPoco.OrderDate); Assert.AreEqual(oDto.OrderDescription, oPoco.OrderDescription); Assert.AreEqual(3, oPoco.OrderLines.Count); Assert.IsNotNull(oPoco.OrderLines[0].Order); Assert.ReferenceEquals(oPoco, oPoco.OrderLines[0].Order); Assert.AreEqual(2, oPoco.OrderLines[1].Comments.Count); Assert.IsNotNull(oPoco.OrderLines[0].Product); Assert.AreEqual(oDto.OrderLines[0].ProductoId, oPoco.OrderLines[0].Product.ProductId); Assert.AreEqual(oDto.OrderLines[0].ProductDescription, oPoco.OrderLines[0].Product.ProductName); Assert.AreEqual(oDto.OrderLines[0].Quantity, oPoco.OrderLines[0].Quantity); Assert.AreEqual(oDto.OrderLines[0].Price, oPoco.OrderLines[0].Price); Assert.AreEqual(oDto.OrderLines[0].Amount, oPoco.OrderLines[0].Amount); Assert.AreEqual(oDto.OrderLines[1].FreebieId, oPoco.OrderLines[1].Freebie.ProductId); Assert.AreEqual(oDto.OrderLines[1].Koments[0].CommentId, oPoco.OrderLines[1].Comments[0].CommentId); Assert.AreEqual(oDto.OrderLines[1].Koments[0].TheComment, oPoco.OrderLines[1].Comments[0].TheComment); Assert.AreEqual(oPoco.OrderLines[1].Comments[1].CommentId, oPoco.OrderLines[1].Comments[1].CommentId); Assert.AreEqual(oDto.OrderLines[1].Koments[1].TheComment, oPoco.OrderLines[1].Comments[1].TheComment); }
public void Test_nested_Live_Nh_Dto_to_Poco() { int customerId = 1; // Arrange OrderDto oDto = new OrderDto { CustomerId = customerId, CustomerName = "Miguel", OrderDescription = "Superb", 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); ISession s = NhDbMapper.GetSession(connectionString); oPoco = s.Merge(oPoco); s.Flush(); Assert.AreEqual(2, oPoco.OrderLines.Count); 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); }
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_nested() { // Arrange int customerId = 1; OrderDto oDto = new OrderDto { CustomerId = customerId, CustomerName = "Michael", OrderDescription = "Superb" }; // 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); Assert.IsNull(oPoco.Customer.Country); }
public void Test_Dto_To_Live_Nh_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 = "Great" }, new CommentDto { TheComment = "Nice" } } }, new OrderLineDto { ProductoId = 1, Quantity = 4, Price = 5, Amount = 20, FreebieId = 2 }, } }; // Act Order oPoco = Mapper.ToPoco<OrderDto,Order>(oDto); // Assert using (ISession s = NhDbMapper.GetSession(connectionString)) { var db = new NH.Repository<Order>(s); db.Save(oPoco); Assert.AreNotEqual(0, oPoco.OrderId); Assert.AreEqual(3, db.Get(oPoco.OrderId).OrderLines.Count); } }