From 3103060f4b62096f67882a52ba28bcafe4c4e1a4 Mon Sep 17 00:00:00 2001 From: steve-chavez Date: Wed, 23 Nov 2022 12:57:15 -0500 Subject: [PATCH] test: move failed json parsing to doctest --- src/PostgREST/ApiRequest/QueryParams.hs | 22 +++++++++++++++ test/spec/Feature/Query/JsonOperatorSpec.hs | 30 ++++++--------------- 2 files changed, 30 insertions(+), 22 deletions(-) diff --git a/src/PostgREST/ApiRequest/QueryParams.hs b/src/PostgREST/ApiRequest/QueryParams.hs index 07513c57c..543ecd028 100644 --- a/src/PostgREST/ApiRequest/QueryParams.hs +++ b/src/PostgREST/ApiRequest/QueryParams.hs @@ -325,6 +325,11 @@ pTreePath = do -- Left (line 1, column 16): -- unexpected '[' -- expecting letter, digit, "-", "!", "(", "->>", "->", "::", ")", "," or end of input +-- +-- >>> P.parse pFieldForest "" "data->>-78xy" +-- Left (line 1, column 11): +-- unexpected 'x' +-- expecting digit, "->", "::", ".", "," or end of input pFieldForest :: Parser [Tree SelectItem] pFieldForest = pFieldTree `sepBy1` lexeme (char ',') where @@ -401,6 +406,23 @@ pFieldName = -- -- >>> P.parse pJsonPath "" "->0.desc" -- Right [JArrow {jOp = JIdx {jVal = "+0"}}] +-- +-- Fails on badly formed negatives +-- +-- >>> P.parse pJsonPath "" "->>-78xy" +-- Left (line 1, column 7): +-- unexpected 'x' +-- expecting digit, "->", "::", ".", "," or end of input +-- +-- >>> P.parse pJsonPath "" "->>--34" +-- Left (line 1, column 5): +-- unexpected "-" +-- expecting digit +-- +-- >>> P.parse pJsonPath "" "->>-xy-4" +-- Left (line 1, column 5): +-- unexpected "x" +-- expecting digit pJsonPath :: Parser JsonPath pJsonPath = many pJsonOperation where diff --git a/test/spec/Feature/Query/JsonOperatorSpec.hs b/test/spec/Feature/Query/JsonOperatorSpec.hs index af3f9cbfc..9e0f0cbff 100644 --- a/test/spec/Feature/Query/JsonOperatorSpec.hs +++ b/test/spec/Feature/Query/JsonOperatorSpec.hs @@ -292,25 +292,11 @@ spec actualPgVersion = describe "json and jsonb operators" $ do [json| [{"data":[{"a": [1,2,3]}, {"b": [4,5]}]}] |] { matchHeaders = [matchContentTypeJson] } - it "should fail on badly formed negatives" $ do - get "/json_arr?select=data->>-78xy" `shouldRespondWith` - [json| - {"details": "unexpected 'x' expecting digit, \"->\", \"::\", \".\", \",\" or end of input", - "message": "\"failed to parse select parameter (data->>-78xy)\" (line 1, column 11)", - "code": "PGRST100", - "hint": null} |] - { matchStatus = 400, matchHeaders = [matchContentTypeJson] } - get "/json_arr?select=data->>--34" `shouldRespondWith` - [json| - {"details": "unexpected \"-\" expecting digit", - "message": "\"failed to parse select parameter (data->>--34)\" (line 1, column 9)", - "code": "PGRST100", - "hint": null} |] - { matchStatus = 400, matchHeaders = [matchContentTypeJson] } - get "/json_arr?select=data->>-xy-4" `shouldRespondWith` - [json| - {"details":"unexpected \"x\" expecting digit", - "message":"\"failed to parse select parameter (data->>-xy-4)\" (line 1, column 9)", - "code": "PGRST100", - "hint": null} |] - { matchStatus = 400, matchHeaders = [matchContentTypeJson] } + it "gives a meaningful error on bad syntax" $ + get "/json_arr?select=data->>--34" `shouldRespondWith` + [json| + {"details": "unexpected \"-\" expecting digit", + "message": "\"failed to parse select parameter (data->>--34)\" (line 1, column 9)", + "code": "PGRST100", + "hint": null} |] + { matchStatus = 400, matchHeaders = [matchContentTypeJson] }