Ejemplo n.º 1
0
        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);
        }
Ejemplo n.º 2
0
        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);
        }
Ejemplo n.º 3
0
        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);
        }