diff --git a/test/Feature/RpcSpec.hs b/test/Feature/RpcSpec.hs index eb95bd6fd..5426d40b3 100644 --- a/test/Feature/RpcSpec.hs +++ b/test/Feature/RpcSpec.hs @@ -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" $ diff --git a/test/Feature/StructureSpec.hs b/test/Feature/StructureSpec.hs index 02ce083be..c288edb8f 100644 --- a/test/Feature/StructureSpec.hs +++ b/test/Feature/StructureSpec.hs @@ -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" diff --git a/test/fixtures/schema.sql b/test/fixtures/schema.sql index 1e36b589c..c56587059 100755 --- a/test/fixtures/schema.sql +++ b/test/fixtures/schema.sql @@ -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);