Skip to content

Commit 1a35eba

Browse files
committed
test: use postgrest.read_stdout in io tests
It's easier to maintain this way in case there are new log lines.
1 parent 69c6ce9 commit 1a35eba

File tree

1 file changed

+26
-30
lines changed

1 file changed

+26
-30
lines changed

test/io/test_io.py

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -772,16 +772,8 @@ def test_admin_works_with_host_special_values(specialhostvalue, defaultenv):
772772
assert response.status_code == 200
773773

774774

775-
@pytest.mark.parametrize(
776-
"level, has_output",
777-
[
778-
("info", [True, True, True]),
779-
("warn", [False, True, True]),
780-
("error", [False, False, True]),
781-
("crit", [False, False, False]),
782-
],
783-
)
784-
def test_log_level(level, has_output, defaultenv):
775+
@pytest.mark.parametrize("level", ["crit", "error", "warn", "info"])
776+
def test_log_level(level, defaultenv):
785777
"log_level should filter request logging"
786778

787779
env = {**defaultenv, "PGRST_LOG_LEVEL": level}
@@ -791,29 +783,31 @@ def test_log_level(level, has_output, defaultenv):
791783
headers = jwtauthheader(claim, SECRET)
792784

793785
with run(env=env) as postgrest:
794-
response = postgrest.session.get("/")
795-
assert response.status_code == 200
796-
if has_output[0]:
797-
assert re.match(
798-
r'- - postgrest_test_anonymous \[.+\] "GET / HTTP/1.1" 200 - "" "python-requests/.+"',
799-
postgrest.process.stdout.readline().decode(),
800-
)
786+
response = postgrest.session.get("/", headers=headers)
787+
assert response.status_code == 500
801788

802789
response = postgrest.session.get("/unknown")
803790
assert response.status_code == 404
804-
if has_output[1]:
805-
assert re.match(
806-
r'- - postgrest_test_anonymous \[.+\] "GET /unknown HTTP/1.1" 404 - "" "python-requests/.+"',
807-
postgrest.process.stdout.readline().decode(),
808-
)
809791

810-
response = postgrest.session.get("/", headers=headers)
811-
assert response.status_code == 500
812-
if has_output[2]:
813-
assert re.match(
814-
r'- - - \[.+\] "GET / HTTP/1.1" 500 - "" "python-requests/.+"',
815-
postgrest.process.stdout.readline().decode(),
816-
)
792+
response = postgrest.session.get("/")
793+
assert response.status_code == 200
794+
795+
output = sorted(postgrest.read_stdout(nlines=3))
796+
797+
if level == "crit":
798+
assert len(output) == 0
799+
elif level == "error":
800+
assert '"GET / HTTP/1.1" 500' in output[0]
801+
assert len(output) == 1
802+
elif level == "warn":
803+
assert '"GET / HTTP/1.1" 500' in output[0]
804+
assert '"GET /unknown HTTP/1.1" 404' in output[1]
805+
assert len(output) == 2
806+
else:
807+
assert '"GET / HTTP/1.1" 500' in output[0]
808+
assert '"GET / HTTP/1.1" 200' in output[1]
809+
assert '"GET /unknown HTTP/1.1" 404' in output[2]
810+
assert len(output) == 3
817811

818812

819813
def test_no_pool_connection_required_on_bad_http_logic(defaultenv):
@@ -1074,9 +1068,11 @@ def test_log_postgrest_version(defaultenv):
10741068
with run(env=defaultenv, no_startup_stdout=False) as postgrest:
10751069
version = postgrest.session.head("/").headers["Server"].split("/")[1]
10761070

1071+
output = sorted(postgrest.read_stdout(nlines=5))
1072+
10771073
assert (
10781074
"Starting PostgREST %s..." % version
1079-
in postgrest.process.stdout.readline().decode()
1075+
in output[3]
10801076
)
10811077

10821078

0 commit comments

Comments
 (0)