Ejemplo n.º 1
0
        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);
        }
Ejemplo n.º 2
0
        public void GetProductShouldCorrect()
        {
            // arrange
            var sut = new MariaDbRepository();

            // action
            var result       = sut.GetProducts();
            var firstProduct = result.FirstOrDefault();

            // assert
            Assert.IsNull(firstProduct);
        }