Skip to content

Commit 2cf5a9f

Browse files
authored
docs: Add a more advance usecase documentation for ServerContainer (#688)
Expanding the docs for using the ServerContainer with an example involving FastAPI container that is using Redis container.
1 parent 5216b02 commit 2cf5a9f

File tree

6 files changed

+76
-3
lines changed

6 files changed

+76
-3
lines changed

modules/generic/README.rst

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,35 @@ FastAPI container that is using :code:`ServerContainer`
1818
... response = client.get("/")
1919
... assert response.status_code == 200
2020
... assert response.json() == {"Status": "Working"}
21+
22+
A more advance use-case, where we are using a FastAPI container that is using Redis container:
23+
24+
.. doctest::
25+
26+
>>> from testcontainers.redis import RedisContainer
27+
>>> from testcontainers.generic import ServerContainer
28+
29+
>>> with RedisContainer() as redis:
30+
... redis_container_port = redis.port
31+
... redis_container_ip_address = redis.get_docker_client().bridge_ip(redis._container.id)
32+
33+
... with DockerImage(path="./modules/generic/tests/samples/advance_1", tag="advance-1:latest") as image:
34+
... web_server = ServerContainer(port=80, image=image)
35+
... web_server.with_env(key="REDIS_HOST", value=redis_container_ip_address)
36+
... web_server.with_env(key="REDIS_PORT", value=redis_container_port)
37+
38+
... with web_server:
39+
... web_server.get_api_url = lambda: web_server._create_connection_url()
40+
... client = web_server.get_client()
41+
42+
... response = client.get("/")
43+
... assert response.status_code == 200, "Server request failed"
44+
... assert response.json() == {"Status": "ok"}
45+
46+
... test_data = {"key": "test_key", "value": "test_value"}
47+
... response = client.post("/set", params=test_data)
48+
... assert response.status_code == 200, "Failed to set data"
49+
50+
... response = client.get(f"/get/{test_data['key']}")
51+
... assert response.status_code == 200, "Failed to get data"
52+
... assert response.json() == {"key": test_data["key"], "value": test_data["value"]}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
FROM python:3.9
2+
3+
WORKDIR /app
4+
5+
RUN pip install fastapi[standard] redis
6+
7+
COPY ./app /app
8+
9+
EXPOSE 80
10+
11+
CMD ["fastapi", "run", "main.py", "--port", "80"]

modules/generic/tests/samples/advance_1/app/__init__.py

Whitespace-only changes.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# This app will use redis to store given key-value pairs.
2+
3+
import os
4+
import redis
5+
6+
from fastapi import FastAPI
7+
8+
9+
app = FastAPI()
10+
11+
redis_host = os.getenv("REDIS_HOST")
12+
redis_port = os.getenv("REDIS_PORT")
13+
redis_client = redis.Redis(host=redis_host, port=redis_port)
14+
redis_client.ping()
15+
16+
17+
@app.get("/")
18+
def health_check():
19+
return {"status": "ok"}
20+
21+
22+
@app.get("/get/{key}")
23+
def read_item(key: str):
24+
return {key: redis_client.get(key)}
25+
26+
27+
@app.post("/set")
28+
def create_item(key: str, value: str):
29+
redis_client.set(key, value)
30+
return {key: value}

poetry.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ cosmosdb = ["azure-cosmos"]
129129
cockroachdb = []
130130
db2 = ["sqlalchemy", "ibm_db_sa"]
131131
elasticsearch = []
132-
generic = ["httpx"]
132+
generic = ["httpx", "redis"] # The advance doctests for ServerContainer require redis
133133
test_module_import = ["httpx"]
134134
google = ["google-cloud-pubsub", "google-cloud-datastore"]
135135
influxdb = ["influxdb", "influxdb-client"]

0 commit comments

Comments
 (0)