Esempio n. 1
0
        public void Save_Updates_Existing_Order_Record()
        {
            int orderIDRetrieved;
            var updatedDate = DateTime.Now;

            using (var testData = new EFDataGenerator(new TestModel()))
            {
                testData.Batch(actions => actions.CreateOrderForCustomer(actions.CreateCustomer()));

                using (var scope = new UnitOfWorkScope())
                {
                    var order = new EFRepository <Order>().First();
                    Assert.That(order, Is.Not.Null);
                    orderIDRetrieved = order.OrderID;
                    order.OrderDate  = updatedDate;

                    scope.Commit();
                }

                using (new UnitOfWorkScope())
                {
                    var orderRepository = new EFRepository <Order>();
                    var order           = (from o in orderRepository
                                           where o.OrderID == orderIDRetrieved
                                           select o).FirstOrDefault();

                    Assert.That(order, Is.Not.Null);
                    Assert.That(order.OrderDate.Date, Is.EqualTo(updatedDate.Date));
                    Assert.That(order.OrderDate.Hour, Is.EqualTo(updatedDate.Hour));
                    Assert.That(order.OrderDate.Minute, Is.EqualTo(updatedDate.Minute));
                    Assert.That(order.OrderDate.Second, Is.EqualTo(updatedDate.Second));
                }
            }
        }
Esempio n. 2
0
        public void UnitOfWork_is_rolledback_when_containing_TransactionScope_is_rolledback()
        {
            using (var testData = new EFDataGenerator(new TestModel()))
            {
                testData.Batch(actions =>
                               actions.CreateOrderForCustomer(actions.CreateCustomer()));
                int      orderId;
                DateTime oldDate;

                using (var txScope = new TransactionScope(TransactionScopeOption.Required))
                    using (var uowScope = new UnitOfWorkScope(IsolationLevel.Serializable))
                    {
                        var ordersRepository = new EFRepository <Order>();
                        var order            = (from o in ordersRepository
                                                select o).First();

                        oldDate         = order.OrderDate;
                        order.OrderDate = DateTime.Now;
                        orderId         = order.OrderID;
                        uowScope.Commit();

                        //Note: txScope has not been committed
                    }

                using (var uowScope = new UnitOfWorkScope())
                {
                    var ordersRepository = new EFRepository <Order>();
                    var order            = (from o in ordersRepository
                                            where o.OrderID == orderId
                                            select o).First();

                    Assert.That(order.OrderDate, Is.EqualTo(oldDate));
                }
            }
        }
Esempio n. 3
0
        public void Query_Throws_Exception_When_LazyLoading_After_UnitOfWork_Is_Finished()
        {
            Customer customer;

            using (var testData = new EFDataGenerator(new TestModel()))
                using (new UnitOfWorkScope())
                {
                    testData.Batch(actions => actions.CreateCustomer());

                    var customerRepository = new EFRepository <Customer>();
                    customer = (from cust in customerRepository select cust).FirstOrDefault();
                }
            Assert.Throws <ObjectDisposedException>(() => customer.Orders.Load());
        }
Esempio n. 4
0
        public void When_Calling_CalculateTotal_On_Order_Returns_Valid_When_Under_UnitOfWork()
        {
            using (var testData = new EFDataGenerator(new TestModel()))
                using (new UnitOfWorkScope())
                {
                    testData.Batch(actions =>
                                   actions.CreateOrderForProducts(actions.CreateProducts(5)));
                    var oredersRepository = new EFRepository <Order>();
                    var order             = (from o in oredersRepository
                                             select o).FirstOrDefault();

                    Assert.That(order.CalculateTotal(), Is.GreaterThan(0));
                }
        }
Esempio n. 5
0
        public void When_No_FetchingStrategy_Registered_For_Makes_No_Changes()
        {
            Order order;

            using (var testData = new EFDataGenerator(new TestModel()))
                using (new UnitOfWorkScope())
                {
                    testData.Batch(actions =>
                                   actions.CreateOrderForProducts(actions.CreateProducts(5)));
                    var oredersRepository = new EFRepository <Order>().For <EFRepositoryTests>();
                    order = (from o in oredersRepository
                             select o).FirstOrDefault();
                }
            Assert.Throws <ObjectDisposedException>(() => order.CalculateTotal());
        }
Esempio n. 6
0
        public void When_Calling_CalculateTotal_On_Order_Returns_Valid_With_No_UnitOfWork_Throws()
        {
            Order order;

            using (var testData = new EFDataGenerator(new TestModel()))
                using (new UnitOfWorkScope())
                {
                    testData.Batch(actions =>
                                   actions.CreateOrderForProducts(actions.CreateProducts(5)));
                    var oredersRepository = new EFRepository <Order>();
                    order = (from o in oredersRepository
                             select o).FirstOrDefault();
                }
            Assert.Throws <ObjectDisposedException>(() => order.CalculateTotal());
        }
Esempio n. 7
0
        public void Query_Using_QueryMethod_Returns_Matched_Records_Only()
        {
            using (var testData = new EFDataGenerator(new TestModel()))
                using (new UnitOfWorkScope())
                {
                    testData.Batch(actions => actions.CreateOrderForCustomer(actions.CreateCustomerInState("PA")));

                    var customersInPA = new Specification <Order>(x => x.Customers.State == "PA");

                    var ordersRepository = new EFRepository <Order>();
                    var results          = from order in ordersRepository.Query(customersInPA) select order;

                    Assert.That(results.Count(), Is.EqualTo(1));
                }
        }
Esempio n. 8
0
        public void Query_Allows_Lazy_Load_While_UnitOfWork_Still_Running()
        {
            using (var testData = new EFDataGenerator(new TestModel()))
                using (new UnitOfWorkScope())
                {
                    testData.Batch(actions => actions.CreateOrderForCustomer(actions.CreateCustomer()));
                    var ordersRepository = new EFRepository <Order>();
                    var results          = from order in ordersRepository
                                           select order;

                    Assert.DoesNotThrow(() => results.ForEach(x =>
                    {
                        x.CustomersReference.Load();
                        Assert.That(!string.IsNullOrEmpty(x.Customers.FirstName));
                    }));
                }
        }
Esempio n. 9
0
        public void Query_Allows_Eger_Loading_Using_With()
        {
            using (var testData = new EFDataGenerator(new TestModel()))
                using (new UnitOfWorkScope())
                {
                    testData.Batch(actions => actions.CreateCustomer());

                    var ordersRepository = new EFRepository <Order>();
                    var results          = from order in ordersRepository.With(x => x.Customers)
                                           select order;

                    Assert.DoesNotThrow(() => results.ForEach(x =>
                    {
                        Assert.That(x.Customers, Is.TypeOf(typeof(Customer)));
                        Assert.That(!string.IsNullOrEmpty(x.Customers.FirstName));
                    }));
                }
        }
Esempio n. 10
0
        public void Repository_For_Uses_Registered_Fetching_Strategies()
        {
            IEnumerable <Order> orders;

            using (var testData = new EFDataGenerator(new TestModel()))
                using (new UnitOfWorkScope())
                {
                    testData.Batch(actions =>
                                   actions.CreateOrderForProducts(actions.CreateProducts(5)));
                    var strategies = new IFetchingStrategy <Order, EFRepositoryTests>[]
                    {
                        new OrderOrderItemsStrategy(),
                        new OrderItemsProductStrategy()
                    };

                    IRepository <Order> ordersRepository = null;
                    ServiceLocator.Current.Expect(x => x.GetAllInstances <IFetchingStrategy <Order, EFRepositoryTests> >())
                    .Return(strategies);

                    ordersRepository = new EFRepository <Order>().For <EFRepositoryTests>();
                    orders           = (from o in ordersRepository select o).ToList();
                }
            orders.ForEach(x => Assert.That(x.CalculateTotal(), Is.GreaterThan(0)));
        }
Esempio n. 11
0
        public void Query_Allows_Projection_Using_Select_Projection()
        {
            using (var testData = new EFDataGenerator(new TestModel()))
                using (new UnitOfWorkScope())
                {
                    testData.Batch(actions => actions.CreateOrderForCustomer(actions.CreateCustomer()));

                    var ordersRepository = new EFRepository <Order>();
                    var results          = from order in ordersRepository
                                           select new
                    {
                        order.Customers.FirstName,
                        order.Customers.LastName,
                        order.ShipDate,
                        order.OrderDate
                    };

                    Assert.DoesNotThrow(() => results.ForEach(x =>
                    {
                        Assert.That(!string.IsNullOrEmpty(x.LastName));
                        Assert.That(!string.IsNullOrEmpty(x.FirstName));
                    }));
                }
        }
Esempio n. 12
0
        public void Query_Allows_Projection_Using_Select_Projection()
        {
            using (var testData = new EFDataGenerator(new TestModel()))
            using (new UnitOfWorkScope())
            {
                testData.Batch(actions => actions.CreateOrderForCustomer(actions.CreateCustomer()));

                var ordersRepository = new EFRepository<Order>();
                var results = from order in ordersRepository
                              select new
                              {
                                  order.Customers.FirstName,
                                  order.Customers.LastName,
                                  order.ShipDate,
                                  order.OrderDate
                              };

                Assert.DoesNotThrow(() => results.ForEach(x =>
                {
                    Assert.That(!string.IsNullOrEmpty(x.LastName));
                    Assert.That(!string.IsNullOrEmpty(x.FirstName));
                }));
            }
        }
Esempio n. 13
0
        public void Query_Throws_Exception_When_LazyLoading_After_UnitOfWork_Is_Finished()
        {
            Customer customer;
            using (var testData = new EFDataGenerator(new TestModel()))
            using (new UnitOfWorkScope())
            {
                testData.Batch(actions => actions.CreateCustomer());

                var customerRepository = new EFRepository<Customer>();
                customer = (from cust in customerRepository select cust).FirstOrDefault();
            }
            Assert.Throws<ObjectDisposedException>(() => customer.Orders.Load());
        }
Esempio n. 14
0
        public void Repository_For_Uses_Registered_Fetching_Strategies()
        {
            IEnumerable<Order> orders;
            using (var testData = new EFDataGenerator(new TestModel()))
            using (new UnitOfWorkScope())
            {
                testData.Batch(actions =>
                               actions.CreateOrderForProducts(actions.CreateProducts(5)));
                var strategies = new IFetchingStrategy<Order, EFRepositoryTests>[]
                {
                    new OrderOrderItemsStrategy(),
                    new OrderItemsProductStrategy()
                };

                IRepository<Order> ordersRepository = null;
                ServiceLocator.Current.Expect(x => x.GetAllInstances<IFetchingStrategy<Order, EFRepositoryTests>>())
                    .Return(strategies);

                ordersRepository = new EFRepository<Order>().For<EFRepositoryTests>();
                orders = (from o in ordersRepository select o).ToList();
            }
            orders.ForEach(x => Assert.That(x.CalculateTotal(), Is.GreaterThan(0)));
        }
Esempio n. 15
0
 public EFDataGeneratorActions(EFDataGenerator generator)
 {
     _generator = generator;
 }
Esempio n. 16
0
 public void When_Calling_CalculateTotal_On_Order_Returns_Valid_With_No_UnitOfWork_Throws()
 {
     Order order;
     using (var testData = new EFDataGenerator(new TestModel()))
     using (new UnitOfWorkScope())
     {
         testData.Batch(actions =>
                        actions.CreateOrderForProducts(actions.CreateProducts(5)));
         var oredersRepository = new EFRepository<Order>();
         order = (from o in oredersRepository
                  select o).FirstOrDefault();
     }
     Assert.Throws<ObjectDisposedException>(() => order.CalculateTotal());
 }
Esempio n. 17
0
        public void When_Calling_CalculateTotal_On_Order_Returns_Valid_When_Under_UnitOfWork()
        {
            using (var testData = new EFDataGenerator(new TestModel()))
            using (new UnitOfWorkScope())
            {
                testData.Batch(actions =>
                               actions.CreateOrderForProducts(actions.CreateProducts(5)));
                var oredersRepository = new EFRepository<Order>();
                var order = (from o in oredersRepository
                             select o).FirstOrDefault();

                Assert.That(order.CalculateTotal(), Is.GreaterThan(0));
            }
        }
Esempio n. 18
0
        public void UnitOfWork_is_rolledback_when_containing_TransactionScope_is_rolledback()
        {
            using (var testData = new EFDataGenerator(new TestModel()))
            {
                testData.Batch(actions =>
                               actions.CreateOrderForCustomer(actions.CreateCustomer()));
                int orderId;
                DateTime oldDate;

                using (var txScope = new TransactionScope(TransactionScopeOption.Required))
                using (var uowScope = new UnitOfWorkScope(IsolationLevel.Serializable))
                {
                    var ordersRepository = new EFRepository<Order>();
                    var order = (from o in ordersRepository
                                 select o).First();

                    oldDate = order.OrderDate;
                    order.OrderDate = DateTime.Now;
                    orderId = order.OrderID;
                    uowScope.Commit();

                    //Note: txScope has not been committed
                }

                using (var uowScope = new UnitOfWorkScope())
                {
                    var ordersRepository = new EFRepository<Order>();
                    var order = (from o in ordersRepository
                                 where o.OrderID == orderId
                                 select o).First();

                    Assert.That(order.OrderDate, Is.EqualTo(oldDate));
                }
            }
        }
Esempio n. 19
0
        public void Save_Updates_Existing_Order_Record()
        {
            int orderIDRetrieved;
            var updatedDate = DateTime.Now;

            using (var testData = new EFDataGenerator(new TestModel()))
            {
                testData.Batch(actions => actions.CreateOrderForCustomer(actions.CreateCustomer()));

                using (var scope = new UnitOfWorkScope())
                {
                    var order = new EFRepository<Order>().First();
                    Assert.That(order, Is.Not.Null);
                    orderIDRetrieved = order.OrderID;
                    order.OrderDate = updatedDate;

                    scope.Commit();
                }

                using (new UnitOfWorkScope())
                {
                    var orderRepository = new EFRepository<Order>();
                    var order = (from o in orderRepository
                                 where o.OrderID == orderIDRetrieved
                                 select o).FirstOrDefault();

                    Assert.That(order, Is.Not.Null);
                    Assert.That(order.OrderDate.Date, Is.EqualTo(updatedDate.Date));
                    Assert.That(order.OrderDate.Hour, Is.EqualTo(updatedDate.Hour));
                    Assert.That(order.OrderDate.Minute, Is.EqualTo(updatedDate.Minute));
                    Assert.That(order.OrderDate.Second, Is.EqualTo(updatedDate.Second));
                }
            }
        }
Esempio n. 20
0
        public void Query_Allows_Lazy_Load_While_UnitOfWork_Still_Running()
        {
            using (var testData = new EFDataGenerator(new TestModel()))
            using (new UnitOfWorkScope())
            {
                testData.Batch(actions => actions.CreateOrderForCustomer(actions.CreateCustomer()));
                var ordersRepository = new EFRepository<Order>();
                var results = from order in ordersRepository
                              select order;

                Assert.DoesNotThrow(() => results.ForEach(x =>
                {
                    x.CustomersReference.Load();
                    Assert.That(!string.IsNullOrEmpty(x.Customers.FirstName));
                }));
            }
        }
Esempio n. 21
0
 public void When_No_FetchingStrategy_Registered_For_Makes_No_Changes()
 {
     Order order;
     using (var testData = new EFDataGenerator(new TestModel()))
     using (new UnitOfWorkScope())
     {
         testData.Batch(actions =>
                        actions.CreateOrderForProducts(actions.CreateProducts(5)));
         var oredersRepository = new EFRepository<Order>().For<EFRepositoryTests>();
         order = (from o in oredersRepository
                  select o).FirstOrDefault();
     }
     Assert.Throws<ObjectDisposedException>(() => order.CalculateTotal());
 }
Esempio n. 22
0
        public void Query_Using_QueryMethod_Returns_Matched_Records_Only()
        {
            using (var testData = new EFDataGenerator(new TestModel()))
            using (new UnitOfWorkScope())
            {
                testData.Batch(actions => actions.CreateOrderForCustomer(actions.CreateCustomerInState("PA")));

                var customersInPA = new Specification<Order>(x => x.Customers.State == "PA");

                var ordersRepository = new EFRepository<Order>();
                var results = from order in ordersRepository.Query(customersInPA) select order;

                Assert.That(results.Count(), Is.EqualTo(1));
            }
        }
Esempio n. 23
0
        public void Query_Allows_Eger_Loading_Using_With()
        {
            using (var testData = new EFDataGenerator(new TestModel()))
            using (new UnitOfWorkScope())
            {
                testData.Batch(actions => actions.CreateCustomer());

                var ordersRepository = new EFRepository<Order>();
                var results = from order in ordersRepository.With(x => x.Customers)
                              select order;

                Assert.DoesNotThrow(() => results.ForEach(x =>
                {
                    Assert.That(x.Customers, Is.TypeOf(typeof (Customer)));
                    Assert.That(!string.IsNullOrEmpty(x.Customers.FirstName));
                }));
            }
        }
Esempio n. 24
0
 public EFDataGeneratorActions(EFDataGenerator generator)
 {
     _generator = generator;
 }