Example #1
0
        public void BeginTransaction_Throws_InvalidOperationException_When_Transaction_Already_Running()
        {
            var mockDataContext = MockRepository.GenerateMock<IEFSession>();
            var mockConnection = MockRepository.GenerateMock<IDbConnection>();
            var mockTransaction = MockRepository.GenerateMock<IDbTransaction>();

            mockDataContext.Stub(x => x.Connection).Return(mockConnection);
            mockConnection.Expect(x => x.BeginTransaction(IsolationLevel.Unspecified))
                            .IgnoreArguments().Return(mockTransaction);

            var unitOfWork = new EFUnitOfWork(mockDataContext);
            unitOfWork.BeginTransaction();

            Assert.That(unitOfWork.IsInTransaction);
            Assert.Throws<InvalidOperationException>(() => unitOfWork.BeginTransaction());

            mockDataContext.VerifyAllExpectations();
            mockConnection.VerifyAllExpectations();
            mockTransaction.VerifyAllExpectations();
        }
Example #2
0
        public void BeginTransaction_Throws_InvalidOperationException_When_Transaction_Already_Running()
        {
            var mockDataContext = MockRepository.GenerateMock <IEFSession>();
            var mockConnection  = MockRepository.GenerateMock <IDbConnection>();
            var mockTransaction = MockRepository.GenerateMock <IDbTransaction>();

            mockDataContext.Stub(x => x.Connection).Return(mockConnection);
            mockConnection.Expect(x => x.BeginTransaction(IsolationLevel.Unspecified))
            .IgnoreArguments().Return(mockTransaction);

            var unitOfWork = new EFUnitOfWork(mockDataContext);

            unitOfWork.BeginTransaction();

            Assert.That(unitOfWork.IsInTransaction);
            Assert.Throws <InvalidOperationException>(() => unitOfWork.BeginTransaction());

            mockDataContext.VerifyAllExpectations();
            mockConnection.VerifyAllExpectations();
            mockTransaction.VerifyAllExpectations();
        }
Example #3
0
        public void Begin_Transaction_Should_Start_A_New_Transaction_With_Default_IsolationLevel()
        {
            var mockDataContext = MockRepository.GenerateMock<IEFSession>();
            var mockConnection = MockRepository.GenerateMock<IDbConnection>();
            var mockTransaction = MockRepository.GenerateMock<IDbTransaction>();

            mockDataContext.Stub(x => x.Connection).Return(mockConnection);
            mockConnection.Expect(x => x.BeginTransaction(IsolationLevel.ReadCommitted)).Return(mockTransaction);

            var unitOfWork = new EFUnitOfWork(mockDataContext);
            Assert.That(!unitOfWork.IsInTransaction);
            unitOfWork.BeginTransaction();

            Assert.That(unitOfWork.IsInTransaction);

            //mockDataContext.VerifyAllExpectations();
            mockConnection.VerifyAllExpectations();
            mockTransaction.VerifyAllExpectations();
        }
Example #4
0
        public void BeginTransaction_Should_Start_A_New_Transaction_With_Specified_IsolatinLevel()
        {
            var isoLevel = IsolationLevel.Snapshot;
            var mockDataContext = MockRepository.GenerateMock<IEFSession>();
            var mockConnection = MockRepository.GenerateMock<IDbConnection>();
            var mockTransaction = MockRepository.GenerateStub<IDbTransaction>();

            mockDataContext.Stub(x => x.Connection).Return(mockConnection);
            mockConnection.Expect(x => x.BeginTransaction(isoLevel)).Return(mockTransaction);

            var unitOfWork = new EFUnitOfWork(mockDataContext);
            Assert.That(!unitOfWork.IsInTransaction);
            unitOfWork.BeginTransaction(IsolationLevel.Snapshot);
            Assert.That(unitOfWork.IsInTransaction);

            mockDataContext.VerifyAllExpectations();
            mockConnection.VerifyAllExpectations();
            mockTransaction.VerifyAllExpectations();
        }
Example #5
0
        public void BeginTransaction_Should_Start_A_New_Transaction_With_Specified_IsolatinLevel()
        {
            var isoLevel        = IsolationLevel.Snapshot;
            var mockDataContext = MockRepository.GenerateMock <IEFSession>();
            var mockConnection  = MockRepository.GenerateMock <IDbConnection>();
            var mockTransaction = MockRepository.GenerateStub <IDbTransaction>();

            mockDataContext.Stub(x => x.Connection).Return(mockConnection);
            mockConnection.Expect(x => x.BeginTransaction(isoLevel)).Return(mockTransaction);

            var unitOfWork = new EFUnitOfWork(mockDataContext);

            Assert.That(!unitOfWork.IsInTransaction);
            unitOfWork.BeginTransaction(IsolationLevel.Snapshot);
            Assert.That(unitOfWork.IsInTransaction);

            mockDataContext.VerifyAllExpectations();
            mockConnection.VerifyAllExpectations();
            mockTransaction.VerifyAllExpectations();
        }
Example #6
0
        public void Begin_Transaction_Should_Start_A_New_Transaction_With_Default_IsolationLevel()
        {
            var mockDataContext = MockRepository.GenerateMock <IEFSession>();
            var mockConnection  = MockRepository.GenerateMock <IDbConnection>();
            var mockTransaction = MockRepository.GenerateMock <IDbTransaction>();

            mockDataContext.Stub(x => x.Connection).Return(mockConnection);
            mockConnection.Expect(x => x.BeginTransaction(IsolationLevel.ReadCommitted)).Return(mockTransaction);

            var unitOfWork = new EFUnitOfWork(mockDataContext);

            Assert.That(!unitOfWork.IsInTransaction);
            unitOfWork.BeginTransaction();

            Assert.That(unitOfWork.IsInTransaction);

            //mockDataContext.VerifyAllExpectations();
            mockConnection.VerifyAllExpectations();
            mockTransaction.VerifyAllExpectations();
        }
Example #7
0
        public void TransactionalFlush_Uses_Existing_Transaction_When_Transactional_AlreadyRunning()
        {
            var mockDataContext = MockRepository.GenerateMock <IEFSession>();
            var mockConnection  = MockRepository.GenerateMock <IDbConnection>();
            var mockTransaction = MockRepository.GenerateMock <IDbTransaction>();

            mockDataContext.Expect(x => x.Connection).Repeat.Any().Return(mockConnection);
            mockConnection.Stub(x => x.BeginTransaction(IsolationLevel.Unspecified))
            .IgnoreArguments()
            .Repeat.Once()
            .Return(mockTransaction);


            var unitOfWork = new EFUnitOfWork(mockDataContext);

            unitOfWork.BeginTransaction();
            unitOfWork.TransactionalFlush();

            mockDataContext.VerifyAllExpectations();
            mockConnection.VerifyAllExpectations();
            mockTransaction.VerifyAllExpectations();
        }
Example #8
0
        public void Dispose_UnitOfWork_Disposed_Underlying_Transaction_And_Session()
        {
            var mockDataContext = MockRepository.GenerateMock <IEFSession>();
            var mockConnection  = MockRepository.GenerateMock <IDbConnection>();
            var mockTransaction = MockRepository.GenerateMock <IDbTransaction>();

            mockDataContext.Stub(x => x.Connection).Return(mockConnection);
            mockConnection.Expect(x => x.BeginTransaction(IsolationLevel.Unspecified))
            .IgnoreArguments()
            .Return(mockTransaction);

            mockTransaction.Expect(x => x.Dispose());
            mockDataContext.Expect(x => x.Dispose());

            using (var unitOfWork = new EFUnitOfWork(mockDataContext))
            {
                unitOfWork.BeginTransaction();
            }

            mockDataContext.VerifyAllExpectations();
            mockConnection.VerifyAllExpectations();
            mockTransaction.VerifyAllExpectations();
        }
Example #9
0
        public void Rollback_Transaction_Releases_Transaction_From_UnitOfWork()
        {
            var mockDataContext = MockRepository.GenerateMock <IEFSession>();
            var mockConnection  = MockRepository.GenerateMock <IDbConnection>();
            var mockTransaction = MockRepository.GenerateMock <IDbTransaction>();

            mockDataContext.Stub(x => x.Connection).Return(mockConnection);
            mockConnection.Expect(x => x.BeginTransaction(IsolationLevel.Unspecified))
            .IgnoreArguments()
            .Return(mockTransaction);
            mockTransaction.Expect(x => x.Rollback());

            var unitOfWork  = new EFUnitOfWork(mockDataContext);
            var transaction = unitOfWork.BeginTransaction();

            Assert.That(unitOfWork.IsInTransaction);
            transaction.Rollback();
            Assert.That(!unitOfWork.IsInTransaction);

            mockDataContext.VerifyAllExpectations();
            mockConnection.VerifyAllExpectations();
            mockTransaction.VerifyAllExpectations();
        }
Example #10
0
        public void Comitting_Transaction_Releases_Transaction_From_UnitOfWork()
        {
            var mockDataContext = MockRepository.GenerateMock<IEFSession>();
            var mockConnection = MockRepository.GenerateMock<IDbConnection>();
            var mockTransaction = MockRepository.GenerateMock<IDbTransaction>();

            mockDataContext.Stub(x => x.Connection).Return(mockConnection);
            mockConnection.Expect(x => x.BeginTransaction(IsolationLevel.Unspecified))
                          .IgnoreArguments()
                          .Return(mockTransaction);
            mockTransaction.Expect(x => x.Commit());

            var unitOfWork = new EFUnitOfWork(mockDataContext);
            var transaction = unitOfWork.BeginTransaction();

            Assert.That(unitOfWork.IsInTransaction);
            transaction.Commit();
            Assert.That(!unitOfWork.IsInTransaction);

            mockDataContext.VerifyAllExpectations();
            mockConnection.VerifyAllExpectations();
            mockTransaction.VerifyAllExpectations();
        }
Example #11
0
        public void TransactionalFlush_Uses_Existing_Transaction_When_Transactional_AlreadyRunning()
        {
            var mockDataContext = MockRepository.GenerateMock<IEFSession>();
            var mockConnection = MockRepository.GenerateMock<IDbConnection>();
            var mockTransaction = MockRepository.GenerateMock<IDbTransaction>();

            mockDataContext.Expect(x => x.Connection).Repeat.Any().Return(mockConnection);
            mockConnection.Stub(x => x.BeginTransaction(IsolationLevel.Unspecified))
                          .IgnoreArguments()
                          .Repeat.Once()
                          .Return(mockTransaction);

            var unitOfWork = new EFUnitOfWork(mockDataContext);

            unitOfWork.BeginTransaction();
            unitOfWork.TransactionalFlush();

            mockDataContext.VerifyAllExpectations();
            mockConnection.VerifyAllExpectations();
            mockTransaction.VerifyAllExpectations();
        }
Example #12
0
        public void Dispose_UnitOfWork_Disposed_Underlying_Transaction_And_Session()
        {
            var mockDataContext = MockRepository.GenerateMock<IEFSession>();
            var mockConnection = MockRepository.GenerateMock<IDbConnection>();
            var mockTransaction = MockRepository.GenerateMock<IDbTransaction>();

            mockDataContext.Stub(x => x.Connection).Return(mockConnection);
            mockConnection.Expect(x => x.BeginTransaction(IsolationLevel.Unspecified))
                          .IgnoreArguments()
                          .Return(mockTransaction);

            mockTransaction.Expect(x => x.Dispose());
            mockDataContext.Expect(x => x.Dispose());

            using (var unitOfWork = new EFUnitOfWork(mockDataContext))
            {
                unitOfWork.BeginTransaction();
            }

            mockDataContext.VerifyAllExpectations();
            mockConnection.VerifyAllExpectations();
            mockTransaction.VerifyAllExpectations();
        }