public void TransactionalFlush_Starts_A_Transaction_With_Default_Isolation_And_Commits_When_Flush_Succeeds()
        {
            var mockDataContext = MockRepository.GenerateMock <ILinqSession>();
            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);

            mockDataContext.Expect(x => x.Transaction = mockTransaction);
            mockDataContext.Expect(x => x.SubmitChanges());
            mockConnection.Expect(x => x.State).Return(ConnectionState.Closed); //First call should be closed
            mockConnection.Expect(x => x.Open());
            mockTransaction.Expect(x => x.Commit());
            mockConnection.Expect(x => x.State).Return(ConnectionState.Open); //Second call should be open
            mockConnection.Expect(x => x.Close());


            var unitOfWork = new LinqToSqlUnitOfWork(mockDataContext);

            unitOfWork.TransactionalFlush();

            mockDataContext.VerifyAllExpectations();
            mockConnection.VerifyAllExpectations();
            mockTransaction.VerifyAllExpectations();
        }
        TransactionalFlush_Starts_A_Transaction_With_Specified_IsolationLevel_And_Commits_When_Flush_Succeeds()
        {
            var mockDataContext = MockRepository.GenerateMock <ILinqSession>();
            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);
            mockDataContext.Expect(x => x.Transaction = mockTransaction);
            mockDataContext.Expect(x => x.SubmitChanges());
            mockTransaction.Expect(x => x.Commit());

            var unitOfWork = new LinqToSqlUnitOfWork(mockDataContext);

            unitOfWork.TransactionalFlush(IsolationLevel.ReadUncommitted);

            mockDataContext.VerifyAllExpectations();
            mockConnection.VerifyAllExpectations();
            mockTransaction.VerifyAllExpectations();
        }
        public void TransactionalFlush_Uses_Existing_Transaction_When_Transactional_AlreadyRunning()
        {
            var mockDataContext = MockRepository.GenerateMock <ILinqSession>();
            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);
            mockDataContext.Expect(x => x.Transaction = mockTransaction);


            var unitOfWork = new LinqToSqlUnitOfWork(mockDataContext);

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

            mockDataContext.VerifyAllExpectations();
            mockConnection.VerifyAllExpectations();
            mockTransaction.VerifyAllExpectations();
        }
        public void TransactionalFlush_Uses_Existing_Transaction_When_Transactional_AlreadyRunning()
        {
            var mockDataContext = MockRepository.GenerateMock<ILinqSession>();
            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);
            mockDataContext.Expect(x => x.Transaction = mockTransaction);

            var unitOfWork = new LinqToSqlUnitOfWork(mockDataContext);

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

            mockDataContext.VerifyAllExpectations();
            mockConnection.VerifyAllExpectations();
            mockTransaction.VerifyAllExpectations();
        }
        public void TransactionalFlush_Starts_A_Transaction_With_Specified_IsolationLevel_And_Commits_When_Flush_Succeeds()
        {
            var mockDataContext = MockRepository.GenerateMock<ILinqSession>();
            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);
            mockDataContext.Expect(x => x.Transaction = mockTransaction);
            mockDataContext.Expect(x => x.SubmitChanges());
            mockTransaction.Expect(x => x.Commit());

            var unitOfWork = new LinqToSqlUnitOfWork(mockDataContext);
            unitOfWork.TransactionalFlush(IsolationLevel.ReadUncommitted);

            mockDataContext.VerifyAllExpectations();
            mockConnection.VerifyAllExpectations();
            mockTransaction.VerifyAllExpectations();
        }
        public void TransactionalFlush_Starts_A_Transaction_With_Default_Isolation_And_Commits_When_Flush_Succeeds()
        {
            var mockDataContext = MockRepository.GenerateMock<ILinqSession>();
            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);

            mockDataContext.Expect(x => x.Transaction = mockTransaction);
            mockDataContext.Expect(x => x.SubmitChanges());
            mockConnection.Expect(x => x.State).Return(ConnectionState.Closed); //First call should be closed
            mockConnection.Expect(x => x.Open());
            mockTransaction.Expect(x => x.Commit());
            mockConnection.Expect(x => x.State).Return(ConnectionState.Open); //Second call should be open
            mockConnection.Expect(x => x.Close());

            var unitOfWork = new LinqToSqlUnitOfWork(mockDataContext);
            unitOfWork.TransactionalFlush();

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