示例#1
0
            public void DbCreateDatabase_dispatches_commands_to_interceptors()
            {
                using (var context = new DdlDatabaseContext(SimpleCeConnection<DdlDatabaseContext>()))
                {
                    var storeItemCollection =
                        (StoreItemCollection)
                        ((IObjectContextAdapter)context).ObjectContext.MetadataWorkspace.GetItemCollection(DataSpace.SSpace);

                    context.Database.Delete();

                    var interceptor = new TestNonQueryInterceptor();
                    Interception.AddInterceptor(interceptor);
                    try
                    {
                        SqlCeProviderServices.Instance.CreateDatabase(context.Database.Connection, null, storeItemCollection);
                    }
                    finally
                    {
                        Interception.RemoveInterceptor(interceptor);
                    }

                    Assert.Equal(3, interceptor.CommandTexts.Count);

                    Assert.True(interceptor.CommandTexts.Any(t => t.StartsWith("CREATE TABLE \"Products\" ")));
                    Assert.True(interceptor.CommandTexts.Any(t => t.StartsWith("CREATE TABLE \"Categories\" ")));
                    Assert.True(interceptor.CommandTexts.Any(t => t.StartsWith("ALTER TABLE \"Products\" ADD CONSTRAINT ")));
                }
            }
示例#2
0
            public void DbCreateDatabase_dispatches_commands_to_interceptors()
            {
                using (var context = new DdlDatabaseContext())
                {
                    var storeItemCollection =
                        (StoreItemCollection)
                        ((IObjectContextAdapter)context).ObjectContext.MetadataWorkspace.GetItemCollection(DataSpace.SSpace);

                    context.Database.Delete();

                    var interceptor = new TestNonQueryInterceptor();
                    DbInterception.Add(interceptor);
                    try
                    {
                        SqlProviderServices.Instance.CreateDatabase(context.Database.Connection, null, storeItemCollection);
                    }
                    finally
                    {
                        DbInterception.Remove(interceptor);
                    }

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

                    var commandTexts = interceptor.Commands.Select(c => c.CommandText);
                    Assert.True(commandTexts.Any(t => t.StartsWith("create database ")));
                    Assert.True(commandTexts.Any(t => t.StartsWith("if serverproperty('EngineEdition') <> 5 execute sp_executesql ")));
                    Assert.True(commandTexts.Any(t => t.StartsWith("create table [dbo].[Categories] ")));
                }
            }
示例#3
0
            public void DbCreateDatabase_dispatches_commands_to_interceptors()
            {
                using (var context = new DdlDatabaseContext(SimpleCeConnection <DdlDatabaseContext>()))
                {
                    var storeItemCollection =
                        (StoreItemCollection)
                        ((IObjectContextAdapter)context).ObjectContext.MetadataWorkspace.GetItemCollection(DataSpace.SSpace);

                    context.Database.Delete();

                    var interceptor = new TestNonQueryInterceptor();
                    Interception.AddInterceptor(interceptor);
                    try
                    {
                        SqlCeProviderServices.Instance.CreateDatabase(context.Database.Connection, null, storeItemCollection);
                    }
                    finally
                    {
                        Interception.RemoveInterceptor(interceptor);
                    }

                    Assert.Equal(3, interceptor.CommandTexts.Count);

                    Assert.True(interceptor.CommandTexts.Any(t => t.StartsWith("CREATE TABLE \"Products\" ")));
                    Assert.True(interceptor.CommandTexts.Any(t => t.StartsWith("CREATE TABLE \"Categories\" ")));
                    Assert.True(interceptor.CommandTexts.Any(t => t.StartsWith("ALTER TABLE \"Products\" ADD CONSTRAINT ")));
                }
            }
示例#4
0
            public void DbDeleteDatabase_dispatches_commands_to_interceptors_for_connections_with_initial_catalog()
            {
                using (var context = new DdlDatabaseContext())
                {
                    context.Database.CreateIfNotExists();
                }

                var interceptor = new TestNonQueryInterceptor();

                DbInterception.Add(interceptor);
                try
                {
                    using (var connection = new SqlConnection(SimpleAttachConnectionString <DdlDatabaseContext>()))
                    {
                        SqlProviderServices.Instance.DeleteDatabase(connection, null, new StoreItemCollection());
                    }
                }
                finally
                {
                    DbInterception.Remove(interceptor);
                }

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

                Assert.Equal(
                    "drop database [System.Data.Entity.SqlServer.SqlProviderServicesTests+DdlDatabaseContext]",
                    interceptor.Commands.Select(c => c.CommandText).Single());
            }
示例#5
0
            public void DbDeleteDatabase_dispatches_to_interceptors()
            {
                using (var context = new DdlDatabaseContext(SimpleCeConnection <DdlDatabaseContext>()))
                {
                    context.Database.CreateIfNotExists();

                    var dbConnectionInterceptorMock = new Mock <IDbConnectionInterceptor>();
                    DbInterception.Add(dbConnectionInterceptorMock.Object);
                    try
                    {
                        SqlCeProviderServices.Instance.DeleteDatabase(context.Database.Connection, null, new StoreItemCollection());
                    }
                    finally
                    {
                        DbInterception.Remove(dbConnectionInterceptorMock.Object);
                    }

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

                    dbConnectionInterceptorMock.Verify(
                        m => m.DataSourceGetting(It.IsAny <DbConnection>(), It.IsAny <DbConnectionInterceptionContext <string> >()),
                        Times.Once());
                    dbConnectionInterceptorMock.Verify(
                        m => m.DataSourceGot(It.IsAny <DbConnection>(), It.IsAny <DbConnectionInterceptionContext <string> >()),
                        Times.Once());
                }
            }
示例#6
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 "));
                }
            }
示例#7
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 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 DbDeleteDatabase_dispatches_to_interceptors_for_connections_with_initial_catalog()
            {
                using (var context = new DdlDatabaseContext())
                {
                    context.Database.CreateIfNotExists();
                }

                var interceptor = new TestNonQueryInterceptor();
                DbInterception.Add(interceptor);
                var dbConnectionInterceptorMock = new Mock<IDbConnectionInterceptor>();
                DbInterception.Add(dbConnectionInterceptorMock.Object);
                try
                {
                    using (var connection = new SqlConnection(SimpleAttachConnectionString<DdlDatabaseContext>()))
                    {
                        SqlProviderServices.Instance.DeleteDatabase(connection, null, new StoreItemCollection());
                    }
                }
                finally
                {
                    DbInterception.Remove(interceptor);
                    DbInterception.Remove(dbConnectionInterceptorMock.Object);
                }

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

                Assert.Equal(
                    "drop database [System.Data.Entity.SqlServer.SqlProviderServicesTests+DdlDatabaseContext]",
                    interceptor.Commands.Select(c => c.CommandText).Single());

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

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

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

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

                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 DbCreateDatabase_dispatches_to_interceptors()
            {
                using (var context = new DdlDatabaseContext())
                {
                    var storeItemCollection =
                        (StoreItemCollection)
                        ((IObjectContextAdapter)context).ObjectContext.MetadataWorkspace.GetItemCollection(DataSpace.SSpace);

                    context.Database.Delete();

                    var interceptor = new TestNonQueryInterceptor();
                    DbInterception.Add(interceptor);
                    var dbConnectionInterceptorMock = new Mock<IDbConnectionInterceptor>();
                    DbInterception.Add(dbConnectionInterceptorMock.Object);
                    try
                    {
                        SqlProviderServices.Instance.CreateDatabase(context.Database.Connection, null, storeItemCollection);
                    }
                    finally
                    {
                        DbInterception.Remove(interceptor);
                        DbInterception.Remove(dbConnectionInterceptorMock.Object);
                    }

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

                    var commandTexts = interceptor.Commands.Select(c => c.CommandText);
                    Assert.True(commandTexts.Any(t => t.StartsWith("create database ")));
                    Assert.True(commandTexts.Any(t => t.StartsWith("if serverproperty('EngineEdition') <> 5 execute sp_executesql ")));
                    Assert.True(commandTexts.Any(t => t.StartsWith("create table [dbo].[Categories] ")));

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

                    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 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 "));
                }
            }
示例#12
0
            public void DbCreateDatabase_dispatches_to_interceptors()
            {
                using (var context = new DdlDatabaseContext(SimpleCeConnection <DdlDatabaseContext>()))
                {
                    var storeItemCollection =
                        (StoreItemCollection)
                        ((IObjectContextAdapter)context).ObjectContext.MetadataWorkspace.GetItemCollection(DataSpace.SSpace);

                    context.Database.Delete();

                    var interceptor = new TestNonQueryInterceptor();
                    DbInterception.Add(interceptor);
                    var dbConnectionInterceptorMock = new Mock <IDbConnectionInterceptor>();
                    DbInterception.Add(dbConnectionInterceptorMock.Object);
                    var transactionInterceptorMock = new Mock <IDbTransactionInterceptor>();
                    DbInterception.Add(transactionInterceptorMock.Object);
                    try
                    {
                        SqlCeProviderServices.Instance.CreateDatabase(context.Database.Connection, null, storeItemCollection);
                    }
                    finally
                    {
                        DbInterception.Remove(interceptor);
                        DbInterception.Remove(dbConnectionInterceptorMock.Object);
                        DbInterception.Remove(transactionInterceptorMock.Object);
                    }

                    Assert.Equal(3, interceptor.CommandTexts.Count);

                    Assert.True(interceptor.CommandTexts.Any(t => t.StartsWith("CREATE TABLE \"Products\" ")));
                    Assert.True(interceptor.CommandTexts.Any(t => t.StartsWith("CREATE TABLE \"Categories\" ")));
                    Assert.True(interceptor.CommandTexts.Any(t => t.StartsWith("ALTER TABLE \"Products\" ADD CONSTRAINT ")));

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

                    dbConnectionInterceptorMock.Verify(
                        m => m.BeginningTransaction(It.IsAny <DbConnection>(), It.IsAny <BeginTransactionInterceptionContext>()),
                        Times.Once());
                    dbConnectionInterceptorMock.Verify(
                        m => m.BeganTransaction(It.IsAny <DbConnection>(), It.IsAny <BeginTransactionInterceptionContext>()),
                        Times.Once());

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

                    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());

                    transactionInterceptorMock.Verify(
                        m => m.Committing(It.IsAny <DbTransaction>(), It.IsAny <DbTransactionInterceptionContext>()),
                        Times.Once());
                    transactionInterceptorMock.Verify(
                        m => m.Committed(It.IsAny <DbTransaction>(), It.IsAny <DbTransactionInterceptionContext>()),
                        Times.Once());
                }
            }
            public void DbCreateDatabase_dispatches_commands_to_interceptors()
            {
                using (var context = new DdlDatabaseContext())
                {
                    var storeItemCollection =
                        (StoreItemCollection)
                        ((IObjectContextAdapter)context).ObjectContext.MetadataWorkspace.GetItemCollection(DataSpace.SSpace);

                    context.Database.Delete();

                    var interceptor = new TestNonQueryInterceptor();
                    Interception.AddInterceptor(interceptor);
                    try
                    {
                        SqlProviderServices.Instance.CreateDatabase(context.Database.Connection, null, storeItemCollection);
                    }
                    finally
                    {
                        Interception.RemoveInterceptor(interceptor);
                    }

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

                    var commandTexts = interceptor.Commands.Select(c => c.CommandText);
                    Assert.True(commandTexts.Any(t => t.StartsWith("create database ")));
                    Assert.True(commandTexts.Any(t => t.StartsWith("if serverproperty('EngineEdition') <> 5 execute sp_executesql ")));
                    Assert.True(commandTexts.Any(t => t.StartsWith("create table [dbo].[Categories] ")));
                }
            }
            public void DbCreateDatabase_dispatches_to_interceptors()
            {
                using (var context = new DdlDatabaseContext(SimpleCeConnection<DdlDatabaseContext>()))
                {
                    var storeItemCollection =
                        (StoreItemCollection)
                            ((IObjectContextAdapter)context).ObjectContext.MetadataWorkspace.GetItemCollection(DataSpace.SSpace);

                    context.Database.Delete();

                    var interceptor = new TestNonQueryInterceptor();
                    DbInterception.Add(interceptor);
                    var dbConnectionInterceptorMock = new Mock<IDbConnectionInterceptor>();
                    DbInterception.Add(dbConnectionInterceptorMock.Object);
                    var transactionInterceptorMock = new Mock<IDbTransactionInterceptor>();
                    DbInterception.Add(transactionInterceptorMock.Object);
                    try
                    {
                        SqlCeProviderServices.Instance.CreateDatabase(context.Database.Connection, null, storeItemCollection);
                    }
                    finally
                    {
                        DbInterception.Remove(interceptor);
                        DbInterception.Remove(dbConnectionInterceptorMock.Object);
                        DbInterception.Remove(transactionInterceptorMock.Object);
                    }

                    Assert.Equal(3, interceptor.CommandTexts.Count);

                    Assert.True(interceptor.CommandTexts.Any(t => t.StartsWith("CREATE TABLE \"Products\" ")));
                    Assert.True(interceptor.CommandTexts.Any(t => t.StartsWith("CREATE TABLE \"Categories\" ")));
                    Assert.True(interceptor.CommandTexts.Any(t => t.StartsWith("ALTER TABLE \"Products\" ADD CONSTRAINT ")));
                    
                    dbConnectionInterceptorMock.Verify(
                        m => m.ConnectionStringGetting(It.IsAny<DbConnection>(), It.IsAny<DbConnectionInterceptionContext<string>>()),
                        Times.Once());
                    dbConnectionInterceptorMock.Verify(
                        m => m.ConnectionStringGot(It.IsAny<DbConnection>(), It.IsAny<DbConnectionInterceptionContext<string>>()),
                        Times.Once());

                    dbConnectionInterceptorMock.Verify(
                        m => m.BeginningTransaction(It.IsAny<DbConnection>(), It.IsAny<BeginTransactionInterceptionContext>()),
                        Times.Once());
                    dbConnectionInterceptorMock.Verify(
                        m => m.BeganTransaction(It.IsAny<DbConnection>(), It.IsAny<BeginTransactionInterceptionContext>()),
                        Times.Once());

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

                    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());

                    transactionInterceptorMock.Verify(
                        m => m.Committing(It.IsAny<DbTransaction>(), It.IsAny<DbTransactionInterceptionContext>()),
                        Times.Once());
                    transactionInterceptorMock.Verify(
                        m => m.Committed(It.IsAny<DbTransaction>(), It.IsAny<DbTransactionInterceptionContext>()),
                        Times.Once());
                }
            }
            public void DbDeleteDatabase_dispatches_to_interceptors()
            {
                using (var context = new DdlDatabaseContext(SimpleCeConnection<DdlDatabaseContext>()))
                {
                    context.Database.CreateIfNotExists();

                    var dbConnectionInterceptorMock = new Mock<IDbConnectionInterceptor>();
                    DbInterception.Add(dbConnectionInterceptorMock.Object);
                    try
                    {
                        SqlCeProviderServices.Instance.DeleteDatabase(context.Database.Connection, null, new StoreItemCollection());
                    }
                    finally
                    {
                        DbInterception.Remove(dbConnectionInterceptorMock.Object);
                    }

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

                    dbConnectionInterceptorMock.Verify(
                        m => m.DataSourceGetting(It.IsAny<DbConnection>(), It.IsAny<DbConnectionInterceptionContext<string>>()),
                        Times.Once());
                    dbConnectionInterceptorMock.Verify(
                        m => m.DataSourceGot(It.IsAny<DbConnection>(), It.IsAny<DbConnectionInterceptionContext<string>>()),
                        Times.Once());
                }
            }
            public void DbCreateDatabase_dispatches_to_interceptors()
            {
                using (var context = new DdlDatabaseContext())
                {
                    var storeItemCollection =
                        (StoreItemCollection)
                        ((IObjectContextAdapter)context).ObjectContext.MetadataWorkspace.GetItemCollection(DataSpace.SSpace);

                    context.Database.Delete();

                    var interceptor = new TestNonQueryInterceptor();
                    DbInterception.Add(interceptor);
                    var dbConnectionInterceptorMock = new Mock <IDbConnectionInterceptor>();
                    DbInterception.Add(dbConnectionInterceptorMock.Object);
                    try
                    {
                        SqlProviderServices.Instance.CreateDatabase(context.Database.Connection, null, storeItemCollection);
                    }
                    finally
                    {
                        DbInterception.Remove(interceptor);
                        DbInterception.Remove(dbConnectionInterceptorMock.Object);
                    }

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

                    var commandTexts = interceptor.Commands.Select(c => c.CommandText);
                    Assert.True(commandTexts.Any(t => t.StartsWith("create database ")));
                    Assert.True(commandTexts.Any(t => t.StartsWith("if serverproperty('EngineEdition') <> 5 execute sp_executesql ")));
                    Assert.True(commandTexts.Any(t => t.StartsWith("create table [dbo].[Categories] ")));

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

                    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 DbDeleteDatabase_dispatches_to_interceptors_for_connections_with_initial_catalog()
            {
                using (var context = new DdlDatabaseContext())
                {
                    context.Database.CreateIfNotExists();
                }

                var interceptor = new TestNonQueryInterceptor();

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

                DbInterception.Add(dbConnectionInterceptorMock.Object);
                try
                {
                    using (var connection = new SqlConnection(SimpleAttachConnectionString <DdlDatabaseContext>()))
                    {
                        SqlProviderServices.Instance.DeleteDatabase(connection, null, new StoreItemCollection());
                    }
                }
                finally
                {
                    DbInterception.Remove(interceptor);
                    DbInterception.Remove(dbConnectionInterceptorMock.Object);
                }

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

                Assert.Equal(
                    "drop database [System.Data.Entity.SqlServer.SqlProviderServicesTests+DdlDatabaseContext]",
                    interceptor.Commands.Select(c => c.CommandText).Single());

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

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

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

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

                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 DbDeleteDatabase_dispatches_commands_to_interceptors_for_connections_with_initial_catalog()
            {
                using (var context = new DdlDatabaseContext())
                {
                    context.Database.CreateIfNotExists();
                }

                var interceptor = new TestNonQueryInterceptor();
                Interception.AddInterceptor(interceptor);
                try
                {
                    using (var connection = new SqlConnection(SimpleAttachConnectionString<DdlDatabaseContext>()))
                    {
                        SqlProviderServices.Instance.DeleteDatabase(connection, null, new StoreItemCollection());
                    }
                }
                finally
                {
                    Interception.RemoveInterceptor(interceptor);
                }

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

                Assert.Equal(
                    "drop database [System.Data.Entity.SqlServer.SqlProviderServicesTests+DdlDatabaseContext]",
                    interceptor.Commands.Select(c => c.CommandText).Single());
            }