def test_sql_debug_log_to_request_adds_attribute(): set_sql_debug(SQL_DEBUG_LEVEL_WORST) request = req('get') set_current_request(request) sql_debug_log_to_request(sql='q', foo='bar') assert request.iommi_sql_debug_log == [dict(sql='q', foo='bar')]
def test_sql_debug_trace_sql_cutoff(caplog): caplog.set_level(logging.DEBUG) set_sql_debug(SQL_DEBUG_LEVEL_WORST) sql_debug_trace_sql('!' * 10_001) assert caplog.records[0].msg.startswith('!' * 10_000) assert caplog.records[0].msg[10_000:] == '... [10001 bytes sql]'
def test_sql_debug_total_time(): set_sql_debug(SQL_DEBUG_LEVEL_WORST) set_current_request(None) assert sql_debug_total_time() == 0.0 request = req('get') set_current_request(request) request.iommi_sql_debug_log = [dict(duration=3), dict(duration=7)] assert sql_debug_total_time() == 10
def test_sql_debug_trace_sql_frame(caplog): caplog.set_level(logging.DEBUG) set_sql_debug(SQL_DEBUG_LEVEL_ALL_WITH_STACKS) with pytest.raises(KeyError): sql_debug_trace_sql('foo') frame = Struct( f_lineno=1, f_back=None, f_locals={}, f_code=Struct( co_name='foo', co_filename='foo.py', ), ) sql_debug_trace_sql('foo', frame=frame) assert caplog.records[0].msg == ' File "foo.py", line 1, in foo =>'
def test_set_sql_debug(): with pytest.raises(AssertionError): set_sql_debug('invalid') set_sql_debug('None') assert get_sql_debug() is None set_sql_debug('worst') assert get_sql_debug() == 'worst' with no_sql_debug(): assert get_sql_debug() is None assert get_sql_debug() == 'worst'