Skip to content

Commit 04e418c

Browse files
committed
Fix doctests
1 parent 80a5355 commit 04e418c

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

modules/generic/README.rst

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,33 @@ A more advance use-case, where we are using a FastAPI container that is using Re
5454
.. autoclass:: testcontainers.generic.SqlContainer
5555
.. title:: testcontainers.generic.SqlContainer
5656

57-
SQL container that is using :code:`SqlContainer`
57+
Postgres container that is using :code:`SqlContainer`
5858

5959
.. doctest::
6060

6161
>>> from testcontainers.generic import SqlContainer
6262
>>> from sqlalchemy import text
6363
>>> import sqlalchemy
6464

65-
>>> with SqlContainer(image="postgres:15-alpine", port=5432, username="test", password="test", dbname="test") as postgres:
65+
>>> class CustomPostgresContainer(SqlContainer):
66+
... def __init__(self, image="postgres:15-alpine",
67+
... port=5432, username="test", password="test", dbname="test"):
68+
... super().__init__(image=image)
69+
... self.port_to_expose = port
70+
... self.username = username
71+
... self.password = password
72+
... self.dbname = dbname
73+
... def get_connection_url(self) -> str:
74+
... host = self.get_container_host_ip()
75+
... port = self.get_exposed_port(self.port_to_expose)
76+
... return f"postgresql://{self.username}:{self.password}@{host}:{port}/{self.dbname}"
77+
... def _configure(self) -> None:
78+
... self.with_exposed_ports(self.port_to_expose)
79+
... self.with_env("POSTGRES_USER", self.username)
80+
... self.with_env("POSTGRES_PASSWORD", self.password)
81+
... self.with_env("POSTGRES_DB", self.dbname)
82+
83+
>>> with CustomPostgresContainer() as postgres:
6684
... engine = sqlalchemy.create_engine(postgres.get_connection_url())
6785
... with engine.connect() as conn:
6886
... result = conn.execute(text("SELECT 1"))

0 commit comments

Comments
 (0)