Allow calling variadic functions with repeated query params or JSON array in body

This commit is contained in:
Wolfgang Walther
2020-10-26 17:49:51 -05:00
committed by Steve Chavez
parent 18cc214c04
commit 302d4e15ad
9 changed files with 128 additions and 23 deletions
+58
View File
@@ -469,6 +469,64 @@ spec actualPgVersion =
get "/rpc/many_inout_params?num=1&str=two&b=false" `shouldRespondWith`
[json| [{"num":1,"str":"two","b":false}]|] { matchHeaders = [matchContentTypeJson] }
context "procs with VARIADIC params" $ do
when (actualPgVersion < pgVersion100) $
it "works with POST (Postgres < 10)" $
post "/rpc/variadic_param"
[json| { "v": "{hi,hello,there}" } |]
`shouldRespondWith`
[json|["hi", "hello", "there"]|]
when (actualPgVersion >= pgVersion100) $ do
it "works with POST (Postgres >= 10)" $
post "/rpc/variadic_param"
[json| { "v": ["hi", "hello", "there"] } |]
`shouldRespondWith`
[json|["hi", "hello", "there"]|]
context "works with GET and repeated params" $ do
it "n=0 (through DEFAULT)" $
get "/rpc/variadic_param"
`shouldRespondWith`
[json|[]|]
it "n=1" $
get "/rpc/variadic_param?v=hi"
`shouldRespondWith`
[json|["hi"]|]
it "n>1" $
get "/rpc/variadic_param?v=hi&v=there"
`shouldRespondWith`
[json|["hi", "there"]|]
context "works with POST and repeated params from html form" $ do
it "n=0 (through DEFAULT)" $
request methodPost "/rpc/variadic_param"
[("Content-Type", "application/x-www-form-urlencoded")]
""
`shouldRespondWith`
[json|[]|]
it "n=1" $
request methodPost "/rpc/variadic_param"
[("Content-Type", "application/x-www-form-urlencoded")]
"v=hi"
`shouldRespondWith`
[json|["hi"]|]
it "n>1" $
request methodPost "/rpc/variadic_param"
[("Content-Type", "application/x-www-form-urlencoded")]
"v=hi&v=there"
`shouldRespondWith`
[json|["hi", "there"]|]
it "returns first value for repeated params without VARIADIC" $
get "/rpc/sayhello?name=world&name=ignored"
`shouldRespondWith`
[json|"Hello, world"|]
it "can handle procs with args that have a DEFAULT value" $ do
get "/rpc/many_inout_params?num=1&str=two" `shouldRespondWith`
[json| [{"num":1,"str":"two","b":true}]|] { matchHeaders = [matchContentTypeJson] }
+4 -4
View File
@@ -29,7 +29,7 @@ main = do
context "call proc query" $ do
it "should not exceed cost when calling setof composite proc" $ do
cost <- exec pool [str| {"id": 3} |] $
requestToCallProcQuery (QualifiedIdentifier "test" "get_projects_below") [PgArg "id" "int" True] False Nothing []
requestToCallProcQuery (QualifiedIdentifier "test" "get_projects_below") [PgArg "id" "int" True False] False Nothing []
liftIO $
cost `shouldSatisfy` (< Just 40)
@@ -41,14 +41,14 @@ main = do
it "should not exceed cost when calling scalar proc" $ do
cost <- exec pool [str| {"a": 3, "b": 4} |] $
requestToCallProcQuery (QualifiedIdentifier "test" "add_them") [PgArg "a" "int" True, PgArg "b" "int" True] True Nothing []
requestToCallProcQuery (QualifiedIdentifier "test" "add_them") [PgArg "a" "int" True False, PgArg "b" "int" True False] True Nothing []
liftIO $
cost `shouldSatisfy` (< Just 10)
context "params=multiple-objects" $ do
it "should not exceed cost when calling setof composite proc" $ do
cost <- exec pool [str| [{"id": 1}, {"id": 4}] |] $
requestToCallProcQuery (QualifiedIdentifier "test" "get_projects_below") [PgArg "id" "int" True] False (Just MultipleObjects) []
requestToCallProcQuery (QualifiedIdentifier "test" "get_projects_below") [PgArg "id" "int" True False] False (Just MultipleObjects) []
liftIO $ do
-- lower bound needed for now to make sure that cost is not Nothing
cost `shouldSatisfy` (> Just 2000)
@@ -56,7 +56,7 @@ main = do
it "should not exceed cost when calling scalar proc" $ do
cost <- exec pool [str| [{"a": 3, "b": 4}, {"a": 1, "b": 2}, {"a": 8, "b": 7}] |] $
requestToCallProcQuery (QualifiedIdentifier "test" "add_them") [PgArg "a" "int" True, PgArg "b" "int" True] True Nothing []
requestToCallProcQuery (QualifiedIdentifier "test" "add_them") [PgArg "a" "int" True False, PgArg "b" "int" True False] True Nothing []
liftIO $
cost `shouldSatisfy` (< Just 10)
+5
View File
@@ -1083,6 +1083,11 @@ create function test.many_inout_params(INOUT num int, INOUT str text, INOUT b bo
select num, str, b;
$$ language sql;
CREATE FUNCTION test.variadic_param(VARIADIC v TEXT[] DEFAULT '{}') RETURNS text[]
LANGUAGE SQL AS $_$
SELECT v
$_$;
create or replace function test.raise_pt402() returns void as $$
begin
raise sqlstate 'PT402' using message = 'Payment Required',