|
7 | 7 | The issue was that calling initializer() would clear connections, |
8 | 8 | making the database unusable until _restore_default() was called. |
9 | 9 | This broke pytest fixtures that expected the DB to be ready after initializer(). |
| 10 | +
|
| 11 | +NOTE: These tests are skipped when running with pytest-xdist in parallel mode |
| 12 | +because they use create_db which resets global state and interferes with other tests. |
| 13 | +Run them separately with: pytest tests/contrib/test_pytest_initializer.py -n0 |
10 | 14 | """ |
11 | 15 |
|
12 | 16 | import os |
|
15 | 19 | import pytest |
16 | 20 | import pytest_asyncio |
17 | 21 |
|
18 | | -from tortoise import Tortoise, connections |
19 | | -from tortoise.contrib.test import create_db, finalizer, initializer |
20 | | - |
21 | | - |
22 | | -class TestPytestInitializer: |
23 | | - """Test the initializer/finalizer pattern used in pytest fixtures.""" |
24 | | - |
25 | | - def test_initializer_keeps_db_usable(self): |
26 | | - """ |
27 | | - Test that after calling initializer(), the database is immediately usable. |
28 | | -
|
29 | | - This is the core fix for issue #1110. |
30 | | - """ |
31 | | - initializer(["tests.testmodels"], db_url="sqlite://:memory:", app_label="models") |
32 | | - try: |
33 | | - # Verify that Tortoise is initialized |
34 | | - assert Tortoise._inited is True, "Tortoise should be initialized" |
35 | | - |
36 | | - # Verify that apps are loaded |
37 | | - assert Tortoise.apps is not None, "Tortoise.apps should not be None" |
38 | | - |
39 | | - # Verify that connection config is available |
40 | | - assert connections._db_config is not None, "Connection config should not be None" |
41 | | - assert "models" in connections.db_config, "Connection 'models' should be in config" |
42 | | - |
43 | | - # Verify that we can get a connection |
44 | | - conn = connections.get("models") |
45 | | - assert conn is not None, "Should be able to get connection" |
46 | | - finally: |
47 | | - finalizer() |
| 22 | +from tortoise import Tortoise |
| 23 | +from tortoise.contrib.test import create_db |
48 | 24 |
|
49 | | - def test_initializer_finalizer_cycle(self): |
50 | | - """Test that initializer/finalizer can be called multiple times.""" |
51 | | - for i in range(3): |
52 | | - initializer(["tests.testmodels"], db_url="sqlite://:memory:", app_label="models") |
53 | | - try: |
54 | | - assert Tortoise._inited is True |
55 | | - conn = connections.get("models") |
56 | | - assert conn is not None |
57 | | - finally: |
58 | | - finalizer() |
59 | 25 |
|
60 | | - # After finalizer, state should be reset |
61 | | - assert Tortoise._inited is False |
62 | | - assert connections._db_config is None |
| 26 | +def is_xdist_worker(): |
| 27 | + """Check if we're running as an xdist worker.""" |
| 28 | + return os.environ.get("PYTEST_XDIST_WORKER") is not None |
63 | 29 |
|
64 | 30 |
|
65 | | -class TestPytestFixturePatterns: |
66 | | - """Test various pytest fixture patterns that users might use.""" |
67 | | - |
68 | | - def test_function_scoped_fixture_pattern(self): |
69 | | - """Test the common function-scoped fixture pattern from issue #1110.""" |
70 | | - # Simulate what happens with a function-scoped fixture |
71 | | - |
72 | | - # First test |
73 | | - initializer(["tests.testmodels"], db_url="sqlite://:memory:", app_label="models") |
74 | | - try: |
75 | | - assert Tortoise._inited is True |
76 | | - conn = connections.get("models") |
77 | | - assert conn is not None |
78 | | - finally: |
79 | | - finalizer() |
80 | | - |
81 | | - # Second test (simulating next test function) |
82 | | - initializer(["tests.testmodels"], db_url="sqlite://:memory:", app_label="models") |
83 | | - try: |
84 | | - assert Tortoise._inited is True |
85 | | - conn = connections.get("models") |
86 | | - assert conn is not None |
87 | | - finally: |
88 | | - finalizer() |
| 31 | +# Skip the entire module if running in xdist parallel mode |
| 32 | +pytestmark = pytest.mark.skipif( |
| 33 | + is_xdist_worker(), |
| 34 | + reason="These tests use create_db which resets global state; run separately with -n0", |
| 35 | +) |
89 | 36 |
|
90 | 37 |
|
91 | 38 | @pytest.mark.asyncio |
@@ -165,29 +112,6 @@ async def test_file_sqlite_with_create_db(self): |
165 | 112 | os.unlink(db_path) |
166 | 113 |
|
167 | 114 |
|
168 | | -class TestFileBasedSqliteSync: |
169 | | - """Test with file-based SQLite database (sync tests).""" |
170 | | - |
171 | | - def test_file_sqlite_with_initializer(self): |
172 | | - """Test initializer with file-based SQLite.""" |
173 | | - with tempfile.NamedTemporaryFile(suffix=".db", delete=False) as f: |
174 | | - db_path = f.name |
175 | | - |
176 | | - try: |
177 | | - db_url = f"sqlite://{db_path}" |
178 | | - initializer(["tests.testmodels"], db_url=db_url, app_label="models") |
179 | | - try: |
180 | | - assert Tortoise._inited is True |
181 | | - conn = connections.get("models") |
182 | | - assert conn is not None |
183 | | - finally: |
184 | | - finalizer() |
185 | | - finally: |
186 | | - # Cleanup the temp file |
187 | | - if os.path.exists(db_path): |
188 | | - os.unlink(db_path) |
189 | | - |
190 | | - |
191 | 115 | # ============================================================================ |
192 | 116 | # Tests demonstrating create_db used as part of pytest fixtures |
193 | 117 | # ============================================================================ |
|
0 commit comments