|
| 1 | +from pathlib import Path |
| 2 | + |
1 | 3 | import pytest
|
2 | 4 |
|
3 | 5 | from testcontainers.postgres import PostgresContainer
|
@@ -77,3 +79,21 @@ def test_quoted_password():
|
77 | 79 | # it raises ValueError, but auth (OperationalError) = more interesting
|
78 | 80 | with sqlalchemy.create_engine(raw_pass_url).begin() as connection:
|
79 | 81 | 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