Skip to main content

Extension of assertNumQueries that can record queries that were executed and show the differences in future runs.

Project description

https://badge.fury.io/py/dj-test-queries.svg https://codecov.io/gh/PetrDlouhy/django-test-queries/branch/master/graph/badge.svg

Extension of assertNumQueries that can record queries that were executed and show the differences in future runs. It also shows tracebac of where the query was called.

Documentation

The full documentation is at https://dj-test-queries.readthedocs.io.

Quickstart

Install dj-test-queries:

pip install dj-test-queries

Apply NumQueriesMixin to your test and use assertNumQueries as you would normally do:

from test_queries import NumQueriesMixin

class XYZTests(NumQueriesMixin, TestCase):
    def test_xyz(self):
        with self.assertNumQueries(3):
            xyz()

Generating SQL log records

Run the tests with TEST_QUERIES_REWRITE_SQLLOGS environment variable to generate sqllog files:

TEST_QUERIES_REWRITE_SQLLOGS="true" manage.py test

Files like test_views.XYZTests.test_xyz.sqllog will appear in sqllog directory next to your test_views.py file.

If you will run the test next time and the queries will differ from previous, the test will print out output detailing the change with stacktrace from where the query was executed. You can also enlist the *.sqllog files to your repository to see the changes.

If the tests are executed without the TEST_QUERIES_REWRITE_SQLLOGS environment variable, the logs are created to files named like test_views.XYZTests.test_xyz.sqllog to make possible to compare the difference.

Running Tests

Does the code actually work?

source <YOURVIRTUALENV>/bin/activate
(myenv) $ pip install tox
(myenv) $ tox

Development commands

pip install -r requirements_dev.txt
invoke -l

Credits

Tools used in rendering this package:

History

0.3.0 (2024-05-16)

  • output messages to the error report and replace Django SQL output

  • sort queries before diffing

  • store stacktrace and other information in .sqllog.full_record file

0.2.2 (2023-08-26)

  • Real fix for DDT 3.8.1

0.2.1 (2023-08-24)

  • Remove DDT version requirements

0.2.0 (2023-08-15)

  • Fixes for new version of DDT (tested in 3.8.1 - 4.2.0)

  • Automatic testing

0.1.0 (2023-04-21)

  • First release on PyPI.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dj-test-queries-0.3.0.tar.gz (20.9 kB view details)

Uploaded Source

Built Distribution

dj_test_queries-0.3.0-py2.py3-none-any.whl (6.1 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file dj-test-queries-0.3.0.tar.gz.

File metadata

  • Download URL: dj-test-queries-0.3.0.tar.gz
  • Upload date:
  • Size: 20.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.6

File hashes

Hashes for dj-test-queries-0.3.0.tar.gz
Algorithm Hash digest
SHA256 f3d2ff683eafe0b5b528b4751d5481ad0efb07a17b38bd6cdfb078f54adff916
MD5 3f0628ba328bff4da1704095351996af
BLAKE2b-256 2c5db35add8c39fa610b0a5e4043841510b478debb75eaa0cff05cd2393e7b23

See more details on using hashes here.

File details

Details for the file dj_test_queries-0.3.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for dj_test_queries-0.3.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 8cf6a36559a4147c0f3468568a7caec0984ebea2277a5fcdff7da95a728e5851
MD5 721660322be7b67e449f2d53b0c0285e
BLAKE2b-256 67c645522bf10d2047413c8af98d3544284f435528d0b8b0c224085aa86849dc

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page