def test_disable_and_enable(self, mock_after_exc, mock_before_exc):
        sqlalchemy.disable()

        sa = mock.MagicMock()
        engine = mock.MagicMock()
        sqlalchemy.add_tracing(sa, engine, "sql")
        self.assertFalse(mock_after_exc.called)
        self.assertFalse(mock_before_exc.called)

        sqlalchemy.enable()
        sqlalchemy.add_tracing(sa, engine, "sql")
        self.assertTrue(mock_after_exc.called)
        self.assertTrue(mock_before_exc.called)
Exemple #2
0
    def test_disable_and_enable(self, mock_after_exc, mock_before_exc):
        sqlalchemy.disable()

        sa = mock.MagicMock()
        engine = mock.MagicMock()
        sqlalchemy.add_tracing(sa, engine, "sql")
        self.assertFalse(mock_after_exc.called)
        self.assertFalse(mock_before_exc.called)

        sqlalchemy.enable()
        sqlalchemy.add_tracing(sa, engine, "sql")
        self.assertTrue(mock_after_exc.called)
        self.assertTrue(mock_before_exc.called)
Exemple #3
0
    def engine(self):
        uri = self.sqlalchemy_conf.uri
        engine = engines.create_engine(uri, sqlite_fk=True)

        if (uri.startswith('mysql://') or uri.startswith('mysql+pymysql://')):
            # oslo_db.create_engine makes a test connection, throw that out
            # first.   mysql time_zone can be added to oslo_db as a
            # startup option
            engine.dispose()
            sa.event.listen(engine, 'connect', self._mysql_on_connect)

        if (self.conf.profiler.enabled
                and self.conf.profiler.trace_message_store):
            sa_tracer.add_tracing(sa, engine, "db")

        return engine
    def test_add_tracing(self, mock_after_exc, mock_before_exc):
        sa = mock.MagicMock()
        engine = mock.MagicMock()

        mock_before_exc.return_value = "before"
        mock_after_exc.return_value = "after"

        sqlalchemy.add_tracing(sa, engine, "sql")

        mock_before_exc.assert_called_once_with("sql")
        mock_after_exc.assert_called_once_with()
        expected_calls = [
            mock.call(engine, "before_cursor_execute", "before"),
            mock.call(engine, "after_cursor_execute", "after")
        ]
        self.assertEqual(sa.event.listen.call_args_list, expected_calls)
    def test_add_tracing(self, mock_after_exc, mock_before_exc):
        sa = mock.MagicMock()
        engine = mock.MagicMock()

        mock_before_exc.return_value = "before"
        mock_after_exc.return_value = "after"

        sqlalchemy.add_tracing(sa, engine, "sql")

        mock_before_exc.assert_called_once_with("sql")
        mock_after_exc.assert_called_once_with()
        expected_calls = [
            mock.call(engine, "before_cursor_execute", "before"),
            mock.call(engine, "after_cursor_execute", "after")
        ]
        self.assertEqual(sa.event.listen.call_args_list, expected_calls)
Exemple #6
0
    def engine(self, *args, **kwargs):
        uri = self.sqlalchemy_conf.uri
        engine = sa.create_engine(uri, **kwargs)

        # TODO(flaper87): Find a better way
        # to do this.
        if uri.startswith('sqlite://'):
            sa.event.listen(engine, 'connect', self._sqlite_on_connect)

        if (uri.startswith('mysql://') or uri.startswith('mysql+pymysql://')):
            sa.event.listen(engine, 'connect', self._mysql_on_connect)

        if (self.conf.profiler.enabled
                and self.conf.profiler.trace_message_store):
            sa_tracer.add_tracing(sa, engine, "db")

        return engine
Exemple #7
0
    def engine(self):
        uri = self.sqlalchemy_conf.uri
        engine = engines.create_engine(uri, sqlite_fk=True)

        if (uri.startswith('mysql://') or
                uri.startswith('mysql+pymysql://')):
            # oslo_db.create_engine makes a test connection, throw that out
            # first.   mysql time_zone can be added to oslo_db as a
            # startup option
            engine.dispose()
            sa.event.listen(engine, 'connect',
                            self._mysql_on_connect)

        if (self.conf.profiler.enabled and
                self.conf.profiler.trace_message_store):
            sa_tracer.add_tracing(sa, engine, "db")

        return engine
Exemple #8
0
    def test_add_tracing(self, mock_after_exc, mock_before_exc,
                         mock_handle_error):
        sa = mock.MagicMock()
        engine = mock.MagicMock()

        mock_before_exc.return_value = "before"
        mock_after_exc.return_value = "after"

        sqlalchemy.add_tracing(sa, engine, "sql")

        mock_before_exc.assert_called_once_with("sql")
        # Default set hide_result=True
        mock_after_exc.assert_called_once_with(hide_result=True)
        expected_calls = [
            mock.call(engine, "before_cursor_execute", "before"),
            mock.call(engine, "after_cursor_execute", "after"),
            mock.call(engine, "handle_error", mock_handle_error),
        ]
        self.assertEqual(sa.event.listen.call_args_list, expected_calls)