Beispiel #1
0
            public void DbDeleteDatabase_dispatches_commands_to_interceptors_for_connections_without_initial_catalog()
            {
                StoreItemCollection storeItemCollection;

                using (var context = new DdlDatabaseContext())
                {
                    storeItemCollection =
                        (StoreItemCollection)
                        ((IObjectContextAdapter)context).ObjectContext.MetadataWorkspace.GetItemCollection(DataSpace.SSpace);
                }

                using (var connection = new SqlConnection(SimpleAttachConnectionString <DdlDatabaseContext>(useInitialCatalog: false)))
                {
                    var nonQueryInterceptor = new TestNonQueryInterceptor();
                    var readerInterceptor   = new TestReaderInterceptor();

                    // See CodePlex 1554 - Handle User Instance flakiness
                    MutableResolver.AddResolver <Func <IDbExecutionStrategy> >(new ExecutionStrategyResolver <IDbExecutionStrategy>(
                                                                                   SqlProviderServices.ProviderInvariantName, null, () => new SqlAzureExecutionStrategy()));
                    try
                    {
                        if (!SqlProviderServices.Instance.DatabaseExists(connection, null, storeItemCollection))
                        {
                            SqlProviderServices.Instance.CreateDatabase(connection, null, storeItemCollection);
                        }

                        DbInterception.Add(nonQueryInterceptor);
                        DbInterception.Add(readerInterceptor);
                        try
                        {
                            SqlProviderServices.Instance.DeleteDatabase(connection, null, storeItemCollection);
                        }
                        finally
                        {
                            DbInterception.Remove(nonQueryInterceptor);
                            DbInterception.Remove(readerInterceptor);
                        }
                    }
                    finally
                    {
                        MutableResolver.ClearResolvers();
                    }

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

                    var commandTexts = nonQueryInterceptor.Commands.Select(c => c.CommandText);
                    Assert.True(commandTexts.Any(t => t.StartsWith("drop database [SYSTEM_DATA_ENTITY_SQLSERVER")));
                    Assert.True(
                        commandTexts.Any(t => t.Contains("SYSTEM.DATA.ENTITY.SQLSERVER.SQLPROVIDERSERVICESTESTS+DDLDATABASECONTEXT.MDF")));

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

                    Assert.True(
                        readerInterceptor.Commands.Select(
                            c => c.CommandText).Single().StartsWith("SELECT [d].[name] FROM sys.databases "));
                }
            }
Beispiel #2
0
            public void DbDeleteDatabase_dispatches_commands_to_interceptors_for_connections_without_initial_catalog()
            {
                StoreItemCollection storeItemCollection;

                using (var context = new DdlDatabaseContext())
                {
                    storeItemCollection =
                        (StoreItemCollection)
                        ((IObjectContextAdapter)context).ObjectContext.MetadataWorkspace.GetItemCollection(DataSpace.SSpace);
                }

                using (var connection = new SqlConnection(SimpleAttachConnectionString <DdlDatabaseContext>(useInitialCatalog: false)))
                {
                    if (!SqlProviderServices.Instance.DatabaseExists(connection, null, storeItemCollection))
                    {
                        SqlProviderServices.Instance.CreateDatabase(connection, null, storeItemCollection);
                    }

                    var nonQueryInterceptor = new TestNonQueryInterceptor();
                    var readerInterceptor   = new TestReaderInterceptor();
                    Interception.AddInterceptor(nonQueryInterceptor);
                    Interception.AddInterceptor(readerInterceptor);
                    try
                    {
                        SqlProviderServices.Instance.DeleteDatabase(connection, null, storeItemCollection);
                    }
                    finally
                    {
                        Interception.RemoveInterceptor(nonQueryInterceptor);
                        Interception.RemoveInterceptor(readerInterceptor);
                    }

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

                    var commandTexts = nonQueryInterceptor.Commands.Select(c => c.CommandText);
                    Assert.True(commandTexts.Any(t => t.StartsWith("drop database [SYSTEM_DATA_ENTITY_SQLSERVER")));
                    Assert.True(
                        commandTexts.Any(t => t.Contains("SYSTEM.DATA.ENTITY.SQLSERVER.SQLPROVIDERSERVICESTESTS+DDLDATABASECONTEXT.MDF")));

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

                    Assert.True(
                        readerInterceptor.Commands.Select(
                            c => c.CommandText).Single().StartsWith("SELECT [d].[name] FROM sys.databases "));
                }
            }
            public void GetServerType_dispatches_commands_to_interceptors()
            {
                var connection = CreateConnectionForAzureQuery(5).Object;

                var interceptor = new TestReaderInterceptor();

                Interception.AddInterceptor(interceptor);
                try
                {
                    SqlVersionUtils.GetServerType(connection);
                }
                finally
                {
                    Interception.RemoveInterceptor(interceptor);
                }

                Assert.Equal(1, interceptor.Commands.Count);
                Assert.Same(connection.CreateCommand(), interceptor.Commands.Single());
            }
            public void DbDeleteDatabase_dispatches_to_interceptors_for_connections_without_initial_catalog()
            {
                StoreItemCollection storeItemCollection;
                using (var context = new DdlDatabaseContext())
                {
                    storeItemCollection =
                        (StoreItemCollection)
                        ((IObjectContextAdapter)context).ObjectContext.MetadataWorkspace.GetItemCollection(DataSpace.SSpace);
                }

                using (var connection = new SqlConnection(SimpleAttachConnectionString<DdlDatabaseContext>(useInitialCatalog: false)))
                {
                    var nonQueryInterceptor = new TestNonQueryInterceptor();
                    var readerInterceptor = new TestReaderInterceptor();
                    var dbConnectionInterceptorMock = new Mock<IDbConnectionInterceptor>();

                    // See CodePlex 1554 - Handle User Instance flakiness
                    MutableResolver.AddResolver<Func<IDbExecutionStrategy>>(new ExecutionStrategyResolver<IDbExecutionStrategy>(
                                        SqlProviderServices.ProviderInvariantName, null, () => new SqlAzureExecutionStrategy())); 
                    try
                    {
                        if (!SqlProviderServices.Instance.DatabaseExists(connection, null, storeItemCollection))
                        {
                            SqlProviderServices.Instance.CreateDatabase(connection, null, storeItemCollection);
                        }

                        DbInterception.Add(nonQueryInterceptor);
                        DbInterception.Add(readerInterceptor);
                        DbInterception.Add(dbConnectionInterceptorMock.Object);
                        try
                        {
                            SqlProviderServices.Instance.DeleteDatabase(connection, null, storeItemCollection);
                        }
                        finally
                        {
                            DbInterception.Remove(nonQueryInterceptor);
                            DbInterception.Remove(readerInterceptor);
                            DbInterception.Remove(dbConnectionInterceptorMock.Object);
                        }
                    }
                    finally
                    {
                        MutableResolver.ClearResolvers();
                    }

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

                    var commandTexts = nonQueryInterceptor.Commands.Select(c => c.CommandText);
                    Assert.True(commandTexts.Any(t => t.StartsWith("drop database [SYSTEM_DATA_ENTITY_SQLSERVER")));
                    Assert.True(
                        commandTexts.Any(t => t.Contains("SYSTEM.DATA.ENTITY.SQLSERVER.SQLPROVIDERSERVICESTESTS+DDLDATABASECONTEXT.MDF")));

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

                    Assert.True(
                        readerInterceptor.Commands.Select(
                            c => c.CommandText).Single().StartsWith("SELECT [d].[name] FROM sys.databases "));

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

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

                    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.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(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(3));
                    dbConnectionInterceptorMock.Verify(
                        m => m.Closed(It.IsAny<DbConnection>(), It.IsAny<DbConnectionInterceptionContext>()),
                        Times.Exactly(3));
                }
            }
            public void GetServerType_dispatches_commands_to_interceptors()
            {
                var connection = CreateConnectionForAzureQuery(5).Object;

                var interceptor = new TestReaderInterceptor();
                Interception.AddInterceptor(interceptor);
                try
                {
                    SqlVersionUtils.GetServerType(connection);
                }
                finally
                {
                    Interception.RemoveInterceptor(interceptor);
                }

                Assert.Equal(1, interceptor.Commands.Count);
                Assert.Same(connection.CreateCommand(), interceptor.Commands.Single());
            }
            public void DbDeleteDatabase_dispatches_commands_to_interceptors_for_connections_without_initial_catalog()
            {
                StoreItemCollection storeItemCollection;
                using (var context = new DdlDatabaseContext())
                {
                    storeItemCollection =
                        (StoreItemCollection)
                        ((IObjectContextAdapter)context).ObjectContext.MetadataWorkspace.GetItemCollection(DataSpace.SSpace);
                }

                using (var connection = new SqlConnection(SimpleAttachConnectionString<DdlDatabaseContext>(useInitialCatalog: false)))
                {
                    if (!SqlProviderServices.Instance.DatabaseExists(connection, null, storeItemCollection))
                    {
                        SqlProviderServices.Instance.CreateDatabase(connection, null, storeItemCollection);
                    }

                    var nonQueryInterceptor = new TestNonQueryInterceptor();
                    var readerInterceptor = new TestReaderInterceptor();
                    Interception.AddInterceptor(nonQueryInterceptor);
                    Interception.AddInterceptor(readerInterceptor);
                    try
                    {
                        SqlProviderServices.Instance.DeleteDatabase(connection, null, storeItemCollection);
                    }
                    finally
                    {
                        Interception.RemoveInterceptor(nonQueryInterceptor);
                        Interception.RemoveInterceptor(readerInterceptor);
                    }

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

                    var commandTexts = nonQueryInterceptor.Commands.Select(c => c.CommandText);
                    Assert.True(commandTexts.Any(t => t.StartsWith("drop database [SYSTEM_DATA_ENTITY_SQLSERVER")));
                    Assert.True(
                        commandTexts.Any(t => t.Contains("SYSTEM.DATA.ENTITY.SQLSERVER.SQLPROVIDERSERVICESTESTS+DDLDATABASECONTEXT.MDF")));

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

                    Assert.True(readerInterceptor.Commands.Select(
                        c => c.CommandText).Single().StartsWith("SELECT [d].[name] FROM sys.databases "));
                }
            }