API Reference
capsqlalchemy_context
¶
capsqlalchemy_context(
db_engine: AsyncEngine,
) -> Generator[SQLAlchemyCaptureContext]
The main fixture to get the SQLAlchemyCaptureContext.
This is the context for the full test, which captures all SQL expressions executed during the test.
To capture only the SQL expressions executed within a specific block, use the
capsqlalchemy fixture.
Source code in pytest_capsqlalchemy/plugin.py
capsqlalchemy
¶
capsqlalchemy(
capsqlalchemy_context: SQLAlchemyCaptureContext,
) -> SQLAlchemyCapturer
The main fixture to get the SQLAlchemyCapturer.
Example Usage:
async def test_some_sql_queries(db_session, capsqlalchemy):
await db_session.execute(select(text("1")))
capsqlalchemy.assert_query_count(1, include_tcl=False)
async with capsqlalchemy:
await db_session.execute(select(text("2")))
await db_session.execute(select(text("3")))
capsqlalchemy.assert_query_count(2, include_tcl=False)
capsqlalchemy.assert_query_count(3, include_tcl=False)
Returns:
| Type | Description |
|---|---|
SQLAlchemyCapturer
|
The capturer object with the full test context already set up. |
Source code in pytest_capsqlalchemy/plugin.py
SQLAlchemyCapturer
¶
The main fixture class for the capsqlalchemy plugin.
Used to perform asserts about the expressions SQLAlchemy has executed during the test.
Can be used either directly using the assert methods (to perform checks for all expressions executed in the test), as a context manager (to perform checks only for the expressions executed in a specific block), or a combination of both.
Intended to be used via the capsqlalchemy fixture
Source code in pytest_capsqlalchemy/capturer.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | |
captured_expressions
property
¶
captured_expressions: list[SQLExpression]
Returns all SQL expressions captured in the current context.
When used outside a context manager block, returns all expressions captured during the entire test. When used inside a context manager block, returns only the expressions captured within that specific block.
This property is useful for performing specific assertions on the captured expressions which cannot be easily achieved with the provided assert methods.
__init__
¶
__init__(full_test_context: SQLAlchemyCaptureContext)
assert_query_types
¶
assert_query_types(
*expected_query_types: Union[SQLExpressionType, str],
include_tcl: bool = True,
) -> None
Asserts that the captured SQL expressions match the expected query types in order.
This is useful for ensuring that your code is generating correct query types but their exact structure is not important (e.g. complex SELECT statements).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*expected_query_types
|
Union[SQLExpressionType, str]
|
Variable number of expected query types |
()
|
include_tcl
|
bool
|
Whether to include transaction control language statements (BEGIN, COMMIT, ROLLBACK) in the comparison |
True
|
Raises:
| Type | Description |
|---|---|
AssertionError
|
If the actual query types don't match the expected ones. |
Source code in pytest_capsqlalchemy/capturer.py
assert_query_count
¶
Asserts that the number of captured SQL expressions matches the expected count.
This is useful for ensuring that your code is not generating more statements than expected (e.g. due to N+1 queries), however the exact queries are not important.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expected_query_count
|
int
|
The expected number of SQL expressions. |
required |
include_tcl
|
bool
|
Whether to include transaction control language statements (BEGIN, COMMIT, ROLLBACK) in the count. |
True
|
Raises:
| Type | Description |
|---|---|
AssertionError
|
If the actual query count doesn't match the expected count. |
Source code in pytest_capsqlalchemy/capturer.py
assert_max_query_count
¶
Asserts that the number of captured SQL expressions doesn't exceed the expected count.
This is useful for ensuring that your code is not generating more statements than expected (e.g. due to N+1 queries), however the exact number of queries is not important -- for example SQLAlchemy's caching mechanism may generate fewer queries than expected.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expected_max_query_count
|
int
|
The expected maximum number of SQL expressions. |
required |
include_tcl
|
bool
|
Whether to include transaction control language statements (BEGIN, COMMIT, ROLLBACK) in the count. |
True
|
Raises:
| Type | Description |
|---|---|
AssertionError
|
If the actual query count exceeds the expected maximum count. |
Source code in pytest_capsqlalchemy/capturer.py
assert_captured_queries
¶
assert_captured_queries(
*expected_queries: str,
include_tcl: bool = True,
bind_params: bool = False,
) -> None
Asserts that the captured SQL queries match the expected SQL strings in order.
This is useful for ensuring that your code is generating the exact SQL statements you expect.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*expected_queries
|
str
|
Variable number of expected SQL query strings. |
()
|
include_tcl
|
bool
|
Whether to include transaction control language statements (BEGIN, COMMIT, ROLLBACK) in the comparison. |
True
|
bind_params
|
bool
|
Whether to include bound parameters in the SQL strings. When |
False
|
Raises:
| Type | Description |
|---|---|
AssertionError
|
If the actual SQL queries don't match the expected ones. |
Source code in pytest_capsqlalchemy/capturer.py
SQLAlchemyCaptureContext
¶
Captures expressions executed on a SQLAlchemy engine within a specific context.
These expressions include:
* SELECT
* INSERT
* UPDATE
* DELETE
* BEGIN
* COMMIT
* ROLLBACK
Every expression is captured as a SQLExpression object, allowing it to be parsed correctly and compared against.
See SQLAlchemyCapturer for the available
assertions on the captured expressions.
Source code in pytest_capsqlalchemy/context.py
captured_expressions
property
¶
captured_expressions: list[SQLExpression]
Returns all SQL expressions captured in the current context.
__init__
¶
Create a new SQLAlchemyCaptureContext instance.
SQLExpressionType
¶
Bases: str, Enum
An enumeration of the different types of SQL expressions that can be captured.
Source code in pytest_capsqlalchemy/expression.py
is_tcl
property
¶
Check if the SQL expression type is a transaction control language statement.
SQLExpression
dataclass
¶
A representation of a single SQL expression captured by SQLAlchemy.
Stores the SQLAlchemy Executable object and any parameters used in the query, so that it can be
compared against expected queries in tests. This is useful for performing specific assertions
on the captured expressions which cannot be easily achieved with the provided assert methods.
Source code in pytest_capsqlalchemy/expression.py
get_sql
¶
Get the SQL string generated by SQLAlchemy of the captured expression.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bind_params
|
bool
|
If True, the SQL string will include the bound parameters in the query. Otherwise the SQL string will contain placeholders for the bound parameters. |
False
|
Returns:
| Type | Description |
|---|---|
str
|
The SQL string of the captured expression |
Source code in pytest_capsqlalchemy/expression.py
temp_sqlalchemy_event
¶
temp_sqlalchemy_event(
target: Any,
identifier: str,
fn: Callable[..., Any],
*args: Any,
**kwargs: Any,
) -> Generator[None, None, None]
Temporarily add a SQLAlchemy event listener to the target object.
The event listener is automatically removed when the context manager exits.