Pytest fixture to record and check SQL Queries made by SQLAlchemy
Project description
pytest-sqlguard 🔍🛡️
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)
- Fail Missing: Fail if the queries are missing in the reference file.
- 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
01fed7317efe7fd45030f1e13d339e90ffb0d82b6f375d985422e65f52f3fd13
|
|
| MD5 |
cbeb8f20803ec963c9f9045791a8ee95
|
|
| BLAKE2b-256 |
3ee314bddd63bf6f667598e1ebbe20b6532d0d5eade5a2201bfa46fbf3113ba7
|
Provenance
The following attestation bundles were made for pytest_sqlguard-2025.606.0.tar.gz:
Publisher:
sqlguard-ci.yml on PayLead/pytest-sqlguard
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pytest_sqlguard-2025.606.0.tar.gz -
Subject digest:
01fed7317efe7fd45030f1e13d339e90ffb0d82b6f375d985422e65f52f3fd13 - Sigstore transparency entry: 230723858
- Sigstore integration time:
-
Permalink:
PayLead/pytest-sqlguard@d6bf31cf0671af05092a9989f2b07c5eaa43637a -
Branch / Tag:
refs/tags/2025.606.0 - Owner: https://github.com/PayLead
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
sqlguard-ci.yml@d6bf31cf0671af05092a9989f2b07c5eaa43637a -
Trigger Event:
push
-
Statement type:
File details
Details for the file pytest_sqlguard-2025.606.0-py3-none-any.whl.
File metadata
- Download URL: pytest_sqlguard-2025.606.0-py3-none-any.whl
- Upload date:
- Size: 11.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d702f68f83d214539896baf5835453986377cf6bfa110c42f3bdfb936e59d2e8
|
|
| MD5 |
282d8aadaf85dab0d03ab607b5bf8092
|
|
| BLAKE2b-256 |
e0927fb48bc644b34b71136ecc5579a6b125138c0b8619ad2d5d1c1f9d4cbae8
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pytest_sqlguard-2025.606.0-py3-none-any.whl -
Subject digest:
d702f68f83d214539896baf5835453986377cf6bfa110c42f3bdfb936e59d2e8 - Sigstore transparency entry: 230723866
- Sigstore integration time:
-
Permalink:
PayLead/pytest-sqlguard@d6bf31cf0671af05092a9989f2b07c5eaa43637a -
Branch / Tag:
refs/tags/2025.606.0 - Owner: https://github.com/PayLead
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
sqlguard-ci.yml@d6bf31cf0671af05092a9989f2b07c5eaa43637a -
Trigger Event:
push
-
Statement type: