Skip to content

Commit

Permalink
add tests to pass regular arrays properly to RPCs
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfgangwalther committed Oct 12, 2020
1 parent ccad9eb commit c9fbcd9
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 20 deletions.
109 changes: 95 additions & 14 deletions test/Feature/RpcSpec.hs
Expand Up @@ -274,21 +274,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| {
"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|"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 "parses embedded JSON arguments as JSON" $
Expand Down
7 changes: 6 additions & 1 deletion test/Feature/StructureSpec.hs
Expand Up @@ -425,7 +425,8 @@ spec = do
"boolean",
"date",
"money",
"enum"
"enum",
"arr"
],
"properties": {
"double": {
Expand All @@ -452,6 +453,10 @@ spec = do
"format": "enum_menagerie_type",
"type": "string"
},
"arr": {
"format": "text[]",
"type": "string"
},
"integer": {
"format": "integer",
"type": "integer"
Expand Down
21 changes: 16 additions & 5 deletions test/fixtures/schema.sql
Expand Up @@ -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);
Expand Down

0 comments on commit c9fbcd9

Please sign in to comment.