Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: Add a more advance usecase documentation for ServerContainer #688

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions modules/generic/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,35 @@ FastAPI container that is using :code:`ServerContainer`
... response = client.get("/")
... assert response.status_code == 200
... assert response.json() == {"Status": "Working"}

A more advance use-case, where we are using a FastAPI container that is using Redis container:

.. doctest::

>>> from testcontainers.redis import RedisContainer
>>> from testcontainers.generic import ServerContainer

>>> with RedisContainer() as redis:
... redis_port = redis.get_exposed_port(redis.port)
... redis_host = redis.get_container_host_ip()

... with DockerImage(path="./modules/generic/tests/samples/advance_1", tag="advance-1:latest") as image:
... web_server = ServerContainer(port=80, image=image)
... web_server.with_env(key="REDIS_HOST", value=redis_host)
... web_server.with_env(key="REDIS_PORT", value=redis_port)

... with web_server:
... web_server.get_api_url = lambda: web_server._create_connection_url() + "/api/v1/"
... client = web_server.get_client()

... response = client.get("/")
... assert response.status_code == 200, "Server request failed"
... assert response.json() == {"Status": "ok"}

... test_data = {"key": "test_key", "value": "test_value"}
... response = client.post("/set", json=test_data)
... assert response.status_code == 200, "Failed to set data"

... response = client.get(f"/get/{test_data['key']}")
... assert response.status_code == 200. "Failed to get data"
... assert response.json() == {"key": test_data["key"], "value": test_data["value"]}
11 changes: 11 additions & 0 deletions modules/generic/tests/samples/advance_1/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM python:3.9

WORKDIR /app

RUN pip install fastapi[standard] redis

COPY ./app /app

EXPOSE 80

CMD ["fastapi", "run", "main.py", "--port", "80"]
Empty file.
30 changes: 30 additions & 0 deletions modules/generic/tests/samples/advance_1/app/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# This app will use redis to store given key-value pairs.

import os
import redis

from fastapi import FastAPI


app = FastAPI()

# connect to redis
redis_host = os.getenv("REDIS_HOST")
redis_port = os.getenv("REDIS_PORT")
redis_client = redis.Redis(host=redis_host, port=redis_port)


@app.get("/")
def health_check():
return {"status": "ok"}


@app.get("/get/{key}")
def read_item(key: str):
return {key: redis_client.get(key)}


@app.post("/set")
def create_item(key: str, value: str):
redis_client.set(key, value)
return {key: value}
4 changes: 2 additions & 2 deletions poetry.lock

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

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ cosmosdb = ["azure-cosmos"]
cockroachdb = []
db2 = ["sqlalchemy", "ibm_db_sa"]
elasticsearch = []
generic = ["httpx"]
generic = ["httpx", "redis"] # The advance doctests for ServerContainer require redis
test_module_import = ["httpx"]
google = ["google-cloud-pubsub", "google-cloud-datastore"]
influxdb = ["influxdb", "influxdb-client"]
Expand Down