public void ExecuteSql_with_transactions_suppressed_dispatches_commands_to_interceptors()
        {
            var mockCommand = new Mock <DbCommand>();

            mockCommand.Setup(m => m.ExecuteNonQuery()).Returns(2013);

            var mockConnection = new Mock <DbConnection>();

            mockConnection.Protected().Setup <DbCommand>("CreateDbCommand").Returns(mockCommand.Object);

            var mockTransaction = new Mock <DbTransaction>();

            mockTransaction.Protected().Setup <DbConnection>("DbConnection").Returns(mockConnection.Object);

            var mockFactory = new Mock <DbProviderFactory>();

            mockFactory.Setup(m => m.CreateConnection()).Returns(mockConnection.Object);

            var objectContext       = new ObjectContext();
            var mockInternalContext = new Mock <InternalContextForMock>();

            mockInternalContext.Setup(m => m.ObjectContext).Returns(objectContext);
            var context = mockInternalContext.Object.Owner;

            objectContext.InterceptionContext = objectContext.InterceptionContext.WithDbContext(context);

            var migrator  = new DbMigrator(context, mockFactory.Object);
            var statement = new MigrationStatement
            {
                Sql = "Some Sql",
                SuppressTransaction = true
            };

            var mockInterceptor = new Mock <DbInterceptor> {
                CallBase = true
            };

            Interception.AddInterceptor(mockInterceptor.Object);

            try
            {
                migrator.ExecuteSql(mockTransaction.Object, statement);
            }
            finally
            {
                Interception.RemoveInterceptor(mockInterceptor.Object);
            }

            mockInterceptor.Verify(m => m.NonQueryExecuting(
                                       mockCommand.Object,
                                       It.Is <DbInterceptionContext>(c => c.DbContexts.Contains(context))));

            mockInterceptor.Verify(m => m.NonQueryExecuted(
                                       mockCommand.Object,
                                       2013,
                                       It.Is <DbInterceptionContext>(c => c.DbContexts.Contains(context))));
        }
Exemple #2
0
        public void ExecuteSql_dispatches_to_interceptors()
        {
            var mockCommand = new Mock <DbCommand>();

            mockCommand.Setup(m => m.ExecuteNonQuery()).Returns(2013);

            var mockConnection = new Mock <DbConnection>();

            mockConnection.Protected().Setup <DbCommand>("CreateDbCommand").Returns(mockCommand.Object);

            var mockTransaction = new Mock <DbTransaction>(MockBehavior.Strict);

            mockTransaction.Protected().Setup <DbConnection>("DbConnection").Returns(mockConnection.Object);

            var migrator  = new DbMigrator();
            var statement = new MigrationStatement
            {
                Sql = "Some Sql"
            };

            var mockInterceptor = new Mock <DbCommandInterceptor> {
                CallBase = true
            };

            DbInterception.Add(mockInterceptor.Object);
            var transactionInterceptorMock = new Mock <IDbTransactionInterceptor>();

            DbInterception.Add(transactionInterceptorMock.Object);
            try
            {
                migrator.ExecuteSql(statement, mockConnection.Object, mockTransaction.Object, new DbInterceptionContext());
            }
            finally
            {
                DbInterception.Remove(mockInterceptor.Object);
                DbInterception.Remove(transactionInterceptorMock.Object);
            }

            mockInterceptor.Verify(m => m.NonQueryExecuting(mockCommand.Object, It.IsAny <DbCommandInterceptionContext <int> >()), Times.Once());
            mockInterceptor.Verify(m => m.NonQueryExecuted(mockCommand.Object, It.IsAny <DbCommandInterceptionContext <int> >()), Times.Once());

            transactionInterceptorMock.Verify(
                m => m.ConnectionGetting(It.IsAny <DbTransaction>(), It.IsAny <DbTransactionInterceptionContext <DbConnection> >()),
                Times.Never());
            transactionInterceptorMock.Verify(
                m => m.ConnectionGot(It.IsAny <DbTransaction>(), It.IsAny <DbTransactionInterceptionContext <DbConnection> >()),
                Times.Never());
            mockTransaction.Protected().Verify <DbConnection>("DbConnection", Times.Never());
        }
Exemple #3
0
        public void ExecuteSql_dispatches_commands_to_interceptors()
        {
            var mockCommand = new Mock <DbCommand>();

            mockCommand.Setup(m => m.ExecuteNonQuery()).Returns(2013);

            var mockConnection = new Mock <DbConnection>();

            mockConnection.Protected().Setup <DbCommand>("CreateDbCommand").Returns(mockCommand.Object);

            var mockTransaction = new Mock <DbTransaction>();

            mockTransaction.Protected().Setup <DbConnection>("DbConnection").Returns(mockConnection.Object);

            var migrator  = new DbMigrator();
            var statement = new MigrationStatement
            {
                Sql = "Some Sql"
            };

            var mockInterceptor = new Mock <DbCommandInterceptor> {
                CallBase = true
            };

            DbInterception.Add(mockInterceptor.Object);

            try
            {
                migrator.ExecuteSql(mockTransaction.Object, statement, new DbInterceptionContext());
            }
            finally
            {
                DbInterception.Remove(mockInterceptor.Object);
            }

            mockInterceptor.Verify(m => m.NonQueryExecuting(mockCommand.Object, It.IsAny <DbCommandInterceptionContext <int> >()));
            mockInterceptor.Verify(m => m.NonQueryExecuted(mockCommand.Object, It.IsAny <DbCommandInterceptionContext <int> >()));
        }
        public void ExecuteSql_with_transactions_suppressed_dispatches_commands_to_interceptors()
        {
            var mockCommand = new Mock<DbCommand>();
            mockCommand.Setup(m => m.ExecuteNonQuery()).Returns(2013);

            var mockConnection = new Mock<DbConnection>();
            mockConnection.Protected().Setup<DbCommand>("CreateDbCommand").Returns(mockCommand.Object);

            var mockTransaction = new Mock<DbTransaction>();
            mockTransaction.Protected().Setup<DbConnection>("DbConnection").Returns(mockConnection.Object);

            var mockFactory = new Mock<DbProviderFactory>();
            mockFactory.Setup(m => m.CreateConnection()).Returns(mockConnection.Object);

            var objectContext = new ObjectContext();
            var mockInternalContext = new Mock<InternalContextForMock>();
            mockInternalContext.Setup(m => m.ObjectContext).Returns(objectContext);
            var context = mockInternalContext.Object.Owner;
            objectContext.InterceptionContext = objectContext.InterceptionContext.WithDbContext(context);

            var migrator = new DbMigrator(context, mockFactory.Object);
            var statement = new MigrationStatement
            {
                Sql = "Some Sql",
                SuppressTransaction = true
            };

            var mockInterceptor = new Mock<DbInterceptor> { CallBase = true };
            Interception.AddInterceptor(mockInterceptor.Object);

            try
            {
                migrator.ExecuteSql(mockTransaction.Object, statement);
            }
            finally
            {
                Interception.RemoveInterceptor(mockInterceptor.Object);
            }

            mockInterceptor.Verify(m => m.NonQueryExecuting(
                mockCommand.Object, 
                It.Is<DbInterceptionContext>(c => c.DbContexts.Contains(context))));
            
            mockInterceptor.Verify(m => m.NonQueryExecuted(
                mockCommand.Object, 
                2013,
                It.Is<DbInterceptionContext>(c => c.DbContexts.Contains(context))));
        }
        public void ExecuteSql_dispatches_commands_to_interceptors()
        {
            var mockCommand = new Mock<DbCommand>();
            mockCommand.Setup(m => m.ExecuteNonQuery()).Returns(2013);

            var mockConnection = new Mock<DbConnection>();
            mockConnection.Protected().Setup<DbCommand>("CreateDbCommand").Returns(mockCommand.Object);

            var mockTransaction = new Mock<DbTransaction>();
            mockTransaction.Protected().Setup<DbConnection>("DbConnection").Returns(mockConnection.Object);

            var migrator = new DbMigrator();
            var statement = new MigrationStatement
                {
                    Sql = "Some Sql"
                };

            var mockInterceptor = new Mock<DbInterceptor> { CallBase = true };
            Interception.AddInterceptor(mockInterceptor.Object);

            try
            {
                migrator.ExecuteSql(mockTransaction.Object, statement);
            }
            finally
            {
                Interception.RemoveInterceptor(mockInterceptor.Object);
            }

            mockInterceptor.Verify(m => m.NonQueryExecuting(mockCommand.Object, It.IsAny<DbInterceptionContext>()));
            mockInterceptor.Verify(m => m.NonQueryExecuted(mockCommand.Object, 2013, It.IsAny<DbInterceptionContext>()));
        }