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 CreateUserShouldCorrect() { // arrange var newUser = new UserEntity { Code = "FromUnitTest", Password = "******", IsActive = false }; // action var sut = new MariaDbRepository(); var insertedUser = sut.CreateUser(newUser); // assert Assert.NotZero(newUser.Id); Assert.NotZero(insertedUser.Id); }
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); }