public void OnTransactionRollingBackError()
        {
            // arrange
            var exception      = Mock.Of <Exception>();
            var isolationLevel = IsolationLevel.Chaos;
            var connection     = Mock.Of <DbConnection>();
            var transaction    = Mock.Of <DbTransaction>();
            var source         = new DbDiagnosticListener();
            var operationId    = Guid.NewGuid();

            // act
            var(eventName, payload) = DbDiagnosticSession.Execute(source,
                                                                  () =>
            {
                source.OnTransactionRollingBackError(operationId, isolationLevel, connection, transaction,
                                                     exception, "operation");
            });

            // assert
            Assert.Equal("System.Data.Common.TransactionRollingBackError", eventName);
            var transactionPayload = Assert.IsType <TransactionPayload>(payload);

            Assert.Equal(transaction.GetId(), transactionPayload.TransactionId);
            Assert.Equal(connection, transactionPayload.Connection);
            Assert.Equal(isolationLevel, transactionPayload.IsolationLevel);
            Assert.Equal(operationId, transactionPayload.OperationId);
            Assert.Equal("operation", transactionPayload.Operation);
            Assert.Equal(exception, transactionPayload.Exception);
        }
        public void OnCommandError()
        {
            // arrange
            var operationId = Guid.NewGuid();
            var exception   = Mock.Of <Exception>();
            var connection  = Mock.Of <DbConnection>();
            var command     = Mock.Of <DbCommand>();

            Mock.Get(command).Protected().SetupGet <DbConnection>("DbConnection").Returns(connection);
            var transaction = Mock.Of <DbTransaction>();
            var source      = new DbDiagnosticListener();

            // act
            var(eventName, payload) = DbDiagnosticSession.Execute(source,
                                                                  () => { source.OnCommandError(operationId, command, transaction, exception, "operation"); });

            // assert
            Assert.Equal("System.Data.Common.CommandExecutingError", eventName);
            var commandPayload = Assert.IsType <CommandPayload>(payload);

            Assert.Equal(command, commandPayload.Command);
            Assert.Equal(connection.GetGuid(), commandPayload.ConnectionId);
            Assert.Equal(operationId, commandPayload.OperationId);
            Assert.Equal("operation", commandPayload.Operation);
            Assert.Equal(transaction.GetId(), commandPayload.TransactionId);
            Assert.Equal(exception, commandPayload.Exception);
        }
        public static (string eventName, object?payload) Execute(DbDiagnosticListener listener, Action action)
        {
            string eventName = null !;
            object?payload   = null;

            using var observer = listener.Subscribe(new Observer(pair => (eventName, payload) = pair));
            action();
            return(eventName, payload);
        }
        public void OnConnectionOpening_IsNotEnabled()
        {
            // arrange
            var connection = Mock.Of <DbConnection>();
            var source     = new DbDiagnosticListener();

            // act
            var operationId = source.OnConnectionOpening(connection, "operation");

            // assert
            Assert.Equal(Guid.Empty, operationId);
        }
        public void OnTransactionCommitting_IsNotEnabled()
        {
            // arrange
            var isolationLevel = IsolationLevel.Chaos;
            var connection     = Mock.Of <DbConnection>();
            var transaction    = Mock.Of <DbTransaction>();
            var source         = new DbDiagnosticListener();

            // act
            var operationId = source.OnTransactionCommitting(isolationLevel, connection, transaction, "operation");

            // assert
            Assert.Equal(Guid.Empty, operationId);
        }
        public void WriteOnCommandExecuting_IsNotEnabled()
        {
            // arrange
            var command = Mock.Of <DbCommand>();

            Mock.Get(command).Protected().SetupGet <DbConnection>("DbConnection").Returns(Mock.Of <DbConnection>());
            var transaction = Mock.Of <DbTransaction>();
            var source      = new DbDiagnosticListener();

            // act
            var operationId = source.OnCommandExecuting(command, transaction, "operation");

            // assert
            Assert.Equal(Guid.Empty, operationId);
        }
        public void OnConnectionOpened()
        {
            // arrange
            var operationId = Guid.NewGuid();
            var connection  = Mock.Of <DbConnection>();
            var source      = new DbDiagnosticListener();

            // act
            var(eventName, payload) = DbDiagnosticSession.Execute(source,
                                                                  () => source.OnConnectionOpened(operationId, connection, "operation"));

            // assert
            Assert.Equal("System.Data.Common.ConnectionOpened", eventName);
            var connectionPayload = Assert.IsType <ConnectionPayload>(payload);

            Assert.Equal(connection, connectionPayload.Connection);
            Assert.Equal(connection.GetGuid(), connectionPayload.ConnectionId);
            Assert.Equal(operationId, connectionPayload.OperationId);
            Assert.Equal("operation", connectionPayload.Operation);
            Assert.Null(connectionPayload.Exception);
        }