Skip to content

Commit 6f9376c

Browse files
test(postgres): add example of initdb.d usage for postgres (#572)
1 parent 49b261e commit 6f9376c

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
create table example
2+
(
3+
id serial not null primary key,
4+
name varchar(255) not null unique,
5+
description text null
6+
);

modules/postgres/tests/test_postgres.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from pathlib import Path
2+
13
import pytest
24

35
from testcontainers.postgres import PostgresContainer
@@ -77,3 +79,21 @@ def test_quoted_password():
7779
# it raises ValueError, but auth (OperationalError) = more interesting
7880
with sqlalchemy.create_engine(raw_pass_url).begin() as connection:
7981
connection.execute(sqlalchemy.text("select 1=1"))
82+
83+
84+
def test_show_how_to_initialize_db_via_initdb_dir():
85+
postgres_container = PostgresContainer("postgres:16-alpine")
86+
script = Path(__file__).parent / "fixtures" / "postgres_create_example_table.sql"
87+
postgres_container.with_volume_mapping(host=str(script), container=f"/docker-entrypoint-initdb.d/{script.name}")
88+
89+
insert_query = "insert into example(name, description) VALUES ('sally', 'sells seashells');"
90+
select_query = "select id, name, description from example;"
91+
92+
with postgres_container as postgres:
93+
engine = sqlalchemy.create_engine(postgres.get_connection_url())
94+
with engine.begin() as connection:
95+
connection.execute(sqlalchemy.text(insert_query))
96+
result = connection.execute(sqlalchemy.text(select_query))
97+
result = result.fetchall()
98+
assert len(result) == 1
99+
assert result[0] == (1, "sally", "sells seashells")

0 commit comments

Comments
 (0)