public void UnitOfWork_Rolledback_When_Containing_TransactionScope_Is_Rolledback() { using (var testData = new Db4oTestDataGenerator(_db4oServer.OpenClient())) { 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 Db4oRepository <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 Db4oRepository <Order>(); var order = (from o in ordersRepository where o.OrderID == orderId select o).First(); Assert.That(order.OrderDate, Is.EqualTo(oldDate)); } } }
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 Db4oRepository <Order>(); using (var testData = new Db4oTestDataGenerator(_db4oServer.OpenClient())) 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 Db4oTestDataGenerator(_db4oServer.OpenClient())) using (new UnitOfWorkScope()) { testData.Batch(actions => actions.CreateOrderForCustomer(actions.CreateCustomer())); var ordersRepository = new Db4oRepository <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 Nested_UnitOfWork_With_Different_Transaction_Compatibility_Works() { var changedShipDate = DateTime.Now.AddDays(1); var changedOrderDate = DateTime.Now.AddDays(2); using (var testData = new Db4oTestDataGenerator(_db4oServer.OpenClient())) { testData.Batch(actions => actions.CreateOrderForCustomer(actions.CreateCustomer())); int orderId; using (new UnitOfWorkScope()) { var ordersRepository = new Db4oRepository <Order>(); orderId = ordersRepository.Select(x => x.OrderID).First(); } Assert.NotNull(orderId); using (new UnitOfWorkScope()) { var outerRepository = new Db4oRepository <Order>(); var outerOrder = outerRepository.Where(x => x.OrderID == orderId).First(); outerOrder.OrderDate = changedOrderDate; using (var innerScope = new UnitOfWorkScope(UnitOfWorkScopeTransactionOptions.CreateNew)) { var innerRepository = new Db4oRepository <Order>(); var innerOrder = innerRepository.Where(x => x.OrderID == orderId).First(); innerOrder.ShipDate = changedShipDate; innerScope.Commit(); } } using (new UnitOfWorkScope()) { var ordersRepository = new Db4oRepository <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 Db4oTestDataGenerator(_db4oServer.OpenClient())) 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 Db4oRepository <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 Save_Updates_Existing_Order_Record() { var updatedDate = DateTime.Now; using (var testData = new Db4oTestDataGenerator(_db4oServer.OpenClient())) { testData.Batch(actions => actions.CreateOrderForCustomer(actions.CreateCustomer())); int orderId; using (var scope = new UnitOfWorkScope()) { var orderRepository = new Db4oRepository <Order>(); var order = orderRepository.FirstOrDefault(); Assert.That(order, Is.Not.Null); orderId = order.OrderID; order.OrderDate = updatedDate; orderRepository.Save(order); //Db4o does not do change tracking! scope.Commit(); } using (new UnitOfWorkScope()) { var orderRepository = new Db4oRepository <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 Nested_UnitOfWork_With_Different_Transaction_Compatibility_Works() { var changedShipDate = DateTime.Now.AddDays(1); var changedOrderDate = DateTime.Now.AddDays(2); using (var testData = new Db4oTestDataGenerator(_db4oServer.OpenClient())) { testData.Batch(actions => actions.CreateOrderForCustomer(actions.CreateCustomer())); int orderId; using (new UnitOfWorkScope()) { var ordersRepository = new Db4oRepository<Order>(); orderId = ordersRepository.Select(x => x.OrderID).First(); } Assert.NotNull(orderId); using (new UnitOfWorkScope()) { var outerRepository = new Db4oRepository<Order>(); var outerOrder = outerRepository.Where(x => x.OrderID == orderId).First(); outerOrder.OrderDate = changedOrderDate; using (var innerScope = new UnitOfWorkScope(UnitOfWorkScopeTransactionOptions.CreateNew)) { var innerRepository = new Db4oRepository<Order>(); var innerOrder = innerRepository.Where(x => x.OrderID == orderId).First(); innerOrder.ShipDate = changedShipDate; innerScope.Commit(); } } using (new UnitOfWorkScope()) { var ordersRepository = new Db4oRepository<Order>(); var order = ordersRepository.First(); Assert.That(order.OrderDate, Is.Not.EqualTo(changedOrderDate)); Assert.That(order.ShipDate, Is.Not.EqualTo(changedShipDate)); } } }
public void UnitOfWork_Rolledback_When_Containing_TransactionScope_Is_Rolledback() { using (var testData = new Db4oTestDataGenerator(_db4oServer.OpenClient())) { 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 Db4oRepository<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 Db4oRepository<Order>(); var order = (from o in ordersRepository where o.OrderID == orderId select o).First(); Assert.That(order.OrderDate, Is.EqualTo(oldDate)); } } }
public void Save_Updates_Existing_Order_Record() { var updatedDate = DateTime.Now; using (var testData = new Db4oTestDataGenerator(_db4oServer.OpenClient())) { testData.Batch(actions => actions.CreateOrderForCustomer(actions.CreateCustomer())); int orderId; using (var scope = new UnitOfWorkScope()) { var orderRepository = new Db4oRepository<Order>(); var order = orderRepository.FirstOrDefault(); Assert.That(order, Is.Not.Null); orderId = order.OrderID; order.OrderDate = updatedDate; orderRepository.Save(order); //Db4o does not do change tracking! scope.Commit(); } using (new UnitOfWorkScope()) { var orderRepository = new Db4oRepository<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 Db4oRepository<Order>(); using (var testData = new Db4oTestDataGenerator(_db4oServer.OpenClient())) 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_Using_QueryMethod_Returns_Matched_Records_Only() { using (var testData = new Db4oTestDataGenerator(_db4oServer.OpenClient())) 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 Db4oRepository<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 Query_Allows_Projection_Using_Select_Projection() { using (var testData = new Db4oTestDataGenerator(_db4oServer.OpenClient())) using (new UnitOfWorkScope()) { testData.Batch(actions => actions.CreateOrderForCustomer(actions.CreateCustomer())); var ordersRepository = new Db4oRepository<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); })); } }