def test_print_queries_without_tracebacks(self):
     da.start_sql_logging()
     self.execute()
     result = da.stop_sql_logging()
     with stdout() as file:
         da.print_queries(result)
         self.assertEqual(
             '1-2@SQL-stub-database SELECT * FROM bar WHERE bing = 42\n' +
             "-" * 70 + "\n", file.getvalue())
 def test_print_queries_without_tracebacks(self):
     da.start_sql_logging()
     self.execute()
     result = da.stop_sql_logging()
     with stdout() as file:
         da.print_queries(result)
         self.assertEqual(
             '1-2@SQL-stub-database SELECT * FROM bar WHERE bing = 42\n' +
             "-" * 70 + "\n", file.getvalue())
 def test_data_logging_with_conditional_stacktrace_normalized_whitespace(
         self):
     # The whitespace in the SQL is normalized
     da.start_sql_logging(
         tracebacks_if=lambda sql: 'FROM KUMQUAT WHERE' in sql)
     self.execute(statement='SELECT * FROM   kumquat \nWHERE bing = 42')
     result = da.stop_sql_logging()
     self.assertEqual(1, len(result))
     self.assertIsNot(None, result[0]['stack'])
 def test_data_logging_with_conditional_stacktrace_normalized_whitespace(
         self):
     # The whitespace in the SQL is normalized
     da.start_sql_logging(
         tracebacks_if=lambda sql: 'FROM KUMQUAT WHERE' in sql)
     self.execute(statement='SELECT * FROM   kumquat \nWHERE bing = 42')
     result = da.stop_sql_logging()
     self.assertEqual(1, len(result))
     self.assertIsNot(None, result[0]['stack'])
 def test_print_queries_with_tracebacks(self):
     da.start_sql_logging(tracebacks_if=True)
     self.execute()
     result = da.stop_sql_logging()
     with stdout() as file:
         da.print_queries(result)
         self.assertStartsWith(file.getvalue(), '  File "')
         self.assertEndsWith(
             file.getvalue(), "." * 70 + "\n" +
             '1-2@SQL-stub-database SELECT * FROM bar WHERE bing = 42\n' +
             "-" * 70 + "\n")
 def test_data_logging_with_conditional_stacktrace(self):
     # Conditions must be normalized to uppercase.
     da.start_sql_logging(tracebacks_if=lambda sql: 'KUMQUAT' in sql)
     with stderr() as file:
         self.execute()
         self.execute(statement='SELECT * FROM kumquat WHERE bing = 42')
         self.assertEqual('', file.getvalue())
     result = da.stop_sql_logging()
     self.assertEqual(2, len(result))
     self.assertIs(None, result[0]['stack'])
     self.assertIsNot(None, result[1]['stack'])
 def test_print_queries_with_tracebacks(self):
     da.start_sql_logging(tracebacks_if=True)
     self.execute()
     result = da.stop_sql_logging()
     with stdout() as file:
         da.print_queries(result)
         self.assertStartsWith(file.getvalue(), '  File "')
         self.assertEndsWith(
             file.getvalue(), "." * 70 + "\n" +
             '1-2@SQL-stub-database SELECT * FROM bar WHERE bing = 42\n' +
             "-" * 70 + "\n")
 def test_data_logging_with_conditional_stacktrace(self):
     # Conditions must be normalized to uppercase.
     da.start_sql_logging(tracebacks_if=lambda sql: 'KUMQUAT' in sql)
     with stderr() as file:
         self.execute()
         self.execute(statement='SELECT * FROM kumquat WHERE bing = 42')
         self.assertEqual('', file.getvalue())
     result = da.stop_sql_logging()
     self.assertEqual(2, len(result))
     self.assertIs(None, result[0]['stack'])
     self.assertIsNot(None, result[1]['stack'])
 def test_data_logging_with_stacktrace(self):
     da.start_sql_logging(tracebacks_if=True)
     with stderr() as file:
         self.execute()
         self.assertEqual('', file.getvalue())
     result = da.stop_sql_logging()
     self.assertEqual(1, len(result))
     self.assertIsNot(None, result[0]['stack'])
     self.assertIs(None, result[0]['exception'])
     self.assertEqual((1, 2, 'SQL-stub-database',
                       'SELECT * FROM bar WHERE bing = 42', None),
                      result[0]['sql'])
 def test_data_logging_with_stacktrace(self):
     da.start_sql_logging(tracebacks_if=True)
     with stderr() as file:
         self.execute()
         self.assertEqual('', file.getvalue())
     result = da.stop_sql_logging()
     self.assertEqual(1, len(result))
     self.assertIsNot(None, result[0]['stack'])
     self.assertIs(None, result[0]['exception'])
     self.assertEqual((1, 2, 'SQL-stub-database',
                       'SELECT * FROM bar WHERE bing = 42', None),
                      result[0]['sql'])
    def test_print_queries_with_exceptions(self):
        def ow(sql):
            raise ValueError('rutebega')

        da.start_sql_logging(tracebacks_if=ow)
        self.execute()
        result = da.stop_sql_logging()
        with stdout() as file:
            da.print_queries(result)
            self.assertStartsWith(
                file.getvalue(),
                'Error when determining whether to generate a stacktrace.\n' +
                'Traceback (most recent call last):\n' + '  File "')
            self.assertEndsWith(
                file.getvalue(), "ValueError: rutebega\n" + "." * 70 + "\n" +
                '1-2@SQL-stub-database SELECT * FROM bar WHERE bing = 42\n' +
                "-" * 70 + "\n")
    def test_data_logging_with_broken_conditional_stacktrace(self):
        error = ValueError('rutebega')

        def ow(sql):
            raise error

        da.start_sql_logging(tracebacks_if=ow)
        with stderr() as file:
            self.execute()
            self.assertEqual('', file.getvalue())
        result = da.stop_sql_logging()
        self.assertEqual(1, len(result))
        self.assertIsNot(None, result[0]['stack'])
        self.assertEqual((ValueError, error), result[0]['exception'])
        self.assertEqual((1, 2, 'SQL-stub-database',
                          'SELECT * FROM bar WHERE bing = 42', None),
                         result[0]['sql'])
    def test_print_queries_with_exceptions(self):
        def ow(sql):
            raise ValueError('rutebega')

        da.start_sql_logging(tracebacks_if=ow)
        self.execute()
        result = da.stop_sql_logging()
        with stdout() as file:
            da.print_queries(result)
            self.assertStartsWith(
                file.getvalue(),
                'Error when determining whether to generate a stacktrace.\n' +
                'Traceback (most recent call last):\n' + '  File "')
            self.assertEndsWith(
                file.getvalue(), "ValueError: rutebega\n" + "." * 70 + "\n" +
                '1-2@SQL-stub-database SELECT * FROM bar WHERE bing = 42\n' +
                "-" * 70 + "\n")
    def test_data_logging_with_broken_conditional_stacktrace(self):
        error = ValueError('rutebega')

        def ow(sql):
            raise error

        da.start_sql_logging(tracebacks_if=ow)
        with stderr() as file:
            self.execute()
            self.assertEqual('', file.getvalue())
        result = da.stop_sql_logging()
        self.assertEqual(1, len(result))
        self.assertIsNot(None, result[0]['stack'])
        self.assertEqual((ValueError, error), result[0]['exception'])
        self.assertEqual((1, 2, 'SQL-stub-database',
                          'SELECT * FROM bar WHERE bing = 42', None),
                         result[0]['sql'])
Exemple #15
0
def _maybe_profile(event):
    """Setup profiling as requested.

    If profiling is enabled, start a profiler for this thread. If memory
    profiling is requested, save the VSS and RSS.

    If already profiling, this is a no-op.
    """
    try:
        if _profilers.profiling:
            # Already profiling - e.g. called in from both start_request and
            # before_traverse, or by successive before_traverse on one
            # request.
            return
    except AttributeError:
        # The first call in on a new thread cannot be profiling at the start.
        pass
    # If this assertion has reason to fail, we'll need to add code
    # to try and stop the profiler before we delete it, in case it is
    # still running.
    assert _profilers.profiler is None
    actions = get_desired_profile_actions(event.request)
    if config.profiling.profile_all_requests:
        actions['callgrind'] = ''
    if config.profiling.memory_profile_log:
        actions['memory_profile_start'] = (memory(), resident())
    if actions:
        if 'sql' in actions:
            condition = actions['sql']
            if condition not in (True, False):
                condition = condition['condition']
            da.start_sql_logging(condition)
        if 'show' in actions or available_profilers.intersection(actions):
            _profilers.profiler = Profiler()
            _profilers.profiler.start()
        _profilers.profiling = True
        _profilers.actions = actions