Skip to main content

A Django test mixin that captures and displays SQL query diffs between test runs, helping to track database-related changes in your tests.

Project description

django-sql-test

A Django test mixin that captures and analyzes SQL queries during tests, with built-in support for displaying diffs between previous and current queries for spotting unexpected changes or regressions.

Usage

Requirements

  • Django >= 4.0
  • Python 3.9 and above.

Installation

Install with:

pip install django-sql-test

Quickstart

In your test.py just import NumNewQueriesMixin and add it as a parent:

from django.test import TestCase
from django_sql_test import NumNewQueriesMixin

class FooTest(NumNewQueriesMixin, TestCase):
    def test_bar(self):
        with self.assertNumQueries(2):
            response = self.client.get(self.url)

        self.assertEqual(response.status_code, 200)

What you got before adding NumNewQueriesMixin:

$ poetry run python manage.py test path.to.test.FooTest.test_bar
======================================================================
FAIL: test_bar (path.to.test.FooTest.test_bar)
  ...
AssertionError: 5 != 2 : 5 queries executed, 2 expected
Captured queries were:
1. SELECT "polls_choice"."id", "polls_choice"."question_id", "polls_choice"."choice_text", "polls_choice"."votes" FROM "polls_choice" WHERE "polls_choice"."votes" >= 0
2. SELECT "polls_question"."id", "polls_question"."question_text", "polls_question"."pub_date" FROM "polls_question" WHERE "polls_question"."id" = 1 LIMIT 21
3. SELECT "polls_question"."id", "polls_question"."question_text", "polls_question"."pub_date" FROM "polls_question" WHERE "polls_question"."id" = 1 LIMIT 21
4. SELECT "polls_question"."id", "polls_question"."question_text", "polls_question"."pub_date" FROM "polls_question" WHERE "polls_question"."id" = 1 LIMIT 21
5. SELECT COUNT(*) AS "__count" FROM "polls_question"

What you get after adding NumNewQueriesMixin:

$ poetry run python manage.py test path.to.test.FooTest.test_bar
======================================================================
FAIL: test_bar (path.to.test.FooTest.test_bar)
 ...
AssertionError: 5 != 2 : 5 queries executed, 2 expected
Queries diff:
- SELECT polls_choice.id, polls_choice.question_id, polls_choice.choice_text, polls_choice.votes FROM polls_choice
+ SELECT polls_choice.id, polls_choice.question_id, polls_choice.choice_text, polls_choice.votes FROM polls_choice WHERE polls_choice.votes >= N
+ SELECT polls_question.id, polls_question.question_text, polls_question.pub_date FROM polls_question WHERE polls_question.id = N LIMIT N
+ SELECT polls_question.id, polls_question.question_text, polls_question.pub_date FROM polls_question WHERE polls_question.id = N LIMIT N
+ SELECT polls_question.id, polls_question.question_text, polls_question.pub_date FROM polls_question WHERE polls_question.id = N LIMIT N
  SELECT COUNT(*) AS __count FROM polls_question

Settings

GENERALIZED_DIFF = True

True by default. If set to True, hides all SQL-query parameters replacing them with placeholders:

Queries diff:
+ SELECT polls_choice.id, polls_choice.question_id, polls_choice.choice_text, polls_choice.votes FROM polls_choice WHERE polls_choice.votes >= N

If set to False:

Queries diff:
+ SELECT "polls_choice"."id", "polls_choice"."question_id", "polls_choice"."choice_text", "polls_choice"."votes" FROM "polls_choice" WHERE "polls_choice"."votes" >= 0

DIFF_ONLY = False

False by default. If set, hides all unchanged SQL-queries.

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

django_sql_test-0.2.0.tar.gz (4.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_sql_test-0.2.0-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file django_sql_test-0.2.0.tar.gz.

File metadata

  • Download URL: django_sql_test-0.2.0.tar.gz
  • Upload date:
  • Size: 4.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.1 CPython/3.13.2 Darwin/24.4.0

File hashes

Hashes for django_sql_test-0.2.0.tar.gz
Algorithm Hash digest
SHA256 60e32d5516831315fdbc88ad1b61293785f72bbedcfccfd72f16a5927448c483
MD5 bc0e15ffb27db0ccb875fddcc38aa2e5
BLAKE2b-256 974291d1238b1b38addc04f0188feb72274b935f42bfc3a6f7613e10b25db606

See more details on using hashes here.

File details

Details for the file django_sql_test-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: django_sql_test-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 5.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.1 CPython/3.13.2 Darwin/24.4.0

File hashes

Hashes for django_sql_test-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cba97b28b0099ec2413af24e5251bbd8057ed02999061983686e974569cd89fa
MD5 9d7dbda80fabe345f750bdba132f87b5
BLAKE2b-256 c589fcc3f822cd81126565156d72e9f1c3ba6b2adbee3f4dab987279941fe69d

See more details on using hashes here.

Supported by

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