public void UnitOfWork_can_rollback() { // Generate Test Data _context = new TestDbContext(this.Configuration); var testData = new EFTestData(_context); var testDataActions = new EFTestDataActions(testData); Customer customer = null; testData.Batch(action => customer = action.CreateCustomer()); // Setup required services var scopeFactory = this.ServiceProvider.GetService <IUnitOfWorkScopeFactory>(); var repo = this.ServiceProvider.GetService <IEagerFetchingRepository <Customer> >(); repo.DataStoreName = "TestDbContext"; //repo.Add(customer); using (var scope = scopeFactory.Create(TransactionMode.Default)) { customer = repo.Find(customer.CustomerId); customer.LastName = "Changed"; repo.Update(customer); } //Dispose here as scope is not comitted. Customer savedCustomer = null; savedCustomer = testDataActions.GetCustomerById(customer.CustomerId); Assert.AreNotEqual(customer.LastName, savedCustomer.LastName); }
public void Batch(Action <EFTestDataActions> action) { var dataActions = new EFTestDataActions(this); action(dataActions); _context.SaveChanges(); }
public async Task Can_eager_load_repository_and_query_async() { _context = new TestDbContext(this.Configuration); var testData = new EFTestData(_context); var testDataActions = new EFTestDataActions(testData); var customer = testDataActions.CreateCustomer(); for (int i = 0; i < 10; i++) { testDataActions.CreateOrderForCustomer(customer); } var repo = this.ServiceProvider.GetService <IEagerFetchingRepository <Customer> >(); repo.EagerlyWith(x => x.Orders); repo.DataStoreName = "TestDbContext"; //var repo = this.ServiceProvider.GetService<IEFCoreRepository<Customer>>(); var savedCustomer = await repo .FindSingleOrDefaultAsync(x => x.CustomerId == customer.CustomerId); Assert.IsNotNull(savedCustomer); Assert.IsTrue(savedCustomer.CustomerId == customer.CustomerId); Assert.IsTrue(savedCustomer.Orders != null); Assert.IsTrue(savedCustomer.Orders.Count == 10); }
public void UnitOfWork_Can_commit() { // Generate Test Data _context = new TestDbContext(this.Configuration); var testData = new EFTestData(_context); var testDataActions = new EFTestDataActions(testData); Customer customer = testDataActions.CreateCustomerStub(); // Setup required services var scopeFactory = this.ServiceProvider.GetService <IUnitOfWorkScopeFactory>(); // Start Test using (var scope = scopeFactory.Create()) { var repo = this.ServiceProvider.GetService <IEagerFetchingRepository <Customer> >(); repo.DataStoreName = "TestDbContext"; repo.Add(customer); scope.Commit(); } Customer savedCustomer = null; savedCustomer = testDataActions.GetCustomerById(customer.CustomerId); Assert.IsNotNull(savedCustomer); Assert.AreEqual(savedCustomer.CustomerId, customer.CustomerId); }
public void UnitOfWork_nested_commit_with_seperate_transaction_commits_when_wrapping_scope_rollsback() { // Generate Test Data this.Logger.LogInformation("Generating Test Data for: " + MethodBase.GetCurrentMethod(), null); _context = new TestDbContext(this.Configuration); var testData = new EFTestData(_context); var testDataActions = new EFTestDataActions(testData); // Setup required services var scopeFactory = this.ServiceProvider.GetService <IUnitOfWorkScopeFactory>(); Customer customer = null; testData.Batch(x => customer = x.CreateCustomerStub()); var order = new Order { OrderDate = DateTime.Now, ShipDate = DateTime.Now }; this.Logger.LogInformation("Starting initial UnitOfWorkScope from " + MethodBase.GetCurrentMethod(), null); using (var scope = scopeFactory.Create(TransactionMode.Default)) { var repo = this.ServiceProvider.GetService <IEagerFetchingRepository <Customer> >(); repo.DataStoreName = "TestDbContext"; this.Logger.LogInformation("Adding New Customer from first UnitOfWorkScope ", customer); repo.Add(customer); this.Logger.LogInformation("Starting new UnitOfWorkScope from " + MethodBase.GetCurrentMethod(), null); using (var scope2 = scopeFactory.Create(TransactionMode.New)) { var repo2 = this.ServiceProvider.GetService <IEagerFetchingRepository <Order> >(); repo2.DataStoreName = "TestDbContext"; this.Logger.LogInformation("Adding New Order from first UnitOfWorkScope ", order); repo2.Add(order); this.Logger.LogInformation("Attempting to Commit second(new) UnitOfWorkScope ", scope2); scope2.Commit(); } } //Rollback this.Logger.LogInformation("Attempting to Rollback back initial UnitofWorkScope ", null); Customer savedCustomer = null; Order savedOrder = null; savedCustomer = testDataActions.GetCustomerById(customer.CustomerId); savedOrder = testDataActions.GetOrderById(order.OrderId); Assert.IsNull(savedCustomer); Assert.IsNotNull(savedOrder); Assert.IsTrue(customer.CustomerId == 0); // First transaction does not commit Assert.AreEqual(order.OrderId, savedOrder.OrderId); // Second transaction does commit because it is marked "new" }
public void Can_perform_simple_query() { _context = new TestDbContext(this.Configuration); var testData = new EFTestData(_context); var testDataActions = new EFTestDataActions(testData); var customer = testDataActions.CreateCustomer(x => x.FirstName = "Albus"); var repo = this.ServiceProvider.GetService <IEagerFetchingRepository <Customer> >(); repo.DataStoreName = "TestDbContext"; //var repo = this.ServiceProvider.GetService<IEFCoreRepository<Customer>>(); var savedCustomer = repo .Find(customer.CustomerId); Assert.IsNotNull(savedCustomer); Assert.IsTrue(savedCustomer.CustomerId == customer.CustomerId); Assert.IsTrue(savedCustomer.FirstName == "Albus"); }
public void UnitOfWork_nested_rollback_works() { var testData = new EFTestData(_context); var testDataActions = new EFTestDataActions(testData); var customer = new Customer { FirstName = "Joe", LastName = "Data" }; var order = new Order { OrderDate = DateTime.Now, ShipDate = DateTime.Now }; // Setup required services var scopeFactory = this.ServiceProvider.GetService <IUnitOfWorkScopeFactory>(); using (var scope = scopeFactory.Create(TransactionMode.Default)) { var repo = this.ServiceProvider.GetService <IEagerFetchingRepository <Customer> >(); repo.DataStoreName = "TestDbContext"; repo.Add(customer); using (var scope2 = scopeFactory.Create(TransactionMode.Default)) { var repo2 = this.ServiceProvider.GetService <IEagerFetchingRepository <Order> >(); repo2.DataStoreName = "TestDbContext"; repo2.Add(order); scope2.Commit(); } } //Rollback. Customer savedCustomer = null; Order savedOrder = null; testData.Batch(actions => { savedCustomer = actions.GetCustomerById(customer.CustomerId); savedOrder = actions.GetOrderById(order.OrderId); }); Assert.IsNull(savedCustomer); Assert.IsNull(savedOrder); }
public async Task Can_Delete_Async() { // Generate Test Data _context = new TestDbContext(this.Configuration); var testData = new EFTestData(_context); var testDataActions = new EFTestDataActions(testData); Customer customer = testDataActions.CreateCustomer(); // Start Test var repo = this.ServiceProvider.GetService <IEagerFetchingRepository <Customer> >(); repo.DataStoreName = "TestDbContext"; await repo.DeleteAsync(customer); Customer savedCustomer = null; savedCustomer = testDataActions.GetCustomerById(customer.CustomerId); Assert.IsNull(savedCustomer); }
public void UnitOfWork_nested_commit_works() { // Generate Test Data _context = new TestDbContext(this.Configuration); var testData = new EFTestData(_context); var testDataActions = new EFTestDataActions(testData); var customer = testDataActions.CreateCustomerStub(); var order = testDataActions.CreateOrderStub(); // Setup required services var scopeFactory = this.ServiceProvider.GetService <IUnitOfWorkScopeFactory>(); using (var scope = scopeFactory.Create(TransactionMode.Default)) { var repo = this.ServiceProvider.GetService <IEagerFetchingRepository <Customer> >(); repo.DataStoreName = "TestDbContext"; repo.Add(customer); //scope.Commit(); using (var scope2 = scopeFactory.Create(TransactionMode.Default)) { var repo2 = this.ServiceProvider.GetService <IEagerFetchingRepository <Order> >(); repo2.DataStoreName = "TestDbContext"; repo2.Add(order); scope2.Commit(); } scope.Commit(); } Customer savedCustomer = null; Order savedOrder = null; savedCustomer = testDataActions.GetCustomerById(customer.CustomerId); savedOrder = testDataActions.GetOrderById(order.OrderId); Assert.IsNotNull(savedCustomer); Assert.AreEqual(customer.CustomerId, savedCustomer.CustomerId); Assert.IsNotNull(savedOrder); Assert.AreEqual(order.OrderId, savedOrder.OrderId); }
public async Task Can_Add_Async() { // Generate Test Data _context = new TestDbContext(this.Configuration); var testData = new EFTestData(_context); var testDataActions = new EFTestDataActions(testData); Customer customer = testDataActions.CreateCustomerStub(x => x.FirstName = "Severnus"); // Start Test var repo = this.ServiceProvider.GetService <IEagerFetchingRepository <Customer> >(); repo.DataStoreName = "TestDbContext"; await repo.AddAsync(customer); Customer savedCustomer = null; savedCustomer = testDataActions.GetFirstCustomer(x => x.FirstName == "Severnus"); Assert.IsNotNull(savedCustomer); Assert.AreEqual(savedCustomer.FirstName, customer.FirstName); }
public void Can_Add_Entity() { // Generate Test Data _context = new TestDbContext(this.Configuration); var testData = new EFTestData(_context); var testDataActions = new EFTestDataActions(testData); Customer customer = testDataActions.CreateCustomerStub(); // Start Test var repo = this.ServiceProvider.GetService <IEagerFetchingRepository <Customer> >(); repo.DataStoreName = "TestDbContext"; repo.Add(customer); Customer savedCustomer = null; savedCustomer = testDataActions.GetCustomerById(customer.CustomerId); Assert.IsNotNull(savedCustomer); Assert.AreEqual(savedCustomer.CustomerId, customer.CustomerId); }