You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<liclass="toctree-l2"><aclass="reference internal" href="#creates-a-new-session-used-internally-by-the-library-you-may-never-need-to-call-it-directly">Creates a new session. Used internally by the library. You may never need to call it directly.</a>
<p>Creates a new session the library uses internally.
229
-
You never need to call it directly. (Only maybe in some special cases.)</p>
230
-
<hr/>
231
+
<h2id="creates-a-new-session-used-internally-by-the-library-you-may-never-need-to-call-it-directly">Creates a new session. Used internally by the library. You may never need to call it directly.</h2>
<h1id="masterreplica-or-several-databases-at-the-same-time">Using multiple databases at the same time (master/replica)</h1>
107
-
<p><code>db_session</code> and other functions accept a <code>DBConnect</code> instance as input. This approach allows you to work with multiple hosts simultaneously. This system allows you to manage scalability and version control.</p>
108
-
<ul>
109
-
<li><b>Master:</b> The primary database which runs the "live" version of your information, the location of your primary data set. The master database
110
-
handles INSERT, DELETE, and UPDATE API calls.
111
-
</li>
112
-
<li><b>Replicas:</b> Secondary database(s) which run READ-ONLY versions of your information and receive updates from the master database.</li>
<p><code>db_session</code> and other functions accept a <code>DBConnect</code> instance as
108
+
input.
109
+
This approach allows you to work with multiple hosts simultaneously.
110
+
This system allows you to manage scalability and reliability.</p>
111
+
<ul>
112
+
<li><strong>Master</strong> (also called <strong>Primary</strong>): The main database host that handles all write operations (INSERT, UPDATE, DELETE) and read operations. It is the single source of truth</li>
113
+
<li><strong>Replica</strong> (also called <strong>Standby</strong> or <strong>Secondary</strong>): Secondary database host(s) which run READ-ONLY versions of your information and receive updates from the master database.</li>
114
+
</ul>
114
115
<p><code>DBConnect</code> accepts factory functions instead of ready-made objects, making it easy to switch hosts when needed.</p>
115
-
116
116
<p><code>libpq</code> can detect the master and replica when creating an engine,
117
117
but it only does this once - at creation time.
118
118
The <code>before_create_session_handler</code> hook allows you to change the host at
119
119
runtime if the master or replica changes.
120
-
121
-
<p><b>You need third-party functionality to determine which host is the master or the replica.</b></p>
122
-
<h4id="change_host">I have an extremely lightweight microservice <ahref="https://github.com/krylosov-aa/pg-status">pg-status</a> that fits perfectly here</h4>
123
-
<p>The engine is not created when <code>DBConnect</code> initializes, also known as lazy initialization. It is created only on the first request.
120
+
You need third-party functionality to determine which host is the master or the replica.</p>
121
+
<h4id="i-have-an-extremely-lightweight-microservice-pg-status-that-fits-perfectly-here">I have an extremely lightweight microservice <ahref="https://github.com/krylosov-aa/pg-status">pg-status</a> that fits perfectly here.</h4>
122
+
<p>The engine is not created when <code>DBConnect</code> initializes, also known as lazy initialization.
123
+
It is created only on the first request.
124
124
The library uses lazy initialization in many places.</p>
Copy file name to clipboardExpand all lines: docs/testing/index.html
+25-19Lines changed: 25 additions & 19 deletions
Original file line number
Diff line number
Diff line change
@@ -106,30 +106,36 @@
106
106
<divclass="section" itemprop="articleBody">
107
107
108
108
<h1id="testing">Testing</h1>
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
-
109
+
<p>When testing with a real database, one important problem needs to be solved: ensuring data isolation between tests.
110
+
There are two approaches:</p>
111
+
<ol>
112
+
<li>
113
+
<p><strong>Separate sessions</strong>: The test uses its own session that prepares data and
114
+
verifies results after execution. The application also has its own session.</p>
115
+
</li>
116
+
<li>
117
+
<p>It is a more "honest" way to test the application.</p>
118
+
</li>
119
+
<li>Verifies how it handles sessions and transactions automatically.</li>
120
+
<li>
121
+
<p>The ability to inspect the database state when the test is paused.
122
+
Complex session management scenarios make other test methods difficult or impossible (e.g., concurrent query execution).</p>
123
+
</li>
124
+
<li>
125
+
<p><strong>Shared session and transaction</strong>: The test and the application share the same session and transaction.</p>
126
+
</li>
127
+
<li>
128
+
<p>Rolling back transactions is faster and takes less time than starting a new session.</p>
129
+
</li>
130
+
</ol>
125
131
<p>In my projects, I use both approaches at the same time:</p>
126
132
<ul>
127
133
<li>For most tests with simple or common logic, I use a shared transaction for the test and the application</li>
128
134
<li>For more complex cases, or ones that cannot be tested with separate sessions, I use separate transactions.</li>
129
135
</ul>
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
-
136
+
<p>The library provides several utilities that can be used in tests (e.g., fixtures).
137
+
They create tests that share a common transaction between the test
138
+
and the application. You achieve data isolation by rolling back transactions.</p>
133
139
<p>You can see these capabilities in the examples:</p>
134
140
<p><ahref="https://github.com/krylosov-aa/context-async-sqlalchemy/blob/main/examples/fastapi_example/tests/transactional">Here are tests with a common transaction between the
0 commit comments