Skip to content

Commit c2caab2

Browse files
authored
Move the integration subdirectories' database gates to the shared helper (#366)
The same conversion as #363, on the 33 modules under tests/integration/scan, admin and audit. Each module keeps its own fixture with its own scope and position; only the body becomes a call. Declared 33 before running, all 33 parse, and the converted files pass.
1 parent 638f783 commit c2caab2

34 files changed

Lines changed: 66 additions & 790 deletions

apps/backend/tests/integration/admin/test_admin_backup_api.py

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
from __future__ import annotations
1515

1616
import json
17-
import os
18-
import subprocess
1917
import uuid
2018
from collections.abc import AsyncIterator
2119
from datetime import UTC, datetime
@@ -28,41 +26,22 @@
2826

2927
from core.security import create_access_token
3028
from models import User
29+
from tests._db_required import migrate_to_head
3130
from tests._helpers import (
3231
make_membership,
3332
make_organization,
3433
make_team,
3534
make_user,
3635
)
3736

38-
BACKEND_ROOT = Path(__file__).resolve().parent.parent.parent.parent
3937
PROBLEM_JSON = "application/problem+json"
4038

4139
pytestmark = pytest.mark.integration
4240

4341

44-
def _require_database_url() -> str:
45-
url = os.getenv("DATABASE_URL")
46-
if not url:
47-
pytest.skip("DATABASE_URL not set — skip admin backup API tests")
48-
return url
49-
50-
5142
@pytest.fixture(scope="module", autouse=True)
5243
def _migrate_once() -> None:
53-
_require_database_url()
54-
result = subprocess.run( # noqa: S603, S607
55-
["alembic", "upgrade", "head"],
56-
cwd=BACKEND_ROOT,
57-
capture_output=True,
58-
text=True,
59-
timeout=120,
60-
)
61-
if result.returncode != 0:
62-
pytest.skip(
63-
f"alembic upgrade head failed; admin backup API tests cannot run\n"
64-
f"stdout:\n{result.stdout}\nstderr:\n{result.stderr}"
65-
)
44+
migrate_to_head()
6645

6746

6847
@pytest.fixture

apps/backend/tests/integration/admin/test_admin_ops_api.py

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
from __future__ import annotations
1818

19-
import os
20-
import subprocess
2119
import uuid
2220
from collections.abc import AsyncIterator
2321
from datetime import UTC, datetime
@@ -28,6 +26,7 @@
2826

2927
from core.security import create_access_token, hash_password
3028
from models import User
29+
from tests._db_required import migrate_to_head
3130
from tests._helpers import (
3231
make_membership,
3332
make_organization,
@@ -38,34 +37,14 @@
3837
principal_for,
3938
)
4039

41-
BACKEND_ROOT = Path(__file__).resolve().parent.parent.parent.parent
4240
PROBLEM_JSON = "application/problem+json"
4341

4442
pytestmark = pytest.mark.integration
4543

4644

47-
def _require_database_url() -> str:
48-
url = os.getenv("DATABASE_URL")
49-
if not url:
50-
pytest.skip("DATABASE_URL not set — skip admin ops API tests")
51-
return url
52-
53-
5445
@pytest.fixture(scope="module", autouse=True)
5546
def _migrate_once() -> None:
56-
_require_database_url()
57-
result = subprocess.run(
58-
["alembic", "upgrade", "head"],
59-
cwd=BACKEND_ROOT,
60-
capture_output=True,
61-
text=True,
62-
timeout=120,
63-
)
64-
if result.returncode != 0:
65-
pytest.skip(
66-
f"alembic upgrade head failed; admin ops API tests cannot run\n"
67-
f"stdout:\n{result.stdout}\nstderr:\n{result.stderr}"
68-
)
47+
migrate_to_head()
6948

7049

7150
@pytest.fixture

apps/backend/tests/integration/admin/test_admin_teams_api.py

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,16 @@
1818

1919
from __future__ import annotations
2020

21-
import os
22-
import subprocess
2321
import uuid
2422
from collections.abc import AsyncIterator
25-
from pathlib import Path
2623

2724
import pytest
2825
from httpx import ASGITransport, AsyncClient
2926
from sqlalchemy import text
3027

3128
from core.security import create_access_token
3229
from models import User
30+
from tests._db_required import migrate_to_head
3331
from tests._helpers import (
3432
make_membership,
3533
make_organization,
@@ -40,34 +38,14 @@
4038
unique_suffix,
4139
)
4240

43-
BACKEND_ROOT = Path(__file__).resolve().parent.parent.parent.parent
4441
PROBLEM_JSON = "application/problem+json"
4542

4643
pytestmark = pytest.mark.integration
4744

4845

49-
def _require_database_url() -> str:
50-
url = os.getenv("DATABASE_URL")
51-
if not url:
52-
pytest.skip("DATABASE_URL not set — skip admin teams API tests")
53-
return url
54-
55-
5646
@pytest.fixture(scope="module", autouse=True)
5747
def _migrate_once() -> None:
58-
_require_database_url()
59-
result = subprocess.run(
60-
["alembic", "upgrade", "head"],
61-
cwd=BACKEND_ROOT,
62-
capture_output=True,
63-
text=True,
64-
timeout=120,
65-
)
66-
if result.returncode != 0:
67-
pytest.skip(
68-
f"alembic upgrade head failed; admin teams API tests cannot run\n"
69-
f"stdout:\n{result.stdout}\nstderr:\n{result.stderr}"
70-
)
48+
migrate_to_head()
7149

7250

7351
@pytest.fixture

apps/backend/tests/integration/admin/test_admin_user_onboarding_api.py

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,51 +14,32 @@
1414

1515
from __future__ import annotations
1616

17-
import os
18-
import subprocess
1917
import uuid
2018
from collections.abc import AsyncIterator
2119
from datetime import UTC, datetime, timedelta
22-
from pathlib import Path
2320

2421
import pytest
2522
from httpx import ASGITransport, AsyncClient
2623
from sqlalchemy import select
2724

2825
from core.security import create_access_token, verify_password
2926
from models import AuditLog, Membership, RefreshToken, User
27+
from tests._db_required import migrate_to_head
3028
from tests._helpers import (
3129
make_organization,
3230
make_team,
3331
make_user,
3432
unique_suffix,
3533
)
3634

37-
BACKEND_ROOT = Path(__file__).resolve().parent.parent.parent.parent
3835
PROBLEM_JSON = "application/problem+json"
3936

4037
pytestmark = pytest.mark.integration
4138

4239

43-
def _require_database_url() -> str:
44-
url = os.getenv("DATABASE_URL")
45-
if not url:
46-
pytest.skip("DATABASE_URL not set, skipping admin onboarding tests")
47-
return url
48-
49-
5040
@pytest.fixture(scope="module", autouse=True)
5141
def _migrate_once() -> None:
52-
_require_database_url()
53-
result = subprocess.run(
54-
["alembic", "upgrade", "head"],
55-
cwd=BACKEND_ROOT,
56-
capture_output=True,
57-
text=True,
58-
check=False,
59-
)
60-
if result.returncode != 0:
61-
pytest.skip(f"alembic upgrade failed: {result.stderr[-400:]}")
42+
migrate_to_head()
6243

6344

6445
@pytest.fixture

apps/backend/tests/integration/admin/test_admin_users_api.py

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,16 @@
1717

1818
from __future__ import annotations
1919

20-
import os
21-
import subprocess
2220
import uuid
2321
from collections.abc import AsyncIterator
24-
from pathlib import Path
2522

2623
import pytest
2724
from httpx import ASGITransport, AsyncClient
2825
from sqlalchemy import select, text
2926

3027
from core.security import create_access_token
3128
from models import RefreshToken, User
29+
from tests._db_required import migrate_to_head
3230
from tests._helpers import (
3331
make_membership,
3432
make_organization,
@@ -37,34 +35,14 @@
3735
unique_suffix,
3836
)
3937

40-
BACKEND_ROOT = Path(__file__).resolve().parent.parent.parent.parent
4138
PROBLEM_JSON = "application/problem+json"
4239

4340
pytestmark = pytest.mark.integration
4441

4542

46-
def _require_database_url() -> str:
47-
url = os.getenv("DATABASE_URL")
48-
if not url:
49-
pytest.skip("DATABASE_URL not set — skip admin users API tests")
50-
return url
51-
52-
5343
@pytest.fixture(scope="module", autouse=True)
5444
def _migrate_once() -> None:
55-
_require_database_url()
56-
result = subprocess.run(
57-
["alembic", "upgrade", "head"],
58-
cwd=BACKEND_ROOT,
59-
capture_output=True,
60-
text=True,
61-
timeout=120,
62-
)
63-
if result.returncode != 0:
64-
pytest.skip(
65-
f"alembic upgrade head failed; admin users API tests cannot run\n"
66-
f"stdout:\n{result.stdout}\nstderr:\n{result.stderr}"
67-
)
45+
migrate_to_head()
6846

6947

7048
@pytest.fixture

apps/backend/tests/integration/audit/test_audit_log_retention_report.py

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,15 @@
2727
from __future__ import annotations
2828

2929
import os
30-
import subprocess
3130
import uuid
3231
from collections.abc import Iterator
3332
from datetime import UTC, datetime, timedelta
34-
from pathlib import Path
3533

3634
import pytest
3735
from sqlalchemy import create_engine, text
3836
from sqlalchemy.orm import Session, sessionmaker
3937

40-
BACKEND_ROOT = Path(__file__).resolve().parent.parent.parent.parent
38+
from tests._db_required import migrate_to_head
4139

4240
pytestmark = pytest.mark.integration
4341

@@ -51,17 +49,7 @@ def _sync_url() -> str:
5149

5250
@pytest.fixture(scope="module", autouse=True)
5351
def _migrate_once() -> None:
54-
if not os.getenv("DATABASE_URL"):
55-
pytest.skip("DATABASE_URL not set")
56-
result = subprocess.run(
57-
["alembic", "upgrade", "head"],
58-
cwd=BACKEND_ROOT,
59-
capture_output=True,
60-
text=True,
61-
check=False,
62-
)
63-
if result.returncode != 0:
64-
pytest.skip(f"alembic upgrade failed: {result.stderr[-400:]}")
52+
migrate_to_head()
6553

6654

6755
@pytest.fixture

apps/backend/tests/integration/audit/test_team_scoped_audit.py

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,52 +14,30 @@
1414

1515
from __future__ import annotations
1616

17-
import os
18-
import subprocess
1917
import uuid
2018
from collections.abc import AsyncIterator
21-
from pathlib import Path
2219

2320
import pytest
2421
from httpx import ASGITransport, AsyncClient
2522

2623
from core.security import create_access_token
2724
from models import User
25+
from tests._db_required import migrate_to_head
2826
from tests._helpers import (
2927
make_membership,
3028
make_organization,
3129
make_team,
3230
make_user,
3331
)
3432

35-
BACKEND_ROOT = Path(__file__).resolve().parent.parent.parent.parent
3633
PROBLEM_JSON = "application/problem+json"
3734

3835
pytestmark = pytest.mark.integration
3936

4037

41-
def _require_database_url() -> str:
42-
url = os.getenv("DATABASE_URL")
43-
if not url:
44-
pytest.skip("DATABASE_URL not set — skip team-scoped audit tests")
45-
return url
46-
47-
4838
@pytest.fixture(scope="module", autouse=True)
4939
def _migrate_once() -> None:
50-
_require_database_url()
51-
result = subprocess.run(
52-
["alembic", "upgrade", "head"],
53-
cwd=BACKEND_ROOT,
54-
capture_output=True,
55-
text=True,
56-
timeout=120,
57-
)
58-
if result.returncode != 0:
59-
pytest.skip(
60-
f"alembic upgrade head failed; team-scoped audit tests cannot run\n"
61-
f"stdout:\n{result.stdout}\nstderr:\n{result.stderr}"
62-
)
40+
migrate_to_head()
6341

6442

6543
@pytest.fixture

0 commit comments

Comments
 (0)