test: move psql_as_superuser as util and reuse it

This commit is contained in:
steve-chavez
2026-07-24 13:44:58 -05:00
parent 1d195bf865
commit 03c67f3410
2 changed files with 29 additions and 33 deletions
+10 -33
View File
@@ -3,7 +3,6 @@
import os
import re
import signal
import subprocess
import time
import pytest
import requests
@@ -16,6 +15,7 @@ from util import (
relativeSeconds,
drain_stdout,
match_log,
psql_as_superuser,
)
from postgrest import (
Admin,
@@ -33,20 +33,6 @@ from postgrest import (
)
def psql_as_superuser(query):
subprocess.check_call(
[
"psql",
"--username",
"postgres",
"--set",
"ON_ERROR_STOP=1",
"-c",
query,
]
)
def test_connect_with_dburi(dburi, defaultenv):
"Connecting with db-uri instead of LIPQ* environment variables should work."
defaultenv_without_libpq = {
@@ -684,24 +670,15 @@ def test_listener_query_is_visible_in_pg_stat_activity(defaultenv):
}
with run(env=env):
query = """
select query
from pg_stat_activity
where application_name = 'listener-query-test'
and query = 'LISTEN "pgrst"'
limit 1;
"""
output = subprocess.check_output(
[
"psql",
"--set",
"ON_ERROR_STOP=1",
"--tuples-only",
"--no-align",
"-c",
query,
],
text=True,
output = psql_as_superuser(
"""
select query
from pg_stat_activity
where application_name = 'listener-query-test'
and query = 'LISTEN "pgrst"'
limit 1;
""",
capture_output=True,
).strip()
assert output == 'LISTEN "pgrst"'
+19
View File
@@ -1,6 +1,7 @@
import re
import threading
import jwt
import subprocess
from datetime import datetime, timedelta, timezone
@@ -75,3 +76,21 @@ def parse_server_timings_header(header):
_, duration = duration_text.split("=")
timings[name.strip()] = float(duration)
return timings
def psql_as_superuser(query, capture_output=False):
cmd = [
"psql",
"--username",
"postgres",
"--set",
"ON_ERROR_STOP=1",
]
if capture_output:
cmd.extend(["--tuples-only", "--no-align"])
cmd.extend(["-c", query])
if capture_output:
return subprocess.check_output(cmd, text=True)
subprocess.check_call(cmd)