Exemplo n.º 1
0
        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);
        }
Exemplo 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);
        }
Exemplo n.º 3
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);
        }
Exemplo n.º 4
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);
        }
Exemplo n.º 5
0
        // disable [TestMethod]
        public void TestNhibernateQuery()
        {
            // var s =;
            // var s =

            /*
            var t = s.Query<Question>().Select(x => new { x.Text, Cx = x.Answers.AsQueryable().Count() } );
            t.ToList();*/

            // IRepository<Question> q = new NhRepository<Question>(NhModelsMapper.GetSession(connectionString));
            IRepository<Question> q = new EF.Repository<Question>(new EfDbMapper(connectionString));

            /*
            var t = s.Query<Question>().Any(x => x.Answers.Any(y => y.Text == "Paul"));

            var xx = new NhRepository<Question>(s);

            var yy = xx.All.Any(x => x.Answers.Any(y => y.Text == "John"));

            var zz = xx.All.Where(x => x.Text== "See");
             */

            /*
            IRepository<Question> db = new EfRepository<Question>(new EfDbMapper(connectionString));
            var zzz = db.All.Any(x => x.Answers.Any(y => y.Text == "John"));*/

            /*
            var t = db.Set<Question>().Select(x => new { x.Text, Cx = x.Answers.AsQueryable().Count() });

            var xxx = t.ToList();
            */

            var www = from x in q.All
                      from y in x.Answers.DefaultIfEmpty()
                      group y by new { QuestionId = x.QuestionId } into grp
                      select new { grp.Key.QuestionId, Count = grp.Sum(x => x.Question.QuestionId != null ? 1 : 0) };

            www.ToList();
        }
Exemplo n.º 6
0
        public void Ef_Can_save_transaction()
        {
            EmptyDatabase();

            var x = new EfDbMapper(connectionString);
            ITransactionBoundFactory xf = new EF.TransactionBoundFactory();

            IRepository<Product> prod = new EF.Repository<Product>(x);
            IRepository<Question> ques = new EF.Repository<Question>(x);
            Common_Can_save_transaction(xf, prod, ques);
        }
Exemplo n.º 7
0
 public void Ef_HasRowVersion()
 {
     EmptyDatabase();
     IRepository<Product> db = new EF.Repository<Product>(new EfDbMapper(connectionString));
     Common_HasRowVersion(db);
 }
Exemplo n.º 8
0
 public void Ef_Can_queue_changes()
 {
     EmptyDatabase();
     IRepository<Question> db = new EF.Repository<Question>(new EfDbMapper(connectionString));
     Common_Can_queue_changes(db);
 }
Exemplo n.º 9
0
 public void Ef_Can_Save_Then_Update()
 {
     EmptyDatabase();
     IRepository<Product> dbProd = new EF.Repository<Product>(new EfDbMapper(connectionString));
     IRepository<ProductPrice> dbPrice = new EF.Repository<ProductPrice>(new EfDbMapper(connectionString));
     Common_Can_Save_Then_Update(dbProd, dbPrice);
 }
Exemplo n.º 10
0
 public void Ef_Can_Orm_do_cascaded_deletions()
 {
     EmptyDatabase();
     IRepository<Question> db = new EF.Repository<Question>(new EfDbMapper(connectionString));
     Common_Can_Orm_do_cascaded_deletions(db);
 }
Exemplo n.º 11
0
 public void Ef_Can_Orm_SaveGraph_Can_Update()
 {
     EmptyDatabase();
     IRepository<Question> db = new EF.Repository<Question>(new EfDbMapper(connectionString));
     Common_Orm_SaveGraph_Can_Update(db);
 }
Exemplo n.º 12
0
        public void Ef_Can_Fetch_Eager_Load()
        {
            EmptyDatabase();
            IRepository<Question> db = new EF.Repository<Question>(new EfDbMapper(connectionString));
            Test_Can_Fetch_Eager_Load_Common(db);

            // Arrange
            EmptyDatabase();
            Question qx = new Question { Text = "42" };
            db.Save(qx);
            db.Evict(qx.QuestionId);

            // Act
            var qq = EF.EagerExtensionHelper.EagerLoad(db.All.Where(x => x.QuestionId == qx.QuestionId), "Answers").SingleOrDefault();

            // Assert
            Assert.AreEqual("42", qq.Text);
        }
Exemplo n.º 13
0
 public void Ef_CanUpdate()
 {
     EmptyDatabase();
     IRepository<Product> db = new EF.Repository<Product>(new EfDbMapper(connectionString));
     Common_CanUpdate(db);
 }
Exemplo n.º 14
0
 public void Ef_CanSaveHeaderDetail_ThenHeaderOnly()
 {
     EmptyDatabase();
     IRepository<Product> db = new EF.Repository<Product>(new EfDbMapper(connectionString));
     // throw new Exception("Provider : " + db.All.Provider.GetType().ToString()); // NHibernate.Linq.NhQueryProvider
     Common_CanSaveHeaderDetail_ThenHeaderOnly(db);
 }
Exemplo n.º 15
0
 public void Ef_CanSaveHeaderDetail()
 {
     EmptyDatabase();
     IRepository<Product> db = new EF.Repository<Product>(new EfDbMapper(connectionString));
     // throw new Exception("Provider : " + db.All.Provider.GetType().ToString()); // System.Data.Entity.Internal.Linq.DbQueryProvider
     Common_CanSaveHeaderDetail(db);
 }
Exemplo n.º 16
0
 public void Ef_CanHaveIncrementingKey()
 {
     EmptyDatabase();
     IRepository<Product> db = new EF.Repository<Product>(new EfDbMapper(connectionString));
     Common_CanHaveIncrementingKey(db);
 }