Exemplo n.º 1
0
        public void Disposed_EagerInternalConnection_created_with_existing_connection_can_be_reused()
        {
            var disposed = false;

            using (var connection = new SqlConnection())
            {
                connection.Disposed += (_, __) => disposed = true;

                var internalConnection = new EagerInternalConnection(connection, connectionOwned: false);
                try
                {
                    Assert.Same(connection, internalConnection.Connection);
                    internalConnection.Dispose();
                    Assert.False(disposed);

                    Assert.Same(connection, internalConnection.Connection);
                    internalConnection.Dispose();
                    Assert.False(disposed);
                }
                finally
                {
                    internalConnection.Dispose();
                }
            }
            Assert.True(disposed);
        }
        public void EagerInternalConnection_Dispose_uses_interception()
        {
            var connectionMock = new Mock <DbConnection>(MockBehavior.Strict);

            connectionMock.Setup(m => m.ConnectionString).Returns("fake");
            connectionMock.Setup(m => m.Database).Returns("fakeDb");
            connectionMock.Setup(m => m.DataSource).Returns("fakeSource");
            connectionMock.Protected().Setup("Dispose", ItExpr.IsAny <bool>());
            connectionMock.Setup(m => m.ToString()).Returns("Mock Connection");

            var dbConnectionInterceptorMock = new Mock <IDbConnectionInterceptor>();

            DbInterception.Add(dbConnectionInterceptorMock.Object);
            try
            {
                using (var internalConnection = new EagerInternalConnection(
                           new DbContext(connectionMock.Object, false),
                           connectionMock.Object, connectionOwned: true))
                {
                    var _ = internalConnection.Connection;
                }
            }
            finally
            {
                DbInterception.Remove(dbConnectionInterceptorMock.Object);
            }

            dbConnectionInterceptorMock.Verify(
                m => m.Disposing(It.IsAny <DbConnection>(), It.IsAny <DbConnectionInterceptionContext>()),
                Times.Once());
            dbConnectionInterceptorMock.Verify(
                m => m.Disposed(It.IsAny <DbConnection>(), It.IsAny <DbConnectionInterceptionContext>()),
                Times.Once());
            connectionMock.Protected().Verify("Dispose", Times.Once(), ItExpr.IsAny <bool>());

            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));
            connectionMock.Verify(m => m.DataSource, Times.Exactly(2));

            dbConnectionInterceptorMock.Verify(
                m => m.DatabaseGetting(It.IsAny <DbConnection>(), It.IsAny <DbConnectionInterceptionContext <string> >()),
                Times.Exactly(2));
            dbConnectionInterceptorMock.Verify(
                m => m.DatabaseGot(It.IsAny <DbConnection>(), It.IsAny <DbConnectionInterceptionContext <string> >()),
                Times.Exactly(2));
            connectionMock.Verify(m => m.Database, Times.Exactly(2));

            dbConnectionInterceptorMock.Verify(
                m => m.ConnectionStringGetting(It.IsAny <DbConnection>(), It.IsAny <DbConnectionInterceptionContext <string> >()),
                Times.Exactly(2));
            dbConnectionInterceptorMock.Verify(
                m => m.ConnectionStringGot(It.IsAny <DbConnection>(), It.IsAny <DbConnectionInterceptionContext <string> >()),
                Times.Exactly(2));
            connectionMock.Verify(m => m.ConnectionString, Times.Exactly(2));
        }
 public void Existing_connection_provided_is_returned()
 {
     using (var connection = new SqlConnection())
     using (var internalConnection = new EagerInternalConnection(connection, connectionOwned: false))
     {
         Assert.Same(connection, internalConnection.Connection);
     }
 }
Exemplo n.º 4
0
 public void Existing_connection_provided_is_returned()
 {
     using (var connection = new SqlConnection())
     {
         using (var internalConnection = new EagerInternalConnection(connection, connectionOwned: false))
         {
             Assert.Same(connection, internalConnection.Connection);
         }
     }
 }
Exemplo n.º 5
0
        public void Existing_connection_is_disposed_after_use_if_it_is_owned_by_the_connection_object()
        {
            var disposed   = false;
            var connection = new SqlConnection();

            connection.Disposed += (_, __) => disposed = true;

            using (var internalConnection = new EagerInternalConnection(connection, connectionOwned: true))
            {
                var _ = internalConnection.Connection;
            }

            Assert.True(disposed);
        }
Exemplo n.º 6
0
        public void Existing_connection_is_not_disposed_after_use()
        {
            var disposed = false;

            using (var connection = new SqlConnection())
            {
                connection.Disposed += (_, __) => disposed = true;

                using (var internalConnection = new EagerInternalConnection(connection, connectionOwned: false))
                {
                    var _ = internalConnection.Connection;
                }

                Assert.False(disposed);
            }
            Assert.True(disposed);
        }
        public void Disposed_EagerInternalConnection_created_with_existing_connection_can_be_reused()
        {
            var disposed = false;
            using (var connection = new SqlConnection())
            {
                connection.Disposed += (_, __) => disposed = true;

                var internalConnection = new EagerInternalConnection(connection, connectionOwned: false);
                try
                {
                    Assert.Same(connection, internalConnection.Connection);
                    internalConnection.Dispose();
                    Assert.False(disposed);

                    Assert.Same(connection, internalConnection.Connection);
                    internalConnection.Dispose();
                    Assert.False(disposed);
                }
                finally
                {
                    internalConnection.Dispose();
                }
            }
            Assert.True(disposed);
        }
        public void Existing_connection_is_disposed_after_use_if_it_is_owned_by_the_connection_object()
        {
            var disposed = false;
            var connection = new SqlConnection();

            connection.Disposed += (_, __) => disposed = true;

            using (var internalConnection = new EagerInternalConnection(connection, connectionOwned: true))
            {
                var _ = internalConnection.Connection;
            }

            Assert.True(disposed);
        }
        public void Existing_connection_is_not_disposed_after_use()
        {
            var disposed = false;
            using (var connection = new SqlConnection())
            {
                connection.Disposed += (_, __) => disposed = true;

                using (var internalConnection = new EagerInternalConnection(connection, connectionOwned: false))
                {
                    var _ = internalConnection.Connection;
                }

                Assert.False(disposed);
            }
            Assert.True(disposed);
        }
        public void EagerInternalConnection_Dispose_uses_interception()
        {
            var connectionMock = new Mock<DbConnection>(MockBehavior.Strict);
            connectionMock.Setup(m => m.ConnectionString).Returns("fake");
            connectionMock.Setup(m => m.Database).Returns("fakeDb");
            connectionMock.Setup(m => m.DataSource).Returns("fakeSource");
            connectionMock.Protected().Setup("Dispose", ItExpr.IsAny<bool>());
            connectionMock.Setup(m => m.ToString()).Returns("Mock Connection");

                var dbConnectionInterceptorMock = new Mock<IDbConnectionInterceptor>();
                DbInterception.Add(dbConnectionInterceptorMock.Object);
            try
            {
                using (var internalConnection = new EagerInternalConnection(
                    new DbContext(connectionMock.Object, false),
                    connectionMock.Object, connectionOwned: true))
                {
                    var _ = internalConnection.Connection;
                }
            }
            finally
            {
                DbInterception.Remove(dbConnectionInterceptorMock.Object);
            }

            dbConnectionInterceptorMock.Verify(
                m => m.Disposing(It.IsAny<DbConnection>(), It.IsAny<DbConnectionInterceptionContext>()),
                Times.Once());
            dbConnectionInterceptorMock.Verify(
                m => m.Disposed(It.IsAny<DbConnection>(), It.IsAny<DbConnectionInterceptionContext>()),
                Times.Once());
            connectionMock.Protected().Verify("Dispose", Times.Once(), ItExpr.IsAny<bool>());

            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));
            connectionMock.Verify(m => m.DataSource, Times.Exactly(2));

            dbConnectionInterceptorMock.Verify(
                m => m.DatabaseGetting(It.IsAny<DbConnection>(), It.IsAny<DbConnectionInterceptionContext<string>>()),
                Times.Exactly(2));
            dbConnectionInterceptorMock.Verify(
                m => m.DatabaseGot(It.IsAny<DbConnection>(), It.IsAny<DbConnectionInterceptionContext<string>>()),
                Times.Exactly(2));
            connectionMock.Verify(m => m.Database, Times.Exactly(2));

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