public void CreateUserRollbackShouldCorrect() { // arrange var newUser = new UserEntity { Code = "FromUnitTest", Password = "******", IsActive = false }; UserEntity insertedUser; int originUserCount, tempUserCount, finalUserCount; // action using (var connection = new MySqlConnection(ConnectionString)) { connection.Open(); using (var transaction = connection.BeginTransaction()) { var sut = new MariaDbRepository(connection); originUserCount = sut.GetUsers().Count(); insertedUser = sut.CreateUser(newUser); tempUserCount = sut.GetUsers().Count(); if (transaction.Connection != null) { transaction.Rollback(); } finalUserCount = sut.GetUsers().Count(); } } // assert Assert.NotZero(insertedUser.Id); Assert.AreEqual(originUserCount + 1, tempUserCount); Assert.AreEqual(originUserCount, finalUserCount); }
public void GetUserRollbackShouldCorrect() { // arrange UserEntity firstUser; // action using (var connection = new MySqlConnection(ConnectionString)) { connection.Open(); using (var transaction = connection.BeginTransaction()) { var sut = new MariaDbRepository(connection); var result = sut.GetUsers(); firstUser = result.FirstOrDefault(); if (transaction.Connection != null) { transaction.Rollback(); } } } // assert Assert.IsNotNull(firstUser); Assert.AreEqual("Admin", firstUser.Code); }
public void InsertUserAndProductRollbackInTransactionScope() { // arrange var newUser = new UserEntity { Code = "UnitTest_Scope", Password = "******", IsActive = false }; var newProduct = new ProductEntity { Name = nameof(CreateProductShouldCorrect), Amount = 10, AccountId = 2 }; UserEntity insertedUser; ProductEntity insertedProduct; int originUserCount, tempUserCount, finalUserCount; int originProductCount, tempProductCount, finalProductCount; // action using (var transactionScope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = IsolationLevel.RepeatableRead }, TransactionScopeAsyncFlowOption.Enabled)) { var userRepo = new MariaDbRepository(); var productRepo = new MariaDbRepository(); originUserCount = userRepo.GetUsers().Count(); insertedUser = userRepo.CreateUser(newUser); tempUserCount = userRepo.GetUsers().Count(); originProductCount = productRepo.GetProducts().Count(); insertedProduct = productRepo.CreateProduct(newProduct); tempProductCount = productRepo.GetProducts().Count(); transactionScope.Dispose(); finalUserCount = userRepo.GetUsers().Count(); finalProductCount = productRepo.GetProducts().Count(); } // assert Assert.NotZero(insertedUser.Id); Assert.AreEqual(originUserCount + 1, tempUserCount); Assert.AreEqual(originUserCount, finalUserCount); Assert.NotZero(insertedProduct.Id); Assert.AreEqual(originProductCount + 1, tempProductCount); Assert.AreEqual(originProductCount, finalProductCount); }
public void GetUserShouldCorrect() { // arrange var sut = new MariaDbRepository(); // action var result = sut.GetUsers(); var firstUser = result.FirstOrDefault(); // assert Assert.IsNotNull(firstUser); Assert.AreEqual("Admin", firstUser.Code); }
public void NestedScopeInnerTransactionRollbackShouldRollback() { // arrange var originPassword = "******"; var expectedPassword = "******"; var newUser2 = new UserEntity { Id = 2, Password = expectedPassword, IsActive = true }; var newUser3 = new UserEntity { Id = 3, Password = expectedPassword, IsActive = true }; // action try { using (new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = IsolationLevel.ReadCommitted }, TransactionScopeAsyncFlowOption.Enabled)) { var userRepoA = new MariaDbRepository(); userRepoA.UpdateUser(newUser2); using (var connection = new MySqlConnection(ConnectionString)) { connection.Open(); using (var transaction = connection.BeginTransaction()) { var userRepoB = new MariaDbRepository(connection); userRepoB.UpdateUser(newUser3); transaction.Rollback(); } } } } catch (TransactionAbortedException) { Console.WriteLine("transaction rollback!"); } var repo = new MariaDbRepository(); var users = repo.GetUsers().Where(u => u.Id == 2 || u.Id == 3).ToArray(); var finalPasswordUser2 = users.FirstOrDefault(u => u.Id == 2)?.Password; var finalPasswordUser3 = users.FirstOrDefault(u => u.Id == 3)?.Password; // assert Assert.AreEqual(originPassword, finalPasswordUser2); Assert.AreEqual(originPassword, finalPasswordUser3); }
public void UpdateUserReadDirtyData() { // arrange var originPassword = "******"; var expectedPassword = "******"; var newUser = new UserEntity { Id = 2, Password = expectedPassword, IsActive = true }; var dirtyPassword = string.Empty; string finalPassword; // action var waiterUpdate = new AutoResetEvent(false); Task.Factory.StartNew(() => { waiterUpdate.WaitOne(); using (var suppressedScope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = IsolationLevel.ReadUncommitted })) { var userRepoB = new MariaDbRepository(); dirtyPassword = userRepoB.GetUsers().FirstOrDefault(u => u.Id == 2)?.Password; suppressedScope.Complete(); } waiterUpdate.Set(); }); using (var transactionScope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = IsolationLevel.ReadCommitted }, TransactionScopeAsyncFlowOption.Enabled)) { var userRepoA = new MariaDbRepository(); userRepoA.UpdateUser(newUser); waiterUpdate.Set(); waiterUpdate.Reset(); waiterUpdate.WaitOne(); transactionScope.Dispose(); finalPassword = userRepoA.GetUsers().FirstOrDefault(u => u.Id == 2)?.Password; } // assert Assert.AreEqual(newUser.Password, dirtyPassword); Assert.AreEqual(originPassword, finalPassword); }
public void UpdateUserRollbackShouldCorrect() { // arrange var originPassword = "******"; var expectedPassword = "******"; var newUser = new UserEntity { Id = 2, Password = expectedPassword, IsActive = true }; UserEntity originUser, tempUser, finalUser; // action using (var connection = new MySqlConnection(ConnectionString)) { connection.Open(); using (var transaction = connection.BeginTransaction()) { var sut = new MariaDbRepository(connection); originUser = sut.GetUsers().FirstOrDefault(u => u.Id == newUser.Id); Assert.DoesNotThrow(() => sut.UpdateUser(newUser)); tempUser = sut.GetUsers().FirstOrDefault(u => u.Id == newUser.Id); if (transaction.Connection != null) { transaction.Rollback(); } finalUser = sut.GetUsers().FirstOrDefault(u => u.Id == newUser.Id); } } // assert Assert.NotNull(originUser); Assert.NotNull(tempUser); Assert.NotNull(finalUser); Assert.AreEqual(originPassword, originUser.Password); Assert.AreEqual(expectedPassword, tempUser.Password); Assert.AreEqual(originUser.Password, finalUser.Password); }
public void ReadDirtyDataWithNestedScope() { // arrange var originPassword = "******"; var expectedPassword = "******"; var newUser = new UserEntity { Id = 2, Password = expectedPassword, IsActive = true }; string dirtyPassword; string finalPassword; // action using (var transactionScope = new TransactionScope(TransactionScopeOption.RequiresNew, new TransactionOptions { IsolationLevel = IsolationLevel.ReadCommitted }, TransactionScopeAsyncFlowOption.Enabled)) { var userRepoA = new MariaDbRepository(); userRepoA.UpdateUser(newUser); using (var suppressedScope = new TransactionScope( TransactionScopeOption.RequiresNew, // can't use Suppress or Required new TransactionOptions { IsolationLevel = IsolationLevel.ReadUncommitted })) { var userRepoB = new MariaDbRepository(); dirtyPassword = userRepoB.GetUsers().FirstOrDefault(u => u.Id == 2)?.Password; suppressedScope.Complete(); } transactionScope.Dispose(); finalPassword = userRepoA.GetUsers().FirstOrDefault(u => u.Id == 2)?.Password; } // assert Assert.AreEqual(newUser.Password, dirtyPassword); Assert.AreEqual(originPassword, finalPassword); }