|
108 | 108 | </li> |
109 | 109 | </ul> |
110 | 110 | </li> |
| 111 | + <li class="toctree-l2"><a class="reference internal" href="#testing">Testing</a> |
| 112 | + <ul> |
| 113 | + <li class="toctree-l3"><a class="reference internal" href="#rollback_session">rollback_session</a> |
| 114 | + </li> |
| 115 | + <li class="toctree-l3"><a class="reference internal" href="#set_test_context">set_test_context</a> |
| 116 | + </li> |
| 117 | + <li class="toctree-l3"><a class="reference internal" href="#put_savepoint_session_in_ctx">put_savepoint_session_in_ctx</a> |
| 118 | + </li> |
| 119 | + </ul> |
| 120 | + </li> |
111 | 121 | </ul> |
112 | 122 | </li> |
113 | 123 | </ul> |
@@ -197,7 +207,10 @@ <h3 id="init">init</h3> |
197 | 207 | <h3 id="connect">connect</h3> |
198 | 208 | <pre><code class="language-python">async def connect(self: DBConnect, host: str) -> None: |
199 | 209 | </code></pre> |
200 | | -<p>Establishes a connection to the specified host. This method doesn’t need to be called explicitly. If it isn’t called, the first session request will automatically establish the connection.</p> |
| 210 | +<p>Establishes a connection to the specified host. |
| 211 | +This method doesn’t need to be called explicitly. |
| 212 | +If it isn’t called, the first session request will automatically |
| 213 | +establish the connection.</p> |
201 | 214 | <hr /> |
202 | 215 | <h3 id="change_host">change_host</h3> |
203 | 216 | <pre><code class="language-python">async def change_host(self: DBConnect, host: str) -> None: |
@@ -349,6 +362,38 @@ <h3 id="run_in_new_ctx">run_in_new_ctx</h3> |
349 | 362 | ), |
350 | 363 | run_in_new_ctx(your_function_with_db_session, ...), |
351 | 364 | ) |
| 365 | +</code></pre> |
| 366 | +<h2 id="testing">Testing</h2> |
| 367 | +<p>You can read more about testing here: <a href="../testing">Testing</a></p> |
| 368 | +<h3 id="rollback_session">rollback_session</h3> |
| 369 | +<pre><code class="language-python">@asynccontextmanager |
| 370 | +async def rollback_session( |
| 371 | + connection: DBConnect, |
| 372 | +) -> AsyncGenerator[AsyncSession]: |
| 373 | +</code></pre> |
| 374 | +<p>A context manager that creates a session which is automatically rolled |
| 375 | +back at the end. |
| 376 | +It’s intended for use in fixtures to execute SQL queries during tests.</p> |
| 377 | +<hr /> |
| 378 | +<h3 id="set_test_context">set_test_context</h3> |
| 379 | +<pre><code class="language-python">@asynccontextmanager |
| 380 | +async def set_test_context() -> AsyncGenerator[None]: |
| 381 | +</code></pre> |
| 382 | +<p>A context manager that creates a new context in which you can place a |
| 383 | +dedicated test session. |
| 384 | +It’s intended for use in tests where the test and the application share |
| 385 | +a single transaction.</p> |
| 386 | +<hr /> |
| 387 | +<h3 id="put_savepoint_session_in_ctx">put_savepoint_session_in_ctx</h3> |
| 388 | +<pre><code class="language-python">async def put_savepoint_session_in_ctx( |
| 389 | + connection: DBConnect, |
| 390 | + session: AsyncSession, |
| 391 | +) -> AsyncGenerator[None]: |
| 392 | +</code></pre> |
| 393 | +<p>Sets the context to a session that uses a save point instead of creating |
| 394 | + a transaction. You need to pass the session you're using inside |
| 395 | + your tests to attach a new session to the same connection.</p> |
| 396 | +<pre><code>It is important to use this function inside set_test_context. |
352 | 397 | </code></pre> |
353 | 398 |
|
354 | 399 | </div> |
|
0 commit comments