Skip to main content

Pytest fixture to record and check SQL Queries made by SQLAlchemy

Project description

pytest-sqlguard 🔍🛡️

pytest Python GitHub License CI


pytest-sqlguard is a pytest plugin that helps you safeguard your SQLAlchemy queries against unintended changes. It records all database queries executed during tests and compares them against previously recorded "reference" queries, alerting you to any differences or regressions that may impact SQL functionality or performance.

✅ Easily detect unintended database query changes, regressions

✅ Prevent accidental database performance degradation

✅ Simple integration with SQLAlchemy & pytest

Installation 📦

uv add pytest-sqlguard

Usage 🚀

Configure your fixture as you like and then it can be as simple as:

def test_your_database_logic(sqlguard, session):
    with sqlguard(session):
        test_client.get("/users/random_id")

When you run the test for the first time, pytest-sqlguard records all queries executed during the test and stores them as "reference data".

On subsequent test runs, it compares current queries against the reference, alerting you if any differences are found. The stored data is in a yaml file named as your test file but with the .queries.yaml extension:

test_your_database_logic:
  queries:
  - statement: |-
      SELECT ...
      FROM public.users
      WHERE public.users.id = %(pk_1)s::UUID

Command Line Options ⚙️

The extension provides two configurable behaviors (that defaults to False for both of them)

  1. Fail Missing: Fail if the queries are missing in the reference file.
  2. Overwrite: If set to True, it will overwrite the reference query

You can add option flag to pytest to control those behaviors:

# In your conftest.py
def pytest_addoption(parser):
    sqlguard_group = parser.getgroup("sqlguard", "Options for SQLGuard")
    sqlguard_group.addoption(
        "--sqlguard-fail-missing",
        dest="sqlguard_fail_missing",
        default=False,
        action="store_true",
        help="SQLGuard: Fail if queries are missing in the stored reference file.",
    )
    sqlguard_group.addoption(
        "--sqlguard-overwrite",
        dest="sqlguard_overwrite",
        default=False,
        action="store_true",
        help="SQLGuard: Overwrite the stored reference file.",
    )

For example, to trigger an error if the stored reference queries are missing, run:

pytest --sqlguard-fail-missing

To update reference queries after intended SQL changes, use:

pytest --sqlguard-overwrite

Configuring the Fixture in Tests ⚗️

You need to define your fixture in your conftest.py file. Here is an example of a working fixture:

@pytest.fixture(scope="function")
def sqlguard(request, tmp_path_factory, session):
    # We use the previously defined pytest cli arguments
    fail_on_missing_reference_data = request.config.option.sqlguard_fail_missing
    overwrite_reference_data = request.config.option.sqlguard_overwrite

    from pytest_sqlguard.sqlguard import sqlguard as sqlguard_function

    yield sqlguard_function(
        request,
        tmp_path_factory,
        fail_on_missing_reference_data,
        overwrite_reference_data,
    )

You can also use the assert representation we created to show the differences:

# In conftest.py
from pytest_sqlguard.sqlguard import assertrepr_compare

def pytest_assertrepr_compare(config, op, left, right):
    return assertrepr_compare(config, op, left, right)

Releasing a new version 🚚

# in your local shell
bumpver update  ## Updates the version number, commits the change, tags the commit
git push  # The deployment is made on tag push

Contributing 🤝

Issues, feature requests, and contributions are warmly welcomed! Feel free to open a pull request or submit an issue.

Credits 🙏

This software has been created inside Paylead. Thanks to all the people that made it possible including but not limited to:

License 📜

pytest-sqlguard is distributed under the MIT license. See LICENSE for details.

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

pytest_sqlguard-2025.606.0.tar.gz (19.7 kB view details)

Uploaded Source

Built Distribution

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

pytest_sqlguard-2025.606.0-py3-none-any.whl (11.3 kB view details)

Uploaded Python 3

File details

Details for the file pytest_sqlguard-2025.606.0.tar.gz.

File metadata

  • Download URL: pytest_sqlguard-2025.606.0.tar.gz
  • Upload date:
  • Size: 19.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for pytest_sqlguard-2025.606.0.tar.gz
Algorithm Hash digest
SHA256 01fed7317efe7fd45030f1e13d339e90ffb0d82b6f375d985422e65f52f3fd13
MD5 cbeb8f20803ec963c9f9045791a8ee95
BLAKE2b-256 3ee314bddd63bf6f667598e1ebbe20b6532d0d5eade5a2201bfa46fbf3113ba7

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytest_sqlguard-2025.606.0.tar.gz:

Publisher: sqlguard-ci.yml on PayLead/pytest-sqlguard

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pytest_sqlguard-2025.606.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_sqlguard-2025.606.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d702f68f83d214539896baf5835453986377cf6bfa110c42f3bdfb936e59d2e8
MD5 282d8aadaf85dab0d03ab607b5bf8092
BLAKE2b-256 e0927fb48bc644b34b71136ecc5579a6b125138c0b8619ad2d5d1c1f9d4cbae8

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytest_sqlguard-2025.606.0-py3-none-any.whl:

Publisher: sqlguard-ci.yml on PayLead/pytest-sqlguard

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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