One of my django application unit test fails with
DatabaseError: ORA-00942: table or view does not exist
I would like to see actual SQL query that caused this error. Do you know how to achieve that?
Join Stack Overflow to learn, share knowledge, and build your career.
One of my django application unit test fails with
DatabaseError: ORA-00942: table or view does not exist
I would like to see actual SQL query that caused this error. Do you know how to achieve that?
If you want to print/log all SQL queries from the tests, try subclassing TestCase
like this:
from django.conf import settings
from django.template import Template, Context
import sys
from django.db import connection
from django.test import TestCase
class LoggingTestCase(TestCase):
@staticmethod
def setUpClass():
# The test runner sets DEBUG to False. Set to True to enable SQL logging.
settings.DEBUG = True
super(LoggingTestCase, LoggingTestCase).setUpClass()
@staticmethod
def tearDownClass():
super(LoggingTestCase, LoggingTestCase).tearDownClass()
time = sum([float(q['time']) for q in connection.queries])
t = Template("{{count}} quer{{count|pluralize:\"y,ies\"}} in {{time}} seconds:\n\n{% for sql in sqllog %}[{{forloop.counter}}] {{sql.time}}s: {{sql.sql|safe}}{% if not forloop.last %}\n\n{% endif %}{% endfor %}")
print >> sys.stderr, t.render(Context({'sqllog': connection.queries, 'count': len(connection.queries), 'time': time}))
# Empty the query list between TestCases.
connection.queries = []
Then use LoggingTestCase
instead of TestCase
as the base class in your tests. Just remember to call this tearDownClass
if you override it.
You can also do the following to get the queries (and then for instance print it or evaluate it in your test).
Actually you shouldn't alter django.conf.settings
nowadays, therefore I use override_settings
.
from django.db import connection, reset_queries
from django.test import override_settings, TransactionTestCase
class TransactionTests(TransactionTestCase):
@override_settings(DEBUG=True)
def test_sql(self):
reset_queries()
try:
# Code that uses the ORM goes here
except Exception as e:
pass
self.assertEqual(connection.queries, [])
TestCase
might also be suitable, see the differences in this answer.
See the Django documentation for details for SQL output.
Another option is to use connection.execute_wrapper()
in your test as follows:
from django.db import connection
def logger(execute, sql, params, many, context):
print(sql, params)
return execute(sql, params, many, context)
class GizmoTest(TestCase):
def test_with_sql_logging(self):
with connection.execute_wrapper(logger):
code_that_uses_database()
Tested with Django 2.2.
Another option is to use CaptureQueriesContext
(tested with pytest
).
from django.db import connection
from django.test.utils import CaptureQueriesContext
def test_foo():
with CaptureQueriesContext(connection) as ctx:
# code that runs SQL queries
print(ctx.captured_queries)
Sources:
The best solution I found so far is debugsqlshell custom django management command provided by django-debugtoolbar.
debugsqlshell
command to run a test. That is not explained in the documentation of django-debugtoolbar.
– gogognome
Jul 18 '19 at 13:31
Its not the cleanest solution but if you just quickly want to debug without installing additional packages you could look for the execute() method in django/db.
For Oracle I guess it is in:
django/db/backends/oracle/base.py and look for:
def execute
For PostgreSQL it is in:
django/db/backends/postgresql_psycopg2/base.py
In CursorWrapper there is a execute() method.
Both are catching IntegrityError and DatabaseError, you can add a print statement there.
For ppl who want to see all sql queries, put the print statement right after the function call.
You can change console level to DEBUG in settings. It worked on Django 1.9.
LOGGING = {
...
'handlers': {
'console': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
'formatter': 'simple'
},
}
...
}
In the case of pytest
and pytest-django
just create a fixture for it
@pytest.fixture
def debug_queries(db):
""" Because pytest run tests with DEBUG=False
the regular query logging will not work, use this fixture instead
"""
from django.db import connection
from django.test.utils import CaptureQueriesContext
with CaptureQueriesContext(connection):
yield connection
then in your tests
@pytest.mark.django_db
def test__queries(debug_queries):
# run your queries here
of course, your logging config should enable logging of queries, something like this:
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'standard': {
'format': '%(asctime)s - %(levelname)s - %(name)s - %(message)s',
},
},
'handlers': {
'default': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
'formatter': 'standard',
'stream': 'ext://sys.stdout',
},
},
'loggers': {
'django.db.backends': {
'level': 'DEBUG',
'handlers': ['default'],
'propagate': False,
},
}
}
This was the solution which worked for me (Django 3.1):
from django.test import TestCase
class TestSomething(TestCase):
@override_settings(DEBUG=True)
def test_something(self):
pass
def tearDown(self):
from django.db import connection
for query in connection.queries:
print(f"✅ {query['sql']}\n")
raise
keyword all by itself and it'll pass through with the stack trace intact. – Andrew Gorcester Oct 31 '12 at 16:55