Skip to content

Django integration breaks assertNumQueries #287

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
salomvary opened this issue Dec 13, 2024 · 1 comment
Open

Django integration breaks assertNumQueries #287

salomvary opened this issue Dec 13, 2024 · 1 comment

Comments

@salomvary
Copy link

salomvary commented Dec 13, 2024

If I have sqlcommenter configured for a Django project, including when running tests with the default Django test runner, the assertNumQueries assertion fails with the following exception:

Traceback (most recent call last):
  File "test_something.py", line 4165, in test_some_feature
    with self.assertNumQueries(5):
         ~~~~~~~~~~~~~~~~~~~~~^^^
  File ".venv/lib/python3.13/site-packages/django/test/testcases.py", line 135, in __exit__
    "\n".join(
    ~~~~~~~~~^
        "%d. %s" % (i, query["sql"])
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        for i, query in enumerate(self.captured_queries, start=1)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    ),
    ^
  File ".venv/lib/python3.13/site-packages/django/test/testcases.py", line 136, in <genexpr>
    "%d. %s" % (i, query["sql"])
                   ~~~~~^^^^^^^
TypeError: string indices must be integers, not 'str'

Django version 5.1.4, Python 3.13.1, sqlcommenter 2.0.0.

@florianschieder
Copy link

+1 same for me on django 5.0.11 and sqlcommenter 2.0.0

i assume the error cause to be there in just appending the plain sql string to the connection query log instead of passing a dictionary like

{"sql": QUERY_STRING_HERE,
# possible further stuff django expects
}

https://github.com/google/sqlcommenter/blob/master/python/sqlcommenter-python/google/cloud/sqlcommenter/django/middleware.py#L94C1-L95C58

going to take a look at this next week and will maybe provide a pull request if I find time

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants