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
+95 -14
View File
@@ -268,21 +268,102 @@ spec actualPgVersion =
{ matchHeaders = [matchContentTypeJson] }
context "proc argument types" $ do
it "accepts a variety of arguments" $
post "/rpc/varied_arguments"
[json| {
"double": 3.1,
"varchar": "hello",
"boolean": true,
"date": "20190101",
"money": 0,
"enum": "foo",
"integer": 43,
"json": {"some key": "some value"},
"jsonb": {"another key": [1, 2, "3"]}
} |]
-- different syntax for array needed for pg<10
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] }
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|"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": {},
"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] }
it "parses embedded JSON arguments as JSON" $
+6 -1
View File
@@ -425,7 +425,8 @@ spec = do
"boolean",
"date",
"money",
"enum"
"enum",
"arr"
],
"properties": {
"double": {
@@ -452,6 +453,10 @@ spec = do
"format": "enum_menagerie_type",
"type": "string"
},
"arr": {
"format": "text[]",
"type": "string"
},
"integer": {
"format": "integer",
"type": "integer"
+16 -5
View File
@@ -221,23 +221,34 @@ CREATE FUNCTION varied_arguments(
date date,
money money,
enum enum_menagerie_type,
arr text[],
"integer" integer default 42,
json json default '{}',
jsonb jsonb default '{}'
) RETURNS text
LANGUAGE sql
) RETURNS json
LANGUAGE sql
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
Just a test for RPC function arguments$_$;
CREATE FUNCTION json_argument(arg json) RETURNS text
LANGUAGE sql
AS $_$
SELECT json_typeof(arg);