Skip to content

Commit

Permalink
maybe we can ditch the ci service altogether and use pytest-docker-co…
Browse files Browse the repository at this point in the history
…mpose...
  • Loading branch information
radusuciu committed Feb 28, 2024
1 parent 9ddbfe0 commit f842823
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 67 deletions.
16 changes: 1 addition & 15 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,6 @@ jobs:
contents: write
# for trusted publishing
id-token: write
services:
postgres:
image: postgres:16
env:
POSTGRES_USER: test
POSTGRES_PASSWORD: test
POSTGRES_DB: test_sqlfun
ports:
- 5432:5432
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- name: Check out repository
uses: actions/checkout@v3
Expand All @@ -42,7 +28,7 @@ jobs:
run: pdm install -v --dev

- name: Run tests
run: pdm run pytest tests/ --ci
run: pdm run pytest tests/

- name: Create GitHub release
uses: softprops/action-gh-release@v1
Expand Down
16 changes: 1 addition & 15 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,6 @@ jobs:
matrix:
python-version: ['3.10', '3.11', '3.12']
django-version: ['4.2', '5.0']
services:
postgres:
image: postgres:16
env:
POSTGRES_USER: test
POSTGRES_PASSWORD: test
POSTGRES_DB: test_sqlfun
ports:
- 5432:5432
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- name: Check out repository
uses: actions/checkout@v4
Expand All @@ -44,4 +30,4 @@ jobs:
pdm install -v --dev
pdm add "Django==${{ matrix.django-version }}"
- name: Run tests
run: pdm run pytest tests/ --ci
run: pdm run pytest tests/
42 changes: 5 additions & 37 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,53 +1,21 @@
import subprocess

import pytest
from tenacity import retry, wait_fixed, stop_after_attempt, RetryError
from tenacity import retry, wait_fixed, stop_after_attempt


pytest_plugins = ["docker_compose"]


def pytest_addoption(parser):
parser.addoption(
"--ci",
action="store_true",
default=False,
help="Run tests as if in a CI environment",
)


def wait_for_postgres_from_compose(session_scoped_container_getter):
"""Wait for the postgres container to be ready."""
@pytest.fixture(scope="session")
@retry(wait=wait_fixed(2), stop=stop_after_attempt(3))
def wait_for_postgres(request, session_scoped_container_getter):
"""Wait for postgres container to be ready."""
container = session_scoped_container_getter.get("postgres")
is_ready = "accepting connections" in container.execute(
["pg_isready", "-h", "localhost", "-U", "postgres"]
)
assert is_ready


def wait_for_postgres_from_ci():
"""Wait for the postgres service to be ready."""
is_ready = (
subprocess.run(["pg_isready", "-h", "localhost", "-U", "postgres"]).returncode
== 0
)
assert is_ready


@pytest.fixture(scope="session")
@retry(wait=wait_fixed(2), stop=stop_after_attempt(3))
def wait_for_postgres(request, session_scoped_container_getter):
"""Wait for postgres to be ready.
Chooses a different strategy depending on whether the tests are running with containerized postgres locally
or a postgres service in a CI environment.
"""
if request.config.getoption("--ci"):
return wait_for_postgres_from_ci()
else:
return wait_for_postgres_from_compose(session_scoped_container_getter)


@pytest.fixture(scope="session")
def django_db_setup(wait_for_postgres, django_db_setup):
pass

0 comments on commit f842823

Please sign in to comment.