|
106 | 106 | <div class="section" itemprop="articleBody"> |
107 | 107 |
|
108 | 108 | <h1 id="testing">Testing</h1> |
109 | | -<p>When testing with a real database, one important problem needs to be |
110 | | -solved - ensuring data isolation between tests.</p> |
111 | | -<p>There are basically two approaches:</p> |
112 | | -<ol> |
113 | | -<li>Separate sessions</li> |
114 | | -</ol> |
115 | | -<p>The test has its own session that it uses to prepare data and verify |
116 | | -results after execution. |
117 | | -The application also has its own session. |
118 | | -Data isolation is achieved by clearing all tables at the end of each test |
119 | | -(and once before running all tests).</p> |
120 | | -<ol> |
121 | | -<li>Shared session and transaction |
122 | | -The test and the application share the same session and transaction. |
123 | | -Data isolation is achieved by rolling back the transaction at |
124 | | -the end of the test.</li> |
125 | | -</ol> |
126 | | -<p>Personally, I prefer the first option, because it is a more "honest" way |
127 | | -to test the application. |
128 | | -We can verify how it handles sessions and transactions on its own. |
129 | | -It’s also convenient to inspect the database state when a test is paused.</p> |
130 | | -<p>Sometimes, there are complex session management scenarios (for example, |
131 | | -concurrent query execution) where other types of testing are either |
132 | | -impossible or very difficult.</p> |
133 | | -<p>The main disadvantage of this approach is the slower execution speed. |
134 | | -Since we clear all tables after each test, this process takes additional time.</p> |
135 | | -<p>This is where the second approach comes in - its main advantage is speed, |
136 | | -as rolling back a transaction is very fast.</p> |
| 109 | +<p>When testing with a real database, one important problem needs to be solved: ensuring data isolation between tests. There are two approaches: |
| 110 | + <ol> |
| 111 | + <li><b>Separate sessions</b>: The test uses its own session that prepares data and verifies results after execution. The application also has its own session. |
| 112 | + <ul> |
| 113 | + <li>It is a more "honest" way to test the application.</li> |
| 114 | + <li>Verifies how it handles sessions and transactions automatically.</li> |
| 115 | + <li>The ability to inspect the database state when the test is paused.</li> |
| 116 | + <li>Complex session management scenarios make other test methods difficult or impossible (e.g., concurrent query execution).</li> |
| 117 | + </ul> |
| 118 | + <li><b>Shared session and transaction:</b> The test and the application share the same session and transaction. |
| 119 | + <ul> |
| 120 | + <li>Rolling back transactions is faster and takes less time than starting a new session.</li> |
| 121 | + </ul> |
| 122 | + </ol> |
| 123 | +</p> |
| 124 | + |
137 | 125 | <p>In my projects, I use both approaches at the same time:</p> |
138 | 126 | <ul> |
139 | | -<li>For most tests with simple or common logic, I use a shared transaction |
140 | | -for the test and the application</li> |
141 | | -<li>For more complex cases, or ones that cannot be tested this way, |
142 | | -I use separate transactions.</li> |
| 127 | +<li>For most tests with simple or common logic, I use a shared transaction for the test and the application</li> |
| 128 | +<li>For more complex cases, or ones that cannot be tested with separate sessions, I use separate transactions.</li> |
143 | 129 | </ul> |
144 | | -<p>This combination allows for both good performance and convenient testing.</p> |
145 | | -<p>The library provides several utilities that can be used in tests - for |
146 | | -example, in fixtures. |
147 | | -They help create tests that share a common transaction between the test |
148 | | -and the application, so data isolation between tests is achieved through |
149 | | -fast transaction rollback.</p> |
| 130 | +<p>The library provides several utilities that can be used in tests (e.g., fixtures). They create tests that share a common transaction between the test |
| 131 | +and the application. You achieve data isolation by rolling back transactions. |
| 132 | + |
150 | 133 | <p>You can see these capabilities in the examples:</p> |
151 | 134 | <p><a href="https://github.com/krylosov-aa/context-async-sqlalchemy/blob/main/examples/fastapi_example/tests/transactional">Here are tests with a common transaction between the |
152 | 135 | application and the tests.</a></p> |
|
0 commit comments