add tests to pass regular arrays properly to RPCs

This commit is contained in:
Wolfgang Walther
2020-10-26 17:49:51 -05:00
committed by Steve Chavez
parent eed0d3a66c
commit 06e85357c7
3 changed files with 117 additions and 20 deletions
+83 -2
View File
@@ -268,7 +268,9 @@ spec actualPgVersion =
{ matchHeaders = [matchContentTypeJson] } { matchHeaders = [matchContentTypeJson] }
context "proc argument types" $ do context "proc argument types" $ do
it "accepts a variety of arguments" $ -- different syntax for array needed for pg<10
when (actualPgVersion < pgVersion100) $
it "accepts a variety of arguments (Postgres < 10)" $
post "/rpc/varied_arguments" post "/rpc/varied_arguments"
[json| { [json| {
"double": 3.1, "double": 3.1,
@@ -277,12 +279,91 @@ spec actualPgVersion =
"date": "20190101", "date": "20190101",
"money": 0, "money": 0,
"enum": "foo", "enum": "foo",
"arr": "{a,b,c}",
"integer": 43, "integer": 43,
"json": {"some key": "some value"}, "json": {"some key": "some value"},
"jsonb": {"another key": [1, 2, "3"]} "jsonb": {"another key": [1, 2, "3"]}
} |] } |]
`shouldRespondWith` `shouldRespondWith`
[json|"Hi"|] [json| {
"double": 3.1,
"varchar": "hello",
"boolean": true,
"date": "2019-01-01",
"money": "$0.00",
"enum": "foo",
"arr": ["a", "b", "c"],
"integer": 43,
"json": {"some key": "some value"},
"jsonb": {"another key": [1, 2, "3"]}
} |]
{ matchHeaders = [matchContentTypeJson] }
when (actualPgVersion >= pgVersion100) $
it "accepts a variety of arguments (Postgres >= 10)" $
post "/rpc/varied_arguments"
[json| {
"double": 3.1,
"varchar": "hello",
"boolean": true,
"date": "20190101",
"money": 0,
"enum": "foo",
"arr": ["a", "b", "c"],
"integer": 43,
"json": {"some key": "some value"},
"jsonb": {"another key": [1, 2, "3"]}
} |]
`shouldRespondWith`
[json| {
"double": 3.1,
"varchar": "hello",
"boolean": true,
"date": "2019-01-01",
"money": "$0.00",
"enum": "foo",
"arr": ["a", "b", "c"],
"integer": 43,
"json": {"some key": "some value"},
"jsonb": {"another key": [1, 2, "3"]}
} |]
{ matchHeaders = [matchContentTypeJson] }
it "accepts a variety of arguments with GET" $
-- without JSON / JSONB here, because passing those via query string is useless - they just become a "json string" all the time
get "/rpc/varied_arguments?double=3.1&varchar=hello&boolean=true&date=20190101&money=0&enum=foo&arr=%7Ba,b,c%7D&integer=43"
`shouldRespondWith`
[json| {
"double": 3.1,
"varchar": "hello",
"boolean": true,
"date": "2019-01-01",
"money": "$0.00",
"enum": "foo",
"arr": ["a", "b", "c"],
"integer": 43,
"json": {},
"jsonb": {}
} |]
{ matchHeaders = [matchContentTypeJson] }
it "accepts a variety of arguments from an html form" $
request methodPost "/rpc/varied_arguments"
[("Content-Type", "application/x-www-form-urlencoded")]
"double=3.1&varchar=hello&boolean=true&date=20190101&money=0&enum=foo&arr=%7Ba,b,c%7D&integer=43"
`shouldRespondWith`
[json| {
"double": 3.1,
"varchar": "hello",
"boolean": true,
"date": "2019-01-01",
"money": "$0.00",
"enum": "foo",
"arr": ["a", "b", "c"],
"integer": 43,
"json": {},
"jsonb": {}
} |]
{ matchHeaders = [matchContentTypeJson] } { matchHeaders = [matchContentTypeJson] }
it "parses embedded JSON arguments as JSON" $ it "parses embedded JSON arguments as JSON" $
+6 -1
View File
@@ -425,7 +425,8 @@ spec = do
"boolean", "boolean",
"date", "date",
"money", "money",
"enum" "enum",
"arr"
], ],
"properties": { "properties": {
"double": { "double": {
@@ -452,6 +453,10 @@ spec = do
"format": "enum_menagerie_type", "format": "enum_menagerie_type",
"type": "string" "type": "string"
}, },
"arr": {
"format": "text[]",
"type": "string"
},
"integer": { "integer": {
"format": "integer", "format": "integer",
"type": "integer" "type": "integer"
+16 -5
View File
@@ -221,23 +221,34 @@ CREATE FUNCTION varied_arguments(
date date, date date,
money money, money money,
enum enum_menagerie_type, enum enum_menagerie_type,
arr text[],
"integer" integer default 42, "integer" integer default 42,
json json default '{}', json json default '{}',
jsonb jsonb default '{}' jsonb jsonb default '{}'
) RETURNS text ) RETURNS json
LANGUAGE sql LANGUAGE sql
AS $_$ AS $_$
SELECT 'Hi'::text; SELECT json_build_object(
'double', double,
'varchar', "varchar",
'boolean', "boolean",
'date', date,
'money', money,
'enum', enum,
'arr', arr,
'integer', "integer",
'json', json,
'jsonb', jsonb
);
$_$; $_$;
COMMENT ON FUNCTION varied_arguments(double precision, character varying, boolean, date, money, enum_menagerie_type, integer, json, jsonb) IS COMMENT ON FUNCTION varied_arguments(double precision, character varying, boolean, date, money, enum_menagerie_type, text[], integer, json, jsonb) IS
$_$An RPC function $_$An RPC function
Just a test for RPC function arguments$_$; Just a test for RPC function arguments$_$;
CREATE FUNCTION json_argument(arg json) RETURNS text CREATE FUNCTION json_argument(arg json) RETURNS text
LANGUAGE sql LANGUAGE sql
AS $_$ AS $_$
SELECT json_typeof(arg); SELECT json_typeof(arg);