From e5c77385ae228069087f3349277ba6ca7986355e Mon Sep 17 00:00:00 2001 From: steve-chavez Date: Wed, 23 Nov 2022 13:19:59 -0500 Subject: [PATCH] test: move failed or/and parsing to doctest --- src/PostgREST/ApiRequest/QueryParams.hs | 29 ++++++++++++++++++++ test/spec/Feature/Query/AndOrParamsSpec.hs | 32 ---------------------- 2 files changed, 29 insertions(+), 32 deletions(-) diff --git a/src/PostgREST/ApiRequest/QueryParams.hs b/src/PostgREST/ApiRequest/QueryParams.hs index 543ecd028..738427b49 100644 --- a/src/PostgREST/ApiRequest/QueryParams.hs +++ b/src/PostgREST/ApiRequest/QueryParams.hs @@ -78,6 +78,8 @@ import Protolude hiding (try) -- >>> deriving instance Show OrderDirection -- >>> deriving instance Show OrderNulls -- >>> deriving instance Show OrderTerm +-- >>> deriving instance Show LogicOperator +-- >>> deriving instance Show LogicTree data QueryParams = QueryParams @@ -694,6 +696,33 @@ pOrder = lexeme (try pOrderRelationTerm <|> pOrderTerm) `sepBy1` char ',' pEnd = try (void $ lookAhead (char ',')) <|> try eof +-- | +-- Parses the elements inside or/and +-- +-- >>> P.parse pLogicTree "" "or()" +-- Left (line 1, column 4): +-- unexpected ")" +-- expecting field name (* or [a..z0..9_$]), negation operator (not) or logic operator (and, or) +-- +-- >>> P.parse pLogicTree "" "or(id.in.1,2,id.eq.3)" +-- Left (line 1, column 10): +-- unexpected "1" +-- expecting "(" +-- +-- >>> P.parse pLogicTree "" "or)(" +-- Left (line 1, column 3): +-- unexpected ")" +-- expecting "(" +-- +-- >>> P.parse pLogicTree "" "and(ord(id.eq.1,id.eq.1),id.eq.2)" +-- Left (line 1, column 7): +-- unexpected "d" +-- expecting "(" +-- +-- >>> P.parse pLogicTree "" "or(id.eq.1,not.xor(id.eq.2,id.eq.3))" +-- Left (line 1, column 16): +-- unexpected "x" +-- expecting logic operator (and, or) pLogicTree :: Parser LogicTree pLogicTree = Stmnt <$> try pLogicFilter <|> Expr <$> pNot <*> pLogicOp <*> (lexeme (char '(') *> pLogicTree `sepBy1` lexeme (char ',') <* lexeme (char ')')) diff --git a/test/spec/Feature/Query/AndOrParamsSpec.hs b/test/spec/Feature/Query/AndOrParamsSpec.hs index 917e2faea..da76e7f42 100644 --- a/test/spec/Feature/Query/AndOrParamsSpec.hs +++ b/test/spec/Feature/Query/AndOrParamsSpec.hs @@ -252,35 +252,3 @@ spec actualPgVersion = it "can query columns that begin with and/or reserved words" $ get "/grandchild_entities?or=(and_starting_col.eq.smth, or_starting_col.eq.smth)" `shouldRespondWith` 200 - - it "fails when using IN without () and provides meaningful error message" $ - get "/entities?or=(id.in.1,2,id.eq.3)" `shouldRespondWith` - [json|{ - "details": "unexpected \"1\" expecting \"(\"", - "message": "\"failed to parse logic tree ((id.in.1,2,id.eq.3))\" (line 1, column 10)", - "code": "PGRST100", - "hint": null - }|] { matchStatus = 400, matchHeaders = [matchContentTypeJson] } - - it "fails on malformed query params and provides meaningful error message" $ do - get "/entities?or=)(" `shouldRespondWith` - [json|{ - "details": "unexpected \")\" expecting \"(\"", - "message": "\"failed to parse logic tree ()()\" (line 1, column 3)", - "code": "PGRST100", - "hint": null - }|] { matchStatus = 400, matchHeaders = [matchContentTypeJson] } - get "/entities?and=(ord(id.eq.1,id.eq.1),id.eq.2)" `shouldRespondWith` - [json|{ - "details": "unexpected \"d\" expecting \"(\"", - "message": "\"failed to parse logic tree ((ord(id.eq.1,id.eq.1),id.eq.2))\" (line 1, column 7)", - "code": "PGRST100", - "hint": null - }|] { matchStatus = 400, matchHeaders = [matchContentTypeJson] } - get "/entities?or=(id.eq.1,not.xor(id.eq.2,id.eq.3))" `shouldRespondWith` - [json|{ - "details": "unexpected \"x\" expecting logic operator (and, or)", - "message": "\"failed to parse logic tree ((id.eq.1,not.xor(id.eq.2,id.eq.3)))\" (line 1, column 16)", - "code": "PGRST100", - "hint": null - }|] { matchStatus = 400, matchHeaders = [matchContentTypeJson] }