From 4a81f6b1a6a5203a82571cbdf328ebd6f7154746 Mon Sep 17 00:00:00 2001 From: steve-chavez Date: Wed, 17 Sep 2025 18:24:21 -0500 Subject: [PATCH] test: refactor test_log_query It's easier to maintain without using explicit array indexes for matching the logs --- test/io/test_io.py | 95 +++++++++++++++++++++++----------------------- 1 file changed, 48 insertions(+), 47 deletions(-) diff --git a/test/io/test_io.py b/test/io/test_io.py index ca5db1ace..a997d82dd 100644 --- a/test/io/test_io.py +++ b/test/io/test_io.py @@ -1042,62 +1042,63 @@ def test_log_query(level, defaultenv): response = postgrest.session.get("/infinite_recursion") assert response.status_code == 500 - root_2xx_regx_ln1 = r".+: SELECT n.nspname AS table_schema, .+ FROM pg_class c .+ ORDER BY table_schema, table_name" - root_2xx_regx_ln2 = r".+: WITH base_types AS \(.+\) SELECT pn.nspname AS proc_schema, .+ FROM pg_proc p.+AND p.pronamespace = \$1::regnamespace" - root_2xx_regx_ln3 = r".+: SELECT pg_catalog\.obj_description\(\$1::regnamespace, 'pg_namespace'\)" get_2xx_regx = r'.+: WITH pgrst_source AS.+SELECT "public"\."projects"\.\* FROM "public"\."projects".+_postgrest_t' get_2xx_count_regx = ( r'.+: EXPLAIN \(FORMAT JSON\) SELECT 1 FROM "public"."projects"' ) infinite_recursion_5xx_regx = r'.+: WITH pgrst_source AS.+SELECT "public"\."infinite_recursion"\.\* FROM "public"\."infinite_recursion".+_postgrest_t' + root_tables_regx = r".+: SELECT n.nspname AS table_schema, .+ FROM pg_class c .+ ORDER BY table_schema, table_name" + root_procs_regx = r".+: WITH base_types AS \(.+\) SELECT pn.nspname AS proc_schema, .+ FROM pg_proc p.+AND p.pronamespace = \$1::regnamespace" + root_descr_regx = r".+: SELECT pg_catalog\.obj_description\(\$1::regnamespace, 'pg_namespace'\)" + + def drain_stdout(proc): + lines = [] + while True: + chunk = proc.read_stdout(nlines=20) + if not chunk: + break + lines.extend(chunk) + return lines + + output = drain_stdout(postgrest) + + project_queries = [line for line in output if re.match(get_2xx_regx, line)] + project_counts = [line for line in output if re.match(get_2xx_count_regx, line)] + infinite_queries = [ + line for line in output if re.match(infinite_recursion_5xx_regx, line) + ] + root_tables = [line for line in output if re.match(root_tables_regx, line)] + root_procs = [line for line in output if re.match(root_procs_regx, line)] + root_descr = [line for line in output if re.match(root_descr_regx, line)] if level == "crit": - output = postgrest.read_stdout(nlines=1) - assert len(output) == 0 - elif level == "error": - output = postgrest.read_stdout(nlines=4) - assert re.match(infinite_recursion_5xx_regx, output[1]) - assert len(output) == 3 - elif level == "warn": - output = postgrest.read_stdout(nlines=2) - assert re.match(infinite_recursion_5xx_regx, output[1]) - assert len(output) == 2 + assert not project_queries + assert not project_counts + assert not infinite_queries + assert not root_tables + assert not root_procs + assert not root_descr + elif level in {"error", "warn"}: + assert len(infinite_queries) == 1 + assert not project_queries + assert not project_counts + assert not root_tables + assert not root_procs + assert not root_descr elif level == "info": - output_root = postgrest.read_stdout(nlines=4) - assert re.match(root_2xx_regx_ln1, output_root[0]) - assert re.match(root_2xx_regx_ln2, output_root[1]) - assert re.match(root_2xx_regx_ln3, output_root[2]) - assert len(output_root) == 4 - output_get = postgrest.read_stdout(nlines=8) - assert re.match(get_2xx_regx, output_get[0]) - assert re.match(get_2xx_regx, output_get[2]) - assert re.match(get_2xx_count_regx, output_get[3]) - assert re.match(get_2xx_regx, output_get[5]) - assert re.match(get_2xx_count_regx, output_get[6]) - assert len(output_get) == 8 - output_err = postgrest.read_stdout(nlines=2) - assert re.match(infinite_recursion_5xx_regx, output_err[1]) - assert len(output_err) == 2 + assert len(project_queries) == 3 + assert len(project_counts) == 2 + assert len(infinite_queries) == 1 + assert len(root_tables) == 1 + assert len(root_procs) == 1 + assert len(root_descr) == 1 elif level == "debug": - output_root = postgrest.read_stdout(nlines=8) - assert re.match(root_2xx_regx_ln1, output_root[4]) - assert re.match(root_2xx_regx_ln2, output_root[5]) - assert re.match(root_2xx_regx_ln3, output_root[6]) - assert len(output_root) == 8 - output_get = postgrest.read_stdout(nlines=6) - assert re.match(get_2xx_regx, output_get[4]) - assert len(output_get) == 6 - output_get_estimated = postgrest.read_stdout(nlines=7) - assert re.match(get_2xx_regx, output_get_estimated[4]) - assert re.match(get_2xx_count_regx, output_get_estimated[5]) - assert len(output_get_estimated) == 7 - output_get_planned = postgrest.read_stdout(nlines=7) - assert re.match(get_2xx_regx, output_get_planned[4]) - assert re.match(get_2xx_count_regx, output_get_planned[5]) - assert len(output_get_planned) == 7 - output_err = postgrest.read_stdout(nlines=6) - assert re.match(infinite_recursion_5xx_regx, output_err[5]) - assert len(output_err) == 6 + assert len(project_queries) == 3 + assert len(project_counts) == 2 + assert len(infinite_queries) == 1 + assert len(root_tables) == 1 + assert len(root_procs) == 1 + assert len(root_descr) == 1 def test_no_pool_connection_required_on_bad_http_logic(defaultenv):