Skip to content

Commit

Permalink
test: introduce pytest-docker
Browse files Browse the repository at this point in the history
  • Loading branch information
Manabu Niseki committed Sep 10, 2023
1 parent 5e30a68 commit 742da1e
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 1 deletion.
50 changes: 49 additions & 1 deletion poetry.lock

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

2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ uvicorn = { extras = ["standard"], version = "^0.23" }
autoflake = "^2.2"
autopep8 = "^2.0"
black = "^23.3"
ci-py = "^1.0.0"
flake8 = "^6.0"
httpx = "^0.24"
mypy = "^1.5"
Expand All @@ -37,6 +38,7 @@ pre-commit = "^3.4"
pytest = "^7.4"
pytest-asyncio = "^0.21"
pytest-cov = "^4.1"
pytest-docker = "^2.0.1"
pytest-mock = "^3.11"
pytest-randomly = "^3.15"
pytest-sugar = "^0.9"
Expand Down
8 changes: 8 additions & 0 deletions test.docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
version: "3"
services:
redis:
image: "redis/redis-stack:6.2.6-v7"
restart: always
ports:
- ${REDIS_PORT:-6379}:6379
- ${REDIS_INSIGHT_PORT:-8001}:8001
28 changes: 28 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import asyncio
import os

import ci
import pytest
import pytest_asyncio
from fastapi import FastAPI
from httpx import AsyncClient
from pytest_docker.plugin import Services

from mihama import crud, models
from mihama.core import settings
Expand All @@ -12,6 +15,31 @@
from mihama.main import create_app
from mihama.redis import setup_redis_om

from .utils import is_responsive


@pytest.fixture(scope="session")
def docker_compose_file(pytestconfig):
return os.path.join(str(pytestconfig.rootdir), "test.docker-compose.yml")


if not ci.is_ci():

@pytest.fixture(scope="session", autouse=True)
def docker_compose(docker_ip: str, docker_services: Services): # type: ignore
port = docker_services.port_for("redis", 8001)
url = f"http://{docker_ip}:{port}"
docker_services.wait_until_responsive(
timeout=30.0, pause=0.1, check=lambda: is_responsive(url)
)
return url

else:

@pytest.fixture
def docker_compose():
return


@pytest.fixture(scope="session")
def event_loop():
Expand Down
9 changes: 9 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import httpx


def is_responsive(url: str) -> bool:
try:
httpx.get(url)
return True
except Exception:
return False

0 comments on commit 742da1e

Please sign in to comment.