django_assert_queries.query_comparator¶
Utilities for comparing executed queries to expectations.
These are used internally for unit testing, but can also be useful when instrumenting code.
New in version 1.0.
Module Attributes
A type representing list of expected queries. |
Functions
|
Assert the number and complexity of queries. |
Classes
Context for compared query results. |
|
An expected query for a query assertion. |
|
Information on a mismatched query. |
|
An attribute for a query that failed to meet expectations. |
- class django_assert_queries.query_comparator.QueryMismatchedAttr[source]¶
Bases:
TypedDictAn attribute for a query that failed to meet expectations.
New in version 1.0.
- class django_assert_queries.query_comparator.QueryMismatch[source]¶
Bases:
TypedDictInformation on a mismatched query.
New in version 1.0.
- executed_query: Query¶
The query that was executed.
- mismatched_attrs: List[QueryMismatchedAttr]¶
The list of mismatched attributes in this query.
- subqueries: CompareQueriesContext | None¶
The results for any subquery matches.
- class django_assert_queries.query_comparator.ExpectedQuery[source]¶
Bases:
TypedDictAn expected query for a query assertion.
This is used for
django_assert_queries.testing.assert_queries().New in version 1.0.
- annotations: NotRequired[Dict[str, BaseExpression]]¶
A dictionary containing applied annotations.
Keys are destination attribute names, and values are the annotation instances.
The default is empty.
- distinct: NotRequired[bool]¶
Whether
django.db.models.query.QuerySet.distinct()was used.The default is
False.
- distinct_fields: NotRequired[Tuple[str, ...]]¶
A list of distinct fields.
These are field names passed to
django.db.models.query.QuerySet.distinct().The default is empty.
- extra: NotRequired[Dict[str, Any]]¶
Extra state passed in the queryset.
This is state passed in calls to
django.db.models.query.QuerySet.extra()when usingselectandselect_params.Each key maps to a key in
select, and each value is a tuple containing the value inselectand the corresponding value (if any) inselect_params.Values are normalized to collapse and strip whitespace, to help with comparison.
The default is empty.
- extra_order_by: NotRequired[Sequence[str]]¶
Extra order-by values.
This is state passed in calls to
django.db.models.query.QuerySet.extra()when usingorder_by.The default is empty.
- extra_tables: NotRequired[Sequence[str]]¶
Extra tables for the query.
This is state passed in calls to
django.db.models.query.QuerySet.extra()when usingtables.The default is empty.
- group_by: NotRequired[bool | Tuple[str, ...] | None]¶
The grouping of results.
This determines whether no fields will be grouped (
None), all fields will be grouped (True), or specific expressions/field names are grouped (a tuple).This is influenced by using
django.db.models.query.QuerySet.annotate().The default is
None.
- limit: NotRequired[int | None]¶
The value for a
LIMITin theSELECT.This will generally only need to be supplied if testing a query using
QuerySet.exists()or when slicing results.Django itself sometimes uses a default of
Noneand sometimes a default currently of21(this exact value, and when it’s used, is considered an implementation detail in Django). Both of these will match a caller-providedlimitvalue ofNone.The default is
None.
- model: NotRequired[Type[Model]]¶
The model backing the queryset.
This represents the results that would be returned or altered by the query.
- only_fields: NotRequired[Set[str]]¶
The specific fields being fetched, or
Noneif fetching all fields.The default is
None.
- select_for_update: NotRequired[bool]¶
Whether this is a select-for-update operation.
The default is
False.
The table names involved in a select-related operation.
These are the names passed to
django.db.models.query.QuerySet.select_related(). If called without any parameters, this would beTrue.
- subquery: NotRequired[bool]¶
Whether this is considered a subquery of another query.
The default is
False.
- tables: NotRequired[Set[str]]¶
The tables involved in the query.
The default is the model’s table name.
- type: NotRequired[str]¶
The query type.
This would be one of
DELETE,INSERT,SELECT, orUPDATE.The default is
SELECT.
- values_select: NotRequired[Tuple[str, ...]]¶
A list of specified fields to return as values.
These are values passed in
values()orvalues_list().
- where: NotRequired[Q]¶
The query expression objects used to represent the filter on the query.
These are normalized to filter out empty or unnecessarily-nested queries, to ease comparison.
- django_assert_queries.query_comparator.ExpectedQueries¶
A type representing list of expected queries.
Note
This type allows either a
ExpectedQuery()or a plain dictionary of string-based keys. This is because with pyright (as of 1.1.332), the type of the right-hand-side of a+=operation cannot be inferred. That makes it difficult to dynamically build expected queries.Until/unless the situation improves, we aim for an
ExpectedQuery()but allow a plain dictionary.New in version 1.0.
- class django_assert_queries.query_comparator.CompareQueriesContext[source]¶
Bases:
TypedDictContext for compared query results.
This is provided and populated when using
compare_queries().This should not be populated by consumers, only by this library.
New in version 1.0.
- query_mismatches: List[QueryMismatch]¶
The list of query expectation failures.
- django_assert_queries.query_comparator.compare_queries(queries: Sequence[ExpectedQuery | Dict[str, Any]], *, _check_join_types: bool = True, _check_subqueries: bool = True) Iterator[CompareQueriesContext][source]¶
Assert the number and complexity of queries.
This provides advanced checking of queries, allowing the caller to match filtering, JOINs, ordering, selected fields, and more.
This takes a list of dictionaries with query information. Each contains the keys in
ExpectedQuery.New in version 1.0.
- Parameters:
queries (
listofExpectedQuery) – The list of query dictionaries to compare executed queries against._check_join_types (
bool, optional) –Whether to check join types.
This is internal for compatibility with the old behavior for
assert_queries()and will be removed in a future release without a deprecation period._check_subqueries (
bool, optional) –Whether to check subqueries.
This is internal for compatibility with the old behavior for
assert_queries()and will be removed in a future release without a deprecation period.
- Context:
dict– The context for compared queries.This will only be populated after the context manager has finished. See
CompareQueriesContextfor details.