From c03ac09dbbdd855c02231f68dc3c5ed7b2fb733a Mon Sep 17 00:00:00 2001 From: Taimoor Zaeem Date: Mon, 6 Jul 2026 18:28:48 +0500 Subject: [PATCH] test: move prepared statements config related tests to spec tests Signed-off-by: Taimoor Zaeem --- postgrest.cabal | 1 + ...est_schema_cache_snapshot[dbRoutines].yaml | 17 ---------- test/io/fixtures/schema.sql | 4 --- test/io/test_io.py | 21 ------------ .../Feature/Query/PreparedStatementsSpec.hs | 33 +++++++++++++++++++ test/spec/Main.hs | 2 ++ test/spec/fixtures/schema.sql | 11 +++++++ 7 files changed, 47 insertions(+), 42 deletions(-) create mode 100644 test/spec/Feature/Query/PreparedStatementsSpec.hs diff --git a/postgrest.cabal b/postgrest.cabal index bbea2d060..cc8dbbc73 100644 --- a/postgrest.cabal +++ b/postgrest.cabal @@ -260,6 +260,7 @@ test-suite spec Feature.Query.Preferences.HandlingSpec Feature.Query.Preferences.MaxAffectedSpec Feature.Query.Preferences.TimezoneSpec + Feature.Query.PreparedStatementsSpec Feature.Query.QueryLimitedSpec Feature.Query.QuerySpec Feature.Query.RangeSpec diff --git a/test/io/__snapshots__/test_cli/test_schema_cache_snapshot[dbRoutines].yaml b/test/io/__snapshots__/test_cli/test_schema_cache_snapshot[dbRoutines].yaml index 6dbeec179..68136f3cb 100644 --- a/test/io/__snapshots__/test_cli/test_schema_cache_snapshot[dbRoutines].yaml +++ b/test/io/__snapshots__/test_cli/test_schema_cache_snapshot[dbRoutines].yaml @@ -512,23 +512,6 @@ pdSchema: public pdVolatility: Volatile -- - qiName: uses_prepared_statements - qiSchema: public - - - pdDescription: null - pdFuncSettings: [] - pdHasVariadic: false - pdName: uses_prepared_statements - pdParams: [] - pdReturnType: - contents: - contents: - qiName: bool - qiSchema: pg_catalog - tag: Scalar - tag: Single - pdSchema: public - pdVolatility: Volatile - - - qiName: repeatable_read_isolation_level qiSchema: public - - pdDescription: null diff --git a/test/io/fixtures/schema.sql b/test/io/fixtures/schema.sql index ae1a6fe28..039ec0c0c 100644 --- a/test/io/fixtures/schema.sql +++ b/test/io/fixtures/schema.sql @@ -54,10 +54,6 @@ create function v1.get_guc_value(name text) returns text as $$ select nullif(current_setting(name), '')::text; $$ language sql; -create function uses_prepared_statements() returns bool as $$ - select count(name) > 0 from pg_catalog.pg_prepared_statements -$$ language sql; - create function change_max_rows_config(val int, notify bool default false) returns void as $_$ begin execute format($$ diff --git a/test/io/test_io.py b/test/io/test_io.py index d1f3fc74b..64be0e5aa 100644 --- a/test/io/test_io.py +++ b/test/io/test_io.py @@ -420,27 +420,6 @@ def test_notify_do_nothing(defaultenv): assert output == [] -def test_db_prepared_statements_enable(defaultenv): - "Should use prepared statements when the setting is enabled." - - with run(env=defaultenv) as postgrest: - response = postgrest.session.post("/rpc/uses_prepared_statements") - assert response.text == "true" - - -def test_db_prepared_statements_disable(defaultenv): - "Should not use any prepared statements when the setting is disabled." - - env = { - **defaultenv, - "PGRST_DB_PREPARED_STATEMENTS": "false", - } - - with run(env=env) as postgrest: - response = postgrest.session.post("/rpc/uses_prepared_statements") - assert response.text == "false" - - def test_statement_timeout(defaultenv, metapostgrest): "Statement timeout times out slow statements" diff --git a/test/spec/Feature/Query/PreparedStatementsSpec.hs b/test/spec/Feature/Query/PreparedStatementsSpec.hs new file mode 100644 index 000000000..06a488491 --- /dev/null +++ b/test/spec/Feature/Query/PreparedStatementsSpec.hs @@ -0,0 +1,33 @@ +module Feature.Query.PreparedStatementsSpec where + +import Network.HTTP.Types +import Test.Hspec hiding (pendingWith) +import Test.Hspec.Wai +import Text.Heredoc + +import PostgREST.Config (AppConfig (..)) + +import Protolude hiding (get) +import SpecHelper + +spec :: SpecWithConfig +spec withConfig = do + withConfig baseCfg { configDbPreparedStatements = True } $ + it "should use prepared statements when the setting is enabled" $ + request methodPost "/rpc/uses_prepared_statements" + [] "" + `shouldRespondWith` + [str|true|] + { matchStatus = 200 } + + withConfig baseCfg { configDbPreparedStatements = False } $ + it "should not use any prepared statements when the setting is disabled" $ do + request methodGet "/never_prepared" + [] "" + `shouldRespondWith` 200 + + request methodPost "/rpc/never_uses_prepared_statements" + [] "" + `shouldRespondWith` + [str|true|] + { matchStatus = 200 } diff --git a/test/spec/Main.hs b/test/spec/Main.hs index 5336bb798..58056fef6 100644 --- a/test/spec/Main.hs +++ b/test/spec/Main.hs @@ -58,6 +58,7 @@ import qualified Feature.Query.PostGISSpec import qualified Feature.Query.Preferences.HandlingSpec import qualified Feature.Query.Preferences.MaxAffectedSpec import qualified Feature.Query.Preferences.TimezoneSpec +import qualified Feature.Query.PreparedStatementsSpec import qualified Feature.Query.QueryLimitedSpec import qualified Feature.Query.QuerySpec import qualified Feature.Query.RangeSpec @@ -148,6 +149,7 @@ main = do , ("Feature.Query.Preferences.HandlingSpec" , Feature.Query.Preferences.HandlingSpec.spec) , ("Feature.Query.Preferences.MaxAffectedSpec" , Feature.Query.Preferences.MaxAffectedSpec.spec) , ("Feature.Query.Preferences.TimezoneSpec.enabledSpec", Feature.Query.Preferences.TimezoneSpec.enabledSpec) + , ("Feature.Query.PreparedStatementsSpec.spec" , Feature.Query.PreparedStatementsSpec.spec) , ("Feature.Query.QueryLimitedSpec" , Feature.Query.QueryLimitedSpec.spec) , ("Feature.Query.QuerySpec" , Feature.Query.QuerySpec.spec actualPgVersion) , ("Feature.Query.RangeSpec" , Feature.Query.RangeSpec.spec) diff --git a/test/spec/fixtures/schema.sql b/test/spec/fixtures/schema.sql index 3c8f2f41a..cce6136e8 100644 --- a/test/spec/fixtures/schema.sql +++ b/test/spec/fixtures/schema.sql @@ -3881,3 +3881,14 @@ $$ language plpgsql; create or replace function "true"() returns boolean as $_$ select true; $_$ language sql; + +create function uses_prepared_statements() returns bool as $$ + select count(*) > 0 from pg_catalog.pg_prepared_statements; +$$ language sql; + +create table never_prepared (id int); + +create function never_uses_prepared_statements() returns bool as $$ + select count(*) = 0 from pg_catalog.pg_prepared_statements + where statement ilike '%never_prepared%'; +$$ language sql;