public void Nested_UnitOfWork_With_Different_Transaction_Compatibility_Works() { var changedShipDate = DateTime.Now.AddDays(1); var changedOrderDate = DateTime.Now.AddDays(2); using (var testData = new LinqToSqlDataGenerator(new TestDataDataContext{Log = Console.Out})) { testData.Batch(actions => actions.CreateOrderForCustomer(actions.CreateCustomer())); int orderId; using (new UnitOfWorkScope()) { var ordersRepository = new LinqToSqlRepository<Order>(); orderId = ordersRepository.Select(x => x.OrderID).First(); } Assert.NotNull(orderId); using (new UnitOfWorkScope()) { var outerRepository = new LinqToSqlRepository<Order>(); var outerOrder = outerRepository.Where(x => x.OrderID == orderId).First(); outerOrder.OrderDate = changedOrderDate; using (var innerScope = new UnitOfWorkScope(UnitOfWorkScopeTransactionOptions.CreateNew)) { var innerRepository = new LinqToSqlRepository<Order>(); var innerOrder = innerRepository.Where(x => x.OrderID == orderId).First(); innerOrder.ShipDate = changedShipDate; innerScope.Commit(); } } using (new UnitOfWorkScope()) { var ordersRepository = new LinqToSqlRepository<Order>(); var order = ordersRepository.First(); Assert.That(order.OrderDate, Is.Not.EqualTo(changedOrderDate)); Assert.That(order.ShipDate, Is.Not.EqualTo(changedShipDate)); } } }
public void When_Calling_CalculateTotal_On_Order_Returns_Valid_With_No_UnitOfWork_Throws() { Order order; using (var testData = new LinqToSqlDataGenerator(new TestDataDataContext())) using (new UnitOfWorkScope()) { testData.Batch(actions => actions.CreateOrderForProducts(actions.CreateProducts(5))); var oredersRepository = new LinqToSqlRepository<Order>(); order = (from o in oredersRepository select o).FirstOrDefault(); } Assert.Throws<ObjectDisposedException>(() => order.CalculateTotal()); }
public void When_No_FetchingStrategy_Registered_For_Makes_No_Changes() { Order order; using (var testData = new LinqToSqlDataGenerator(new TestDataDataContext())) using (new UnitOfWorkScope()) { testData.Batch(actions => actions.CreateOrderForProducts(actions.CreateProducts(5))); var oredersRepository = new LinqToSqlRepository<Order>(); order = (from o in oredersRepository select o).FirstOrDefault(); } Assert.Throws<ObjectDisposedException>(() => order.CalculateTotal()); }
public void UnitOfWork_Is_Rolledback_When_Containing_TransactionScope_Is_Rolledback() { using (var testData = new LinqToSqlDataGenerator(new TestDataDataContext())) { 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 LinqToSqlRepository<Order>(); var order = (from o in ordersRepository select o).First(); oldDate = order.OrderDate; order.OrderDate = DateTime.Now; orderId = order.OrderID; uowScope.Commit(); } using (var uowScope = new UnitOfWorkScope()) { var ordersRepository = new LinqToSqlRepository<Order>(); var order = (from o in ordersRepository where o.OrderID == orderId select o).First(); Assert.That(order.OrderDate, Is.EqualTo(oldDate)); } } }
public void When_Calling_CalculateTotal_On_Order_Returns_Valid_When_Under_UnitOfWork() { using (var testData = new LinqToSqlDataGenerator(new TestDataDataContext())) using (new UnitOfWorkScope()) { testData.Batch(actions => actions.CreateOrderForProducts(actions.CreateProducts(5))); var oredersRepository = new LinqToSqlRepository<Order>(); var order = (from o in oredersRepository select o).FirstOrDefault(); Assert.That(order.CalculateTotal(), Is.GreaterThan(0)); } }
public void Repository_For_Uses_Registered_Fetching_Strategies() { IEnumerable<Order> orders; using (var testData = new LinqToSqlDataGenerator(new TestDataDataContext())) using (new UnitOfWorkScope()) { testData.Batch(actions => actions.CreateOrderForProducts(actions.CreateProducts(5))); var strategies = new IFetchingStrategy<Order, LinqToSqlRepositoryTests>[] { new OrderOrderItemsStrategy(), new OrderItemsProductStrategy() }; IRepository<Order> ordersRepository = null; ServiceLocator.Current.Expect( x => x.GetAllInstances<IFetchingStrategy<Order, LinqToSqlRepositoryTests>>()) .Return(strategies); ordersRepository = new LinqToSqlRepository<Order>().For<LinqToSqlRepositoryTests>(); orders = (from o in ordersRepository select o).ToList(); } orders.ForEach(x => Assert.That(x.CalculateTotal(), Is.GreaterThan(0))); }
public void Save_Updates_Existing_Order_Record() { int orderIDRetrieved; var updatedDate = DateTime.Now; using (var testData = new LinqToSqlDataGenerator(new TestDataDataContext())) { testData.Batch(actions => actions.CreateOrderForCustomer(actions.CreateCustomer())); using (var scope = new UnitOfWorkScope()) { var order = new LinqToSqlRepository<Order>().FirstOrDefault(); Assert.That(order, Is.Not.Null); orderIDRetrieved = order.OrderID; order.OrderDate = updatedDate; scope.Commit(); } using (new UnitOfWorkScope()) { var orderRepository = new LinqToSqlRepository<Order>(); var order = (from o in orderRepository where o.OrderID == orderIDRetrieved select o).FirstOrDefault(); Assert.That(order, Is.Not.Null); Assert.That(order.OrderDate.Value.Date, Is.EqualTo(updatedDate.Date)); Assert.That(order.OrderDate.Value.Hour, Is.EqualTo(updatedDate.Hour)); Assert.That(order.OrderDate.Value.Minute, Is.EqualTo(updatedDate.Minute)); Assert.That(order.OrderDate.Value.Second, Is.EqualTo(updatedDate.Second)); } } }
public void Query_Using_QueryMethod_Returns_Matched_Records_Only() { using (var testData = new LinqToSqlDataGenerator(new TestDataDataContext())) using (new UnitOfWorkScope()) { testData.Batch(actions => actions.CreateOrderForCustomer(actions.CreateCustomerInState("PA"))); var customersInPA = new Specification<Order>(x => x.Customer.State == "PA"); var ordersRepository = new LinqToSqlRepository<Order>(); var results = from order in ordersRepository.Query(customersInPA) select order; Assert.That(results.Count() > 0); } }
public void Query_Throws_Exception_When_LazyLoading_After_UnitOfWork_Is_Finished() { Customer customer; using (var testData = new LinqToSqlDataGenerator(new TestDataDataContext())) using (new UnitOfWorkScope()) { testData.Batch(actions => actions.CreateCustomer()); var customerRepository = new LinqToSqlRepository<Customer>(); customer = (from cust in customerRepository select cust).FirstOrDefault(); } Assert.That(customer, Is.Not.Null); Assert.Throws<ObjectDisposedException>(() => customer.Orders.Count()); }
public void Query_Allows_Projection_Using_Select_Projection() { using (var testData = new LinqToSqlDataGenerator(new TestDataDataContext())) using (new UnitOfWorkScope()) { testData.Batch(actions => actions.CreateOrderForCustomer(actions.CreateCustomer())); var ordersRepository = new LinqToSqlRepository<Order>(); var results = from order in ordersRepository select new { order.Customer.FirstName, order.Customer.LastName, order.ShipDate, order.OrderDate }; Assert.DoesNotThrow(() => results.ForEach(x => { Assert.That(!string.IsNullOrEmpty(x.LastName)); Assert.That(!string.IsNullOrEmpty(x.FirstName)); })); } }
public void Query_Allows_Lazy_Load_While_UnitOfWork_Still_Running() { using (var testData = new LinqToSqlDataGenerator(new TestDataDataContext())) using (new UnitOfWorkScope()) { testData.Batch(actions => actions.CreateOrderForCustomer(actions.CreateCustomer())); var ordersRepository = new LinqToSqlRepository<Order>(); var results = from order in ordersRepository select order; Assert.DoesNotThrow(() => results.ForEach( x => Assert.That(!string.IsNullOrEmpty(x.Customer.FirstName)))); } }
public void Query_Allows_Eger_Loading_Using_With() { using (var testData = new LinqToSqlDataGenerator(new TestDataDataContext())) using (new UnitOfWorkScope()) { testData.Batch(actions => actions.CreateCustomer()); var ordersRepository = new LinqToSqlRepository<Order>(); var results = from order in ordersRepository.With(x => x.Customer) select order; Assert.DoesNotThrow(() => results.ForEach(x => { Assert.That(x.Customer, Is.TypeOf(typeof (Customer))); Assert.That(!string.IsNullOrEmpty(x.Customer.FirstName)); })); } }