public async Task BeginTransaction_Initializes_Transaction_When_One_Not_Present() { _repos.Add(BuildRepo().Object); _unitOfWork = new AsyncUnitOfWork(_provider.Object, _repos); await _unitOfWork.BeginTransactionAsync(); _unitOfWork.InTransaction.Should().BeTrue(); _transactionCount.Should().Be(1); }
public async Task RollbackTransaction_Clears_Transaction_When_Executed() { _repos.Add(BuildRepo().Object); _unitOfWork = new AsyncUnitOfWork(_provider.Object, _repos); await _unitOfWork.BeginTransactionAsync(); _unitOfWork.InTransaction.Should().BeTrue(); _unitOfWork.RollbackTransaction(); _unitOfWork.InTransaction.Should().BeFalse(); _transactionCount.Should().Be(1); }
public async void MultiRepo_With_Using_Statement_Happy_Path() { _repos.Add(BuildRepo().Object); _repos.Add(BuildRepo().Object); _repos.Add(BuildRepo().Object); _repos.Add(BuildRepo().Object); // initialize multi-operation Unit Of Work (UOW) using (_unitOfWork = new AsyncUnitOfWork(_provider.Object, _repos)) { await _unitOfWork.BeginTransactionAsync(); for (int idx = 0; idx < _statements.Keys.Count; idx++) { string key = _statements.Keys.ToList()[idx]; int result = await _repos[idx].ExecuteAsync(key); // did correct statement get called? result.Should().Be(_statements[key]); // is call considered in transaction? _unitOfWork.InTransaction.Should().BeTrue(); // are all repos sharing same UOW _repos[idx].AsyncUnitOfWork.Should().Be(_unitOfWork); // sync uow is null _repos[idx].UnitOfWork.Should().BeNull(); // is UOW disposed? _unitOfWork.Disposed.Should().BeFalse(); } // commit multi-operation work _unitOfWork.CommitTransaction(); } // Transaction over? _unitOfWork.InTransaction.Should().BeFalse(); // Transaction count correct? _transactionCount.Should().Be(1); // Object disposed? _unitOfWork.Disposed.Should().BeTrue(); foreach (IConnectionRepo repo in _repos) { // repo's cleaned up repo.UnitOfWork.Should().BeNull(); } }
public async void MultiRepo_With_Using_Statement_UnHappy_Path() { Mock <IConnectionRepo> forError = BuildRepo(); forError.Setup(r => r.ExecuteAsync("error", null, null)).Throws(new Exception("boom")); _repos.Add(BuildRepo().Object); _repos.Add(forError.Object); // initialize multi-operation Unit Of Work (UOW) using (_unitOfWork = new AsyncUnitOfWork(_provider.Object, _repos)) { try { await _unitOfWork.BeginTransactionAsync(); await _repos[0].ExecuteAsync("insert"); _unitOfWork.InTransaction.Should().BeTrue(); await _repos[1].ExecuteAsync("error"); _unitOfWork.Disposed.Should().BeFalse(); } catch (Exception e) { _unitOfWork.RollbackTransaction(); } } // Transaction over? _unitOfWork.InTransaction.Should().BeFalse(); // Transaction count correct? _transactionCount.Should().Be(1); // Object disposed? _unitOfWork.Disposed.Should().BeTrue(); foreach (IConnectionRepo repo in _repos) { // repo's cleaned up repo.AsyncUnitOfWork.Should().BeNull(); } }