Пример #1
0
            public void DbDatabaseExists_dispatches_commands_to_interceptors_for_connections_with_no_initial_catalog()
            {
                // See CodePlex 1554 - Handle User Instance flakiness
                MutableResolver.AddResolver <Func <IDbExecutionStrategy> >(new ExecutionStrategyResolver <IDbExecutionStrategy>(
                                                                               SqlProviderServices.ProviderInvariantName, null, () => new SqlAzureExecutionStrategy()));
                var interceptor = new TestScalarInterceptor();

                DbInterception.Add(interceptor);
                try
                {
                    using (var connection =
                               new SqlConnection(ModelHelpers.SimpleAttachConnectionString("I.Do.Not.Exist", useInitialCatalog: false)))
                    {
                        SqlProviderServices.Instance.DatabaseExists(connection, null, new StoreItemCollection());
                    }
                }
                finally
                {
                    MutableResolver.ClearResolvers();
                    DbInterception.Remove(interceptor);
                }

                Assert.Equal(1, interceptor.Commands.Count);

                Assert.True(
                    interceptor.Commands.Select(c => c.CommandText)
                    .Single()
                    .StartsWith("SELECT Count(*) FROM sys.master_files WHERE [physical_name]=N'"));
            }
Пример #2
0
            public void DbDatabaseExists_dispatches_commands_to_interceptors_for_connections_with_no_initial_catalog()
            {
                var interceptor = new TestScalarInterceptor();

                Interception.AddInterceptor(interceptor);
                try
                {
                    using (var connection =
                               new SqlConnection(ModelHelpers.SimpleAttachConnectionString("I.Do.Not.Exist", useInitialCatalog: false)))
                    {
                        SqlProviderServices.Instance.DatabaseExists(connection, null, new StoreItemCollection());
                    }
                }
                finally
                {
                    Interception.RemoveInterceptor(interceptor);
                }

                Assert.Equal(1, interceptor.Commands.Count);

                Assert.True(
                    interceptor.Commands.Select(c => c.CommandText)
                    .Single()
                    .StartsWith("SELECT Count(*) FROM sys.master_files WHERE [physical_name]=N'"));
            }
Пример #3
0
            public void DbDatabaseExists_dispatches_commands_to_interceptors_for_connections_with_initial_catalog()
            {
                var interceptor = new TestScalarInterceptor();

                DbInterception.Add(interceptor);
                try
                {
                    using (var connection = new SqlConnection(ModelHelpers.SimpleAttachConnectionString("I.Do.Not.Exist")))
                    {
                        SqlProviderServices.Instance.DatabaseExists(connection, null, new StoreItemCollection());
                    }
                }
                finally
                {
                    DbInterception.Remove(interceptor);
                }

                Assert.Equal(2, interceptor.Commands.Count);

                Assert.True(
                    interceptor.Commands.Select(c => c.CommandText).All(
                        t => t == "SELECT Count(*) FROM sys.databases WHERE [name]=N'I.Do.Not.Exist'"));
            }
            public void DbDatabaseExists_dispatches_commands_to_interceptors_for_connections_with_no_initial_catalog()
            {
                // See CodePlex 1554 - Handle User Instance flakiness
                MutableResolver.AddResolver <Func <IDbExecutionStrategy> >(new ExecutionStrategyResolver <IDbExecutionStrategy>(
                                                                               SqlProviderServices.ProviderInvariantName, null, () => new SqlAzureExecutionStrategy()));
                var interceptor = new TestScalarInterceptor();

                DbInterception.Add(interceptor);
                var dbConnectionInterceptorMock = new Mock <IDbConnectionInterceptor>();

                DbInterception.Add(dbConnectionInterceptorMock.Object);
                try
                {
                    using (var connection =
                               new SqlConnection(ModelHelpers.SimpleAttachConnectionString("I.Do.Not.Exist", useInitialCatalog: false)))
                    {
                        SqlProviderServices.Instance.DatabaseExists(connection, null, new StoreItemCollection());
                    }
                }
                finally
                {
                    MutableResolver.ClearResolvers();
                    DbInterception.Remove(interceptor);
                    DbInterception.Remove(dbConnectionInterceptorMock.Object);
                }

                Assert.Equal(1, interceptor.Commands.Count);

                Assert.True(
                    interceptor.Commands.Select(c => c.CommandText)
                    .Single()
                    .StartsWith("SELECT Count(*) FROM sys.master_files WHERE [physical_name]=N'"));

                dbConnectionInterceptorMock.Verify(
                    m => m.ConnectionStringGetting(It.IsAny <DbConnection>(), It.IsAny <DbConnectionInterceptionContext <string> >()),
                    Times.Exactly(7));
                dbConnectionInterceptorMock.Verify(
                    m => m.ConnectionStringGot(It.IsAny <DbConnection>(), It.IsAny <DbConnectionInterceptionContext <string> >()),
                    Times.Exactly(7));

                dbConnectionInterceptorMock.Verify(
                    m => m.StateGetting(It.IsAny <DbConnection>(), It.IsAny <DbConnectionInterceptionContext <ConnectionState> >()),
                    Times.Exactly(5));
                dbConnectionInterceptorMock.Verify(
                    m => m.StateGot(It.IsAny <DbConnection>(), It.IsAny <DbConnectionInterceptionContext <ConnectionState> >()),
                    Times.Exactly(5));

                dbConnectionInterceptorMock.Verify(
                    m => m.DataSourceGetting(It.IsAny <DbConnection>(), It.IsAny <DbConnectionInterceptionContext <string> >()),
                    Times.Exactly(2));
                dbConnectionInterceptorMock.Verify(
                    m => m.DataSourceGot(It.IsAny <DbConnection>(), It.IsAny <DbConnectionInterceptionContext <string> >()),
                    Times.Exactly(2));

                dbConnectionInterceptorMock.Verify(
                    m => m.ServerVersionGetting(It.IsAny <DbConnection>(), It.IsAny <DbConnectionInterceptionContext <string> >()),
                    Times.Once());
                dbConnectionInterceptorMock.Verify(
                    m => m.ServerVersionGot(It.IsAny <DbConnection>(), It.IsAny <DbConnectionInterceptionContext <string> >()),
                    Times.Once());

                dbConnectionInterceptorMock.Verify(
                    m => m.Opening(It.IsAny <DbConnection>(), It.IsAny <DbConnectionInterceptionContext>()),
                    Times.Exactly(2));
                dbConnectionInterceptorMock.Verify(
                    m => m.Opened(It.IsAny <DbConnection>(), It.IsAny <DbConnectionInterceptionContext>()),
                    Times.Exactly(2));

                dbConnectionInterceptorMock.Verify(
                    m => m.Closing(It.IsAny <DbConnection>(), It.IsAny <DbConnectionInterceptionContext>()),
                    Times.Once());
                dbConnectionInterceptorMock.Verify(
                    m => m.Closed(It.IsAny <DbConnection>(), It.IsAny <DbConnectionInterceptionContext>()),
                    Times.Once());
            }
            public void DbDatabaseExists_dispatches_commands_to_interceptors_for_connections_with_initial_catalog()
            {
                var interceptor = new TestScalarInterceptor();

                DbInterception.Add(interceptor);
                var dbConnectionInterceptorMock = new Mock <IDbConnectionInterceptor>();

                DbInterception.Add(dbConnectionInterceptorMock.Object);
                try
                {
                    using (var connection = new SqlConnection(ModelHelpers.SimpleAttachConnectionString("I.Do.Not.Exist")))
                    {
                        SqlProviderServices.Instance.DatabaseExists(connection, null, new StoreItemCollection());
                    }
                }
                finally
                {
                    DbInterception.Remove(interceptor);
                    DbInterception.Remove(dbConnectionInterceptorMock.Object);
                }

                Assert.Equal(2, interceptor.Commands.Count);

                Assert.True(
                    interceptor.Commands.Select(c => c.CommandText).All(
                        t => t == "IF db_id(N'I.Do.Not.Exist') IS NOT NULL SELECT 1 ELSE SELECT Count(*) FROM sys.databases WHERE [name]=N'I.Do.Not.Exist'"));

                dbConnectionInterceptorMock.Verify(
                    m => m.ConnectionStringGetting(It.IsAny <DbConnection>(), It.IsAny <DbConnectionInterceptionContext <string> >()),
                    Times.Exactly(11));
                dbConnectionInterceptorMock.Verify(
                    m => m.ConnectionStringGot(It.IsAny <DbConnection>(), It.IsAny <DbConnectionInterceptionContext <string> >()),
                    Times.Exactly(11));

                dbConnectionInterceptorMock.Verify(
                    m => m.StateGetting(It.IsAny <DbConnection>(), It.IsAny <DbConnectionInterceptionContext <ConnectionState> >()),
                    Times.Exactly(8));
                dbConnectionInterceptorMock.Verify(
                    m => m.StateGot(It.IsAny <DbConnection>(), It.IsAny <DbConnectionInterceptionContext <ConnectionState> >()),
                    Times.Exactly(8));

                dbConnectionInterceptorMock.Verify(
                    m => m.DataSourceGetting(It.IsAny <DbConnection>(), It.IsAny <DbConnectionInterceptionContext <string> >()),
                    Times.Exactly(3));
                dbConnectionInterceptorMock.Verify(
                    m => m.DataSourceGot(It.IsAny <DbConnection>(), It.IsAny <DbConnectionInterceptionContext <string> >()),
                    Times.Exactly(3));

                dbConnectionInterceptorMock.Verify(
                    m => m.ServerVersionGetting(It.IsAny <DbConnection>(), It.IsAny <DbConnectionInterceptionContext <string> >()),
                    Times.Exactly(0));
                dbConnectionInterceptorMock.Verify(
                    m => m.ServerVersionGot(It.IsAny <DbConnection>(), It.IsAny <DbConnectionInterceptionContext <string> >()),
                    Times.Exactly(0));

                dbConnectionInterceptorMock.Verify(
                    m => m.Opening(It.IsAny <DbConnection>(), It.IsAny <DbConnectionInterceptionContext>()),
                    Times.Exactly(3));
                dbConnectionInterceptorMock.Verify(
                    m => m.Opened(It.IsAny <DbConnection>(), It.IsAny <DbConnectionInterceptionContext>()),
                    Times.Exactly(3));

                dbConnectionInterceptorMock.Verify(
                    m => m.Closing(It.IsAny <DbConnection>(), It.IsAny <DbConnectionInterceptionContext>()),
                    Times.Exactly(2));
                dbConnectionInterceptorMock.Verify(
                    m => m.Closed(It.IsAny <DbConnection>(), It.IsAny <DbConnectionInterceptionContext>()),
                    Times.Exactly(2));
            }
            public void DbDatabaseExists_dispatches_commands_to_interceptors_for_connections_with_no_initial_catalog()
            {
                // See CodePlex 1554 - Handle User Instance flakiness
                MutableResolver.AddResolver<Func<IDbExecutionStrategy>>(new ExecutionStrategyResolver<IDbExecutionStrategy>(
                                    SqlProviderServices.ProviderInvariantName, null, () => new SqlAzureExecutionStrategy()));
                var interceptor = new TestScalarInterceptor();
                DbInterception.Add(interceptor);
                var dbConnectionInterceptorMock = new Mock<IDbConnectionInterceptor>();
                DbInterception.Add(dbConnectionInterceptorMock.Object);
                try
                {
                    using (var connection =
                        new SqlConnection(ModelHelpers.SimpleAttachConnectionString("I.Do.Not.Exist", useInitialCatalog: false)))
                    {
                        SqlProviderServices.Instance.DatabaseExists(connection, null, new StoreItemCollection());
                    }
                }
                finally
                {
                    MutableResolver.ClearResolvers();
                    DbInterception.Remove(interceptor);
                    DbInterception.Remove(dbConnectionInterceptorMock.Object);
                }

                Assert.Equal(1, interceptor.Commands.Count);

                Assert.True(
                    interceptor.Commands.Select(c => c.CommandText)
                        .Single()
                        .StartsWith("SELECT Count(*) FROM sys.master_files WHERE [physical_name]=N'"));

                dbConnectionInterceptorMock.Verify(
                    m => m.ConnectionStringGetting(It.IsAny<DbConnection>(), It.IsAny<DbConnectionInterceptionContext<string>>()),
                    Times.Exactly(7));
                dbConnectionInterceptorMock.Verify(
                    m => m.ConnectionStringGot(It.IsAny<DbConnection>(), It.IsAny<DbConnectionInterceptionContext<string>>()),
                    Times.Exactly(7));

                dbConnectionInterceptorMock.Verify(
                    m => m.StateGetting(It.IsAny<DbConnection>(), It.IsAny<DbConnectionInterceptionContext<ConnectionState>>()),
                    Times.Exactly(5));
                dbConnectionInterceptorMock.Verify(
                    m => m.StateGot(It.IsAny<DbConnection>(), It.IsAny<DbConnectionInterceptionContext<ConnectionState>>()),
                    Times.Exactly(5));

                dbConnectionInterceptorMock.Verify(
                    m => m.DataSourceGetting(It.IsAny<DbConnection>(), It.IsAny<DbConnectionInterceptionContext<string>>()),
                    Times.Exactly(2));
                dbConnectionInterceptorMock.Verify(
                    m => m.DataSourceGot(It.IsAny<DbConnection>(), It.IsAny<DbConnectionInterceptionContext<string>>()),
                    Times.Exactly(2));

                dbConnectionInterceptorMock.Verify(
                    m => m.ServerVersionGetting(It.IsAny<DbConnection>(), It.IsAny<DbConnectionInterceptionContext<string>>()),
                    Times.Once());
                dbConnectionInterceptorMock.Verify(
                    m => m.ServerVersionGot(It.IsAny<DbConnection>(), It.IsAny<DbConnectionInterceptionContext<string>>()),
                    Times.Once());

                dbConnectionInterceptorMock.Verify(
                    m => m.Opening(It.IsAny<DbConnection>(), It.IsAny<DbConnectionInterceptionContext>()),
                    Times.Exactly(2));
                dbConnectionInterceptorMock.Verify(
                    m => m.Opened(It.IsAny<DbConnection>(), It.IsAny<DbConnectionInterceptionContext>()),
                    Times.Exactly(2));

                dbConnectionInterceptorMock.Verify(
                    m => m.Closing(It.IsAny<DbConnection>(), It.IsAny<DbConnectionInterceptionContext>()),
                    Times.Once());
                dbConnectionInterceptorMock.Verify(
                    m => m.Closed(It.IsAny<DbConnection>(), It.IsAny<DbConnectionInterceptionContext>()),
                    Times.Once());
            }
            public void DbDatabaseExists_dispatches_commands_to_interceptors_for_connections_with_initial_catalog()
            {
                var interceptor = new TestScalarInterceptor();
                DbInterception.Add(interceptor);
                var dbConnectionInterceptorMock = new Mock<IDbConnectionInterceptor>();
                DbInterception.Add(dbConnectionInterceptorMock.Object);
                try
                {
                    using (var connection = new SqlConnection(ModelHelpers.SimpleAttachConnectionString("I.Do.Not.Exist")))
                    {
                        SqlProviderServices.Instance.DatabaseExists(connection, null, new StoreItemCollection());
                    }
                }
                finally
                {
                    DbInterception.Remove(interceptor);
                    DbInterception.Remove(dbConnectionInterceptorMock.Object);
                }

                Assert.Equal(2, interceptor.Commands.Count);

                Assert.True(
                    interceptor.Commands.Select(c => c.CommandText).All(
                        t => t == "IF db_id(N'I.Do.Not.Exist') IS NOT NULL SELECT 1 ELSE SELECT Count(*) FROM sys.databases WHERE [name]=N'I.Do.Not.Exist'"));

                dbConnectionInterceptorMock.Verify(
                    m => m.ConnectionStringGetting(It.IsAny<DbConnection>(), It.IsAny<DbConnectionInterceptionContext<string>>()),
                    Times.Exactly(11));
                dbConnectionInterceptorMock.Verify(
                    m => m.ConnectionStringGot(It.IsAny<DbConnection>(), It.IsAny<DbConnectionInterceptionContext<string>>()),
                    Times.Exactly(11));

                dbConnectionInterceptorMock.Verify(
                    m => m.StateGetting(It.IsAny<DbConnection>(), It.IsAny<DbConnectionInterceptionContext<ConnectionState>>()),
                    Times.Exactly(8));
                dbConnectionInterceptorMock.Verify(
                    m => m.StateGot(It.IsAny<DbConnection>(), It.IsAny<DbConnectionInterceptionContext<ConnectionState>>()),
                    Times.Exactly(8));

                dbConnectionInterceptorMock.Verify(
                    m => m.DataSourceGetting(It.IsAny<DbConnection>(), It.IsAny<DbConnectionInterceptionContext<string>>()),
                    Times.Exactly(3));
                dbConnectionInterceptorMock.Verify(
                    m => m.DataSourceGot(It.IsAny<DbConnection>(), It.IsAny<DbConnectionInterceptionContext<string>>()),
                    Times.Exactly(3));

                dbConnectionInterceptorMock.Verify(
                    m => m.ServerVersionGetting(It.IsAny<DbConnection>(), It.IsAny<DbConnectionInterceptionContext<string>>()),
                    Times.Exactly(0));
                dbConnectionInterceptorMock.Verify(
                    m => m.ServerVersionGot(It.IsAny<DbConnection>(), It.IsAny<DbConnectionInterceptionContext<string>>()),
                    Times.Exactly(0));

                dbConnectionInterceptorMock.Verify(
                    m => m.Opening(It.IsAny<DbConnection>(), It.IsAny<DbConnectionInterceptionContext>()),
                    Times.Exactly(3));
                dbConnectionInterceptorMock.Verify(
                    m => m.Opened(It.IsAny<DbConnection>(), It.IsAny<DbConnectionInterceptionContext>()),
                    Times.Exactly(3));

                dbConnectionInterceptorMock.Verify(
                    m => m.Closing(It.IsAny<DbConnection>(), It.IsAny<DbConnectionInterceptionContext>()),
                    Times.Exactly(2));
                dbConnectionInterceptorMock.Verify(
                    m => m.Closed(It.IsAny<DbConnection>(), It.IsAny<DbConnectionInterceptionContext>()),
                    Times.Exactly(2));
            }
            public void DbDatabaseExists_dispatches_commands_to_interceptors_for_connections_with_no_initial_catalog()
            {
                var interceptor = new TestScalarInterceptor();
                Interception.AddInterceptor(interceptor);
                try
                {
                    using (var connection =
                        new SqlConnection(ModelHelpers.SimpleAttachConnectionString("I.Do.Not.Exist", useInitialCatalog: false)))
                    {
                        SqlProviderServices.Instance.DatabaseExists(connection, null, new StoreItemCollection());
                    }
                }
                finally
                {
                    Interception.RemoveInterceptor(interceptor);
                }

                Assert.Equal(1, interceptor.Commands.Count);

                Assert.True(
                    interceptor.Commands.Select(c => c.CommandText)
                               .Single()
                               .StartsWith("SELECT Count(*) FROM sys.master_files WHERE [physical_name]=N'"));
            }