public void Test_Updating_Money_Amount_Updates_Amount_In_Store_And_Returns_Updated_Figure() { var newAmount = (decimal) new Random().Next(); using (var testData = new NHTestDataGenerator(Factory.OpenSession())) { testData.Batch(actions => actions.CreateMonthlySalesSummaryForSalesPerson(1)); using (var scope = new UnitOfWorkScope()) { var repository = new NHRepository<MonthlySalesSummary>(); var report = (from summary in repository where summary.SalesPersonId == 1 select summary).SingleOrDefault(); report.TotalSale.Amount = newAmount; scope.Commit(); } using (var scope = new UnitOfWorkScope()) { var repository = new NHRepository<MonthlySalesSummary>(); var report = (from summary in repository where summary.SalesPersonId == 1 select summary).SingleOrDefault(); Assert.That(report.TotalSale.Amount, Is.EqualTo(newAmount)); scope.Commit(); } } }
public void Test_Can_Query_MonthlySalesSummary_Based_On_Currency() { IList<MonthlySalesSummary> report; using (var testData = new NHTestDataGenerator(Factory.OpenSession())) using (var scope = new UnitOfWorkScope()) { testData.Batch(actions => actions.CreateMonthlySalesSummaryWithAmount( new Money{Amount = 100, Currency = "YEN"} )); var repository = new NHRepository<MonthlySalesSummary>(); report = (from summary in repository where summary.TotalSale.Currency == "YEN" select summary).ToList(); scope.Commit(); } Assert.That(report, Is.Not.Null); Assert.That(report.Count, Is.GreaterThan(0)); report.ForEach(rep => { Assert.That(rep.TotalSale, Is.Not.Null); Assert.That(rep.TotalSale.Amount, Is.GreaterThan(0)); Assert.That(rep.TotalSale.Currency, Is.Not.Null); Assert.That(rep.TotalSale.Currency, Is.EqualTo("YEN")); }); }
public void Test_Updating_Money_Amount_Updates_Amount_In_Store_And_Returns_Updated_Figure() { var newAmount = (decimal) new Random().Next(); using (var testData = new NHTestDataGenerator(Factory.OpenSession())) { testData.Batch(actions => actions.CreateMonthlySalesSummaryForSalesPerson(1)); using (var scope = new UnitOfWorkScope()) { var repository = new NHRepository <MonthlySalesSummary>(); var report = (from summary in repository where summary.SalesPersonId == 1 select summary).SingleOrDefault(); report.TotalSale.Amount = newAmount; scope.Commit(); } using (var scope = new UnitOfWorkScope()) { var repository = new NHRepository <MonthlySalesSummary>(); var report = (from summary in repository where summary.SalesPersonId == 1 select summary).SingleOrDefault(); Assert.That(report.TotalSale.Amount, Is.EqualTo(newAmount)); scope.Commit(); } } }
public void Test_Can_Query_MonthlySalesSummary_Based_On_Currency() { IList <MonthlySalesSummary> report; using (var testData = new NHTestDataGenerator(Factory.OpenSession())) using (var scope = new UnitOfWorkScope()) { testData.Batch(actions => actions.CreateMonthlySalesSummaryWithAmount( new Money { Amount = 100, Currency = "YEN" } )); var repository = new NHRepository <MonthlySalesSummary>(); report = (from summary in repository where summary.TotalSale.Currency == "YEN" select summary).ToList(); scope.Commit(); } Assert.That(report, Is.Not.Null); Assert.That(report.Count, Is.GreaterThan(0)); report.ForEach(rep => { Assert.That(rep.TotalSale, Is.Not.Null); Assert.That(rep.TotalSale.Amount, Is.GreaterThan(0)); Assert.That(rep.TotalSale.Currency, Is.Not.Null); Assert.That(rep.TotalSale.Currency, Is.EqualTo("YEN")); }); }
public void Query_Using_Specifications_With_Closure_Works() { //This test demonstrates how closures can be used to modify a pre-defined specification using //parameters. The specification in this test searches for all customers in the state specified by //the queryState local variable. The test then proceeds to build a query using the specification //and enumerates over the states array and executes the query by changing the queryState parameter. var states = new[] { "PA", "LA" }; var queryState = string.Empty; var spec = new Specification <Order>((order) => order.Customer.Address.State == queryState); var repository = new NHRepository <Order>(); using (var testData = new NHTestDataGenerator(Factory.OpenSession())) using (new UnitOfWorkScope()) { testData.Batch(actions => { actions.CreateOrdersForCustomers(actions.CreateCustomersInState("PA", 2)); actions.CreateOrdersForCustomers(actions.CreateCustomersInState("DE", 5)); actions.CreateOrdersForCustomers(actions.CreateCustomersInState("LA", 3)); }); var query = repository.With(x => x.Customer).Query(spec); states.ForEach(testState => { queryState = testState; var results = query.ToArray(); results.ForEach(result => Assert.That(result.Customer.Address.State, Is.EqualTo(testState))); }); } }
public void Query_Allows_Projection_Using_Select_Projection() { using (var testData = new NHTestDataGenerator(Factory.OpenSession())) using (new UnitOfWorkScope()) { testData.Batch(actions => actions.CreateOrderForCustomer(actions.CreateCustomer())); var ordersRepository = new NHRepository <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), Is.False); Assert.That(string.IsNullOrEmpty(x.FirstName), Is.False); })); } }
public void Repository_For_Uses_Registered_Fetching_Strategies() { IEnumerable <Order> orders; using (var testData = new NHTestDataGenerator(Factory.OpenSession())) using (new UnitOfWorkScope()) { testData.Batch(actions => actions.CreateOrderForProducts(actions.CreateProducts(5))); var strategies = new IFetchingStrategy <Order, NHRepositoryTests>[] { new OrderOrderItemsStrategy(), new OrderItemsProductStrategy() }; ServiceLocator.Current.Expect(x => x.GetAllInstances <IFetchingStrategy <Order, NHRepositoryTests> >()) .Return(strategies); orders = new NHRepository <Order>() .For <NHRepositoryTests>() .ToList(); } orders.ForEach(order => { Assert.That(NHibernateUtil.IsInitialized(order.Items), Is.True); order.Items.ForEach(item => Assert.That(NHibernateUtil.IsInitialized(item.Product), Is.True)); }); }
public void UnitOfWork_Rolledback_When_Containing_TransactionScope_Is_Rolledback() { using (var testData = new NHTestDataGenerator(Factory.OpenSession())) { 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 NHRepository <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 (new UnitOfWorkScope()) { var ordersRepository = new NHRepository <Order>(); var order = (from o in ordersRepository where o.OrderID == orderId select o).First(); Assert.That(order.OrderDate, Is.EqualTo(oldDate)); } } }
public void Test_Can_Get_MonthlySalesSummary_With_Money_Type() { IList<MonthlySalesSummary> report; using (var testData = new NHTestDataGenerator(Factory.OpenSession())) using (var scope = new UnitOfWorkScope()) { testData.Batch(action => action.CreateMonthlySalesSummaryForMonth(1)); var repository = new NHRepository<MonthlySalesSummary>(); report = (from summary in repository where summary.Month == 1 select summary).ToList(); scope.Commit(); } Assert.That(report, Is.Not.Null); Assert.That(report.Count, Is.GreaterThan(0)); report.ForEach(rep => { Assert.That(rep.Month == 1); Assert.That(rep.TotalSale, Is.Not.Null); Assert.That(rep.TotalSale.Amount, Is.GreaterThan(0)); Assert.That(rep.TotalSale.Currency, Is.Not.Null); }); }
public void Test_Can_Get_MonthlySalesSummary_With_Money_Type() { IList <MonthlySalesSummary> report; using (var testData = new NHTestDataGenerator(Factory.OpenSession())) using (var scope = new UnitOfWorkScope()) { testData.Batch(action => action.CreateMonthlySalesSummaryForMonth(1)); var repository = new NHRepository <MonthlySalesSummary>(); report = (from summary in repository where summary.Month == 1 select summary).ToList(); scope.Commit(); } Assert.That(report, Is.Not.Null); Assert.That(report.Count, Is.GreaterThan(0)); report.ForEach(rep => { Assert.That(rep.Month == 1); Assert.That(rep.TotalSale, Is.Not.Null); Assert.That(rep.TotalSale.Amount, Is.GreaterThan(0)); Assert.That(rep.TotalSale.Currency, Is.Not.Null); }); }
public void When_Calling_CalculateTotal_On_Order_Returns_Valid_When_Under_UnitOfWork() { using (var testData = new NHTestDataGenerator(Factory.OpenSession())) using (new UnitOfWorkScope()) { testData.Batch(actions => actions.CreateOrderForProducts(actions.CreateProducts(5))); var oredersRepository = new NHRepository <Order>(); var order = (from o in oredersRepository select o).FirstOrDefault(); Assert.That(order.CalculateTotal(), Is.GreaterThan(0)); } }
public void when_ambient_transaction_is_running_and_a_previous_scope_rollsback_new_scope_still_works() { using (var testData = new NHTestDataGenerator(Factory.OpenSession())) { testData.Batch(actions => actions.CreateCustomer()); string oldCustomerName; var newCustomerName = "NewCustomer" + new Random().Next(0, int.MaxValue); var newCustomer = new Customer { FirstName = newCustomerName, LastName = "Save", Address = new Address { StreetAddress1 = "This record was inserted via a test", City = "Fictional City", State = "LA", ZipCode = "00000" } }; using (var ambientScope = new TransactionScope()) { using (var firstUOW = new UnitOfWorkScope()) { var customer = new NHRepository <Customer>().First(); oldCustomerName = customer.FirstName; customer.FirstName = "Changed"; } //Rollback using (var secondUOW = new UnitOfWorkScope()) { new NHRepository <Customer>().Add(newCustomer); secondUOW.Commit(); } } using (var scope = new UnitOfWorkScope()) { var repository = new NHRepository <Customer>(); Assert.That(repository.First().FirstName, Is.EqualTo(oldCustomerName)); Assert.That(repository.Where(x => x.FirstName == newCustomerName).Count(), Is.GreaterThan(0)); repository.Attach(newCustomer); repository.Delete(newCustomer); scope.Commit(); } } }
public void When_Calling_CalculateTotal_On_Order_Returns_Valid_With_No_UnitOfWork_Throws() { Order order; using (var testData = new NHTestDataGenerator(Factory.OpenSession())) using (new UnitOfWorkScope()) { testData.Batch(actions => actions.CreateOrderForProducts(actions.CreateProducts(5))); var oredersRepository = new NHRepository <Order>(); order = (from o in oredersRepository select o).FirstOrDefault(); } Assert.Throws <LazyInitializationException>(() => order.CalculateTotal()); }
public void When_No_FetchingStrategy_Registered_For_Makes_No_Changes() { Order order; using (var testData = new NHTestDataGenerator(Factory.OpenSession())) using (new UnitOfWorkScope()) { testData.Batch(actions => actions.CreateOrderForCustomer(actions.CreateCustomer())); var oredersRepository = new NHRepository <Order>().For <NHRepositoryTests>(); order = (from o in oredersRepository select o).FirstOrDefault(); } Assert.That(NHibernateUtil.IsInitialized(order.Customer), Is.False); }
public void Query_Allows_Eger_Loading_Using_With() { using (var testData = new NHTestDataGenerator(Factory.OpenSession())) { testData.Batch(actions => actions.CreateOrderForCustomer(actions.CreateCustomer())); List <Order> results; using (new UnitOfWorkScope()) { var ordersRepository = new NHRepository <Order>(); results = (from order in ordersRepository.With(x => x.Customer) select order).ToList(); } Assert.DoesNotThrow(() => results.ForEach(x => Assert.That(NHibernateUtil.IsInitialized(x.Customer)))); } }
public void NHUOW_Issue_6_Replication() { var readCustomerFunc = new Func <Customer>(() => { using (var scope = new UnitOfWorkScope()) { var customer = new NHRepository <Customer>().First(); scope.Commit(); return(customer); } }); var updateCustomerFunc = new Func <Customer, Customer>(customer => { using (var scope = new UnitOfWorkScope()) { var repository = new NHRepository <Customer>(); repository.Attach(customer); scope.Commit(); return(customer); } }); var newCustomerName = "Changed" + new Random().Next(0, int.MaxValue); using (var testData = new NHTestDataGenerator(Factory.OpenSession())) { testData.Batch(actions => actions.CreateCustomer()); using (var masterScope = new UnitOfWorkScope()) { using (var childScope = new UnitOfWorkScope(UnitOfWorkScopeTransactionOptions.CreateNew)) { var customer = readCustomerFunc(); customer.FirstName = newCustomerName; updateCustomerFunc(customer); childScope.Commit(); } } //Rollback var checkCustomer = readCustomerFunc(); Assert.That(checkCustomer.FirstName, Is.EqualTo(newCustomerName)); } }
public void Query_Allows_Lazy_Load_While_UnitOfWork_Still_Running() { using (var testData = new NHTestDataGenerator(Factory.OpenSession())) using (new UnitOfWorkScope()) { testData.Batch(actions => actions.CreateOrderForCustomer(actions.CreateCustomer())); var ordersRepository = new NHRepository <Order>(); var results = from order in ordersRepository select order; Assert.DoesNotThrow(() => results.ForEach(x => { Assert.That(NHibernateUtil.IsInitialized(x.Customer), Is.False); Assert.That(string.IsNullOrEmpty(x.Customer.FirstName), Is.False); })); } }
public void Query_Throws_Exception_When_LazyLoading_After_UnitOfWork_Is_Finished() { using (var testData = new NHTestDataGenerator(Factory.OpenSession())) { testData.Batch(actions => actions.CreateOrderForCustomer(actions.CreateCustomer())); Order order; using (new UnitOfWorkScope()) { var ordersRepository = new NHRepository <Order>(); order = (from orders in ordersRepository select orders).FirstOrDefault(); } Assert.That(order, Is.Not.Null); Assert.That(NHibernateUtil.IsInitialized(order.Customer), Is.False); Assert.Throws <LazyInitializationException>(() => { var firstName = order.Customer.FirstName; }); } }
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 NHTestDataGenerator(Factory.OpenSession())) { testData.Batch(actions => actions.CreateOrderForCustomer(actions.CreateCustomer())); int orderId; using (new UnitOfWorkScope()) { var ordersRepository = new NHRepository <Order>(); orderId = ordersRepository.Select(x => x.OrderID).First(); } Assert.NotNull(orderId); using (new UnitOfWorkScope()) { var outerRepository = new NHRepository <Order>(); var outerOrder = outerRepository.Where(x => x.OrderID == orderId).First(); outerOrder.OrderDate = changedOrderDate; using (var innerScope = new UnitOfWorkScope(UnitOfWorkScopeTransactionOptions.CreateNew)) { var innerRepository = new NHRepository <Order>(); var innerOrder = innerRepository.Where(x => x.OrderID == orderId).First(); innerOrder.ShipDate = changedShipDate; innerScope.Commit(); } } using (new UnitOfWorkScope()) { var ordersRepository = new NHRepository <Order>(); var order = ordersRepository.First(); Assert.That(order.OrderDate, Is.Not.EqualTo(changedOrderDate)); Assert.That(order.ShipDate, Is.Not.EqualTo(changedShipDate)); } } }
public void Query_Using_QueryMethod_Returns_Matched_Records_Only() { using (var testData = new NHTestDataGenerator(Factory.OpenSession())) using (new UnitOfWorkScope()) { testData.Batch(actions => { actions.CreateOrdersForCustomers(actions.CreateCustomersInState("PA", 2)); actions.CreateOrdersForCustomers(actions.CreateCustomersInState("DE", 5)); actions.CreateOrdersForCustomers(actions.CreateCustomersInState("LA", 3)); }); var customersInPA = new Specification <Order>(x => x.Customer.Address.State == "DE"); var ordersRepository = new NHRepository <Order>(); var results = from order in ordersRepository.Query(customersInPA) select order; Assert.That(results.Count(), Is.GreaterThan(0)); Assert.That(results.Count(), Is.EqualTo(5)); } }
public void changes_are_not_persisted_when_ambient_transaction_rolls_back() { using (var testData = new NHTestDataGenerator(Factory.OpenSession())) { testData.Batch(actions => actions.CreateCustomer()); using (var ambientScope = new TransactionScope()) { using (var scope = new UnitOfWorkScope()) { var customer = new NHRepository<Customer>().First(); customer.FirstName = "Changed"; scope.Commit(); } } //Auto rollback using (var scope = new UnitOfWorkScope()) { var customer = new NHRepository<Customer>().First(); Assert.That(customer.FirstName, Is.Not.EqualTo("Changed")); } } }
public void changes_are_not_persisted_when_ambient_transaction_rolls_back() { using (var testData = new NHTestDataGenerator(Factory.OpenSession())) { testData.Batch(actions => actions.CreateCustomer()); using (var ambientScope = new TransactionScope()) { using (var scope = new UnitOfWorkScope()) { var customer = new NHRepository <Customer>().First(); customer.FirstName = "Changed"; scope.Commit(); } } //Auto rollback using (var scope = new UnitOfWorkScope()) { var customer = new NHRepository <Customer>().First(); Assert.That(customer.FirstName, Is.Not.EqualTo("Changed")); } } }
public void when_ambient_transaction_is_running_multiple_scopes_work() { using (var testData = new NHTestDataGenerator(Factory.OpenSession())) { testData.Batch(actions => actions.CreateCustomerInState("LA")); using (var ambientScope = new TransactionScope()) { using (var firstUOW = new UnitOfWorkScope()) { var repository = new NHRepository <Customer>(); var query = repository.Where(x => x.Address.State == "LA"); Assert.That(query.Count(), Is.GreaterThan(0)); firstUOW.Commit(); } using (var secondUOW = new UnitOfWorkScope()) { var repository = new NHRepository <Customer>(); repository.Add(new Customer { FirstName = "NHUnitOfWorkTransactionTest", LastName = "Customer", Address = new Address { StreetAddress1 = "This recrd was insertd via a test", City = "Fictional City", State = "LA", ZipCode = "00000" } }); secondUOW.Commit(); } //Rolling back changes. } } }
public void Save_Updates_Existing_Order_Record() { var updatedDate = DateTime.Now; using (var testData = new NHTestDataGenerator(Factory.OpenSession())) { testData.Batch(actions => actions.CreateOrderForCustomer(actions.CreateCustomer())); int orderId; using (var scope = new UnitOfWorkScope()) { var orderRepository = new NHRepository <Order>(); var order = orderRepository.FirstOrDefault(); Assert.That(order, Is.Not.Null); orderId = order.OrderID; order.OrderDate = updatedDate; scope.Commit(); } using (new UnitOfWorkScope()) { var orderRepository = new NHRepository <Order>(); var order = (from o in orderRepository where o.OrderID == orderId 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)); } } }
public void Query_Using_Specifications_With_Closure_Works() { //This test demonstrates how closures can be used to modify a pre-defined specification using //parameters. The specification in this test searches for all customers in the state specified by //the queryState local variable. The test then proceeds to build a query using the specification //and enumerates over the states array and executes the query by changing the queryState parameter. var states = new[] {"PA", "LA"}; var queryState = string.Empty; var spec = new Specification<Order>((order) => order.Customer.Address.State == queryState); var repository = new NHRepository<Order>(); using (var testData = new NHTestDataGenerator(Factory.OpenSession())) using (new UnitOfWorkScope()) { testData.Batch(actions => { actions.CreateOrdersForCustomers(actions.CreateCustomersInState("PA", 2)); actions.CreateOrdersForCustomers(actions.CreateCustomersInState("DE", 5)); actions.CreateOrdersForCustomers(actions.CreateCustomersInState("LA", 3)); }); var query = repository.With(x => x.Customer).Query(spec); states.ForEach(testState => { queryState = testState; var results = query.ToArray(); results.ForEach(result => Assert.That(result.Customer.Address.State, Is.EqualTo(testState))); }); } }
public void When_Calling_CalculateTotal_On_Order_Returns_Valid_When_Under_UnitOfWork() { using (var testData = new NHTestDataGenerator(Factory.OpenSession())) using (new UnitOfWorkScope()) { testData.Batch(actions => actions.CreateOrderForProducts(actions.CreateProducts(5))); var oredersRepository = new NHRepository<Order>(); var order = (from o in oredersRepository select o).FirstOrDefault(); Assert.That(order.CalculateTotal(), Is.GreaterThan(0)); } }
public void When_Calling_CalculateTotal_On_Order_Returns_Valid_With_No_UnitOfWork_Throws() { Order order; using (var testData = new NHTestDataGenerator(Factory.OpenSession())) using (new UnitOfWorkScope()) { testData.Batch(actions => actions.CreateOrderForProducts(actions.CreateProducts(5))); var oredersRepository = new NHRepository<Order>(); order = (from o in oredersRepository select o).FirstOrDefault(); } Assert.Throws<LazyInitializationException>(() => order.CalculateTotal()); }
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 NHTestDataGenerator(Factory.OpenSession())) { testData.Batch(actions => actions.CreateOrderForCustomer(actions.CreateCustomer())); int orderId; using (new UnitOfWorkScope()) { var ordersRepository = new NHRepository<Order>(); orderId = ordersRepository.Select(x => x.OrderID).First(); } Assert.NotNull(orderId); using (new UnitOfWorkScope()) { var outerRepository = new NHRepository<Order>(); var outerOrder = outerRepository.Where(x => x.OrderID == orderId).First(); outerOrder.OrderDate = changedOrderDate; using (var innerScope = new UnitOfWorkScope(UnitOfWorkScopeTransactionOptions.CreateNew)) { var innerRepository = new NHRepository<Order>(); var innerOrder = innerRepository.Where(x => x.OrderID == orderId).First(); innerOrder.ShipDate = changedShipDate; innerScope.Commit(); } } using (new UnitOfWorkScope()) { var ordersRepository = new NHRepository<Order>(); var order = ordersRepository.First(); Assert.That(order.OrderDate, Is.Not.EqualTo(changedOrderDate)); Assert.That(order.ShipDate, Is.Not.EqualTo(changedShipDate)); } } }
public void Query_Allows_Eger_Loading_Using_With() { using (var testData = new NHTestDataGenerator(Factory.OpenSession())) { testData.Batch(actions => actions.CreateOrderForCustomer(actions.CreateCustomer())); List<Order> results; using (new UnitOfWorkScope()) { var ordersRepository = new NHRepository<Order>(); results = (from order in ordersRepository.With(x => x.Customer) select order).ToList(); } Assert.DoesNotThrow(() => results.ForEach(x => Assert.That(NHibernateUtil.IsInitialized(x.Customer)))); } }
public void Query_Using_QueryMethod_Returns_Matched_Records_Only() { using (var testData = new NHTestDataGenerator(Factory.OpenSession())) using (new UnitOfWorkScope()) { testData.Batch(actions => { actions.CreateOrdersForCustomers(actions.CreateCustomersInState("PA", 2)); actions.CreateOrdersForCustomers(actions.CreateCustomersInState("DE", 5)); actions.CreateOrdersForCustomers(actions.CreateCustomersInState("LA", 3)); }); var customersInPA = new Specification<Order>(x => x.Customer.Address.State == "DE"); var ordersRepository = new NHRepository<Order>(); var results = from order in ordersRepository.Query(customersInPA) select order; Assert.That(results.Count(), Is.GreaterThan(0)); Assert.That(results.Count(), Is.EqualTo(5)); } }
public void UnitOfWork_Rolledback_When_Containing_TransactionScope_Is_Rolledback() { using (var testData = new NHTestDataGenerator(Factory.OpenSession())) { 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 NHRepository<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 (new UnitOfWorkScope()) { var ordersRepository = new NHRepository<Order>(); var order = (from o in ordersRepository where o.OrderID == orderId select o).First(); Assert.That(order.OrderDate, Is.EqualTo(oldDate)); } } }
public void when_ambient_transaction_is_running_and_a_previous_scope_rollsback_new_scope_still_works() { using (var testData = new NHTestDataGenerator(Factory.OpenSession())) { testData.Batch(actions => actions.CreateCustomer()); string oldCustomerName; var newCustomerName = "NewCustomer" + new Random().Next(0, int.MaxValue); var newCustomer = new Customer { FirstName = newCustomerName, LastName = "Save", Address = new Address { StreetAddress1 = "This record was inserted via a test", City = "Fictional City", State = "LA", ZipCode = "00000" } }; using (var ambientScope = new TransactionScope()) { using (var firstUOW = new UnitOfWorkScope()) { var customer = new NHRepository<Customer>().First(); oldCustomerName = customer.FirstName; customer.FirstName = "Changed"; } //Rollback using (var secondUOW = new UnitOfWorkScope()) { new NHRepository<Customer>().Add(newCustomer); secondUOW.Commit(); } } using (var scope = new UnitOfWorkScope()) { var repository = new NHRepository<Customer>(); Assert.That(repository.First().FirstName, Is.EqualTo(oldCustomerName)); Assert.That(repository.Where(x => x.FirstName == newCustomerName).Count(), Is.GreaterThan(0)); repository.Attach(newCustomer); repository.Delete(newCustomer); scope.Commit(); } } }
public void When_No_FetchingStrategy_Registered_For_Makes_No_Changes() { Order order; using (var testData = new NHTestDataGenerator(Factory.OpenSession())) using (new UnitOfWorkScope()) { testData.Batch(actions => actions.CreateOrderForCustomer(actions.CreateCustomer())); var oredersRepository = new NHRepository<Order>().For<NHRepositoryTests>(); order = (from o in oredersRepository select o).FirstOrDefault(); } Assert.That(NHibernateUtil.IsInitialized(order.Customer), Is.False); }
public void Query_Allows_Projection_Using_Select_Projection() { using (var testData = new NHTestDataGenerator(Factory.OpenSession())) using (new UnitOfWorkScope()) { testData.Batch(actions => actions.CreateOrderForCustomer(actions.CreateCustomer())); var ordersRepository = new NHRepository<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), Is.False); Assert.That(string.IsNullOrEmpty(x.FirstName), Is.False); })); } }
public void Query_Throws_Exception_When_LazyLoading_After_UnitOfWork_Is_Finished() { using (var testData = new NHTestDataGenerator(Factory.OpenSession())) { testData.Batch(actions => actions.CreateOrderForCustomer(actions.CreateCustomer())); Order order; using (new UnitOfWorkScope()) { var ordersRepository = new NHRepository<Order>(); order = (from orders in ordersRepository select orders).FirstOrDefault(); } Assert.That(order, Is.Not.Null); Assert.That(NHibernateUtil.IsInitialized(order.Customer), Is.False); Assert.Throws<LazyInitializationException>(() => { var firstName = order.Customer.FirstName; }); } }
public void Repository_For_Uses_Registered_Fetching_Strategies() { IEnumerable<Order> orders; using (var testData = new NHTestDataGenerator(Factory.OpenSession())) using (new UnitOfWorkScope()) { testData.Batch(actions => actions.CreateOrderForProducts(actions.CreateProducts(5))); var strategies = new IFetchingStrategy<Order, NHRepositoryTests>[] { new OrderOrderItemsStrategy(), new OrderItemsProductStrategy() }; ServiceLocator.Current.Expect(x => x.GetAllInstances<IFetchingStrategy<Order, NHRepositoryTests>>()) .Return(strategies); orders = new NHRepository<Order>() .For<NHRepositoryTests>() .ToList(); } orders.ForEach(order => { Assert.That(NHibernateUtil.IsInitialized(order.Items), Is.True); order.Items.ForEach(item => Assert.That(NHibernateUtil.IsInitialized(item.Product), Is.True)); }); }
public void when_ambient_transaction_is_running_multiple_scopes_work() { using (var testData = new NHTestDataGenerator(Factory.OpenSession())) { testData.Batch(actions => actions.CreateCustomerInState("LA")); using (var ambientScope = new TransactionScope()) { using (var firstUOW = new UnitOfWorkScope()) { var repository = new NHRepository<Customer>(); var query = repository.Where(x => x.Address.State == "LA"); Assert.That(query.Count(), Is.GreaterThan(0)); firstUOW.Commit(); } using (var secondUOW = new UnitOfWorkScope()) { var repository = new NHRepository<Customer>(); repository.Add(new Customer { FirstName = "NHUnitOfWorkTransactionTest", LastName = "Customer", Address = new Address { StreetAddress1 = "This recrd was insertd via a test", City = "Fictional City", State = "LA", ZipCode = "00000" } }); secondUOW.Commit(); } //Rolling back changes. } } }
public void Query_Allows_Lazy_Load_While_UnitOfWork_Still_Running() { using (var testData = new NHTestDataGenerator(Factory.OpenSession())) using (new UnitOfWorkScope()) { testData.Batch(actions => actions.CreateOrderForCustomer(actions.CreateCustomer())); var ordersRepository = new NHRepository<Order>(); var results = from order in ordersRepository select order; Assert.DoesNotThrow(() => results.ForEach(x => { Assert.That(NHibernateUtil.IsInitialized(x.Customer), Is.False); Assert.That(string.IsNullOrEmpty(x.Customer.FirstName), Is.False); })); } }
public void NHUOW_Issue_6_Replication() { var readCustomerFunc = new Func<Customer>(() => { using (var scope = new UnitOfWorkScope()) { var customer = new NHRepository<Customer>().First(); scope.Commit(); return customer; } }); var updateCustomerFunc = new Func<Customer, Customer>(customer => { using (var scope = new UnitOfWorkScope()) { var repository = new NHRepository<Customer>(); repository.Attach(customer); scope.Commit(); return customer; } }); var newCustomerName = "Changed" + new Random().Next(0, int.MaxValue); using (var testData = new NHTestDataGenerator(Factory.OpenSession())) { testData.Batch(actions => actions.CreateCustomer()); using (var masterScope = new UnitOfWorkScope()) { using (var childScope = new UnitOfWorkScope(UnitOfWorkScopeTransactionOptions.CreateNew)) { var customer = readCustomerFunc(); customer.FirstName = newCustomerName; updateCustomerFunc(customer); childScope.Commit(); } } //Rollback var checkCustomer = readCustomerFunc(); Assert.That(checkCustomer.FirstName, Is.EqualTo(newCustomerName)); } }
public void Save_Updates_Existing_Order_Record() { var updatedDate = DateTime.Now; using (var testData = new NHTestDataGenerator(Factory.OpenSession())) { testData.Batch(actions => actions.CreateOrderForCustomer(actions.CreateCustomer())); int orderId; using (var scope = new UnitOfWorkScope()) { var orderRepository = new NHRepository<Order>(); var order = orderRepository.FirstOrDefault(); Assert.That(order, Is.Not.Null); orderId = order.OrderID; order.OrderDate = updatedDate; scope.Commit(); } using (new UnitOfWorkScope()) { var orderRepository = new NHRepository<Order>(); var order = (from o in orderRepository where o.OrderID == orderId 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)); } } }