Skip to content

Commit

Permalink
move pytest settings to pyproject and actually make use of pytest-doc…
Browse files Browse the repository at this point in the history
…ker-compose for testing locally
  • Loading branch information
radusuciu committed Feb 28, 2024
1 parent 4b32ea0 commit b4a9337
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 5 deletions.
12 changes: 11 additions & 1 deletion pdm.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,13 @@ dev = [
"types-mock>=5.1.0",
"bumpver>=2023.1129",
"git-cliff>=2.0.4",
"tenacity>=8.2.3",
]

[tool.pytest.ini_options]
addopts = "'--docker-compose' './tests/docker-compose.yml' '--docker-compose-no-build' '--use-running-containers'"
DJANGO_SETTINGS_MODULE = "test_project.settings"

[tool.ruff]
line-length = 88
target-version = "py311"
Expand Down
54 changes: 53 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1 +1,53 @@
pytest_plugins = ['docker_compose']
import subprocess

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


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."""
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
3 changes: 0 additions & 3 deletions tests/pytest.ini

This file was deleted.

0 comments on commit b4a9337

Please sign in to comment.