Skip to content

Commit 7ce90cf

Browse files
committed
update docs
1 parent dca2ab1 commit 7ce90cf

File tree

6 files changed

+124
-12
lines changed

6 files changed

+124
-12
lines changed

docs_sources/docs/testing.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ solved - ensuring data isolation between tests.
77
There are basically two approaches:
88

99

10-
1. Separate sessions.
10+
1. Separate sessions
1111

1212
The test has its own session that it uses to prepare data and verify
1313
results after execution.
1414
The application also has its own session.
1515
Data isolation is achieved by clearing all tables at the end of each test
1616
(and once before running all tests).
1717

18-
2. Shared session and transaction.
18+
2. Shared session and transaction
1919
The test and the application share the same session and transaction.
2020
Data isolation is achieved by rolling back the transaction at
2121
the end of the test.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# An example of integration tests.
2+
I recommend this approach because it’s independent of the application’s
3+
architecture and allows you to test your application in a realistic way.
4+
5+
When testing with a real database, one important problem needs to be
6+
solved - ensuring data isolation between tests.
7+
8+
There are basically two approaches:
9+
10+
11+
1. Separate sessions
12+
13+
The test has its own session that it uses to prepare data and verify
14+
results after execution.
15+
The application also has its own session.
16+
Data isolation is achieved by clearing all tables at the end of each test
17+
(and once before running all tests).
18+
19+
2. Shared session and transaction
20+
The test and the application share the same session and transaction.
21+
Data isolation is achieved by rolling back the transaction at
22+
the end of the test.
23+
24+
Personally, I prefer the first option, because it is a more "honest" way
25+
to test the application.
26+
We can verify how it handles sessions and transactions on its own.
27+
It’s also convenient to inspect the database state when a test is paused.
28+
29+
Sometimes, there are complex session management scenarios (for example,
30+
concurrent query execution) where other types of testing are either
31+
impossible or very difficult.
32+
33+
The main disadvantage of this approach is the slower execution speed.
34+
Since we clear all tables after each test, this process takes additional time.
35+
36+
This is where the second approach comes in - its main advantage is speed,
37+
as rolling back a transaction is very fast.
38+
39+
In my projects, I use both approaches at the same time:
40+
41+
- For most tests with simple or common logic, I use a shared transaction
42+
for the test and the application
43+
- For more complex cases, or ones that cannot be tested this way,
44+
I use separate transactions.
45+
46+
47+
This combination allows for both good performance and convenient testing.
48+
49+
The library provides several utilities that can be used in tests - for
50+
example, in fixtures.
51+
They help create tests that share a common transaction between the test
52+
and the application, so data isolation between tests is achieved through
53+
fast transaction rollback.
54+
55+
You can see these capabilities in the examples:
56+
57+
[Here are tests with a common transaction between the
58+
application and the tests.](transactional)
59+
60+
61+
[And here's an example with different transactions.](non_transactional)
Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
11
"""
22
An example of integration tests.
3-
I recommend them because they are independent of the application
4-
architecture and allow you to fairly test your application.
3+
I recommend this approach because it’s independent of the application’s
4+
architecture and allows you to test your application in a realistic way.
5+
6+
When testing with a real database, one important problem needs to be
7+
solved - ensuring data isolation between tests.
8+
9+
There are basically two approaches:
10+
11+
1. Separate sessions
12+
13+
The test has its own session that it uses to prepare data and verify
14+
results after execution.
15+
The application also has its own session.
16+
Data isolation is achieved by clearing all tables at the end of each test
17+
(and once before running all tests).
18+
19+
2. Shared session and transaction
20+
The test and the application share the same session and transaction.
21+
Data isolation is achieved by rolling back the transaction at
22+
the end of the test.
523
"""
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Separate sessions
2+
3+
The test has its own session that it uses to prepare data and verify
4+
results after execution.
5+
The application also has its own session.
6+
Data isolation is achieved by clearing all tables at the end of each test
7+
(and once before running all tests).
8+
9+
10+
We can verify how it handles sessions and transactions on its own.
11+
It’s also convenient to inspect the database state when a test is paused.
12+
13+
Sometimes, there are complex session management scenarios (for example,
14+
concurrent query execution) where other types of testing are either
15+
impossible or very difficult.
16+
17+
The main disadvantage of this approach is the slower execution speed.
18+
Since we clear all tables after each test, this process takes additional time.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Shared session and transaction
2+
3+
The test and the application share the same session and transaction.
4+
Data isolation is achieved by rolling back the transaction at
5+
the end of the test.
6+
7+
The library provides several utilities that can be used in tests - for
8+
example, in fixtures.
9+
They help create tests that share a common transaction between the test
10+
and the application, so data isolation between tests is achieved through
11+
fast transaction rollback.
12+
13+
14+
Its main advantage is speed,
15+
as rolling back a transaction is very fast.
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
"""
2-
An example of fast-running tests, as the transaction for both the test and
3-
the application is shared.
4-
This allows data isolation to be achieved by rolling back the transaction
5-
rather than deleting data from tables.
2+
The test and the application share the same session and transaction.
3+
Data isolation is achieved by rolling back the transaction at
4+
the end of the test.
65
7-
It's not exactly fair testing, because the app doesn't manage the session
8-
itself.
9-
But for most basic tests, it's sufficient.
10-
On the plus side, these tests run faster.
6+
The library provides several utilities that can be used in tests - for
7+
example, in fixtures.
8+
They help create tests that share a common transaction between the test
9+
and the application, so data isolation between tests is achieved through
10+
fast transaction rollback.
1111
"""

0 commit comments

Comments
 (0)