Skip to content

Commit 4492278

Browse files
committed
.
1 parent 17bf084 commit 4492278

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

tests/integrations/asyncpg/test_asyncpg.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,24 @@ def _get_db_name():
6060

6161
@pytest_asyncio.fixture(autouse=True)
6262
async def _clean_pg():
63+
# Create the test database if it doesn't exist
64+
try:
65+
default_conn = await connect(PG_CONNECTION_URI)
66+
try:
67+
# Check if database exists, create if not
68+
result = await default_conn.fetchval(
69+
"SELECT 1 FROM pg_database WHERE datname = $1", PG_NAME
70+
)
71+
if not result:
72+
await default_conn.execute(f'CREATE DATABASE "{PG_NAME}"')
73+
finally:
74+
await default_conn.close()
75+
except Exception:
76+
# If we can't connect to default postgres db, assume our test db already exists
77+
# or that we're connecting to the same database (PG_NAME == PG_NAME_BASE)
78+
pass
79+
80+
# Now connect to our test database and set up the table
6381
conn = await connect(PG_CONNECTION_URI)
6482
await conn.execute("DROP TABLE IF EXISTS users")
6583
await conn.execute(

0 commit comments

Comments
 (0)