Skip to content

Commit fc5d97f

Browse files
committed
tests: execute multiple statements in a single connection
1 parent de20418 commit fc5d97f

1 file changed

Lines changed: 17 additions & 8 deletions

File tree

tests/branches/test_clone_restore.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@
88
_BRANCH_PASSWORD = "SecurePass1!"
99

1010

11-
def _execute_sql(db_info: dict, password: str, sql: str) -> list[tuple]:
12-
"""Execute SQL with retries for DNS propagation delay."""
11+
def _execute_sql(db_info: dict, password: str, *statements: str) -> list[tuple]:
12+
"""Execute one or more SQL statements with retries for DNS propagation delay.
13+
14+
Returns the result of the *last* statement that produces rows.
15+
"""
1316
host = db_info["host"]
1417
port = db_info["port"] or 5432
1518
deadline = time.monotonic() + 120 # Max wait
@@ -28,10 +31,12 @@ def _execute_sql(db_info: dict, password: str, sql: str) -> list[tuple]:
2831
) as conn,
2932
conn.cursor() as cur,
3033
):
31-
cur.execute(sql)
32-
if cur.description:
33-
return cur.fetchall()
34-
return []
34+
result: list[tuple] = []
35+
for sql in statements:
36+
cur.execute(sql)
37+
if cur.description:
38+
result = cur.fetchall()
39+
return result
3540
except psycopg.OperationalError as exc:
3641
last_exc = exc
3742
time.sleep(15)
@@ -74,8 +79,12 @@ def populated_branch_id(client, org, project, branch_id):
7479
assert r.status_code == 200
7580
db_info = r.json()["database"]
7681

77-
_execute_sql(db_info, _BRANCH_PASSWORD, "CREATE TABLE test_data_integrity (id SERIAL PRIMARY KEY, value TEXT)")
78-
_execute_sql(db_info, _BRANCH_PASSWORD, "INSERT INTO test_data_integrity (value) VALUES ('original_data')")
82+
_execute_sql(
83+
db_info,
84+
_BRANCH_PASSWORD,
85+
"CREATE TABLE test_data_integrity (id SERIAL PRIMARY KEY, value TEXT)",
86+
"INSERT INTO test_data_integrity (value) VALUES ('original_data')",
87+
)
7988

8089
return branch_id
8190

0 commit comments

Comments
 (0)